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 /** \brief Closure represents a deferred computation */ 20 21 typedef struct { 22 void (*mHandler)(void *, int); 23 void *mContext; 24 int mParameter; 25 } Closure; 26 27 /** \brief ThreadPool manages a pool of worker threads that execute Closures */ 28 29 typedef struct { 30 unsigned mInitialized; ///< Indicates which of the following 3 fields are initialized 31 pthread_mutex_t mMutex; 32 pthread_cond_t mCondNotFull; ///< Signalled when a client thread could be unblocked 33 pthread_cond_t mCondNotEmpty; ///< Signalled when a worker thread could be unblocked 34 SLboolean mShutdown; ///< Whether shutdown of thread pool has been requested 35 unsigned mWaitingNotFull; ///< Number of client threads waiting to enqueue 36 unsigned mWaitingNotEmpty; ///< Number of worker threads waiting to dequeue 37 unsigned mMaxClosures; ///< Number of slots in circular buffer for closures, not counting spare 38 unsigned mMaxThreads; ///< Number of worker threads 39 Closure **mClosureArray; ///< The circular buffer of closures 40 Closure **mClosureFront, **mClosureRear; 41 /// Saves a malloc in the typical case 42 #define CLOSURE_TYPICAL 15 43 Closure *mClosureTypical[CLOSURE_TYPICAL+1]; 44 pthread_t *mThreadArray; ///< The worker threads 45 #ifdef ANDROID 46 #define THREAD_TYPICAL 0 47 #else 48 #define THREAD_TYPICAL 4 49 #endif 50 pthread_t mThreadTypical[THREAD_TYPICAL]; 51 } ThreadPool; 52 53 extern SLresult ThreadPool_init(ThreadPool *tp, unsigned maxClosures, unsigned maxThreads); 54 extern void ThreadPool_deinit(ThreadPool *tp); 55 extern SLresult ThreadPool_add(ThreadPool *tp, void (*handler)(void *, int), void *context, 56 int parameter); 57 extern Closure *ThreadPool_remove(ThreadPool *tp); 58