blob: 1fa8788c4777b009e0afd87163169073be735ea7 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001// 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
cristy05065f42011-09-08 01:11:30 +000011#include <cstring>
cristy3ed852e2009-09-05 21:47:34 +000012#include "Magick++/Thread.h"
13#include "Magick++/Exception.h"
14
15#include <string.h>
16
17// Default constructor
18Magick::MutexLock::MutexLock(void)
19#if defined(MAGICKCORE_HAVE_PTHREAD)
20 // POSIX threads
21 : _mutex()
22{
23 ::pthread_mutexattr_t attr;
cristy4e0eef02010-05-30 21:52:21 +000024 int sysError;
cristy3ed852e2009-09-05 21:47:34 +000025 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",
cristy05065f42011-09-08 01:11:30 +000032 strerror(sysError) );
cristy3ed852e2009-09-05 21:47:34 +000033}
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
60Magick::MutexLock::~MutexLock(void)
61{
62#if defined(MAGICKCORE_HAVE_PTHREAD)
cristy4e0eef02010-05-30 21:52:21 +000063 int sysError;
cristy3ed852e2009-09-05 21:47:34 +000064 if ( (sysError = ::pthread_mutex_destroy( &_mutex )) == 0 )
65 return;
66 throwExceptionExplicit( OptionError, "mutex destruction failed",
cristy05065f42011-09-08 01:11:30 +000067 strerror(sysError) );
cristy3ed852e2009-09-05 21:47:34 +000068#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
77void Magick::MutexLock::lock(void)
78{
79#if defined(MAGICKCORE_HAVE_PTHREAD)
cristy4e0eef02010-05-30 21:52:21 +000080 int sysError;
cristy3ed852e2009-09-05 21:47:34 +000081 if ( (sysError = ::pthread_mutex_lock( &_mutex )) == 0)
82 return;
83 throwExceptionExplicit( OptionError, "mutex lock failed",
cristy05065f42011-09-08 01:11:30 +000084 strerror(sysError));
cristy3ed852e2009-09-05 21:47:34 +000085#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
94void Magick::MutexLock::unlock(void)
95{
96#if defined(MAGICKCORE_HAVE_PTHREAD)
cristy4e0eef02010-05-30 21:52:21 +000097 int sysError;
cristy3ed852e2009-09-05 21:47:34 +000098 if ( (sysError = ::pthread_mutex_unlock( &_mutex )) == 0)
99 return;
100 throwExceptionExplicit( OptionError, "mutex unlock failed",
cristy05065f42011-09-08 01:11:30 +0000101 strerror(sysError) );
cristy3ed852e2009-09-05 21:47:34 +0000102#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}