thread.h

00001 /*              $Id: $          */
00002 
00003 /*
00004  * Copyright (c) 2007 Mark Heily <devel@heily.com>
00005  *
00006  * Permission to use, copy, modify, and distribute this software for any
00007  * purpose with or without fee is hereby granted, provided that the above
00008  * copyright notice and this permission notice appear in all copies.
00009  *
00010  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
00011  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
00012  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
00013  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00014  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00015  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
00016  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00017  */
00018 
00019 #ifndef _THREAD_H_
00020 #define _THREAD_H_
00021 
00022 /* 
00023  * These macros provide a subset of the POSIX threads API 
00024  * even when compiling without support for threads.
00025  */
00026 #ifdef _REENTRANT
00027 
00028 #include <pthread.h>
00029 
00030 /*
00031  * Convenience macros for locking/unlocking structures 
00032  * which contain a 'mutex' member.
00033  */
00034 #define mutex_debug 0
00035 
00036 #define mutex_lock(st)          do {                                    \
00037    if (mutex_debug)                                                     \
00038         printf("%s takes the lock\n", __func__);                        \
00039    (void) pthread_mutex_lock(&st->mutex);                               \
00040 } while (0)
00041 
00042 #define mutex_unlock(st)        do {                                    \
00043    if (mutex_debug)                                                     \
00044         printf("  %s releases the lock\n", __func__);                   \
00045    (void) pthread_mutex_unlock(&st->mutex);                             \
00046 } while (0)
00047 
00048 
00049 #else 
00050 
00051 /* All locking operations become a NOOP when thread support is disabled */
00052 
00053 #define pthread_noop             (0)
00054 #define pthread_t                int
00055 #define pthread_key_t            int
00056 #define pthread_mutex_t          int
00057 #define pthread_mutex_init(x,y)  pthread_noop
00058 #define pthread_mutex_lock(x)    pthread_noop
00059 #define pthread_mutex_unlock(x)  pthread_noop
00060 #define pthread_key_create(x,y)  pthread_noop
00061 #define pthread_key_delete(x)    pthread_noop
00062 #define pthread_getspecific(x)   pthread_noop
00063 #define pthread_setspecific(x,y) pthread_noop
00064 #define pthread_sigmask(x,y,z)   setsigmask(x,y,z)
00065 #define mutex_lock(x)            do { ; } while (0)
00066 #define mutex_unlock(x)          do { ; } while (0)
00067 
00068 /* pthread_once is emulated for single-threaded code */
00069 #define pthread_once_t           int
00070 #define PTHREAD_ONCE_INIT        0
00071 #define pthread_once(var,func)   (*var = 1)
00072 #define FIXME_pthread_once(var,func)    do {                            \
00073           if (!*var)                                                    \
00074                { func(); *var = 1; }                                    \
00075         } while (0)
00076 
00077 /* 
00078  * pthread_cleanup_push() is not emulated since all resources are freed
00079  * when a process exits. This means that cleanup handlers should *only*
00080  * be used to deallocate memory.
00081  */
00082 #define FIXME pthread_cleanup_push(x,y) pthread_noop
00083 
00084 #endif /* _REENTRANT */
00085 
00086 #endif /* _THREAD_H_ */

Generated on Wed Aug 22 23:15:42 2007 for pnotify by  doxygen 1.5.1