blob: 55a4d8fb66c8e704f420c996020ab9300b9be010 [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
11#include "Magick++/Thread.h"
12#include "Magick++/Exception.h"
13
14#include <string.h>
15
16// Default constructor
17Magick::MutexLock::MutexLock(void)
18#if defined(MAGICKCORE_HAVE_PTHREAD)
19 // POSIX threads
20 : _mutex()
21{
22 ::pthread_mutexattr_t attr;
cristy4e0eef02010-05-30 21:52:21 +000023 int sysError;
cristy3ed852e2009-09-05 21:47:34 +000024 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",
cristy05065f42011-09-08 01:11:30 +000031 strerror(sysError) );
cristy3ed852e2009-09-05 21:47:34 +000032}
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
59Magick::MutexLock::~MutexLock(void)
60{
61#if defined(MAGICKCORE_HAVE_PTHREAD)
cristy4e0eef02010-05-30 21:52:21 +000062 int sysError;
cristy3ed852e2009-09-05 21:47:34 +000063 if ( (sysError = ::pthread_mutex_destroy( &_mutex )) == 0 )
64 return;
65 throwExceptionExplicit( OptionError, "mutex destruction failed",
cristy05065f42011-09-08 01:11:30 +000066 strerror(sysError) );
cristy3ed852e2009-09-05 21:47:34 +000067#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
76void Magick::MutexLock::lock(void)
77{
78#if defined(MAGICKCORE_HAVE_PTHREAD)
cristy4e0eef02010-05-30 21:52:21 +000079 int sysError;
cristy3ed852e2009-09-05 21:47:34 +000080 if ( (sysError = ::pthread_mutex_lock( &_mutex )) == 0)
81 return;
82 throwExceptionExplicit( OptionError, "mutex lock failed",
cristy05065f42011-09-08 01:11:30 +000083 strerror(sysError));
cristy3ed852e2009-09-05 21:47:34 +000084#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
93void Magick::MutexLock::unlock(void)
94{
95#if defined(MAGICKCORE_HAVE_PTHREAD)
cristy4e0eef02010-05-30 21:52:21 +000096 int sysError;
cristy3ed852e2009-09-05 21:47:34 +000097 if ( (sysError = ::pthread_mutex_unlock( &_mutex )) == 0)
98 return;
99 throwExceptionExplicit( OptionError, "mutex unlock failed",
cristy05065f42011-09-08 01:11:30 +0000100 strerror(sysError) );
cristy3ed852e2009-09-05 21:47:34 +0000101#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}