C Standard Library Extensions  1.1
cxthread.h
00001 /* $Id$
00002  *
00003  * This file is part of the ESO C Extension Library
00004  * Copyright (C) 2001-2011 European Southern Observatory
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019  */
00020 
00021 /*
00022  * $Author$
00023  * $Date$
00024  * $Revision$
00025  * $Name$
00026  */
00027 
00028 #ifndef CXTHREAD_H_
00029 #define CXTHREAD_H_
00030 
00031 #if HAVE_CONFIG_H
00032 #  include "config.h"
00033 #endif
00034 
00035 #ifdef HAVE_PTHREAD_H
00036 #  include <pthread.h>
00037 #endif
00038 
00039 #include <cxtypes.h>
00040 
00041 
00042 /*
00043  * Map local types and functions to the POSIX thread model implementation
00044  */
00045 
00046 #if defined(CX_THREADS_ENABLED)
00047 
00048 #if defined(HAVE_PTHREAD_H)
00049 
00050 #define CX_STATIC_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
00051 #define CX_STATIC_ONCE_INIT  PTHREAD_ONCE_INIT
00052 
00053 typedef pthread_mutex_t cx_mutex;
00054 typedef pthread_once_t  cx_once;
00055 typedef pthread_key_t   cx_private;
00056 
00057 #define cx_mutex_lock(mutex)    pthread_mutex_lock((mutex))
00058 #define cx_mutex_trylock(mutex) pthread_mutex_trylock((mutex))
00059 #define cx_mutex_unlock(mutex)  pthread_mutex_unlock((mutex))
00060 
00061 #define cx_thread_once(name, func, args)  pthread_once(&(name), (func))
00062 
00063 #define cx_private_init(name, func)  pthread_key_create(&(name), (func))
00064 #define cx_private_set(name, data)   pthread_setspecific((name), (data))
00065 #define cx_private_get(name)         pthread_getspecific((name))
00066 
00067 #else  /* !HAVE_PTHREAD_H */
00068 #  error "Thread support is requested, but POSIX thread model is not present!"
00069 #endif /* !HAVE_PTHREAD_H */
00070 
00071 #else  /* !CX_THREADS_ENABLED */
00072 
00073 typedef struct cx_private cx_private;
00074 
00075 #define cx_mutex_lock(mutex)    /* empty */
00076 #define cx_mutex_trylock(mutex) /* empty */
00077 #define cx_mutex_unlock(mutex)  /* empty */
00078 
00079 #define cx_thread_once(name, func, args) (func)()
00080 
00081 #define cx_private_init(name, func)  /* empty */
00082 #define cx_private_set(name, data)   ((name) = (data))
00083 #define cx_private_get(name)         (name)
00084 
00085 #endif /* !CX_THREADS_ENABLED */
00086 
00087 
00088 /*
00089  * Convenience macros to setup locks for global variables.
00090  * These macros expand to nothing, if thread support was not enabled.
00091  */
00092 
00093 #define CX_LOCK_NAME(name)  _cx__ ## name ## _lock
00094 
00095 #if defined(CX_THREADS_ENABLED)
00096 
00097 #  define CX_LOCK_DEFINE_STATIC(name)  static CX_LOCK_DEFINE(name)
00098 #  define CX_LOCK_DEFINE(name)         cx_mutex CX_LOCK_NAME(name) = CX_STATIC_MUTEX_INIT
00099 #  define CX_LOCK_EXTERN(name)         extern cx_mutex CX_LOCK_NAME(name)
00100 
00101 #  define CX_LOCK(name)     cx_mutex_lock(&CX_LOCK_NAME(name))
00102 #  define CX_TRYLOCK(name)  cx_mutex_trylock(&CX_LOCK_NAME(name))
00103 #  define CX_UNLOCK(name)   cx_mutex_unlock(&CX_LOCK_NAME(name))
00104 
00105 #else /* !CX_THREADS_ENABLED */
00106 
00107 #  define CX_LOCK_DEFINE_STATIC(name)  /* empty */
00108 #  define CX_LOCK_DEFINE(name)         /* empty */
00109 #  define CX_LOCK_EXTERN(name)         /* empty */
00110 
00111 #  define CX_LOCK(name)     /* empty */
00112 #  define CX_TRYLOCK(name)  (TRUE)
00113 #  define CX_UNLOCK(name)   /* empty */
00114 
00115 #endif /* !CX_THREADS_ENABLED */
00116 
00117 
00118 /*
00119  * Convenience macros for setting up mutexes for one time initalizations
00120  */
00121 
00122 #if defined(CX_THREADS_ENABLED)
00123 
00124 #  define CX_ONCE_DEFINE_STATIC(name)  static CX_ONCE_DEFINE(name)
00125 #  define CX_ONCE_DEFINE(name)         cx_once (name) = CX_STATIC_ONCE_INIT
00126 
00127 #else /* !CX_THREADS_ENABLED */
00128 
00129 #  define CX_ONCE_DEFINE_STATIC(name)  /* empty */
00130 #  define CX_ONCE_DEFINE(name)         /* empty */
00131 
00132 #endif /* !CX_THREADS_ENABLED */
00133 
00134 
00135 /*
00136  * Convenience macros for setting up thread-specific data
00137  */
00138 
00139 #if defined(CX_THREADS_ENABLED)
00140 
00141 #  define CX_PRIVATE_DEFINE_STATIC(name)  cx_private (name)
00142 
00143 #else /* !CX_THREADS_ENABLED */
00144 
00145 #  define CX_PRIVATE_DEFINE_STATIC(name)  static cx_private *(name)
00146 
00147 #endif /* !CX_THREADS_ENABLED */
00148 
00149 #endif /* CXTHREAD_H_ */