Magick++  7.0.9
Thread.h
Go to the documentation of this file.
1 // This may look like C code, but it is really -*- C++ -*-
2 //
3 // Copyright Bob Friesenhahn, 1999, 2000, 2001, 2003
4 //
5 // Definition of types and classes to support threads
6 //
7 // This class is a Magick++ implementation class and is not intended
8 // for use by end-users.
9 //
10 #if !defined (Magick_Thread_header)
11 #define Magick_Thread_header
12 
13 #include "Magick++/Include.h"
14 
15 #if defined(_VISUALC_)
16 #include <windows.h>
17 #if defined(_MT)
18 struct win32_mutex {
19  HANDLE id;
20 };
21 
22 // This is a binary semphore -- increase for a counting semaphore
23 #define MAXSEMLEN 1
24 #endif // defined(_MT)
25 #endif // defined(_VISUALC_)
26 
27 #if defined(MAGICKCORE_HAVE_PTHREAD)
28 # include <pthread.h>
29 #endif // defined(MAGICKCORE_HAVE_PTHREAD)
30 
31 namespace Magick
32 {
33  // Mutex lock wrapper
35  {
36  public:
37  // Default constructor
38  MutexLock(void);
39 
40  // Destructor
41  ~MutexLock(void);
42 
43  // Lock mutex
44  void lock(void);
45 
46  // Unlock mutex
47  void unlock(void);
48 
49  private:
50 
51  // Don't support copy constructor
52  MutexLock ( const MutexLock& original_ );
53 
54  // Don't support assignment
55  MutexLock& operator = ( const MutexLock& original_ );
56 
57 #if defined(MAGICKCORE_HAVE_PTHREAD)
58  pthread_mutex_t _mutex;
59 #endif
60 #if defined(_MT) && defined(_VISUALC_)
61  win32_mutex _mutex;
62 #endif
63  };
64 
65  // Lock mutex while object is in scope
67  {
68  public:
69  // Construct with mutex lock (locks mutex)
70  Lock( MutexLock *mutexLock_ );
71 
72  // Destrutor (unlocks mutex)
73  ~Lock( void );
74  private:
75 
76  // Don't support copy constructor
77  Lock ( const Lock& original_ );
78 
79  // Don't support assignment
80  Lock& operator = ( const Lock& original_ );
81 
82  MutexLock* _mutexLock;
83  };
84 }
85 
86 // Construct with mutex lock (locks mutex)
87 inline Magick::Lock::Lock( MutexLock *mutexLock_ )
88  : _mutexLock(mutexLock_)
89 {
90  _mutexLock->lock();
91 }
92 
93 // Destrutor (unlocks mutex)
94 inline Magick::Lock::~Lock( void )
95 {
96  _mutexLock->unlock();
97  _mutexLock=0;
98 }
99 
100 #endif // Magick_Thread_header
~Lock(void)
Definition: Thread.h:94
void lock(void)
Definition: Thread.cpp:71
#define MagickPPExport
Definition: Include.h:261
Lock(MutexLock *mutexLock_)
Definition: Thread.h:87