Split out marshall code into a new file
[invirt/third/libt4.git] / rpc / thr_pool.cc
1 #include "thr_pool.h"
2
3 // if blocking, then addJob() blocks when queue is full
4 // otherwise, addJob() simply returns false when queue is full
5 ThrPool::ThrPool(size_t sz, bool blocking)
6 : nthreads_(sz),blockadd_(blocking),jobq_(100*sz) 
7 {
8         for (size_t i=0; i<nthreads_; i++)
9         th_.emplace_back(&ThrPool::do_worker, this);
10 }
11
12 // IMPORTANT: this function can be called only when no external thread 
13 // will ever use this thread pool again or is currently blocking on it
14 ThrPool::~ThrPool()
15 {
16         for (size_t i=0; i<nthreads_; i++)
17                 jobq_.enq(job_t());
18
19         for (size_t i=0; i<nthreads_; i++)
20         th_[i].join();
21 }
22
23 bool 
24 ThrPool::addJob(const job_t &j)
25 {
26         return jobq_.enq(j,blockadd_);
27 }
28
29 void
30 ThrPool::do_worker()
31 {
32     job_t j;
33         while (1) {
34         jobq_.deq(&j);
35                 if (!j)
36                         break;
37                 j();
38         }
39 }