Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
workerpool.h
Go to the documentation of this file.
1 
18 #ifndef ION_BASE_WORKERPOOL_H_
19 #define ION_BASE_WORKERPOOL_H_
20 
21 #include <atomic>
22 #include <functional>
23 
25 #include "ion/port/mutex.h"
26 #include "ion/port/semaphore.h"
27 #include "ion/port/threadutils.h"
28 
29 namespace ion {
30 namespace base {
31 
47  public:
51  class Worker {
52  public:
64  virtual void DoWork() = 0;
65 
67  virtual const std::string& GetName() const = 0;
68 
69  protected:
71  virtual ~Worker() {}
72  };
73 
75  explicit WorkerPool(Worker* worker);
76  ~WorkerPool() override;
77 
79  const std::string& GetName() const { return worker_->GetName(); }
80 
83  void Suspend();
85  void Resume();
87  bool IsSuspended() const;
88 
90  void ResizeThreadPool(size_t thread_count);
91 
94  port::Semaphore* GetWorkSemaphore() { return &work_sema_; }
95 
96  protected:
103  virtual void ThreadEntryPoint();
104 
105  private:
107  void KillAllThreads();
108 
110  static void Wait(port::Semaphore* sema);
111  static void Post(port::Semaphore* sema);
112 
113  Worker* const worker_;
115  port::Semaphore work_sema_;
116  port::Semaphore active_threads_sema_;
117  std::atomic<bool> suspended_;
120  std::atomic<bool> killing_;
125  std::atomic<bool> slow_path_;
126  std::function<bool()> spawn_func_;
127  mutable ion::port::Mutex mutex_;
128 
129  DISALLOW_IMPLICIT_CONSTRUCTORS(WorkerPool);
130 };
131 
132 } // namespace base
133 } // namespace ion
134 
135 #endif // ION_BASE_WORKERPOOL_H_
Interface to enable pluggable worker behavior.
Definition: workerpool.h:51
A Semaphore enables threads and process synchronization.
Definition: semaphore.h:40
Manages one or more threads that run in a loop, performing some work with each iteration (if any work...
Definition: workerpool.h:46
Allocatable is an abstract base class for classes whose memory is managed by an Allocator.
Definition: allocatable.h:60
void ResizeThreadPool(size_t thread_count)
Changes the number of theads in the pool.
virtual ~Worker()
Virtual classes need virtual desructors.
Definition: workerpool.h:71
void Suspend()
Suspends all threads until Resume() is called.
void Resume()
Resumes all threads.
WorkerPool(Worker *worker)
Constructor/destructor.
Definition: workerpool.cc:25
bool IsSuspended() const
Return true if pool's threads are suspended.
port::Semaphore * GetWorkSemaphore()
Returns the semaphore that is used to signal that a unit of work is available to process.
Definition: workerpool.h:94
virtual void DoWork()=0
Called repeatedly in worker thread loop, whenever GetWorkSemaphore() is signaled to indicate that the...
const std::string & GetName() const
Gets a descriptive name for the pool from the worker.
Definition: workerpool.h:79
A Mutex is used to ensure that only one thread or process can access a block of code at one time...
Definition: mutex.h:34
virtual void ThreadEntryPoint()
ThreadEntryPoint() is run on each created thread.
virtual const std::string & GetName() const =0
Return the worker's name.