Facebook
From HappyGoKaboom, 3 Months ago, written in C++.
Embed
Download Paste or View Raw
Hits: 67
  1. //
  2. // Created by unknown on 04/01/19.
  3. //
  4.  
  5. #ifndef WORKER_H
  6. #define WORKER_H
  7.  
  8. #include <vector>
  9. #include <thread>
  10. #include <functional>
  11.  
  12. enum DISTRIBUTION {
  13.     STATIC_MODEL,
  14.     DYNAMIC_MODEL
  15. };
  16.  
  17. enum LOCKCONFIG {
  18.     READ,
  19.     WRITE,
  20.     READWRITE
  21. };
  22.  
  23. // T is your data type, A is the threading abstraction class ie StdThreading or SDL2Threading
  24. template <typename T>
  25. class Worker{
  26. public:
  27.     Worker(
  28.             DISTRIBUTION model,
  29.             unsigned int batch,
  30.             unsigned int maxThreads,
  31.             bool autoLock,
  32.             bool autoUnlock,
  33.             std::vector<std::vector<int>> threads,
  34.             std::vector<int> process,
  35.             std::vector<void*> processes,
  36.             std::vector<LOCKCONFIG> lockConfig)
  37.             :
  38.             model (model),
  39.             batch (batch),
  40.             autoLock (autoLock),
  41.             autoUnlock (autoUnlock),
  42.             maxThreads (maxThreads),
  43.             threads (std::move(threads)),
  44.             process (std::move(process)),
  45.             processes (std::move(processes)),
  46.             lockConfig (std::move(lockConfig))
  47.     {
  48.     };
  49.  
  50.     // copy constructor
  51.     Worker(const Worker &other) = delete;
  52.  
  53.     // copy assignment
  54.     Worker &operator=(const Worker &other) = delete;
  55.  
  56.     // move constructor
  57.     Worker(Worker &&other) noexcept {
  58.         threads = std::move(other.threads);
  59.         process = std::move(other.process);
  60.         processes = std::move(other.processes);
  61.         workloads = std::move(other.workloads);
  62.         lockConfig = std::move(other.lockConfig);
  63.         batch = std::move(other.batch);
  64.         maxThreads = std::move(other.maxThreads);
  65.         index = std::move(other.index);
  66.         model = std::move(other.model);
  67.         autoLock = std::move(other.autoLock);
  68.         autoUnlock = std::move(other.autoUnlock);
  69.     }
  70.  
  71.     // move assignment
  72.     Worker &operator=(Worker &&other) noexcept {
  73.         threads = std::move(other.threads);
  74.         process = std::move(other.process);
  75.         processes = std::move(other.processes);
  76.         workloads = std::move(other.workloads);
  77.         lockConfig = std::move(other.lockConfig);
  78.         batch = std::move(other.batch);
  79.         maxThreads = std::move(other.maxThreads);
  80.         index = std::move(other.index);
  81.         model = std::move(other.model);
  82.         autoLock = std::move(other.autoLock);
  83.         autoUnlock = std::move(other.autoUnlock);
  84.         return *this;
  85.     }
  86.  
  87.     // move pointer constructor
  88.     explicit Worker(Worker *other) {
  89.         threads = std::move(other->threads);
  90.         process = std::move(other->process);
  91.         processes = std::move(other->processes);
  92.         workloads = std::move(other->workloads);
  93.         lockConfig = std::move(other->lockConfig);
  94.         batch = std::move(other->batch);
  95.         maxThreads = std::move(other->maxThreads);
  96.         index = std::move(other->index);
  97.         model = std::move(other->model);
  98.         autoLock = std::move(other->autoLock);
  99.         autoUnlock = std::move(other->autoUnlock);
  100.     }
  101.  
  102.     // Worker Functions
  103.     void start (); // start the threads
  104.     void pause (); // pauses the threads
  105.     void stop (); // stop the threads
  106.     void terminate (); // terminate the threads
  107.     T* next (); // next workload item
  108.     void done (T*& item); // completed workload
  109.     static void workhandler(Worker<T>& worker, unsigned int threadID);
  110.  
  111.     // Thread Functions
  112.     void create(unsigned int threadID);
  113.     void detach();
  114.     void join();
  115.     void swap();
  116.     void getPriority();
  117.     void setPriority();
  118.  
  119.     std::vector<std::vector<int>> threads; // thread Configuration
  120.     std::vector<std::thread> threadsIds; // thread objects / ids
  121.     std::vector<int> process; // process order (functions)
  122.     std::vector<void*> processes; // the process functions themselves
  123.     std::vector<std::vector<int>> workloads; // list of each threads workload
  124.     std::vector<LOCKCONFIG> lockConfig; // lock config
  125.     int batch = 1;
  126.     unsigned int maxThreads = 0;
  127.     unsigned int index = 0;
  128.     DISTRIBUTION model;
  129.     bool autoLock = false;
  130.     bool autoUnlock = false;
  131. };
  132.  
  133. /* -------------------------------------------------------------------------------------------------------/
  134.  *      Worker Functions
  135.  * ----------------------------------------------------------------------------------------------------*/
  136. template <typename T>
  137. void Worker<T>::start() {
  138.     std::cout << "Worker Start" << std::endl;
  139.  
  140.     // check threads have not being started
  141.  
  142.     // create configuration
  143.  
  144.     // start / create threads according to thread config
  145.     for (unsigned int thread = 0; thread < maxThreads; ++thread) {
  146.         create(thread); // will need to pass the
  147.     }
  148. }
  149.  
  150. template <typename T>
  151. void Worker<T>::pause() {
  152.  
  153. }
  154.  
  155. template <typename T>
  156. void Worker<T>::stop() {
  157.  
  158. }
  159.  
  160. template <typename T>
  161. void Worker<T>::terminate() {
  162.  
  163. }
  164.  
  165. template <typename T>
  166. T* Worker<T>::next() {
  167.  
  168. }
  169.  
  170. template <typename T>
  171. void Worker<T>::done(T*& item) {
  172.  
  173. }
  174.  
  175. template<typename T>
  176. void Worker<T>::workhandler(Worker<T>& worker, unsigned int threadID) { // the static callback function
  177.     std::cout << "THREAD HANDLER RUN" << std::endl;
  178.     return;
  179.     /* IGNORE
  180.     while (worker.hasWork) { // process each item until no work
  181.         T work = *worker.next(); // pointer to next item or nullptr if no valid item
  182.  
  183.         // Lock if there is a locking config
  184.         if (worker.autoLock) {
  185.             switch (work.lock[work.stage]) {
  186.                 case READ:
  187.                     work.read.lock();
  188.                     break;
  189.                 case WRITE:
  190.                     work.write.lock();
  191.                     break;
  192.                 case READWRITE:
  193.                     work.readwrite.lock();
  194.                     break;
  195.             }
  196.         }
  197.  
  198.         worker.funcs[work.stage](work); // call the current stage function
  199.  
  200.         // Unlock if there is a locking config (user can unlock in there function, this is a garantee)
  201.         if (worker.autoUnlock) {
  202.             switch (work.lock[work.stage]) {
  203.                 case READ:
  204.                     work.read.lock();
  205.                     break;
  206.                 case WRITE:
  207.                     work.write.lock();
  208.                     break;
  209.                 case READWRITE:
  210.                     work.readwrite.lock();
  211.                     break;
  212.             }
  213.         }
  214.  
  215.         // work is now done, move to next stage batch
  216.         worker.done(work); // or worker.done(work, FINAL_PROCESS) // skip to specific process
  217.     }
  218.  */
  219.  
  220. }
  221.  
  222. /* -------------------------------------------------------------------------------------------------------/
  223.  *      Thread Functions -- std::thread @TODO and DEFINES
  224.  * ----------------------------------------------------------------------------------------------------*/
  225. template<typename T>
  226. void Worker<T>::create(unsigned int threadID) {
  227.     // create thread, call static handler, instance and threadID
  228.     // ERROR !!! No matching function call
  229.     std::thread f(std::bind(&Worker<T>::workhandler, this, threadID));
  230. }
  231.  
  232. template<typename T>
  233. void Worker<T>::detach() {
  234.  
  235. }
  236.  
  237. template<typename T>
  238. void Worker<T>::join() {
  239.  
  240. }
  241.  
  242. template<typename T>
  243. void Worker<T>::swap() {
  244.  
  245. }
  246.  
  247. template<typename T>
  248. void Worker<T>::getPriority() {
  249.  
  250. }
  251.  
  252. template<typename T>
  253. void Worker<T>::setPriority() {
  254.  
  255. }
  256.  
  257. /* -------------------------------------------------------------------------------------------------------/
  258.  *      Thread Functions -- SDL2 threads @TODO and DEFINES
  259.  * ----------------------------------------------------------------------------------------------------*/
  260.  
  261. #endif //GLTESTING_WORKER_H
  262.