MeVisLabToolboxReference
MeVis/Foundation/Sources/DicomTree/MLDicomTree/DCMTree_ThreadSupport.h
Go to the documentation of this file.
00001 //-----------------------------------------------------------------------------
00002 // **InsertLicense** code
00003 //-----------------------------------------------------------------------------
00005 
00010 //-----------------------------------------------------------------------------
00011 
00012 #ifndef CLASS_DCMTREE_THREADSUPPORT
00013 #define CLASS_DCMTREE_THREADSUPPORT
00014 
00015 #ifdef WIN32
00016     #ifndef NOMINMAX
00017         #define NOMINMAX
00018     #endif
00019     #include <process.h>
00020     #include <winsock2.h>
00021     #include <windows.h>
00022     #include <winbase.h>
00023     #undef small
00024 #else
00025     #include <pthread.h>
00026     #include <signal.h>
00027     
00028     #ifdef MACOS
00029         #include <mach/semaphore.h>
00030     #else
00031         #ifndef sem_t
00032             #include <semaphore.h>
00033         #endif
00034     #endif
00035 #endif
00036 
00037 #include "DCMTree_Defines.h"
00038 
00039 
00040 namespace DCMTree_Utils
00041 {
00043     typedef void* thread_parameter;
00044 
00045     #ifdef WIN32
00046         #define thread_modifier  __stdcall
00047         typedef unsigned int     thread_result;
00048         typedef HANDLE           thread_id;
00049         typedef thread_result    (thread_modifier *thread_function)(thread_parameter);
00050         typedef CRITICAL_SECTION thread_criticalsection;
00051         typedef HANDLE           thread_semaphore;
00052     #else
00053         #define thread_modifier 
00054         typedef void*            thread_result;
00055         typedef pthread_t        thread_id;
00056         typedef thread_result    (*thread_function)(thread_parameter);
00057         typedef pthread_mutex_t  thread_criticalsection;
00058         #ifdef MACOS
00059             typedef semaphore_t  thread_semaphore;
00060         #else
00061             typedef sem_t        thread_semaphore;
00062         #endif
00063     #endif
00064 
00065 
00066 
00067 
00068     // system independent multi threading functions
00069 
00071 
00075     thread_id DCMTREE_EXPORT thread_start (thread_function func,thread_parameter param);
00076 
00078 
00081     void DCMTREE_EXPORT thread_await (thread_id id);
00082 
00084 
00087     bool DCMTREE_EXPORT thread_running (thread_id id);
00088 
00090 
00093     void DCMTREE_EXPORT thread_initcriticalsection (thread_criticalsection &cs);
00094 
00096 
00099     void DCMTREE_EXPORT thread_entercriticalsection (thread_criticalsection &cs);
00100 
00102 
00105     void DCMTREE_EXPORT thread_leavecriticalsection (thread_criticalsection &cs);
00106 
00108 
00111     void DCMTREE_EXPORT thread_destroycriticalsection (thread_criticalsection &cs);
00112 
00113 
00115 
00119     void DCMTREE_EXPORT thread_initsemaphore (thread_semaphore &s,int initcount=0);
00120 
00122 
00125     void DCMTREE_EXPORT thread_destroysemaphore (thread_semaphore &s);
00126 
00128 
00131     void DCMTREE_EXPORT thread_waitsemaphore (thread_semaphore &s);
00132 
00134 
00139     bool DCMTREE_EXPORT thread_waitsemaphore (thread_semaphore &s, unsigned long timeout);
00140 
00142 
00145     void DCMTREE_EXPORT thread_postsemaphore (thread_semaphore &s);
00146 
00148     class DCMTREE_EXPORT thread_scoped_criticalsection
00149     {
00150     public:
00151         thread_scoped_criticalsection (thread_criticalsection &cs):_cs(cs)
00152         {
00153             thread_entercriticalsection (_cs);
00154         };
00155         
00156         ~thread_scoped_criticalsection()
00157         {
00158             thread_leavecriticalsection (_cs);
00159         };
00160 
00161     private:
00162         thread_criticalsection &_cs;
00163         
00166         thread_scoped_criticalsection& operator=(const thread_scoped_criticalsection&);
00167     };
00168 }
00169 
00170 #endif
00171 
00172