20#ifndef KICAD_DB_WORKER_THREAD_H
21#define KICAD_DB_WORKER_THREAD_H
55 bool Emit( std::vector<DB_VALUE>&& aRow );
106 DB_WORKER_THREAD( std::string aSourceKey, std::function<
bool()> aThreadInit =
nullptr,
107 std::function<
void()> aThreadExit =
nullptr,
bool aPumpMessages =
false,
108 std::function<
void()> aOnAbandon =
nullptr );
138 template <
typename R>
139 std::optional<R>
Run( std::function<R(
DB_JOB_CONTEXT& )> aJob, std::chrono::milliseconds aBound,
142 auto result = std::make_shared<std::optional<R>>();
146 *
result = job( aContext );
149 if( !
runJob( std::move( wrapped ), aBound, aSink,
false ) )
152 return std::move( *
result );
162 bool RunCleanup( std::function<
void()> aCleanup, std::chrono::milliseconds aBound );
179 static std::string
SourceKeyFor(
const std::string& aPath );
182 static std::string
SourceKeyFor(
const std::string& aPath,
bool aWindowsPath );
189 static std::string
NormalizePath(
const std::string& aPath,
bool aWindowsPath );
192 static std::chrono::milliseconds
CallerBound(
int aTimeoutSeconds );
What a job running on a DB_WORKER_THREAD uses to hand rows to the caller.
static constexpr size_t BATCH_ROWS
static constexpr size_t MAX_QUEUED_BATCHES
bool Flush()
Delivers the rows still held back and waits until the caller's sink has taken every one.
DB_JOB_CONTEXT(DB_WORKER_CORE &aCore)
bool Emit(std::vector< DB_VALUE > &&aRow)
Queues a row for the caller's sink.
friend struct DB_WORKER_CORE
std::vector< std::vector< DB_VALUE > > m_pending
bool HasLiveThread() const
~DB_WORKER_THREAD()
Waits up to the shutdown bound for a running job, then abandons it.
static constexpr size_t MAX_ABANDONED
std::chrono::milliseconds m_shutdownBound
bool runJob(std::function< void(DB_JOB_CONTEXT &)> aJob, std::chrono::milliseconds aBound, const DB_ROW_SINK *aSink, bool aCleanup)
OUTCOME
Why the last Run or RunCleanup returned without a result.
@ UNAVAILABLE
the worker thread could not start
@ REFUSED
two abandoned jobs hold the source, or no slot freed within the bound
@ TOO_LATE
a slot freed with under half the bound left, so nothing was started
@ TIMED_OUT
no progress within the bound, so the job was abandoned
OUTCOME LastOutcome() const
static std::chrono::milliseconds CallerBound(int aTimeoutSeconds)
std::shared_ptr< OUTCOME > m_outcome
void SetShutdownBound(std::chrono::milliseconds aBound)
Sets how long the destructor waits for a running job before abandoning it.
bool RunCleanup(std::function< void()> aCleanup, std::chrono::milliseconds aBound)
Runs aCleanup on a worker thread for work that must not happen on the caller, such as closing a conne...
DB_WORKER_THREAD(const DB_WORKER_THREAD &)=delete
static std::string SourceKeyFor(const std::string &aPath)
std::shared_ptr< DB_WORKER_STATE > m_state
std::optional< R > Run(std::function< R(DB_JOB_CONTEXT &)> aJob, std::chrono::milliseconds aBound, const DB_ROW_SINK *aSink)
Runs aJob on the worker thread and waits for it.
DB_WORKER_THREAD & operator=(const DB_WORKER_THREAD &)=delete
static std::string NormalizePath(const std::string &aPath, bool aWindowsPath)
Rewrites the spellings of one file that Windows treats alike into one form, without touching the file...
DB_WORKER_THREAD(std::string aSourceKey, std::function< bool()> aThreadInit=nullptr, std::function< void()> aThreadExit=nullptr, bool aPumpMessages=false, std::function< void()> aOnAbandon=nullptr)
static bool OnWorkerThread()
std::function< void(std::vector< DB_VALUE > &&)> DB_ROW_SINK
Receives one row at a time, in the order of the labels the read produced.
State shared by the owner and the worker thread; the thread keeps it alive after the owner is gone.
The part of a worker that outlives its owner while a caller is still inside Run.
wxString result
Test unit parsing edge cases and error handling.