OSDN Git Service

Async callback queue updates, applied to XAStreamInformationItf
[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     CLOSURE_KIND_PIIPP  // void *, int, int, void *, void *
25 } ClosureKind;
26
27 /** Closure handlers */
28
29 typedef void (*ClosureHandler_generic)(void *p1, void *p2, void *p3, int i1, int i2);
30 typedef void (*ClosureHandler_ppi)    (void *p1, void *p2, int i1);
31 typedef void (*ClosureHandler_ppii)   (void *p1, void *p2, int i1, int i2);
32 typedef void (*ClosureHandler_piipp)  (void *p1, int i1, int i2, void *p2, void *p3);
33
34 /** \brief Closure represents a deferred computation */
35
36 typedef struct {
37     union {
38         ClosureHandler_ppi mHandler_ppi;
39         ClosureHandler_ppii mHandler_ppii;
40         ClosureHandler_piipp mHandler_piipp;
41     } mHandler;
42     ClosureKind mKind;
43     void *mContext1;
44     void *mContext2;
45     void *mContext3;
46     int mParameter1;
47     int mParameter2;
48 } Closure;
49
50 /** \brief ThreadPool manages a pool of worker threads that execute Closures */
51
52 typedef struct {
53     unsigned mInitialized; ///< Indicates which of the following 3 fields are initialized
54     pthread_mutex_t mMutex;
55     pthread_cond_t mCondNotFull;    ///< Signalled when a client thread could be unblocked
56     pthread_cond_t mCondNotEmpty;   ///< Signalled when a worker thread could be unblocked
57     SLboolean mShutdown;   ///< Whether shutdown of thread pool has been requested
58     unsigned mWaitingNotFull;   ///< Number of client threads waiting to enqueue
59     unsigned mWaitingNotEmpty;  ///< Number of worker threads waiting to dequeue
60     unsigned mMaxClosures;  ///< Number of slots in circular buffer for closures, not counting spare
61     unsigned mMaxThreads;   ///< Number of worker threads
62     Closure **mClosureArray;    ///< The circular buffer of closures
63     Closure **mClosureFront, **mClosureRear;
64     /// Saves a malloc in the typical case
65 #define CLOSURE_TYPICAL 15
66     Closure *mClosureTypical[CLOSURE_TYPICAL+1];
67     pthread_t *mThreadArray;    ///< The worker threads
68 #ifdef ANDROID
69 #define THREAD_TYPICAL 2
70 #else
71 #define THREAD_TYPICAL 4
72 #endif
73     pthread_t mThreadTypical[THREAD_TYPICAL];
74 } ThreadPool;
75
76 extern SLresult ThreadPool_init(ThreadPool *tp, unsigned maxClosures, unsigned maxThreads);
77 extern void ThreadPool_deinit(ThreadPool *tp);
78 extern SLresult ThreadPool_add(ThreadPool *tp, ClosureKind kind,
79         ClosureHandler_generic,
80         void *cntxt1, void *cntxt2, int param1, int param2);
81 extern Closure *ThreadPool_remove(ThreadPool *tp);
82 extern SLresult ThreadPool_add_ppi(ThreadPool *tp, ClosureHandler_ppi handler,
83         void *cntxt1, void *cntxt2, int param1);
84 extern SLresult ThreadPool_add_ppii(ThreadPool *tp, ClosureHandler_ppii handler,
85         void *cntxt1, void *cntxt2, int param1, int param2);
86 extern SLresult ThreadPool_add_piipp(ThreadPool *tp, ClosureHandler_piipp handler,
87         void *cntxt1, int param1, int param2, void *cntxt2, void *cntxt3);