X-Git-Url: http://git.vrable.net/?a=blobdiff_plain;f=kvstore%2Fprotobufrpc%2Fworkqueue.h;fp=kvstore%2Fprotobufrpc%2Fworkqueue.h;h=cfb2e02cf45766ca6886d33bfff4276338f131f6;hb=3c2cbef21a11c4d86952922f4da7b830a91423f9;hp=0000000000000000000000000000000000000000;hpb=db0d4c10ea7abfa2546f73e96784ebf554342977;p=bluesky.git diff --git a/kvstore/protobufrpc/workqueue.h b/kvstore/protobufrpc/workqueue.h new file mode 100644 index 0000000..cfb2e02 --- /dev/null +++ b/kvstore/protobufrpc/workqueue.h @@ -0,0 +1,78 @@ +#ifndef __WORKQUEUE_H__ +#define __WORKQUEUE_H__ 1 + +#include +#include +#include +#include "util.h" + +using namespace boost; +using namespace std; + +namespace bicker +{ + struct interrupted_error : public virtual std::exception { }; + + class WorkUnit + { + public: + virtual ~WorkUnit() {}; + virtual void run() = 0; + }; + + class WorkQueue + { + public: + WorkQueue(); + WorkQueue(int thread_count); + + ~WorkQueue(); + + shared_ptr get(); + void put(shared_ptr work_unit); + + protected: + void spawnThread(); + + private: + class Worker + { + public: + Worker(WorkQueue* queue); + void operator()(); + private: + WorkQueue *_queue; + }; + + int _thread_count; + int _min_threads; + int _max_threads; + mutex _queue_lock; + condition_variable _queue_non_empty; + queue > _queue; + thread_group _threads; + volatile bool _running; + }; + + class TaskNotification + { + public: + TaskNotification(); + + void registerTask(); + void completeTask(bool success = true); + + void waitForComplete(); + + bool failCount(); + private: + int _expected; + int _count; + int _fail_count; + mutex _lock; + condition_variable _cond; + }; + +} // namespace bicker + +#endif