| 1 |
/***************************************************************************** |
| 2 |
* $Id: thread.h 2938 2003-11-26 23:01:18Z dun $ |
| 3 |
***************************************************************************** |
| 4 |
* Copyright (C) 2003 The Regents of the University of California. |
| 5 |
* Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER). |
| 6 |
* Written by Chris Dunlap <[email protected]>. |
| 7 |
* |
| 8 |
* This file is from LSD-Tools, the LLNL Software Development Toolbox. |
| 9 |
* |
| 10 |
* LSD-Tools is free software; you can redistribute it and/or modify it under |
| 11 |
* the terms of the GNU General Public License as published by the Free |
| 12 |
* Software Foundation; either version 2 of the License, or (at your option) |
| 13 |
* any later version. |
| 14 |
* |
| 15 |
* LSD-Tools is distributed in the hope that it will be useful, but WITHOUT |
| 16 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 17 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 18 |
* more details. |
| 19 |
* |
| 20 |
* You should have received a copy of the GNU General Public License along |
| 21 |
* with LSD-Tools; if not, write to the Free Software Foundation, Inc., |
| 22 |
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 23 |
*****************************************************************************/ |
| 24 |
|
| 25 |
|
| 26 |
#ifndef LSD_THREAD_H |
| 27 |
#define LSD_THREAD_H |
| 28 |
|
| 29 |
#if WITH_PTHREADS |
| 30 |
# include <errno.h> |
| 31 |
# include <pthread.h> |
| 32 |
# include <stdlib.h> |
| 33 |
#endif /* WITH_PTHREADS */ |
| 34 |
|
| 35 |
|
| 36 |
/***************************************************************************** |
| 37 |
* Macros |
| 38 |
*****************************************************************************/ |
| 39 |
|
| 40 |
#if WITH_PTHREADS |
| 41 |
|
| 42 |
# ifdef WITH_LSD_FATAL_ERROR_FUNC |
| 43 |
# undef lsd_fatal_error |
| 44 |
extern void lsd_fatal_error (char *file, int line, char *mesg); |
| 45 |
# else /* !WITH_LSD_FATAL_ERROR_FUNC */ |
| 46 |
# ifndef lsd_fatal_error |
| 47 |
# define lsd_fatal_error(file, line, mesg) (abort ()) |
| 48 |
# endif /* !lsd_fatal_error */ |
| 49 |
# endif /* !WITH_LSD_FATAL_ERROR_FUNC */ |
| 50 |
|
| 51 |
# define lsd_mutex_init(pmutex) \ |
| 52 |
do { \ |
| 53 |
int e = pthread_mutex_init (pmutex, NULL); \ |
| 54 |
if (e != 0) { \ |
| 55 |
errno = e; \ |
| 56 |
lsd_fatal_error (__FILE__, __LINE__, "mutex_init"); \ |
| 57 |
abort (); \ |
| 58 |
} \ |
| 59 |
} while (0) |
| 60 |
|
| 61 |
# define lsd_mutex_lock(pmutex) \ |
| 62 |
do { \ |
| 63 |
int e = pthread_mutex_lock (pmutex); \ |
| 64 |
if (e != 0) { \ |
| 65 |
errno = e; \ |
| 66 |
lsd_fatal_error (__FILE__, __LINE__, "mutex_lock"); \ |
| 67 |
abort (); \ |
| 68 |
} \ |
| 69 |
} while (0) |
| 70 |
|
| 71 |
# define lsd_mutex_unlock(pmutex) \ |
| 72 |
do { \ |
| 73 |
int e = pthread_mutex_unlock (pmutex); \ |
| 74 |
if (e != 0) { \ |
| 75 |
errno = e; \ |
| 76 |
lsd_fatal_error (__FILE__, __LINE__, "mutex_unlock"); \ |
| 77 |
abort (); \ |
| 78 |
} \ |
| 79 |
} while (0) |
| 80 |
|
| 81 |
# define lsd_mutex_destroy(pmutex) \ |
| 82 |
do { \ |
| 83 |
int e = pthread_mutex_destroy (pmutex); \ |
| 84 |
if (e != 0) { \ |
| 85 |
errno = e; \ |
| 86 |
lsd_fatal_error (__FILE__, __LINE__, "mutex_destroy"); \ |
| 87 |
abort (); \ |
| 88 |
} \ |
| 89 |
} while (0) |
| 90 |
|
| 91 |
# ifndef NDEBUG |
| 92 |
int lsd_mutex_is_locked (pthread_mutex_t *pmutex); |
| 93 |
# endif /* !NDEBUG */ |
| 94 |
|
| 95 |
#else /* !WITH_PTHREADS */ |
| 96 |
|
| 97 |
# define lsd_mutex_init(mutex) |
| 98 |
# define lsd_mutex_lock(mutex) |
| 99 |
# define lsd_mutex_unlock(mutex) |
| 100 |
# define lsd_mutex_destroy(mutex) |
| 101 |
# define lsd_mutex_is_locked(mutex) (1) |
| 102 |
|
| 103 |
#endif /* !WITH_PTHREADS */ |
| 104 |
|
| 105 |
|
| 106 |
#endif /* !LSD_THREAD_H */ |