]> granicus.if.org Git - imagemagick/blob - Magick++/lib/Thread.cpp
Changed order of private members.
[imagemagick] / Magick++ / lib / Thread.cpp
1 // This may look like C code, but it is really -*- C++ -*-
2 //
3 // Copyright Bob Friesenhahn, 1999, 2000, 2001, 2002
4 //
5 // Implementation of thread support
6 //
7
8 #define MAGICKCORE_IMPLEMENTATION  1
9 #define MAGICK_PLUSPLUS_IMPLEMENTATION 1
10
11 #include "Magick++/Thread.h"
12 #include "Magick++/Exception.h"
13
14 #include <string.h>
15
16 // Default constructor
17 Magick::MutexLock::MutexLock(void)
18 #if defined(MAGICKCORE_HAVE_PTHREAD)
19   // POSIX threads
20   : _mutex()
21 {
22   ::pthread_mutexattr_t attr;
23   int sysError;
24   if ( (sysError = ::pthread_mutexattr_init( &attr )) == 0 )
25     if ( (sysError = ::pthread_mutex_init( &_mutex, &attr )) == 0 )
26       {
27         ::pthread_mutexattr_destroy( &attr );
28         return;
29       }
30   throwExceptionExplicit( OptionError, "mutex initialization failed",
31                           strerror(sysError) );
32 }
33 #else
34 #if defined(_VISUALC_) && defined(_MT)
35 // Win32 threads
36   : _mutex()
37 {
38   SECURITY_ATTRIBUTES security;
39
40   /* Allow the semaphore to be inherited */
41   security.nLength = sizeof(security);
42   security.lpSecurityDescriptor = NULL;
43   security.bInheritHandle = TRUE;
44
45   /* Create the semaphore, with initial value signaled */
46   _mutex.id = ::CreateSemaphore(&security, 1, MAXSEMLEN, NULL);
47   if ( _mutex.id != NULL )
48     return;
49   throwExceptionExplicit( OptionError, "mutex initialization failed" );
50 }
51 #else
52 // Threads not supported
53 {
54 }
55 #endif
56 #endif
57
58 // Destructor
59 Magick::MutexLock::~MutexLock(void)
60 {
61 #if defined(MAGICKCORE_HAVE_PTHREAD)
62   int sysError;
63   if ( (sysError = ::pthread_mutex_destroy( &_mutex )) == 0 )
64     return;
65   throwExceptionExplicit( OptionError, "mutex destruction failed",
66                           strerror(sysError) );
67 #endif
68 #if defined(_MT) && defined(_VISUALC_)
69   if ( ::CloseHandle(_mutex.id) != 0 )
70     return;
71   throwExceptionExplicit( OptionError, "mutex destruction failed" );
72 #endif
73 }
74
75 // Lock mutex
76 void Magick::MutexLock::lock(void)
77 {
78 #if defined(MAGICKCORE_HAVE_PTHREAD)
79   int sysError;
80   if ( (sysError = ::pthread_mutex_lock( &_mutex )) == 0)
81     return;
82   throwExceptionExplicit( OptionError, "mutex lock failed",
83                           strerror(sysError));
84 #endif
85 #if defined(_MT) && defined(_VISUALC_)
86   if (WaitForSingleObject(_mutex.id,INFINITE) != WAIT_FAILED)
87     return;
88   throwExceptionExplicit( OptionError, "mutex lock failed" );
89 #endif
90 }
91
92 // Unlock mutex
93 void Magick::MutexLock::unlock(void)
94 {
95 #if defined(MAGICKCORE_HAVE_PTHREAD)
96   int sysError;
97   if ( (sysError = ::pthread_mutex_unlock( &_mutex )) == 0)
98     return;
99   throwExceptionExplicit( OptionError, "mutex unlock failed",
100                           strerror(sysError) );
101 #endif
102 #if defined(_MT) && defined(_VISUALC_)
103   if ( ReleaseSemaphore(_mutex.id, 1, NULL) == TRUE )
104     return;
105   throwExceptionExplicit( OptionError, "mutex unlock failed" );
106 #endif
107 }