|
| 1 | +#ifndef BUFFER_DISPATCHER_H |
| 2 | +#define BUFFER_DISPATCHER_H |
| 3 | + |
| 4 | +#include <atomic> |
| 5 | +#include <vector> |
| 6 | +#include <Utilities.h> |
| 7 | +#include <JobQueue.h> |
| 8 | +#include <Pool.h> |
| 9 | +#include <Buffer.h> |
| 10 | +#include <AlgorithmWrapper.h> |
| 11 | + |
| 12 | +namespace ToolFramework{ |
| 13 | + |
| 14 | + template<class T> struct BufferDispatcher_args:Thread_args{ |
| 15 | + BufferDispatcher_args(){;} |
| 16 | + ~BufferDispatcher_args(){;} |
| 17 | + |
| 18 | + Buffer<T>* buffer = 0; |
| 19 | + std::vector<T> local_buffer; |
| 20 | + std::vector<AlgorithmWrapper<T> >* algorithms = 0; |
| 21 | + Job* job = 0; |
| 22 | + |
| 23 | + JobQueue* job_queue = 0; |
| 24 | + Pool<Job>* job_pool = 0; |
| 25 | + |
| 26 | + std::atomic<uint64_t>* counter = 0; |
| 27 | + |
| 28 | + }; |
| 29 | + |
| 30 | + template<class T> class BufferDispatcher{ |
| 31 | + |
| 32 | + public: |
| 33 | + |
| 34 | + BufferDispatcher(){;} |
| 35 | + ~BufferDispatcher(){Close();} |
| 36 | + bool Init(Buffer<T>* buffer, std::vector<AlgorithmWrapper<T> >* algorithms, JobQueue* job_queue, Pool<Job>* job_pool){ |
| 37 | + |
| 38 | + args.buffer = buffer; |
| 39 | + args.algorithms = algorithms; |
| 40 | + args.job_queue = job_queue; |
| 41 | + args.job_pool = job_pool; |
| 42 | + counter = 0; |
| 43 | + args.counter = &counter; |
| 44 | + |
| 45 | + if(buffer == 0 || algorithms == 0 || job_queue == 0 || job_pool == 0) return false; |
| 46 | + |
| 47 | + m_util.CreateThread("BufferDispatcher", &Thread, &args); |
| 48 | + |
| 49 | + } |
| 50 | + |
| 51 | + void Close(){ |
| 52 | + if(args.buffer == 0 || args.algorithms == 0 || args.job_queue == 0 || args.job_pool == 0) return; |
| 53 | + m_util.KillThread(&args); |
| 54 | + |
| 55 | + args.buffer = 0; |
| 56 | + args.algorithms = 0; |
| 57 | + args.job_queue = 0; |
| 58 | + args.job_pool = 0; |
| 59 | + |
| 60 | + } |
| 61 | + |
| 62 | + std::atomic<uint64_t> counter; |
| 63 | + |
| 64 | + |
| 65 | + private: |
| 66 | + |
| 67 | + static void Thread(Thread_args* arg){ |
| 68 | + BufferDispatcher_args<T>* args=reinterpret_cast<BufferDispatcher_args<T>*>(arg); |
| 69 | + if(args->algorithms->size()==0){ |
| 70 | + usleep(100); |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + args->buffer->Swap(args->local_buffer); |
| 75 | + |
| 76 | + if(args->local_buffer.size() == 0){ |
| 77 | + usleep(100); |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + for(size_t i = 0; i < args->local_buffer.size(); i++){ |
| 82 | + |
| 83 | + for(size_t j = 0; j < args->algorithms->size(); j++){ |
| 84 | + |
| 85 | + args->job = args->job_pool->GetNew(args->algorithms->at(j).name); |
| 86 | + args->job->m_id = args->algorithms->at(j).name; |
| 87 | + args->job->func = args->algorithms->at(j).algo; |
| 88 | + args->job->fail_func = args->algorithms->at(j).fail_func; |
| 89 | + args->job->data = args->algorithms->at(j).setup_func(args->local_buffer.at(i)); |
| 90 | + args->job->out_pool = args->job_pool; |
| 91 | + |
| 92 | + args->job_queue->AddJob(args->job); |
| 93 | + args->job = 0; |
| 94 | + (*args->counter)++; |
| 95 | + } |
| 96 | + |
| 97 | + args->local_buffer.at(i) = 0; |
| 98 | + |
| 99 | + } |
| 100 | + |
| 101 | + args->local_buffer.clear(); |
| 102 | + |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + BufferDispatcher_args<T> args; |
| 107 | + Utilities m_util; |
| 108 | + |
| 109 | + |
| 110 | + }; |
| 111 | + |
| 112 | +} |
| 113 | + |
| 114 | +#endif |
0 commit comments