Magick++  6.9.3
Thread.cpp
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, 2002
4 // Copyright Dirk Lemstra 2017
5 //
6 // Implementation of thread support
7 //
8 
9 #define MAGICKCORE_IMPLEMENTATION 1
10 #define MAGICK_PLUSPLUS_IMPLEMENTATION 1
11 
12 #include "Magick++/Thread.h"
13 #include "Magick++/Exception.h"
14 
15 #include <string.h>
16 
17 // Default constructor
19 #if defined(MAGICKCORE_HAVE_PTHREAD)
20  // POSIX threads
21  : _mutex()
22 {
23  ::pthread_mutexattr_t attr;
24  int sysError;
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",
32  strerror(sysError) );
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
61 {
62 #if defined(MAGICKCORE_HAVE_PTHREAD)
63  (void) ::pthread_mutex_destroy(&_mutex);
64 #endif
65 #if defined(_MT) && defined(_VISUALC_)
66  (void) ::CloseHandle(_mutex.id);
67 #endif
68 }
69 
70 // Lock mutex
72 {
73 #if defined(MAGICKCORE_HAVE_PTHREAD)
74  int sysError;
75  if ( (sysError = ::pthread_mutex_lock( &_mutex )) == 0)
76  return;
77  throwExceptionExplicit( OptionError, "mutex lock failed",
78  strerror(sysError));
79 #endif
80 #if defined(_MT) && defined(_VISUALC_)
81  if (WaitForSingleObject(_mutex.id,INFINITE) != WAIT_FAILED)
82  return;
83  throwExceptionExplicit( OptionError, "mutex lock failed" );
84 #endif
85 }
86 
87 // Unlock mutex
89 {
90 #if defined(MAGICKCORE_HAVE_PTHREAD)
91  int sysError;
92  if ( (sysError = ::pthread_mutex_unlock( &_mutex )) == 0)
93  return;
94  throwExceptionExplicit( OptionError, "mutex unlock failed",
95  strerror(sysError) );
96 #endif
97 #if defined(_MT) && defined(_VISUALC_)
98  if ( ReleaseSemaphore(_mutex.id, 1, NULL) == TRUE )
99  return;
100  throwExceptionExplicit( OptionError, "mutex unlock failed" );
101 #endif
102 }
~MutexLock(void)
Definition: Thread.cpp:60
void unlock(void)
Definition: Thread.cpp:88
void lock(void)
Definition: Thread.cpp:71
MagickPPExport void throwExceptionExplicit(const MagickCore::ExceptionType severity_, const char *reason_, const char *description_=(char *) NULL)