ThreadPool.h revision dd177e2d3923d4653eaa4226f07b89a999907970
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
21typedef 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
29typedef void (*ClosureHandler_generic)(void *p1, void *p2, void *p3, int i1, int i2);
30typedef void (*ClosureHandler_ppi)    (void *p1, void *p2, int i1);
31typedef void (*ClosureHandler_ppii)   (void *p1, void *p2, int i1, int i2);
32typedef void (*ClosureHandler_piipp)  (void *p1, int i1, int i2, void *p2, void *p3);
33
34/** \brief Closure represents a deferred computation */
35
36typedef 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
52typedef 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// Note: if you set THREAD_TYPICAL to a non-zero value because you
70// want to use asynchronous callbacks, be aware that any value greater
71// than 1 can result in out-of-order callbacks on a given player, in the
72// current implementation.  Thus you should probably configure 1 total or
73// change the implementation so that it uses at most 1 thread per player.
74#if defined(USE_ASYNCHRONOUS_PLAY_CALLBACK) || \
75        defined(USE_ASYNCHRONOUS_STREAMCBEVENT_PROPERTYCHANGE_CALLBACK)
76#define THREAD_TYPICAL 1
77#else
78#define THREAD_TYPICAL 0
79#endif
80#else // !ANDROID
81#define THREAD_TYPICAL 4
82#endif
83    pthread_t mThreadTypical[THREAD_TYPICAL];
84} ThreadPool;
85
86extern SLresult ThreadPool_init(ThreadPool *tp, unsigned maxClosures, unsigned maxThreads);
87extern void ThreadPool_deinit(ThreadPool *tp);
88extern SLresult ThreadPool_add(ThreadPool *tp, ClosureKind kind,
89        ClosureHandler_generic,
90        void *cntxt1, void *cntxt2, int param1, int param2);
91extern Closure *ThreadPool_remove(ThreadPool *tp);
92extern SLresult ThreadPool_add_ppi(ThreadPool *tp, ClosureHandler_ppi handler,
93        void *cntxt1, void *cntxt2, int param1);
94extern SLresult ThreadPool_add_ppii(ThreadPool *tp, ClosureHandler_ppii handler,
95        void *cntxt1, void *cntxt2, int param1, int param2);
96extern SLresult ThreadPool_add_piipp(ThreadPool *tp, ClosureHandler_piipp handler,
97        void *cntxt1, int param1, int param2, void *cntxt2, void *cntxt3);
98