OSDN Git Service

5da7fa990a905426c0b1ad614903ce67932b4db4
[android-x86/system-media.git] / wilhelm / src / ThreadPool.h
1 /*
2  * Copyright (C) 2010 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 /** \file ThreadPool.h ThreadPool interface */
18
19 /** Kind of closure */
20
21 typedef enum {
22     CLOSURE_KIND_PPI,   // void *, void *, int
23     CLOSURE_KIND_PPII   // void *, void *, int, int
24 } ClosureKind;
25
26 /** Closure handlers */
27
28 typedef void (*ClosureHandler_ppi)(void *context1, void *context2, int parameter1);
29 typedef void (*ClosureHandler_ppii)(void *context1, void *context2, int parameter1, int parameter2);
30
31 /** \brief Closure represents a deferred computation */
32
33 typedef struct {
34     union {
35         ClosureHandler_ppi mHandler_ppi;
36         ClosureHandler_ppii mHandler_ppii;
37     } mHandler;
38     ClosureKind mKind;
39     void *mContext1;
40     void *mContext2;
41     int mParameter1;
42     int mParameter2;
43 } Closure;
44
45 /** \brief ThreadPool manages a pool of worker threads that execute Closures */
46
47 typedef struct {
48     unsigned mInitialized; ///< Indicates which of the following 3 fields are initialized
49     pthread_mutex_t mMutex;
50     pthread_cond_t mCondNotFull;    ///< Signalled when a client thread could be unblocked
51     pthread_cond_t mCondNotEmpty;   ///< Signalled when a worker thread could be unblocked
52     SLboolean mShutdown;   ///< Whether shutdown of thread pool has been requested
53     unsigned mWaitingNotFull;   ///< Number of client threads waiting to enqueue
54     unsigned mWaitingNotEmpty;  ///< Number of worker threads waiting to dequeue
55     unsigned mMaxClosures;  ///< Number of slots in circular buffer for closures, not counting spare
56     unsigned mMaxThreads;   ///< Number of worker threads
57     Closure **mClosureArray;    ///< The circular buffer of closures
58     Closure **mClosureFront, **mClosureRear;
59     /// Saves a malloc in the typical case
60 #define CLOSURE_TYPICAL 15
61     Closure *mClosureTypical[CLOSURE_TYPICAL+1];
62     pthread_t *mThreadArray;    ///< The worker threads
63 #ifdef ANDROID
64 #define THREAD_TYPICAL 2
65 #else
66 #define THREAD_TYPICAL 4
67 #endif
68     pthread_t mThreadTypical[THREAD_TYPICAL];
69 } ThreadPool;
70
71 extern SLresult ThreadPool_init(ThreadPool *tp, unsigned maxClosures, unsigned maxThreads);
72 extern void ThreadPool_deinit(ThreadPool *tp);
73 extern SLresult ThreadPool_add(ThreadPool *tp, ClosureKind kind,
74     void (*handler)(void *, void *, int, int), void *context1,
75     void *context2, int parameter1, int parameter2);
76 extern Closure *ThreadPool_remove(ThreadPool *tp);
77 extern SLresult ThreadPool_add_ppi(ThreadPool *tp, ClosureHandler_ppi handler,
78     void *context1, void *context2, int parameter1);
79 extern SLresult ThreadPool_add_ppii(ThreadPool *tp, ClosureHandler_ppii handler,
80     void *context1, void *context2, int parameter1, int parameter2);