[77] | 1 | /* $Id: thread.h 13 2011-04-20 15:41:43Z mmamonski $ */ |
---|
| 2 | /* |
---|
| 3 | * PSNC DRMAA 2.0 utilities library |
---|
| 4 | * Copyright (C) 2012 Poznan Supercomputing and Networking Center |
---|
| 5 | * |
---|
| 6 | * This program is free software: you can redistribute it and/or modify |
---|
| 7 | * it under the terms of the GNU General Public License as published by |
---|
| 8 | * the Free Software Foundation, either version 3 of the License, or |
---|
| 9 | * (at your option) any later version. |
---|
| 10 | * |
---|
| 11 | * This program is distributed in the hope that it will be useful, |
---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 14 | * GNU General Public License for more details. |
---|
| 15 | * |
---|
| 16 | * You should have received a copy of the GNU General Public License |
---|
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
| 18 | */ |
---|
| 19 | |
---|
| 20 | /** |
---|
| 21 | * @file thread.h |
---|
| 22 | * |
---|
| 23 | * Thread and synchronization primitives. |
---|
| 24 | */ |
---|
| 25 | |
---|
| 26 | #ifndef __DRMAA_UTILS__THREAD_H |
---|
| 27 | #define __DRMAA_UTILS__THREAD_H |
---|
| 28 | |
---|
| 29 | #ifdef HAVE_CONFIG_H |
---|
| 30 | # include <config.h> |
---|
| 31 | #endif |
---|
| 32 | |
---|
| 33 | #include <pthread.h> |
---|
| 34 | #include <time.h> |
---|
| 35 | |
---|
| 36 | #include <drmaa_utils/compat.h> |
---|
| 37 | |
---|
| 38 | /** |
---|
| 39 | * @defgroup recursive_mutex Recursive mutexes implementation. |
---|
| 40 | * It uses recursive mutexes if supplied by POSIX threads library |
---|
| 41 | * or implements it over plain mutexes. |
---|
| 42 | */ |
---|
| 43 | /* @{ */ |
---|
| 44 | |
---|
| 45 | #if defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP) |
---|
| 46 | # define HAVE_RECURSIVE_MUTEXES 1 |
---|
| 47 | #else |
---|
| 48 | # define HAVE_RECURSIVE_MUTEXES 0 |
---|
| 49 | #endif /* ! PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP */ |
---|
| 50 | |
---|
| 51 | |
---|
| 52 | #if HAVE_RECURSIVE_MUTEXES |
---|
| 53 | |
---|
| 54 | typedef pthread_mutex_t fsd_mutex_t; |
---|
| 55 | # define FSD_MUTEX_INITIALIZER PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP |
---|
| 56 | typedef pthread_cond_t fsd_cond_t; |
---|
| 57 | # define FSD_COND_INITIALIZER PTHREAD_COND_INITAILIZER |
---|
| 58 | |
---|
| 59 | #else /* !HAVE_RECURSIVE_MUTEXES */ |
---|
| 60 | |
---|
| 61 | /** |
---|
| 62 | * Recursive mutex build on top of non-recursive mutex |
---|
| 63 | * (used when recursive mutexes are not provided by POSIX |
---|
| 64 | * thread library). |
---|
| 65 | */ |
---|
| 66 | typedef struct fsd_mutex_s { |
---|
| 67 | pthread_mutex_t mutex; /**< Non-recursive mutex. */ |
---|
| 68 | pthread_t owner; /**< Thread which owns critical section. */ |
---|
| 69 | int acquired; /**< How many times |
---|
| 70 | owning thread acquired this mutex. */ |
---|
| 71 | } fsd_mutex_t; |
---|
| 72 | # define FSD_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, (pthread_t)-1, 0 } |
---|
| 73 | typedef pthread_cond_t fsd_cond_t; |
---|
| 74 | # define FSD_COND_INITIALIZER PTHREAD_COND_INITAILIZER |
---|
| 75 | |
---|
| 76 | #endif /* ! HAVE_RECURSIVE_MUTEXES */ |
---|
| 77 | |
---|
| 78 | |
---|
| 79 | /** pthread_mutex_init wrapper. Initializes recursive mutex. */ |
---|
| 80 | void fsd_mutex_init ( fsd_mutex_t *mutex ); |
---|
| 81 | void fsd_mutex_destroy ( fsd_mutex_t *mutex ); |
---|
| 82 | bool fsd_mutex_lock ( fsd_mutex_t *mutex ); |
---|
| 83 | bool fsd_mutex_unlock ( fsd_mutex_t *mutex ); |
---|
| 84 | bool fsd_mutex_trylock ( fsd_mutex_t *mutex ); |
---|
| 85 | |
---|
| 86 | /** |
---|
| 87 | * Try to unlock mutex as many times as possible |
---|
| 88 | * returning the count of locks previously granted |
---|
| 89 | * to current thread. |
---|
| 90 | */ |
---|
| 91 | int fsd_mutex_unlock_times( fsd_mutex_t *mutex ); |
---|
| 92 | |
---|
| 93 | void fsd_cond_init ( fsd_cond_t *cond ); |
---|
| 94 | void fsd_cond_destroy ( fsd_cond_t *cond ); |
---|
| 95 | void fsd_cond_signal ( fsd_cond_t *cond ); |
---|
| 96 | void fsd_cond_broadcast ( fsd_cond_t *cond ); |
---|
| 97 | void fsd_cond_wait ( fsd_cond_t *cond, fsd_mutex_t *mutex ); |
---|
| 98 | bool fsd_cond_timedwait ( fsd_cond_t *cond, fsd_mutex_t *mutex, |
---|
| 99 | const struct timespec *abstime ); |
---|
| 100 | /* @} */ |
---|
| 101 | |
---|
| 102 | |
---|
| 103 | /** |
---|
| 104 | * @defgroup thread Wrapper around POSIX thread functions. |
---|
| 105 | */ |
---|
| 106 | /* @{ */ |
---|
| 107 | typedef pthread_t fsd_thread_t; |
---|
| 108 | /** pthread_create wrapper */ |
---|
| 109 | void fsd_thread_create( fsd_thread_t *thread, void* (*func)(void*), void *arg ); |
---|
| 110 | #define fsd_thread_self pthraed_self |
---|
| 111 | #define fsd_thraed_equal pthread_equal |
---|
| 112 | #define fsd_thread_exit pthread_exit |
---|
| 113 | /** pthread_join wrapper */ |
---|
| 114 | void fsd_thread_join( fsd_thread_t th, void **thread_return ); |
---|
| 115 | /** pthread_detach wrapper */ |
---|
| 116 | void fsd_thread_detach( fsd_thread_t th ); |
---|
| 117 | /* void fsd_thread_cancel( fsd_thread_t th ); */ |
---|
| 118 | /* @} */ |
---|
| 119 | |
---|
| 120 | |
---|
| 121 | /** Returns thread identifier. */ |
---|
| 122 | int fsd_thread_id(void); |
---|
| 123 | |
---|
| 124 | #endif /* __DRMAA_UTILS__THREAD_H */ |
---|
| 125 | |
---|