OFFIS DCMTK  Version 3.6.0
ofthread.h
1 /*
2  *
3  * Copyright (C) 1997-2011, OFFIS e.V.
4  * All rights reserved. See COPYRIGHT file for details.
5  *
6  * This software and supporting documentation were developed by
7  *
8  * OFFIS e.V.
9  * R&D Division Health
10  * Escherweg 2
11  * D-26121 Oldenburg, Germany
12  *
13  *
14  * Module: ofstd
15  *
16  * Author: Marco Eichelberg
17  *
18  * Purpose: Provides operating system independent abstractions for basic
19  * multi-thread concepts: threads, thread specific data,
20  * semaphores, mutexes and read/write locks. The implementation
21  * of these classes supports the Solaris, POSIX and Win32
22  * multi-thread APIs.
23  *
24  * Last Update: $Author: onken $
25  * Update Date: $Date: 2011-01-04 14:47:09 $
26  * CVS/RCS Revision: $Revision: 1.12 $
27  * Status: $State: Exp $
28  *
29  * CVS/RCS Log at end of file
30  *
31  */
32 
33 
34 #ifndef OFTHREAD_H
35 #define OFTHREAD_H
36 
37 #include "dcmtk/config/osconfig.h"
38 #include "dcmtk/ofstd/oftypes.h" /* for class OFBool */
39 #include "dcmtk/ofstd/ofstring.h" /* for class OFString */
40 
45 extern "C"
46 {
47 #ifdef HAVE_WINDOWS_H
48  unsigned int __stdcall thread_stub(void *arg);
49 #else
50  void *thread_stub(void *arg);
51 #endif
52 }
53 
54 
62 class OFThread
63 {
64 public:
65 
70  OFThread();
71 
79  virtual ~OFThread();
80 
96  int start();
97 
107  int join();
108 
117  unsigned long threadID();
118 
124  OFBool equal(unsigned long tID);
125 
131  static void errorstr(OFString& description, int code);
132 
139  static const int busy;
140 
141 protected:
142 
146  static void thread_exit();
147 
157  static unsigned long self();
158 
159 private:
160 
168  virtual void run() = 0;
169 
170 #ifdef HAVE_WINDOWS_H
171 
172  unsigned long theThreadHandle;
173 #endif
174 
176 #ifdef HAVE_POINTER_TYPE_PTHREAD_T
177  void *theThread;
178 #else
179  unsigned long theThread;
180 #endif
181 
183  OFThread(const OFThread& arg);
184 
186  OFThread& operator=(const OFThread& arg);
187 
189 #ifdef HAVE_WINDOWS_H
190  friend unsigned int __stdcall thread_stub(void *arg);
191 #else
192  friend void *thread_stub(void *arg);
193 #endif
194 };
195 
196 
207 {
208 public:
209 
212 
217 
221  OFBool initialized() const;
222 
229  int set(void *value);
230 
237  int get(void *&value);
238 
244  static void errorstr(OFString& description, int code);
245 
246 private:
247 
249 #ifdef HAVE_CXX_VOLATILE
250  volatile
251 #endif
252  void *theKey;
253 
256 
259 };
260 
261 
262 /* Mac OS X only permits named Semaphores. The code below compiles on Mac OS X
263  but does not work. This will be corrected in the next snapshot. For now, the
264  semaphore code is completely disabled for that OS (it is not used in other
265  parts of the toolkit so far.
266  */
267 #ifndef _DARWIN_C_SOURCE
268 
278 {
279 public:
280 
284  OFSemaphore(unsigned int numResources);
285 
287  ~OFSemaphore();
288 
292  OFBool initialized() const;
293 
298  int wait();
299 
305  int trywait();
306 
311  int post();
312 
318  static void errorstr(OFString& description, int code);
319 
325  static const int busy;
326 
327 private:
329 #ifdef HAVE_CXX_VOLATILE
330  volatile
331 #endif
332  void * theSemaphore;
333 
335  OFSemaphore(const OFSemaphore& arg);
336 
338  OFSemaphore& operator=(const OFSemaphore& arg);
339 };
340 
341 
342 #endif // _DARWIN_C_SOURCE
343 
352 class OFMutex
353 {
354 public:
355 
357  OFMutex();
358 
360  ~OFMutex();
361 
365  OFBool initialized() const;
366 
373  int lock();
374 
380  int trylock();
381 
389  int unlock();
390 
396  static void errorstr(OFString& description, int code);
397 
403  static const int busy;
404 
405 private:
407 #ifdef HAVE_CXX_VOLATILE
408  volatile
409 #endif
410  void * theMutex;
411 
413  OFMutex(const OFMutex& arg);
414 
416  OFMutex& operator=(const OFMutex& arg);
417 };
418 
419 
428 {
429 public:
430 
432  OFReadWriteLock();
433 
436 
440  OFBool initialized() const;
441 
448  int rdlock();
449 
456  int wrlock();
457 
463  int tryrdlock();
464 
470  int trywrlock();
471 
479  int unlock();
480 
486  static void errorstr(OFString& description, int code);
487 
493  static const int busy;
494 
495 private:
497 #ifdef HAVE_CXX_VOLATILE
498  volatile
499 #endif
500  void * theLock;
501 
503  OFReadWriteLock(const OFReadWriteLock& arg);
504 
507 };
508 
515 public:
520 
523 
528  int rdlock();
529 
534  int wrlock();
535 
541  int tryrdlock();
542 
548  int trywrlock();
549 
554  int unlock();
555 
556 private:
559 
561  OFBool locked;
562 
565 
568 };
569 
570 #endif
571 
572 /*
573  *
574  * CVS/RCS Log:
575  * $Log: ofthread.h,v $
576  * Revision 1.12 2011-01-04 14:47:09 onken
577  * Disable and hide OFSemaphore class on Mac OS X since implementation is
578  * broken on that OS (needs named semaphores instead).
579  *
580  * Revision 1.11 2010-10-14 13:15:50 joergr
581  * Updated copyright header. Added reference to COPYRIGHT file.
582  *
583  * Revision 1.10 2010-06-04 13:58:42 uli
584  * Added class OFReadWriteLocker which simplifies unlocking OFReadWriteLocks.
585  *
586  * Revision 1.9 2005-12-08 16:06:08 meichel
587  * Changed include path schema for all DCMTK header files
588  *
589  * Revision 1.8 2004/08/03 16:44:16 meichel
590  * Updated code to correctly handle pthread_t both as an integral integer type
591  * (e.g. Linux, Solaris) and as a pointer type (e.g. BSD, OSF/1).
592  *
593  * Revision 1.7 2003/12/05 10:37:41 joergr
594  * Removed leading underscore characters from preprocessor symbols (reserved
595  * symbols). Updated copyright date where appropriate.
596  *
597  * Revision 1.6 2003/07/04 13:29:51 meichel
598  * Replaced forward declarations for OFString with explicit includes,
599  * needed when compiling with HAVE_STD_STRING
600  *
601  * Revision 1.5 2003/06/06 10:31:04 meichel
602  * Added volatile keyword to data pointers in multi-thread wrapper classes
603  *
604  * Revision 1.4 2002/02/27 14:13:19 meichel
605  * Changed initialized() methods to const. Fixed some return values when
606  * compiled without thread support.
607  *
608  * Revision 1.3 2001/06/01 15:51:36 meichel
609  * Updated copyright header
610  *
611  * Revision 1.2 2000/06/26 09:27:26 joergr
612  * Replaced _WIN32 by HAVE_WINDOWS_H to avoid compiler errors using CygWin-32.
613  *
614  * Revision 1.1 2000/03/29 16:41:23 meichel
615  * Added new classes providing an operating system independent abstraction
616  * for threads, thread specific data, semaphores, mutexes and read/write locks.
617  *
618  *
619  */
OFBool initialized() const
checks whether creation of the object was successful.
int wrlock()
gets a write lock.
int wait()
blocks the calling thread until the semaphore counter is greater than zero and then atomically decrea...
int trylock()
tries to lock the mutex object.
int trywrlock()
trys to get a write lock.
int set(void *value)
sets the thread specific value for this object.
OFBool locked
did we sucessfully lock the lock?
Definition: ofthread.h:561
static const int busy
this constant is returned by the trylock() method if the mutex is already locked. ...
Definition: ofthread.h:403
int unlock()
releases the lock on the mutex object.
OFReadWriteLock & theLock
the lock on which we are operating
Definition: ofthread.h:558
static const int busy
this constant is returned by the tryrdlock() and trywrlock() methods if the read/write lock is alread...
Definition: ofthread.h:493
static void errorstr(OFString &description, int code)
converts any of the error codes returned by the methods of this class into a textual description...
OFThread()
default constructor.
OFReadWriteLocker & operator=(const OFReadWriteLocker &arg)
unimplemented private assignment operator
int trywait()
atomically decreases the counter if it is larger than zero, otherwise returns OFSemaphore::busy.
void * theKey
thread specific data key resource
Definition: ofthread.h:252
OFSemaphore & operator=(const OFSemaphore &arg)
unimplemented private assignment operator
OFBool initialized() const
checks whether creation of the object was successful.
static void errorstr(OFString &description, int code)
converts any of the error codes returned by the methods of this class into a textual description...
OFReadWriteLocker(OFReadWriteLock &lock)
constructor
static void thread_exit()
terminates the calling thread, in a similar way that exit() terminates the calling process...
OFThreadSpecificData()
default constructor
provides an operating system independent abstraction for semaphores.
Definition: ofthread.h:277
OFReadWriteLock()
default constructor
OFMutex()
default constructor
int post()
atomically increases the counter.
static const int busy
this constant is returned by the join() method if another thread is already waiting for termination o...
Definition: ofthread.h:139
int join()
blocks the calling thread until the thread referenced by the OFThread object terminates.
~OFReadWriteLocker()
destructor, unlocks the mutex if necessary
friend void * thread_stub(void *arg)
thread stub must be friend to call run()
void * theLock
read/write lock resource
Definition: ofthread.h:500
OFThread & operator=(const OFThread &arg)
unimplemented private assignment operator
int unlock()
unlocks the read/write lock.
int rdlock()
gets a read lock.
This class aims to provide an easy way for making sure OFReadWriteLocks are unlocked in an exception ...
Definition: ofthread.h:514
virtual ~OFThread()
destructor.
virtual void run()=0
this method implements the thread that is run by calling the start method of the OFThread object...
static void errorstr(OFString &description, int code)
converts any of the error codes returned by the methods of this class into a textual description...
~OFReadWriteLock()
destructor
OFBool equal(unsigned long tID)
checks if the given thread ID matches the thread ID of the thread referenced by this object...
provides an operating system independent abstraction for mutexes (mutual exclusion locks)...
Definition: ofthread.h:352
provides an operating system independent abstraction for thread specific data.
Definition: ofthread.h:206
OFBool initialized() const
checks whether creation of the object was successful.
int tryrdlock()
try to lock the lock for reading
int rdlock()
lock the lock for reading
int trywrlock()
try to lock the lock for writing
static void errorstr(OFString &description, int code)
converts any of the error codes returned by the methods of this class into a textual description...
void * theMutex
mutex resource
Definition: ofthread.h:410
~OFMutex()
destructor
int start()
adds a new thread of control to the current process.
OFSemaphore(unsigned int numResources)
constructor.
static const int busy
this constant is returned by the trywait() method if the semaphore is already locked.
Definition: ofthread.h:325
OFReadWriteLock & operator=(const OFReadWriteLock &arg)
unimplemented private assignment operator
~OFThreadSpecificData()
destructor.
OFThreadSpecificData & operator=(const OFThreadSpecificData &arg)
unimplemented private assignment operator
~OFSemaphore()
destructor
provides an operating system independent abstraction for threads.
Definition: ofthread.h:62
int unlock()
unlock the lock
OFMutex & operator=(const OFMutex &arg)
unimplemented private assignment operator
a simple string class that implements a subset of std::string.
Definition: ofstring.h:86
provides an operating system independent abstraction for read/write locks.
Definition: ofthread.h:427
int tryrdlock()
trys to get a read lock.
unsigned long threadID()
returns the thread identifier of the thread referenced by the OFThread object, if the thread has alre...
static void errorstr(OFString &description, int code)
converts any of the error codes returned by the methods of this class into a textual description...
void * theSemaphore
semaphore resource
Definition: ofthread.h:332
unsigned long theThread
thread identifier
Definition: ofthread.h:179
OFBool initialized() const
checks whether creation of the object was successful.
int wrlock()
lock the lock for writing
int lock()
locks the mutex object.


Generated on Thu Aug 27 2015 for OFFIS DCMTK Version 3.6.0 by Doxygen 1.8.9.1