00001 /* 00002 * 00003 * Copyright (C) 1997-2005, 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: meichel $ 00029 * Update Date: $Date: 2005/12/08 16:06:08 $ 00030 * CVS/RCS Revision: $Revision: 1.9 $ 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 "dcmtk/config/osconfig.h" 00042 #include "dcmtk/ofstd/oftypes.h" /* for class OFBool */ 00043 #include "dcmtk/ofstd/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 #ifdef HAVE_POINTER_TYPE_PTHREAD_T 00181 void *theThread; 00182 #else 00183 unsigned long theThread; 00184 #endif 00185 00187 OFThread(const OFThread& arg); 00188 00190 OFThread& operator=(const OFThread& arg); 00191 00193 #ifdef HAVE_WINDOWS_H 00194 friend unsigned int __stdcall thread_stub(void *arg); 00195 #else 00196 friend void *thread_stub(void *arg); 00197 #endif 00198 }; 00199 00200 00210 class OFThreadSpecificData 00211 { 00212 public: 00213 00215 OFThreadSpecificData(); 00216 00220 ~OFThreadSpecificData(); 00221 00225 OFBool initialized() const; 00226 00233 int set(void *value); 00234 00241 int get(void *&value); 00242 00248 static void errorstr(OFString& description, int code); 00249 00250 private: 00251 00253 #ifdef HAVE_CXX_VOLATILE 00254 volatile 00255 #endif 00256 void *theKey; 00257 00259 OFThreadSpecificData(const OFThreadSpecificData& arg); 00260 00262 OFThreadSpecificData& operator=(const OFThreadSpecificData& arg); 00263 }; 00264 00265 00274 class OFSemaphore 00275 { 00276 public: 00277 00281 OFSemaphore(unsigned int numResources); 00282 00284 ~OFSemaphore(); 00285 00289 OFBool initialized() const; 00290 00295 int wait(); 00296 00302 int trywait(); 00303 00308 int post(); 00309 00315 static void errorstr(OFString& description, int code); 00316 00322 static const int busy; 00323 00324 private: 00326 #ifdef HAVE_CXX_VOLATILE 00327 volatile 00328 #endif 00329 void * theSemaphore; 00330 00332 OFSemaphore(const OFSemaphore& arg); 00333 00335 OFSemaphore& operator=(const OFSemaphore& arg); 00336 }; 00337 00338 00347 class OFMutex 00348 { 00349 public: 00350 00352 OFMutex(); 00353 00355 ~OFMutex(); 00356 00360 OFBool initialized() const; 00361 00368 int lock(); 00369 00375 int trylock(); 00376 00384 int unlock(); 00385 00391 static void errorstr(OFString& description, int code); 00392 00398 static const int busy; 00399 00400 private: 00402 #ifdef HAVE_CXX_VOLATILE 00403 volatile 00404 #endif 00405 void * theMutex; 00406 00408 OFMutex(const OFMutex& arg); 00409 00411 OFMutex& operator=(const OFMutex& arg); 00412 }; 00413 00414 00422 class OFReadWriteLock 00423 { 00424 public: 00425 00427 OFReadWriteLock(); 00428 00430 ~OFReadWriteLock(); 00431 00435 OFBool initialized() const; 00436 00443 int rdlock(); 00444 00451 int wrlock(); 00452 00458 int tryrdlock(); 00459 00465 int trywrlock(); 00466 00474 int unlock(); 00475 00481 static void errorstr(OFString& description, int code); 00482 00488 static const int busy; 00489 00490 private: 00492 #ifdef HAVE_CXX_VOLATILE 00493 volatile 00494 #endif 00495 void * theLock; 00496 00498 OFReadWriteLock(const OFReadWriteLock& arg); 00499 00501 OFReadWriteLock& operator=(const OFReadWriteLock& arg); 00502 }; 00503 00504 #endif 00505 00506 /* 00507 * 00508 * CVS/RCS Log: 00509 * $Log: ofthread.h,v $ 00510 * Revision 1.9 2005/12/08 16:06:08 meichel 00511 * Changed include path schema for all DCMTK header files 00512 * 00513 * Revision 1.8 2004/08/03 16:44:16 meichel 00514 * Updated code to correctly handle pthread_t both as an integral integer type 00515 * (e.g. Linux, Solaris) and as a pointer type (e.g. BSD, OSF/1). 00516 * 00517 * Revision 1.7 2003/12/05 10:37:41 joergr 00518 * Removed leading underscore characters from preprocessor symbols (reserved 00519 * symbols). Updated copyright date where appropriate. 00520 * 00521 * Revision 1.6 2003/07/04 13:29:51 meichel 00522 * Replaced forward declarations for OFString with explicit includes, 00523 * needed when compiling with HAVE_STD_STRING 00524 * 00525 * Revision 1.5 2003/06/06 10:31:04 meichel 00526 * Added volatile keyword to data pointers in multi-thread wrapper classes 00527 * 00528 * Revision 1.4 2002/02/27 14:13:19 meichel 00529 * Changed initialized() methods to const. Fixed some return values when 00530 * compiled without thread support. 00531 * 00532 * Revision 1.3 2001/06/01 15:51:36 meichel 00533 * Updated copyright header 00534 * 00535 * Revision 1.2 2000/06/26 09:27:26 joergr 00536 * Replaced _WIN32 by HAVE_WINDOWS_H to avoid compiler errors using CygWin-32. 00537 * 00538 * Revision 1.1 2000/03/29 16:41:23 meichel 00539 * Added new classes providing an operating system independent abstraction 00540 * for threads, thread specific data, semaphores, mutexes and read/write locks. 00541 * 00542 * 00543 */