1 // This may look like C code, but it is really -*- C++ -*-
3 // Copyright Bob Friesenhahn, 1999, 2000, 2001, 2002
5 // Implementation of thread support
8 #define MAGICKCORE_IMPLEMENTATION 1
9 #define MAGICK_PLUSPLUS_IMPLEMENTATION 1
11 #include "Magick++/Thread.h"
12 #include "Magick++/Exception.h"
16 // Default constructor
17 Magick::MutexLock::MutexLock(void)
18 #if defined(MAGICKCORE_HAVE_PTHREAD)
22 ::pthread_mutexattr_t attr;
24 if ( (sysError = ::pthread_mutexattr_init( &attr )) == 0 )
25 if ( (sysError = ::pthread_mutex_init( &_mutex, &attr )) == 0 )
27 ::pthread_mutexattr_destroy( &attr );
30 throwExceptionExplicit( OptionError, "mutex initialization failed",
34 #if defined(_VISUALC_) && defined(_MT)
38 SECURITY_ATTRIBUTES security;
40 /* Allow the semaphore to be inherited */
41 security.nLength = sizeof(security);
42 security.lpSecurityDescriptor = NULL;
43 security.bInheritHandle = TRUE;
45 /* Create the semaphore, with initial value signaled */
46 _mutex.id = ::CreateSemaphore(&security, 1, MAXSEMLEN, NULL);
47 if ( _mutex.id != NULL )
49 throwExceptionExplicit( OptionError, "mutex initialization failed" );
52 // Threads not supported
59 Magick::MutexLock::~MutexLock(void)
61 #if defined(MAGICKCORE_HAVE_PTHREAD)
63 if ( (sysError = ::pthread_mutex_destroy( &_mutex )) == 0 )
65 throwExceptionExplicit( OptionError, "mutex destruction failed",
68 #if defined(_MT) && defined(_VISUALC_)
69 if ( ::CloseHandle(_mutex.id) != 0 )
71 throwExceptionExplicit( OptionError, "mutex destruction failed" );
76 void Magick::MutexLock::lock(void)
78 #if defined(MAGICKCORE_HAVE_PTHREAD)
80 if ( (sysError = ::pthread_mutex_lock( &_mutex )) == 0)
82 throwExceptionExplicit( OptionError, "mutex lock failed",
85 #if defined(_MT) && defined(_VISUALC_)
86 if (WaitForSingleObject(_mutex.id,INFINITE) != WAIT_FAILED)
88 throwExceptionExplicit( OptionError, "mutex lock failed" );
93 void Magick::MutexLock::unlock(void)
95 #if defined(MAGICKCORE_HAVE_PTHREAD)
97 if ( (sysError = ::pthread_mutex_unlock( &_mutex )) == 0)
99 throwExceptionExplicit( OptionError, "mutex unlock failed",
100 strerror(sysError) );
102 #if defined(_MT) && defined(_VISUALC_)
103 if ( ReleaseSemaphore(_mutex.id, 1, NULL) == TRUE )
105 throwExceptionExplicit( OptionError, "mutex unlock failed" );