www/api/Magick++/Thread_8cpp_source.html
| Magick++ 7.1.0 |
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 //
5 // Copyright @ 2014 ImageMagick Studio LLC, a non-profit organization
6 // dedicated to making software imaging solutions freely available.
7 //
8 // Implementation of thread support
9 //
10
11 #define MAGICKCORE_IMPLEMENTATION 1
12 #define MAGICK_PLUSPLUS_IMPLEMENTATION 1
13
14 #include "Magick++/Thread.h"
15 #include "Magick++/Exception.h"
16
17 #include <string.h>
18
19 // Default constructor
20 Magick::MutexLock::MutexLock(void)
21 #if defined(MAGICKCORE_HAVE_PTHREAD)
22// POSIX threads
23 : _mutex()
24 {
25 ::pthread_mutexattr_t
26 attr;
27
28int
29 sysError;
30
31if ((sysError=::pthread_mutexattr_init(&attr)) == 0)
32if ((sysError=::pthread_mutex_init(&_mutex,&attr)) == 0)
33 {
34 ::pthread_mutexattr_destroy(&attr);
35return;
36 }
37throwExceptionExplicit(MagickCore::OptionError,"mutex initialization failed",
38 strerror(sysError));
39 }
40 #else
41 #if defined(_VISUALC_) && defined(_MT)
42 // Win32 threads
43 {
44 SECURITY_ATTRIBUTES
45 security;
46
47/* Allow the semaphore to be inherited */
48 security.nLength=sizeof(security);
49 security.lpSecurityDescriptor=(LPVOID) NULL;
50 security.bInheritHandle=TRUE;
51
52/* Create the semaphore, with initial value signaled */
53 _mutex=::CreateSemaphore(&security,1,1,(LPCSTR) NULL);
54if (_mutex != (HANDLE) NULL)
55return;
56throwExceptionExplicit(MagickCore::OptionError,
57"mutex initialization failed");
58 }
59 #else
60 // Threads not supported
61 {
62 }
63 #endif
64 #endif
65
66 // Destructor
67 Magick::MutexLock::~MutexLock(void)
68 {
69 #if defined(MAGICKCORE_HAVE_PTHREAD)
70 (void) ::pthread_mutex_destroy(&_mutex);
71 #endif
72 #if defined(_MT) && defined(_VISUALC_)
73 (void) ::CloseHandle(_mutex);
74 #endif
75 }
76
77 // Lock mutex
78 void Magick::MutexLock::lock(void)
79 {
80 #if defined(MAGICKCORE_HAVE_PTHREAD)
81int
82 sysError;
83
84if ((sysError=::pthread_mutex_lock(&_mutex)) == 0)
85return;
86throwExceptionExplicit(MagickCore::OptionError,"mutex lock failed",
87 strerror(sysError));
88 #endif
89 #if defined(_MT) && defined(_VISUALC_)
90if (WaitForSingleObject(_mutex,INFINITE) != WAIT_FAILED)
91return;
92throwExceptionExplicit(MagickCore::OptionError,"mutex lock failed");
93 #endif
94 }
95
96 // Unlock mutex
97 void Magick::MutexLock::unlock(void)
98 {
99 #if defined(MAGICKCORE_HAVE_PTHREAD)
100int
101 sysError;
102
103if ((sysError=::pthread_mutex_unlock(&_mutex)) == 0)
104return;
105throwExceptionExplicit(MagickCore::OptionError,"mutex unlock failed",
106 strerror(sysError));
107 #endif
108 #if defined(_MT) && defined(_VISUALC_)
109if (ReleaseSemaphore(_mutex,1,(LPLONG) NULL) == TRUE)
110return;
111throwExceptionExplicit(MagickCore::OptionError,"mutex unlock failed");
112 #endif
113 }
~MutexLock(void)
Definition: Thread.cpp:67
void unlock(void)
Definition: Thread.cpp:97
void lock(void)
Definition: Thread.cpp:78
Magick::throwExceptionExplicit
MagickPPExport void throwExceptionExplicit(const MagickCore::ExceptionType severity_, const char *reason_, const char *description_=(char *) NULL)
Definition: Exception.cpp:808
MutexLock(void)
Definition: Thread.cpp:20