1 /*---------------------------------------------------------------------------* 2 * pmutex.h * 3 * * 4 * Copyright 2007, 2008 Nuance Communciations, Inc. * 5 * * 6 * Licensed under the Apache License, Version 2.0 (the 'License'); * 7 * you may not use this file except in compliance with the License. * 8 * * 9 * You may obtain a copy of the License at * 10 * http://www.apache.org/licenses/LICENSE-2.0 * 11 * * 12 * Unless required by applicable law or agreed to in writing, software * 13 * distributed under the License is distributed on an 'AS IS' BASIS, * 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * 15 * See the License for the specific language governing permissions and * 16 * limitations under the License. * 17 * * 18 *---------------------------------------------------------------------------*/ 19 20 #ifndef __PMUTEX_H 21 #define __PMUTEX_H 22 23 #if defined(__vxworks) && !defined(REAL_PTHREADS) 24 #undef USE_THREAD 25 #define USE_THREAD 26 #undef POSIX 27 #define POSIX 28 #endif 29 30 31 32 #include "ESR_ReturnCode.h" 33 34 #define SECOND2NSECOND 1000000000 35 #define SECOND2MSECOND 1000 36 #define MSECOND2NSECOND 1000000 37 38 #ifdef USE_THREAD 39 40 #ifdef _WIN32 41 42 #ifndef WIN32_LEAN_AND_MEAN 43 #define WIN32_LEAN_AND_MEAN 44 #endif 45 #include "windows.h" 46 47 typedef HANDLE MUTEX; 48 typedef HANDLE EVENT; 49 50 #define createMutex(mutex, locked) \ 51 (*mutex = CreateMutex(NULL, locked, NULL)) == 0 ? ESR_MUTEX_CREATION_ERROR : ESR_SUCCESS 52 53 #define lockMutex(mutex) waitForHandle(mutex, INFINITE) 54 #define unlockMutex(mutex) (ReleaseMutex(*mutex) ? ESR_SUCCESS : ESR_FATAL_ERROR) 55 #define deleteMutex(mutex) ((void) CloseHandle(*mutex)) 56 ESR_ReturnCode waitForHandle(HANDLE* handle, asr_uint32_t timeout); 57 58 #elif defined(POSIX) 59 60 #if defined(__vxworks) && !defined(REAL_PTHREADS) 61 #include "pthread_vx.h" 62 #else 63 #include <pthread.h> 64 65 #ifndef _POSIX_THREADS 66 #error "Thread is not defined!" 67 #endif 68 #endif /* _VX_WORKS_ */ 69 70 typedef pthread_mutex_t MUTEX; 71 typedef pthread_cond_t EVENT; 72 73 ESR_ReturnCode createMutex_posix(MUTEX *mutex, ESR_BOOL locked); 74 ESR_ReturnCode deleteMutex_posix(MUTEX *mutex); 75 76 #define createMutex(mutex, locked) createMutex_posix(mutex, locked) 77 #define deleteMutex(mutex) deleteMutex_posix(mutex) 78 #define lockMutex(mutex) (pthread_mutex_lock(mutex) == 0 ? ESR_SUCCESS : ESR_FATAL_ERROR) 79 #define unlockMutex(mutex) (pthread_mutex_unlock(mutex) == 0 ? ESR_SUCCESS : ESR_FATAL_ERROR) 80 81 #else /* NON_POSIX */ 82 83 #error Portable Synchronization not defined for this OS! 84 85 #endif /* _WIN32 */ 86 87 #else /* USE_THREAD */ 88 89 #define createMutex(mutex, locked) (ESR_SUCCESS) 90 #define deleteMutex(mutex) 91 #define lockMutex(mutex) ((void) 0) 92 #define unlockMutex(mutex) ((void) 0) 93 94 #endif /* USE_THREAD */ 95 96 #endif 97