forked from antmicro/android-camera-hal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorkers.h
More file actions
93 lines (72 loc) · 1.98 KB
/
Workers.h
File metadata and controls
93 lines (72 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#ifndef WORKERS_H
#define WORKERS_H
#include <pthread.h>
#include <utils/Mutex.h>
#include <utils/Condition.h>
#include <utils/Vector.h>
#include <utils/List.h>
namespace android {
class Workers {
public:
class Task {
public:
typedef void (*Function)(void *);
Task(Function fn, void *data): mFn(fn), mData(data), mCompleted(false) {}
Task(): Task(NULL, NULL) {}
Task& operator=(Task &&other) {
mFn = other.mFn;
mData = other.mData;
mCompleted = other.mCompleted;
return *this;
}
void waitForCompletion() {
mMutex.lock();
if(!mCompleted) {
mCond.wait(mMutex);
}
mMutex.unlock();
}
void execute() {
Mutex::Autolock lock(mMutex);
mFn(mData);
mCompleted = true;
mCond.signal();
}
private:
Mutex mMutex;
Condition mCond;
Function mFn;
void *mData;
bool mCompleted;
};
Workers();
~Workers() {}
bool start();
void stop();
bool isRunning() const { return mRunning; }
unsigned threadsNum() { return (unsigned)mThreads.size(); }
void queueTask(Task *task);
private:
class Thread {
public:
Thread(int id, Workers *parent): mId(id), mParent(parent) {}
Thread(): mId(-1), mParent(NULL) {}
void run() { pthread_create(&mThread, NULL, threadLoop, this); }
void join() { pthread_join(mThread, NULL); }
private:
int mId;
pthread_t mThread;
Workers *mParent;
static void * threadLoop(void *t);
};
friend class Thread;
bool mRunning;
bool mExitRequest;
Mutex mMutex;
Condition mCond;
List<Task *> mTasks;
Vector<Thread> mThreads;
};
extern Workers gWorkers;
}; /* namespace android */
#endif // WORKERS_H