cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 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; |
cristy | 4e0eef0 | 2010-05-30 21:52:21 +0000 | [diff] [blame] | 23 | int sysError; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 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", |
cristy | 05065f4 | 2011-09-08 01:11:30 +0000 | [diff] [blame] | 31 | strerror(sysError) ); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 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) |
cristy | 4e0eef0 | 2010-05-30 21:52:21 +0000 | [diff] [blame] | 62 | int sysError; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 63 | if ( (sysError = ::pthread_mutex_destroy( &_mutex )) == 0 ) |
| 64 | return; |
| 65 | throwExceptionExplicit( OptionError, "mutex destruction failed", |
cristy | 05065f4 | 2011-09-08 01:11:30 +0000 | [diff] [blame] | 66 | strerror(sysError) ); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 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) |
cristy | 4e0eef0 | 2010-05-30 21:52:21 +0000 | [diff] [blame] | 79 | int sysError; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 80 | if ( (sysError = ::pthread_mutex_lock( &_mutex )) == 0) |
| 81 | return; |
| 82 | throwExceptionExplicit( OptionError, "mutex lock failed", |
cristy | 05065f4 | 2011-09-08 01:11:30 +0000 | [diff] [blame] | 83 | strerror(sysError)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 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) |
cristy | 4e0eef0 | 2010-05-30 21:52:21 +0000 | [diff] [blame] | 96 | int sysError; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 97 | if ( (sysError = ::pthread_mutex_unlock( &_mutex )) == 0) |
| 98 | return; |
| 99 | throwExceptionExplicit( OptionError, "mutex unlock failed", |
cristy | 05065f4 | 2011-09-08 01:11:30 +0000 | [diff] [blame] | 100 | strerror(sysError) ); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 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 | } |