• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
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);
getWorkerCount()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         // returns true if the signal occured
52         // false for timeout
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     //static void * threadProc(void *);
77     static void * helperThreadProc(void *);
78 
79 
80 };
81 
82 
83 #endif
84