Home | History | Annotate | Download | only in src

Lines Matching refs:tp

25     ThreadPool *tp = (ThreadPool *) context;
26 assert(NULL != tp);
28 Closure *pClosure = ThreadPool_remove(tp);
81 static void ThreadPool_deinit_internal(ThreadPool *tp, unsigned initialized, unsigned nThreads);
87 SLresult ThreadPool_init(ThreadPool *tp, unsigned maxClosures, unsigned maxThreads)
89 assert(NULL != tp);
90 memset(tp, 0, sizeof(ThreadPool));
91 tp->mShutdown = SL_BOOLEAN_FALSE;
98 err = pthread_mutex_init(&tp->mMutex, (const pthread_mutexattr_t *) NULL);
103 err = pthread_cond_init(&tp->mCondNotFull, (const pthread_condattr_t *) NULL);
108 err = pthread_cond_init(&tp->mCondNotEmpty, (const pthread_condattr_t *) NULL);
115 tp->mWaitingNotFull = 0;
116 tp->mWaitingNotEmpty = 0;
119 tp->mMaxClosures = maxClosures;
122 tp->mMaxThreads = maxThreads;
126 tp->mClosureArray = tp->mClosureTypical;
128 tp->mClosureArray = (Closure **) malloc((maxClosures + 1) * sizeof(Closure *));
129 if (NULL == tp->mClosureArray) {
134 tp->mClosureFront = tp->mClosureArray;
135 tp->mClosureRear = tp->mClosureArray;
139 tp->mThreadArray = tp->mThreadTypical;
141 tp->mThreadArray = (pthread_t *) malloc(maxThreads * sizeof(pthread_t));
142 if (NULL == tp->mThreadArray) {
149 int err = pthread_create(&tp->mThreadArray[i], (const pthread_attr_t *) NULL,
150 ThreadPool_start, tp);
156 tp->mInitialized = initialized;
163 ThreadPool_deinit_internal(tp, initialized, nThreads);
167 static void ThreadPool_deinit_internal(ThreadPool *tp, unsigned initialized, unsigned nThreads)
171 assert(NULL != tp);
175 ok = pthread_mutex_lock(&tp->mMutex);
177 tp->mShutdown = SL_BOOLEAN_TRUE;
178 ok = pthread_cond_broadcast(&tp->mCondNotEmpty);
180 ok = pthread_cond_broadcast(&tp->mCondNotFull);
182 ok = pthread_mutex_unlock(&tp->mMutex);
186 ok = pthread_join(tp->mThreadArray[i], (void **) NULL);
191 ok = pthread_mutex_lock(&tp->mMutex);
193 Closure **oldFront = tp->mClosureFront;
194 while (oldFront != tp->mClosureRear) {
196 if (++newFront == &tp->mClosureArray[tp->mMaxClosures + 1])
197 newFront = tp->mClosureArray;
201 tp->mClosureFront = newFront;
202 ok = pthread_mutex_unlock(&tp->mMutex);
205 ok = pthread_mutex_lock(&tp->mMutex);
208 ok = pthread_mutex_unlock(&tp->mMutex);
215 ok = pthread_cond_destroy(&tp->mCondNotEmpty);
219 ok = pthread_cond_destroy(&tp->mCondNotFull);
223 ok = pthread_mutex_destroy(&tp->mMutex);
226 tp->mInitialized = INITIALIZED_NONE;
229 if (tp->mClosureTypical != tp->mClosureArray && NULL != tp->mClosureArray) {
230 free(tp->mClosureArray);
231 tp->mClosureArray = NULL;
235 if (tp->mThreadTypical != tp->mThreadArray && NULL != tp->mThreadArray) {
236 free(tp->mThreadArray);
237 tp->mThreadArray = NULL;
242 void ThreadPool_deinit(ThreadPool *tp)
244 ThreadPool_deinit_internal(tp, tp->mInitialized, tp->mMaxThreads);
250 SLresult ThreadPool_add(ThreadPool *tp, ClosureKind kind, ClosureHandler_generic handler,
253 assert(NULL != tp);
280 ok = pthread_mutex_lock(&tp->mMutex);
283 if (tp->mShutdown) {
284 ok = pthread_mutex_unlock(&tp->mMutex);
290 Closure **oldRear = tp->mClosureRear;
292 if (++newRear == &tp->mClosureArray[tp->mMaxClosures + 1])
293 newRear = tp->mClosureArray;
295 if (newRear == tp->mClosureFront) {
296 ++tp->mWaitingNotFull;
297 ok = pthread_cond_wait(&tp->mCondNotFull, &tp->mMutex);
300 if (tp->mShutdown) {
301 assert(0 < tp->mWaitingNotFull);
302 --tp->mWaitingNotFull;
303 ok = pthread_mutex_unlock(&tp->mMutex);
312 tp->mClosureRear = newRear;
314 if (0 < tp->mWaitingNotEmpty) {
315 --tp->mWaitingNotEmpty;
316 ok = pthread_cond_signal(&tp->mCondNotEmpty);
321 ok = pthread_mutex_unlock(&tp->mMutex);
327 Closure *ThreadPool_remove(ThreadPool *tp)
331 ok = pthread_mutex_lock(&tp->mMutex);
335 if (tp->mShutdown) {
339 Closure **oldFront = tp->mClosureFront;
341 if (oldFront == tp->mClosureRear) {
342 ++tp->mWaitingNotEmpty;
343 ok = pthread_cond_wait(&tp->mCondNotEmpty, &tp->mMutex);
350 if (++newFront == &tp->mClosureArray[tp->mMaxClosures + 1]) {
351 newFront = tp->mClosureArray;
356 tp->mClosureFront = newFront;
358 if (0 < tp->mWaitingNotFull) {
359 --tp->mWaitingNotFull;
360 ok = pthread_cond_signal(&tp->mCondNotFull);
365 ok = pthread_mutex_unlock(&tp->mMutex);
371 SLresult ThreadPool_add_ppi(ThreadPool *tp, ClosureHandler_ppi handler,
375 return ThreadPool_add(tp, CLOSURE_KIND_PPI, (ClosureHandler_generic) handler,
379 SLresult ThreadPool_add_ppii(ThreadPool *tp, ClosureHandler_ppii handler,
383 return ThreadPool_add(tp, CLOSURE_KIND_PPII, (ClosureHandler_generic) handler,
387 SLresult ThreadPool_add_piipp(ThreadPool *tp, ClosureHandler_piipp handler,
391 return ThreadPool_add(tp, CLOSURE_KIND_PIIPP, (ClosureHandler_generic) handler,