00001 /* 00002 * 00003 * Copyright (C) 1997-2003, OFFIS 00004 * 00005 * This software and supporting documentation were developed by 00006 * 00007 * Kuratorium OFFIS e.V. 00008 * Healthcare Information and Communication Systems 00009 * Escherweg 2 00010 * D-26121 Oldenburg, Germany 00011 * 00012 * THIS SOFTWARE IS MADE AVAILABLE, AS IS, AND OFFIS MAKES NO WARRANTY 00013 * REGARDING THE SOFTWARE, ITS PERFORMANCE, ITS MERCHANTABILITY OR 00014 * FITNESS FOR ANY PARTICULAR USE, FREEDOM FROM ANY COMPUTER DISEASES OR 00015 * ITS CONFORMITY TO ANY SPECIFICATION. THE ENTIRE RISK AS TO QUALITY AND 00016 * PERFORMANCE OF THE SOFTWARE IS WITH THE USER. 00017 * 00018 * Module: ofstd 00019 * 00020 * Author: Marco Eichelberg 00021 * 00022 * Purpose: Provides operating system independent abstractions for basic 00023 * multi-thread concepts: threads, thread specific data, 00024 * semaphores, mutexes and read/write locks. The implementation 00025 * of these classes supports the Solaris, POSIX and Win32 00026 * multi-thread APIs. 00027 * 00028 * Last Update: $Author: joergr $ 00029 * Update Date: $Date: 2003/12/05 10:37:41 $ 00030 * CVS/RCS Revision: $Revision: 1.7 $ 00031 * Status: $State: Exp $ 00032 * 00033 * CVS/RCS Log at end of file 00034 * 00035 */ 00036 00037 00038 #ifndef OFTHREAD_H 00039 #define OFTHREAD_H 00040 00041 #include "osconfig.h" 00042 #include "oftypes.h" /* for class OFBool */ 00043 #include "ofstring.h" /* for class OFString */ 00044 00049 extern "C" 00050 { 00051 #ifdef HAVE_WINDOWS_H 00052 unsigned int __stdcall thread_stub(void *arg); 00053 #else 00054 void *thread_stub(void *arg); 00055 #endif 00056 } 00057 00058 00066 class OFThread 00067 { 00068 public: 00069 00074 OFThread(); 00075 00083 virtual ~OFThread(); 00084 00100 int start(); 00101 00111 int join(); 00112 00121 unsigned long threadID(); 00122 00128 OFBool equal(unsigned long tID); 00129 00135 static void errorstr(OFString& description, int code); 00136 00143 static const int busy; 00144 00145 protected: 00146 00150 static void thread_exit(); 00151 00161 static unsigned long self(); 00162 00163 private: 00164 00172 virtual void run() = 0; 00173 00174 #ifdef HAVE_WINDOWS_H 00175 00176 unsigned long theThreadHandle; 00177 #endif 00178 00180 unsigned long theThread; 00181 00183 OFThread(const OFThread& arg); 00184 00186 OFThread& operator=(const OFThread& arg); 00187 00189 #ifdef HAVE_WINDOWS_H 00190 friend unsigned int __stdcall thread_stub(void *arg); 00191 #else 00192 friend void *thread_stub(void *arg); 00193 #endif 00194 }; 00195 00196 00206 class OFThreadSpecificData 00207 { 00208 public: 00209 00211 OFThreadSpecificData(); 00212 00216 ~OFThreadSpecificData(); 00217 00221 OFBool initialized() const; 00222 00229 int set(void *value); 00230 00237 int get(void *&value); 00238 00244 static void errorstr(OFString& description, int code); 00245 00246 private: 00247 00249 #ifdef HAVE_CXX_VOLATILE 00250 volatile 00251 #endif 00252 void *theKey; 00253 00255 OFThreadSpecificData(const OFThreadSpecificData& arg); 00256 00258 OFThreadSpecificData& operator=(const OFThreadSpecificData& arg); 00259 }; 00260 00261 00270 class OFSemaphore 00271 { 00272 public: 00273 00277 OFSemaphore(unsigned int numResources); 00278 00280 ~OFSemaphore(); 00281 00285 OFBool initialized() const; 00286 00291 int wait(); 00292 00298 int trywait(); 00299 00304 int post(); 00305 00311 static void errorstr(OFString& description, int code); 00312 00318 static const int busy; 00319 00320 private: 00322 #ifdef HAVE_CXX_VOLATILE 00323 volatile 00324 #endif 00325 void * theSemaphore; 00326 00328 OFSemaphore(const OFSemaphore& arg); 00329 00331 OFSemaphore& operator=(const OFSemaphore& arg); 00332 }; 00333 00334 00343 class OFMutex 00344 { 00345 public: 00346 00348 OFMutex(); 00349 00351 ~OFMutex(); 00352 00356 OFBool initialized() const; 00357 00364 int lock(); 00365 00371 int trylock(); 00372 00380 int unlock(); 00381 00387 static void errorstr(OFString& description, int code); 00388 00394 static const int busy; 00395 00396 private: 00398 #ifdef HAVE_CXX_VOLATILE 00399 volatile 00400 #endif 00401 void * theMutex; 00402 00404 OFMutex(const OFMutex& arg); 00405 00407 OFMutex& operator=(const OFMutex& arg); 00408 }; 00409 00410 00418 class OFReadWriteLock 00419 { 00420 public: 00421 00423 OFReadWriteLock(); 00424 00426 ~OFReadWriteLock(); 00427 00431 OFBool initialized() const; 00432 00439 int rdlock(); 00440 00447 int wrlock(); 00448 00454 int tryrdlock(); 00455 00461 int trywrlock(); 00462 00470 int unlock(); 00471 00477 static void errorstr(OFString& description, int code); 00478 00484 static const int busy; 00485 00486 private: 00488 #ifdef HAVE_CXX_VOLATILE 00489 volatile 00490 #endif 00491 void * theLock; 00492 00494 OFReadWriteLock(const OFReadWriteLock& arg); 00495 00497 OFReadWriteLock& operator=(const OFReadWriteLock& arg); 00498 }; 00499 00500 #endif 00501 00502 /* 00503 * 00504 * CVS/RCS Log: 00505 * $Log: ofthread.h,v $ 00506 * Revision 1.7 2003/12/05 10:37:41 joergr 00507 * Removed leading underscore characters from preprocessor symbols (reserved 00508 * symbols). Updated copyright date where appropriate. 00509 * 00510 * Revision 1.6 2003/07/04 13:29:51 meichel 00511 * Replaced forward declarations for OFString with explicit includes, 00512 * needed when compiling with HAVE_STD_STRING 00513 * 00514 * Revision 1.5 2003/06/06 10:31:04 meichel 00515 * Added volatile keyword to data pointers in multi-thread wrapper classes 00516 * 00517 * Revision 1.4 2002/02/27 14:13:19 meichel 00518 * Changed initialized() methods to const. Fixed some return values when 00519 * compiled without thread support. 00520 * 00521 * Revision 1.3 2001/06/01 15:51:36 meichel 00522 * Updated copyright header 00523 * 00524 * Revision 1.2 2000/06/26 09:27:26 joergr 00525 * Replaced _WIN32 by HAVE_WINDOWS_H to avoid compiler errors using CygWin-32. 00526 * 00527 * Revision 1.1 2000/03/29 16:41:23 meichel 00528 * Added new classes providing an operating system independent abstraction 00529 * for threads, thread specific data, semaphores, mutexes and read/write locks. 00530 * 00531 * 00532 */