1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
/* $Id$
*
* This file is part of the ESO C Extension Library
* Copyright (C) 2001-2011 European Southern Observatory
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
* $Author$
* $Date$
* $Revision$
* $Name$
*/
#ifndef CXTHREAD_H_
#define CXTHREAD_H_
#if HAVE_CONFIG_H
# include "config.h"
#endif
#ifdef HAVE_PTHREAD_H
# include <pthread.h>
#endif
#include <cxtypes.h>
/*
* Map local types and functions to the POSIX thread model implementation
*/
#if defined(CX_THREADS_ENABLED)
#if defined(HAVE_PTHREAD_H)
#define CX_STATIC_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
#define CX_STATIC_ONCE_INIT PTHREAD_ONCE_INIT
typedef pthread_mutex_t cx_mutex;
typedef pthread_once_t cx_once;
typedef pthread_key_t cx_private;
#define cx_mutex_lock(mutex) pthread_mutex_lock((mutex))
#define cx_mutex_trylock(mutex) pthread_mutex_trylock((mutex))
#define cx_mutex_unlock(mutex) pthread_mutex_unlock((mutex))
#define cx_thread_once(name, func, args) pthread_once(&(name), (func))
#define cx_private_init(name, func) pthread_key_create(&(name), (func))
#define cx_private_set(name, data) pthread_setspecific((name), (data))
#define cx_private_get(name) pthread_getspecific((name))
#else /* !HAVE_PTHREAD_H */
# error "Thread support is requested, but POSIX thread model is not present!"
#endif /* !HAVE_PTHREAD_H */
#else /* !CX_THREADS_ENABLED */
typedef struct cx_private cx_private;
#define cx_mutex_lock(mutex) /* empty */
#define cx_mutex_trylock(mutex) /* empty */
#define cx_mutex_unlock(mutex) /* empty */
#define cx_thread_once(name, func, args) (func)()
#define cx_private_init(name, func) /* empty */
#define cx_private_set(name, data) ((name) = (data))
#define cx_private_get(name) (name)
#endif /* !CX_THREADS_ENABLED */
/*
* Convenience macros to setup locks for global variables.
* These macros expand to nothing, if thread support was not enabled.
*/
#define CX_LOCK_NAME(name) _cx__ ## name ## _lock
#if defined(CX_THREADS_ENABLED)
# define CX_LOCK_DEFINE_STATIC(name) static CX_LOCK_DEFINE(name)
# define CX_LOCK_DEFINE(name) cx_mutex CX_LOCK_NAME(name) = CX_STATIC_MUTEX_INIT
# define CX_LOCK_EXTERN(name) extern cx_mutex CX_LOCK_NAME(name)
# define CX_LOCK(name) cx_mutex_lock(&CX_LOCK_NAME(name))
# define CX_TRYLOCK(name) cx_mutex_trylock(&CX_LOCK_NAME(name))
# define CX_UNLOCK(name) cx_mutex_unlock(&CX_LOCK_NAME(name))
#else /* !CX_THREADS_ENABLED */
# define CX_LOCK_DEFINE_STATIC(name) /* empty */
# define CX_LOCK_DEFINE(name) /* empty */
# define CX_LOCK_EXTERN(name) /* empty */
# define CX_LOCK(name) /* empty */
# define CX_TRYLOCK(name) (TRUE)
# define CX_UNLOCK(name) /* empty */
#endif /* !CX_THREADS_ENABLED */
/*
* Convenience macros for setting up mutexes for one time initalizations
*/
#if defined(CX_THREADS_ENABLED)
# define CX_ONCE_DEFINE_STATIC(name) static CX_ONCE_DEFINE(name)
# define CX_ONCE_DEFINE(name) cx_once (name) = CX_STATIC_ONCE_INIT
#else /* !CX_THREADS_ENABLED */
# define CX_ONCE_DEFINE_STATIC(name) /* empty */
# define CX_ONCE_DEFINE(name) /* empty */
#endif /* !CX_THREADS_ENABLED */
/*
* Convenience macros for setting up thread-specific data
*/
#if defined(CX_THREADS_ENABLED)
# define CX_PRIVATE_DEFINE_STATIC(name) cx_private (name)
#else /* !CX_THREADS_ENABLED */
# define CX_PRIVATE_DEFINE_STATIC(name) static cx_private *(name)
#endif /* !CX_THREADS_ENABLED */
#endif /* CXTHREAD_H_ */
|