2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 /** \file ThreadPool.h ThreadPool interface */
19 /** Kind of closure */
22 CLOSURE_KIND_PPI, // void *, void *, int
23 CLOSURE_KIND_PPII // void *, void *, int, int
26 /** Closure handlers */
28 typedef void (*ClosureHandler_ppi)(void *context1, void *context2, int parameter1);
29 typedef void (*ClosureHandler_ppii)(void *context1, void *context2, int parameter1, int parameter2);
31 /** \brief Closure represents a deferred computation */
35 ClosureHandler_ppi mHandler_ppi;
36 ClosureHandler_ppii mHandler_ppii;
45 /** \brief ThreadPool manages a pool of worker threads that execute Closures */
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
64 #define THREAD_TYPICAL 2
66 #define THREAD_TYPICAL 4
68 pthread_t mThreadTypical[THREAD_TYPICAL];
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);