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 | |
cristy | 05065f4 | 2011-09-08 01:11:30 +0000 | [diff] [blame^] | 11 | #include <cstring> |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 12 | #include "Magick++/Thread.h" |
| 13 | #include "Magick++/Exception.h" |
| 14 | |
| 15 | #include <string.h> |
| 16 | |
| 17 | // Default constructor |
| 18 | Magick::MutexLock::MutexLock(void) |
| 19 | #if defined(MAGICKCORE_HAVE_PTHREAD) |
| 20 | // POSIX threads |
| 21 | : _mutex() |
| 22 | { |
| 23 | ::pthread_mutexattr_t attr; |
cristy | 4e0eef0 | 2010-05-30 21:52:21 +0000 | [diff] [blame] | 24 | int sysError; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 25 | if ( (sysError = ::pthread_mutexattr_init( &attr )) == 0 ) |
| 26 | if ( (sysError = ::pthread_mutex_init( &_mutex, &attr )) == 0 ) |
| 27 | { |
| 28 | ::pthread_mutexattr_destroy( &attr ); |
| 29 | return; |
| 30 | } |
| 31 | throwExceptionExplicit( OptionError, "mutex initialization failed", |
cristy | 05065f4 | 2011-09-08 01:11:30 +0000 | [diff] [blame^] | 32 | strerror(sysError) ); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 33 | } |
| 34 | #else |
| 35 | #if defined(_VISUALC_) && defined(_MT) |
| 36 | // Win32 threads |
| 37 | : _mutex() |
| 38 | { |
| 39 | SECURITY_ATTRIBUTES security; |
| 40 | |
| 41 | /* Allow the semaphore to be inherited */ |
| 42 | security.nLength = sizeof(security); |
| 43 | security.lpSecurityDescriptor = NULL; |
| 44 | security.bInheritHandle = TRUE; |
| 45 | |
| 46 | /* Create the semaphore, with initial value signaled */ |
| 47 | _mutex.id = ::CreateSemaphore(&security, 1, MAXSEMLEN, NULL); |
| 48 | if ( _mutex.id != NULL ) |
| 49 | return; |
| 50 | throwExceptionExplicit( OptionError, "mutex initialization failed" ); |
| 51 | } |
| 52 | #else |
| 53 | // Threads not supported |
| 54 | { |
| 55 | } |
| 56 | #endif |
| 57 | #endif |
| 58 | |
| 59 | // Destructor |
| 60 | Magick::MutexLock::~MutexLock(void) |
| 61 | { |
| 62 | #if defined(MAGICKCORE_HAVE_PTHREAD) |
cristy | 4e0eef0 | 2010-05-30 21:52:21 +0000 | [diff] [blame] | 63 | int sysError; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 64 | if ( (sysError = ::pthread_mutex_destroy( &_mutex )) == 0 ) |
| 65 | return; |
| 66 | throwExceptionExplicit( OptionError, "mutex destruction failed", |
cristy | 05065f4 | 2011-09-08 01:11:30 +0000 | [diff] [blame^] | 67 | strerror(sysError) ); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 68 | #endif |
| 69 | #if defined(_MT) && defined(_VISUALC_) |
| 70 | if ( ::CloseHandle(_mutex.id) != 0 ) |
| 71 | return; |
| 72 | throwExceptionExplicit( OptionError, "mutex destruction failed" ); |
| 73 | #endif |
| 74 | } |
| 75 | |
| 76 | // Lock mutex |
| 77 | void Magick::MutexLock::lock(void) |
| 78 | { |
| 79 | #if defined(MAGICKCORE_HAVE_PTHREAD) |
cristy | 4e0eef0 | 2010-05-30 21:52:21 +0000 | [diff] [blame] | 80 | int sysError; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 81 | if ( (sysError = ::pthread_mutex_lock( &_mutex )) == 0) |
| 82 | return; |
| 83 | throwExceptionExplicit( OptionError, "mutex lock failed", |
cristy | 05065f4 | 2011-09-08 01:11:30 +0000 | [diff] [blame^] | 84 | strerror(sysError)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 85 | #endif |
| 86 | #if defined(_MT) && defined(_VISUALC_) |
| 87 | if (WaitForSingleObject(_mutex.id,INFINITE) != WAIT_FAILED) |
| 88 | return; |
| 89 | throwExceptionExplicit( OptionError, "mutex lock failed" ); |
| 90 | #endif |
| 91 | } |
| 92 | |
| 93 | // Unlock mutex |
| 94 | void Magick::MutexLock::unlock(void) |
| 95 | { |
| 96 | #if defined(MAGICKCORE_HAVE_PTHREAD) |
cristy | 4e0eef0 | 2010-05-30 21:52:21 +0000 | [diff] [blame] | 97 | int sysError; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 98 | if ( (sysError = ::pthread_mutex_unlock( &_mutex )) == 0) |
| 99 | return; |
| 100 | throwExceptionExplicit( OptionError, "mutex unlock failed", |
cristy | 05065f4 | 2011-09-08 01:11:30 +0000 | [diff] [blame^] | 101 | strerror(sysError) ); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 102 | #endif |
| 103 | #if defined(_MT) && defined(_VISUALC_) |
| 104 | if ( ReleaseSemaphore(_mutex.id, 1, NULL) == TRUE ) |
| 105 | return; |
| 106 | throwExceptionExplicit( OptionError, "mutex unlock failed" ); |
| 107 | #endif |
| 108 | } |