| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#ifndef ANDROID_WORKER_POOL_H |
| 18 |
#define ANDROID_WORKER_POOL_H |
| 19 |
|
| 20 |
#include <pthread.h> |
| 21 |
#include <string.h> |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
class WorkerPool { |
| 26 |
public: |
| 27 |
WorkerPool(); |
| 28 |
~WorkerPool(); |
| 29 |
|
| 30 |
typedef void (*WorkerCallback_t)(void *usr, uint32_t idx); |
| 31 |
|
| 32 |
bool init(int threadCount = -1); |
| 33 |
int getWorkerCount() const {return mCount;} |
| 34 |
|
| 35 |
void waitForAll() const; |
| 36 |
void waitFor(uint64_t) const; |
| 37 |
uint64_t launchWork(WorkerCallback_t cb, void *usr, int maxThreads = -1); |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
protected: |
| 43 |
class Signal { |
| 44 |
public: |
| 45 |
Signal(); |
| 46 |
~Signal(); |
| 47 |
|
| 48 |
bool init(); |
| 49 |
void set(); |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
bool wait(uint64_t timeout = 0); |
| 54 |
|
| 55 |
protected: |
| 56 |
bool mSet; |
| 57 |
pthread_mutex_t mMutex; |
| 58 |
pthread_cond_t mCondition; |
| 59 |
}; |
| 60 |
|
| 61 |
bool mExit; |
| 62 |
volatile int mRunningCount; |
| 63 |
volatile int mLaunchCount; |
| 64 |
uint32_t mCount; |
| 65 |
pthread_t *mThreadId; |
| 66 |
pid_t *mNativeThreadId; |
| 67 |
Signal mCompleteSignal; |
| 68 |
Signal *mLaunchSignals; |
| 69 |
WorkerCallback_t mLaunchCallback; |
| 70 |
void *mLaunchData; |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
private: |
| 76 |
|
| 77 |
static void * helperThreadProc(void *); |
| 78 |
|
| 79 |
|
| 80 |
}; |
| 81 |
|
| 82 |
|
| 83 |
#endif |