21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

typedef struct _spc_threadpool_task {<br />

spc_threadpool_fnptr fnptr;<br />

void *arg;<br />

struct _spc_threadpool_task *next;<br />

} spc_threadpool_task;<br />

typedef struct {<br />

int size;<br />

int destroy;<br />

#ifndef WIN32<br />

pthread_t *tids;<br />

pthread_cond_t cond;<br />

#else<br />

HANDLE *tids;<br />

HANDLE cond;<br />

#endif<br />

spc_threadpool_task *tasks;<br />

spc_threadpool_task *tail;<br />

} spc_threadpool_t;<br />

#ifndef WIN32<br />

#define SPC_ACQUIRE_MUTEX(mtx) pthread_mutex_lock(&(mtx))<br />

#define SPC_RELEASE_MUTEX(mtx) pthread_mutex_unlock(&(mtx))<br />

#define SPC_CREATE_COND(cond) pthread_cond_init(&(cond), 0)<br />

#define SPC_DESTROY_COND(cond) pthread_cond_destroy(&(cond))<br />

#define SPC_SIGNAL_COND(cond) pthread_cond_signal(&(cond))<br />

#define SPC_BROADCAST_COND(cond) pthread_cond_broadcast(&(cond))<br />

#define SPC_WAIT_COND(cond, mtx) pthread_cond_wait(&(cond), &(mtx))<br />

#define SPC_CLEANUP_PUSH(func, arg) pthread_cleanup_push(func, arg)<br />

#define SPC_CLEANUP_POP(exec) pthread_cleanup_pop(exec)<br />

#define SPC_CREATE_THREAD(t, f, arg) (!pthread_create(&(t), 0, (f), (arg)))<br />

static pthread_mutex_t threadpool_mutex = PTHREAD_MUTEX_INITIALIZER;<br />

#else<br />

#define SPC_ACQUIRE_MUTEX(mtx) WaitForSingleObjectEx((mtx), INFINITE, FALSE)<br />

#define SPC_RELEASE_MUTEX(mtx) ReleaseMutex((mtx))<br />

#define SPC_CREATE_COND(cond) (cond) = CreateEvent(0, TRUE, FALSE, 0)<br />

#define SPC_DESTROY_COND(cond) CloseHandle((cond))<br />

#define SPC_SIGNAL_COND(cond) SetEvent((cond))<br />

#define SPC_BROADCAST_COND(cond) PulseEvent((cond))<br />

#define SPC_WAIT_COND(cond, mtx) spc_win32_wait_cond((cond), (mtx))<br />

#define SPC_CLEANUP_PUSH(func, arg) { void (*__spc_func)(void *) = (func); \<br />

void *__spc_arg = (arg)<br />

#define SPC_CLEANUP_POP(exec) if ((exec)) __spc_func(__spc_arg); } \<br />

do { } while (0)<br />

#define SPC_CREATE_THREAD(t, f, arg) ((t) = CreateThread(0, 0, (f), (arg), 0, 0))<br />

static HANDLE threadpool_mutex = 0;<br />

#endif<br />

#ifdef WIN32<br />

static void spc_win32_wait_cond(HANDLE cond, HANDLE mutex) {<br />

HANDLE handles[2];<br />

720 | Chapter 13: Other Topics<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!