lib/pthreadWrap.c

Go to the documentation of this file.
00001 /* pthreadWrap - error checking wrappers around Posix
00002  * thread functions.  Most of the errors here are invariably
00003  * fatal, but shouldn't happen unless the kernal or
00004  * the program is hosed. */
00005 
00006 #include "common.h"
00007 #include "errabort.h"
00008 #include "pthreadWrap.h"
00009 
00010 static char const rcsid[] = "$Id: pthreadWrap.c,v 1.3 2003/05/06 07:33:44 kate Exp $";
00011 
00012 static void pwarn(char *function, int err)
00013 /* Print a warning message on non-zero error code. */
00014 {
00015 if (err != 0)
00016     warn("Couldn't %s: %s\n", function, strerror(err));
00017 }
00018 
00019 static void perr(char *function, int err)
00020 /* Print out error for function and abort on
00021  * non-zero error code.. */
00022 {
00023 if (err != 0)
00024     {
00025     pwarn(function, err);
00026     noWarnAbort();
00027     }
00028 }
00029 
00030 void pthreadCreate(pthread_t *thread, const pthread_attr_t *attr,
00031         void *(*start_routine)(void *), void *arg)
00032 /* Create a thread or squawk and die. */
00033 {
00034 int err = pthread_create(thread, attr, start_routine, arg);
00035 perr("pthread_create", err);
00036 }
00037 
00038 boolean pthreadMayCreate(pthread_t *thread, const pthread_attr_t *attr,
00039         void *(*start_routine)(void *), void *arg)
00040 /* Create a thread.  Warn and return FALSE if there is a problem. */
00041 {
00042 int err = pthread_create(thread, attr, start_routine, arg);
00043 pwarn("pthread_create", err);
00044 return err == 0;
00045 }
00046 
00047 void pthreadMutexInit(pthread_mutex_t *mutex)
00048 /* Initialize mutex or die trying */
00049 {
00050 int err = pthread_mutex_init(mutex, NULL);
00051 perr("pthread_mutex_init", err);
00052 }
00053 
00054 void pthreadMutexDestroy(pthread_mutex_t *mutex)
00055 /* Free up mutex. */
00056 {
00057 int err = pthread_mutex_destroy(mutex);
00058 perr("pthread_mutex_destroy", err);
00059 }
00060 
00061 void pthreadMutexLock(pthread_mutex_t *mutex)
00062 /* Lock a mutex to gain exclusive access or die trying. */
00063 {
00064 int err = pthread_mutex_lock(mutex);
00065 perr("pthread_mutex_lock", err);
00066 }
00067 
00068 void pthreadMutexUnlock(pthread_mutex_t *mutex)
00069 /* Unlock a mutex or die trying. */
00070 {
00071 int err = pthread_mutex_unlock(mutex);
00072 perr("pthread_mutex_unlock", err);
00073 }
00074 
00075 void pthreadCondInit(pthread_cond_t *cond)
00076 /* Initialize pthread conditional. */
00077 {
00078 int err = pthread_cond_init(cond, NULL);
00079 perr("pthread_cond_init", err);
00080 }
00081 
00082 void pthreadCondDestroy(pthread_cond_t *cond)
00083 /* Free up conditional. */
00084 {
00085 int err = pthread_cond_destroy(cond);
00086 perr("pthread_cond_destroy", err);
00087 }
00088 
00089 void pthreadCondSignal(pthread_cond_t *cond)
00090 /* Set conditional signal to wake up a sleeping thread, or
00091  * die trying. */
00092 {
00093 int err = pthread_cond_signal(cond);
00094 perr("pthread_cond_signal", err);
00095 }
00096 
00097 void pthreadCondWait(pthread_cond_t *cond, pthread_mutex_t *mutex)
00098 /* Wait for conditional signal. */
00099 {
00100 int err = pthread_cond_wait(cond, mutex);
00101 perr("pthread_cond_wait", err);
00102 }
00103 

Generated on Tue Dec 25 18:39:31 2007 for blat by  doxygen 1.5.2