// Note that thread pool workers will set Thread#setCanCallIntoJava to false. class AbstractThreadPool { public: // Returns the number of threads in the thread pool.
size_t GetThreadCount() const { return threads_.size();
}
// Broadcast to the workers and tell them to empty out the work queue.
EXPORT void StartWorkers(Thread* self) REQUIRES(!task_queue_lock_);
// Do not allow workers to grab any new tasks.
EXPORT void StopWorkers(Thread* self) REQUIRES(!task_queue_lock_);
// Returns if the thread pool has started. bool HasStarted(Thread* self) REQUIRES(!task_queue_lock_);
// Add a new task, the first available started worker will process it. Does not delete the task // after running it, it is the caller's responsibility. virtualvoid AddTask(Thread* self, Task* task) REQUIRES(!task_queue_lock_) = 0;
// Remove all tasks in the queue. virtualvoid RemoveAllTasks(Thread* self) REQUIRES(!task_queue_lock_) = 0;
// Create the threads of this pool.
EXPORT void CreateThreads();
// Stops and deletes all threads in this pool. void DeleteThreads();
// Wait for all tasks currently on queue to get completed. If the pool has been stopped, only // wait till all already running tasks are done. // When the pool was created with peers for workers, do_work must not be true (see ThreadPool()).
EXPORT void Wait(Thread* self, bool do_work, bool may_hold_locks) REQUIRES(!task_queue_lock_);
// Returns the total amount of workers waited for tasks.
uint64_t GetWaitTime() const { return total_wait_time_;
}
// Provides a way to bound the maximum number of worker threads, threads must be less the the // thread count of the thread pool. void SetMaxActiveWorkers(size_t threads) REQUIRES(!task_queue_lock_);
// Set the "nice" priority for threads in the pool. void SetPthreadPriority(int priority);
// CHECK that the "nice" priority of threads in the pool is the given // `priority`. void CheckPthreadPriority(int priority);
// Wait for workers to be created. void WaitForWorkersToBeCreated();
virtual ~AbstractThreadPool() {}
protected: // get a task to run, blocks if there are no tasks left
Task* GetTask(Thread* self) REQUIRES(!task_queue_lock_);
// Try to get a task, returning null if there is none available.
Task* TryGetTask(Thread* self) REQUIRES(!task_queue_lock_); virtual Task* TryGetTaskLocked() REQUIRES(task_queue_lock_) = 0;
// Are we shutting down? bool IsShuttingDown() const REQUIRES(task_queue_lock_) { return shutting_down_;
}
class EXPORT ThreadPool : public AbstractThreadPool { public: // Create a named thread pool with the given number of threads. // // If create_peers is true, all worker threads will have a Java peer object. Note that if the // pool is asked to do work on the current thread (see Wait), a peer may not be available. Wait // will conservatively abort if create_peers and do_work are true. static ThreadPool* Create(constchar* name,
size_t num_threads, bool create_peers = false,
size_t worker_stack_size = ThreadPoolWorker::kDefaultStackSize) {
ThreadPool* pool = new ThreadPool(name, num_threads, create_peers, worker_stack_size);
pool->CreateThreads(); return pool;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.