Back to index

im-sdk  12.3.91
threaddef.h
Go to the documentation of this file.
00001 /*
00002   threaddef.h  --- Absorb trivial differences amoung thread interfaces.
00003 */
00004 
00005 #ifndef _THREADDEF_H_
00006 #define _THREADDEF_H_
00007 
00008 #include <config.h>
00009 
00010 #ifdef HAVE_PTHREAD
00011 
00012 #include <pthread.h>
00013 #define THREAD_OBJECT pthread_t
00014 #define THREAD_ID_OBJECT pthread_t
00015 #define THREAD_SYNC_OBJECT pthread_mutex_t
00016 #define THREAD_EVENT_OBJECT pthread_cond_t
00017 #define LOCK_SYNC_OBJECT(so) (pthread_mutex_lock(&(so)))
00018 #define UNLOCK_SYNC_OBJECT(so) (pthread_mutex_unlock(&(so)))
00019 #define LOCK_SYNC_OBJECT_FOR_EVENT(so) (pthread_mutex_lock(&(so)))
00020 #define UNLOCK_SYNC_OBJECT_FOR_EVENT(so) (pthread_mutex_unlock(&(so)))
00021 #define INIT_SYNC_OBJECT(so) (pthread_mutex_init(&(so), NULL))
00022 #define DESTROY_SYNC_OBJECT(so) (pthread_mutex_destroy(&(so)))
00023 #define INIT_EVENT_OBJECT(eo) (pthread_cond_init(&(eo), NULL))
00024 #define DESTROY_EVENT_OBJECT(eo) (pthread_cond_destroy(&(eo)))
00025 #define THREAD_CREATE(so, handler, arg) \
00026   (pthread_create(&(so), NULL, (handler), (arg)))
00027 #define THREAD_DETACH(so) (pthread_detach(so))
00028 #define THREAD_WAIT_AND_FREE(so) (pthread_join((so), NULL))
00029 #define THREAD_SET_CURRENT_ID(tid) ((tid) = pthread_self())
00030 #define THREAD_ID(so) (so)
00031 #define THREAD_ID_EQUAL(tid1, tid2) (pthread_equal((tid1), (tid2)))
00032 #define THREAD_RESET_EVENT(eo) /* do nothing */
00033 #define THREAD_WAIT_EVENT(eo, so) (pthread_cond_wait(&(eo), &(so)))
00034 #define THREAD_SIGNAL_EVENT(eo, so) (pthread_cond_signal(&(eo)))
00035 
00036 #elif defined(HAVE_UITHREAD)
00037 
00038 // TODO
00039 
00040 #elif defined(WIN32)
00041 
00042 #include <windows.h>
00043 #define THREAD_OBJECT HANDLE
00044 #define THREAD_SYNC_OBJECT HANDLE
00045 #define THREAD_ID_OBJECT DWORD
00046 #define THREAD_SYNC_OBJECT CRITICAL_SECTION
00047 #define THREAD_EVENT_OBJCET HANDLE
00048 #define LOCK_SYNC_OBJECT(so) (EnterCriticalSection(&(so)))
00049 #define UNLOCK_SYNC_OBJECT(so) (LeaveCriticalSection(&(so)))
00050 #define LOCK_SYNC_OBJECT_FOR_EVENT(so) /* do nothing */
00051 #define UNLOCK_SYNC_OBJECT_FOR_EVENT(so) /* do nothing */
00052 #define INIT_SYNC_OBJECT(so) (InitializeCriticalSection(&(so))
00053 #define DESTROY_SYNC_OBJECT(so) (DeleteCriticalSection(&(so)))
00054 /* NOTE THAT manual, nonsignaled event!!! */
00055 #define INIT_EVENT_OBJECT(eo) ((eo) = CreateEvent(NULL, TRUE, FALSE, NULL)) 
00056 #define DESTROY_EVENT_OBJECT(eo) (CloseHandle(eo))
00057 #define THREAD_CREATE(so, handler, arg) \
00058   ((so) = _beginthread((handler), 0, (arg)))
00059 #define THREAD_DETACH(so) (CloseHandle(so))
00060 #define THREAD_WAIT_AND_FREE(so) \
00061   ((WaitForSingleObject(so, INFINITE) == WAIT_OBJECT_0) && CloseHandle(so))
00062 #define THREAD_SET_CURRENT_ID(tid) ((tid) = GetCurrentThreadId())
00063 #define THREAD_ID_EQUAL(tid1, tid2) ((tid1) == (tid2))
00064 #define THREAD_RESET_EVENT(eo) (ResetEvent(eo))
00065 #define THREAD_WAIT_EVENT(eo, so) (WaitForSingleObject(eo, INFINITE))
00066 #define THREAD_SIGNAL_EVENT(eo, so) (SetEvent(so))
00067 
00068 #elif defined(HAVE_NO_THREAD)
00069 
00070 #define THREAD_OBJECT
00071 #define THREAD_SYNC_OBJECT
00072 #define THREAD_ID_OBJECT
00073 #define THREAD_SYNC_OBJECT
00074 #define THREAD_EVENT_OBJCET
00075 #define LOCK_SYNC_OBJECT(so) ((void) 0)
00076 #define UNLOCK_SYNC_OBJECT(so) ((void) 0)
00077 #define DEFINE_SYNC_OBJECT(so) ((void) 0)
00078 #define DEFINE_SYNC_OBJECT_STATIC(so) ((void) 0)
00079 #define LOCK_SYNC_OBJECT_FOR_EVENT(so) ((void) 0)
00080 #define UNLOCK_SYNC_OBJECT_FOR_EVENT(so) ((void) 0)
00081 #define INIT_SYNC_OBJECT(so) ((void) 0)
00082 #define DESTROY_SYNC_OBJECT(so) ((void) 0)
00083 #define INIT_EVENT_OBJECT(eo) ((void) 0)
00084 #define DESTROY_EVENT_OBJECT(eo) ((void) 0)
00085 #define THREAD_CREATE(so, handler, arg) ((void) 0)
00086 #define THREAD_DETACH(so) ((void) 0)
00087 #define THREAD_WAIT_AND_FREE(so) ((void) 0)
00088 #define THREAD_SET_CURRENT_ID(tid) ((void) 0)
00089 #define THREAD_ID_EQUAL(tid1, tid2) ((void) 0)
00090 #define THREAD_RESET_EVENT(eo) ((void) 0)
00091 #define THREAD_WAIT_EVENT(eo, so) ((void) 0)
00092 #define THREAD_SIGNAL_EVENT(eo, so) ((void) 0)
00093 
00094 #else
00095 #error "This system dose not have thread feature!"
00096 #endif
00097 
00098 #endif /* not _THREADDEF_H_ */
00099