pmutex.h

00001 /*---------------------------------------------------------------------------*
00002  *  pmutex.h  *
00003  *                                                                           *
00004  *  Copyright 2007 Nuance Communciations, Inc.                               *
00005  *                                                                           *
00006  *  Licensed under the Apache License, Version 2.0 (the 'License');          *
00007  *  you may not use this file except in compliance with the License.         *
00008  *                                                                           *
00009  *  You may obtain a copy of the License at                                  *
00010  *      http://www.apache.org/licenses/LICENSE-2.0                           *
00011  *                                                                           *
00012  *  Unless required by applicable law or agreed to in writing, software      *
00013  *  distributed under the License is distributed on an 'AS IS' BASIS,        *
00014  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * 
00015  *  See the License for the specific language governing permissions and      *
00016  *  limitations under the License.                                           *
00017  *                                                                           *
00018  *---------------------------------------------------------------------------*/
00019 
00020 #ifndef __PMUTEX_H
00021 #define __PMUTEX_H
00022 
00023 #if defined(__vxworks) && !defined(REAL_PTHREADS)
00024 #undef  USE_THREAD
00025 #define USE_THREAD
00026 #undef  POSIX
00027 #define POSIX
00028 #endif
00029 
00030 
00031 
00032 #include "ESR_ReturnCode.h"
00033 
00034 #define SECOND2NSECOND   1000000000
00035 #define SECOND2MSECOND   1000
00036 #define MSECOND2NSECOND  1000000
00037 
00038 #ifdef USE_THREAD
00039 
00040 #ifdef _WIN32
00041 
00042 #ifndef WIN32_LEAN_AND_MEAN
00043 #define WIN32_LEAN_AND_MEAN
00044 #endif
00045 #include "windows.h"
00046 
00047 typedef HANDLE MUTEX;
00048 typedef HANDLE EVENT;
00049 
00050 #define createMutex(mutex, locked) \
00051   (*mutex = CreateMutex(NULL, locked, NULL)) == 0 ? ESR_MUTEX_CREATION_ERROR : ESR_SUCCESS
00052 
00053 #define lockMutex(mutex) waitForHandle(mutex, INFINITE)
00054 #define unlockMutex(mutex) (ReleaseMutex(*mutex) ? ESR_SUCCESS : ESR_FATAL_ERROR)
00055 #define deleteMutex(mutex) ((void) CloseHandle(*mutex))
00056 ESR_ReturnCode waitForHandle(HANDLE* handle, asr_uint32_t timeout);
00057 
00058 #elif defined(POSIX)
00059 
00060 #if defined(__vxworks) && !defined(REAL_PTHREADS)
00061 #include "pthread_vx.h"
00062 #else
00063 #include <pthread.h>
00064 
00065 #ifndef _POSIX_THREADS
00066 #error "Thread is not defined!"
00067 #endif
00068 #endif /* _VX_WORKS_ */
00069 
00070 typedef pthread_mutex_t MUTEX;
00071 typedef pthread_cond_t EVENT;
00072 
00073 ESR_ReturnCode createMutex_posix(MUTEX *mutex, ESR_BOOL locked);
00074 ESR_ReturnCode deleteMutex_posix(MUTEX *mutex);
00075 
00076 #define createMutex(mutex, locked) createMutex_posix(mutex, locked)
00077 #define deleteMutex(mutex) deleteMutex_posix(mutex)
00078 #define lockMutex(mutex) (pthread_mutex_lock(mutex) == 0 ? ESR_SUCCESS : ESR_FATAL_ERROR)
00079 #define unlockMutex(mutex) (pthread_mutex_unlock(mutex) == 0 ? ESR_SUCCESS : ESR_FATAL_ERROR)
00080 
00081 #else /* NON_POSIX */
00082 
00083 #error Portable Synchronization not defined for this OS!
00084 
00085 #endif /* _WIN32 */
00086 
00087 #else /* USE_THREAD */
00088 
00089 #define createMutex(mutex, locked) (ESR_SUCCESS)
00090 #define deleteMutex(mutex) 
00091 #define lockMutex(mutex) ((void) 0)
00092 #define unlockMutex(mutex) ((void) 0)
00093 
00094 #endif /* USE_THREAD */
00095 
00096 #endif  

Generated on Thu May 1 15:37:25 2008 for SREC by  doxygen 1.5.3