2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % SSSSS EEEEE M M AAA PPPP H H OOO RRRR EEEEE %
7 % SS E MM MM A A P P H H O O R R E %
8 % SSS EEE M M M AAAAA PPPP HHHHH O O RRRR EEE %
9 % SS E M M A A P H H O O R R E %
10 % SSSSS EEEEE M M A A P H H OOO R R EEEEE %
13 % MagickCore Semaphore Methods %
21 % Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
22 % dedicated to making software imaging solutions freely available. %
24 % You may not use this file except in compliance with the License. You may %
25 % obtain a copy of the License at %
27 % http://www.imagemagick.org/script/license.php %
29 % Unless required by applicable law or agreed to in writing, software %
30 % distributed under the License is distributed on an "AS IS" BASIS, %
31 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
32 % See the License for the specific language governing permissions and %
33 % limitations under the License. %
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
44 #include "MagickCore/studio.h"
45 #include "MagickCore/exception.h"
46 #include "MagickCore/exception-private.h"
47 #include "MagickCore/memory_.h"
48 #include "MagickCore/memory-private.h"
49 #include "MagickCore/semaphore.h"
50 #include "MagickCore/semaphore-private.h"
51 #include "MagickCore/string_.h"
52 #include "MagickCore/thread_.h"
53 #include "MagickCore/thread-private.h"
74 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
78 % A c q u i r e S e m a p h o r e I n f o %
82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
84 % AcquireSemaphoreInfo() acquires a semaphore.
86 % The format of the AcquireSemaphoreInfo method is:
88 % void AcquireSemaphoreInfo(SemaphoreInfo **semaphore_info)
90 % A description of each parameter follows:
92 % o semaphore_info: Specifies a pointer to an SemaphoreInfo structure.
95 MagickExport void AcquireSemaphoreInfo(SemaphoreInfo **semaphore_info)
97 assert(semaphore_info != (SemaphoreInfo **) NULL);
98 if (*semaphore_info == (SemaphoreInfo *) NULL)
101 if (*semaphore_info == (SemaphoreInfo *) NULL)
102 *semaphore_info=AllocateSemaphoreInfo();
108 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
112 % A l l o c a t e S e m a p h o r e I n f o %
116 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
118 % AllocateSemaphoreInfo() initializes the SemaphoreInfo structure.
120 % The format of the AllocateSemaphoreInfo method is:
122 % SemaphoreInfo *AllocateSemaphoreInfo(void)
126 static void *AcquireSemaphoreMemory(const size_t size)
128 #if defined(MAGICKCORE_HAVE_POSIX_MEMALIGN)
133 if (posix_memalign(&memory,CACHE_LINE_SIZE,CacheAlign(size)) == 0)
137 return(malloc(CacheAlign(size)));
140 MagickExport SemaphoreInfo *AllocateSemaphoreInfo(void)
148 semaphore_info=(SemaphoreInfo *) AcquireSemaphoreMemory(
149 sizeof(*semaphore_info));
150 if (semaphore_info == (SemaphoreInfo *) NULL)
151 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
152 (void) ResetMagickMemory(semaphore_info,0,sizeof(SemaphoreInfo));
154 Initialize the semaphore.
156 #if defined(MAGICKCORE_THREAD_SUPPORT)
164 status=pthread_mutexattr_init(&mutex_info);
168 ThrowFatalException(ResourceLimitFatalError,
169 "UnableToInitializeSemaphore");
171 #if defined(MAGICKCORE_DEBUG)
172 #if defined(PTHREAD_MUTEX_ERRORCHECK)
173 status=pthread_mutex_settype(&mutex_info,PTHREAD_MUTEX_ERRORCHECK);
177 ThrowFatalException(ResourceLimitFatalError,
178 "UnableToInitializeSemaphore");
182 status=pthread_mutex_init(&semaphore_info->mutex,&mutex_info);
186 ThrowFatalException(ResourceLimitFatalError,
187 "UnableToInitializeSemaphore");
189 status=pthread_mutexattr_destroy(&mutex_info);
193 ThrowFatalException(ResourceLimitFatalError,
194 "UnableToInitializeSemaphore");
197 #elif defined(MAGICKCORE_HAVE_WINTHREADS)
202 status=InitializeCriticalSectionAndSpinCount(&semaphore_info->mutex,0x0400);
206 ThrowFatalException(ResourceLimitFatalError,
207 "UnableToInitializeSemaphore");
211 semaphore_info->id=GetMagickThreadId();
212 semaphore_info->reference_count=0;
213 semaphore_info->signature=MagickSignature;
214 return(semaphore_info);
218 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
222 % D e s t r o y S e m a p h o r e I n f o %
226 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
228 % DestroySemaphoreInfo() destroys a semaphore.
230 % The format of the DestroySemaphoreInfo method is:
232 % void DestroySemaphoreInfo(SemaphoreInfo **semaphore_info)
234 % A description of each parameter follows:
236 % o semaphore_info: Specifies a pointer to an SemaphoreInfo structure.
239 MagickExport void DestroySemaphoreInfo(SemaphoreInfo **semaphore_info)
241 assert(semaphore_info != (SemaphoreInfo **) NULL);
242 assert((*semaphore_info) != (SemaphoreInfo *) NULL);
243 assert((*semaphore_info)->signature == MagickSignature);
245 #if defined(MAGICKCORE_THREAD_SUPPORT)
250 status=pthread_mutex_destroy(&(*semaphore_info)->mutex);
254 ThrowFatalException(ResourceLimitFatalError,"UnableToDestroySemaphore");
257 #elif defined(MAGICKCORE_HAVE_WINTHREADS)
258 DeleteCriticalSection(&(*semaphore_info)->mutex);
260 (*semaphore_info)->signature=(~MagickSignature);
261 free(*semaphore_info);
262 *semaphore_info=(SemaphoreInfo *) NULL;
267 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
271 % L o c k S e m a p h o r e I n f o %
275 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
277 % LockSemaphoreInfo() locks a semaphore.
279 % The format of the LockSemaphoreInfo method is:
281 % void LockSemaphoreInfo(SemaphoreInfo *semaphore_info)
283 % A description of each parameter follows:
285 % o semaphore_info: Specifies a pointer to an SemaphoreInfo structure.
288 MagickExport void LockSemaphoreInfo(SemaphoreInfo *semaphore_info)
290 assert(semaphore_info != (SemaphoreInfo *) NULL);
291 assert(semaphore_info->signature == MagickSignature);
292 #if defined(MAGICKCORE_THREAD_SUPPORT)
297 status=pthread_mutex_lock(&semaphore_info->mutex);
301 ThrowFatalException(ResourceLimitFatalError,"UnableToLockSemaphore");
304 #elif defined(MAGICKCORE_HAVE_WINTHREADS)
305 EnterCriticalSection(&semaphore_info->mutex);
307 #if defined(MAGICKCORE_DEBUG)
308 if ((semaphore_info->reference_count > 0) &&
309 (IsMagickThreadEqual(semaphore_info->id) != MagickFalse))
311 (void) FormatLocaleFile(stderr,"Warning: unexpected recursive lock!\n");
312 (void) fflush(stderr);
315 semaphore_info->id=GetMagickThreadId();
316 semaphore_info->reference_count++;
320 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
324 % R e l i n g u i s h S e m a p h o r e I n f o %
328 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
330 % RelinquishSemaphoreInfo() relinquishes a semaphore.
332 % The format of the RelinquishSemaphoreInfo method is:
334 % RelinquishSemaphoreInfo(SemaphoreInfo *semaphore_info)
336 % A description of each parameter follows:
338 % o semaphore_info: Specifies a pointer to an SemaphoreInfo structure.
341 MagickExport void RelinquishSemaphoreInfo(SemaphoreInfo *semaphore_info)
343 assert(semaphore_info != (SemaphoreInfo *) NULL);
344 assert(semaphore_info->signature == MagickSignature);
345 UnlockSemaphoreInfo(semaphore_info);
349 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
353 % S e m a p h o r e C o m p o n e n t G e n e s i s %
357 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
359 % SemaphoreComponentGenesis() instantiates the semaphore environment.
361 % The format of the SemaphoreComponentGenesis method is:
363 % MagickBooleanType SemaphoreComponentGenesis(void)
366 MagickPrivate MagickBooleanType SemaphoreComponentGenesis(void)
374 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
378 % S e m a p h o r e C o m p o n e n t T e r m i n u s %
382 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
384 % SemaphoreComponentTerminus() destroys the semaphore component.
386 % The format of the SemaphoreComponentTerminus method is:
388 % SemaphoreComponentTerminus(void)
391 MagickPrivate void SemaphoreComponentTerminus(void)
396 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
400 % U n l o c k S e m a p h o r e I n f o %
404 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
406 % UnlockSemaphoreInfo() unlocks a semaphore.
408 % The format of the UnlockSemaphoreInfo method is:
410 % void UnlockSemaphoreInfo(SemaphoreInfo *semaphore_info)
412 % A description of each parameter follows:
414 % o semaphore_info: Specifies a pointer to an SemaphoreInfo structure.
417 MagickExport void UnlockSemaphoreInfo(SemaphoreInfo *semaphore_info)
419 assert(semaphore_info != (SemaphoreInfo *) NULL);
420 assert(semaphore_info->signature == MagickSignature);
421 #if defined(MAGICKCORE_DEBUG)
422 assert(IsMagickThreadEqual(semaphore_info->id) != MagickFalse);
423 if (semaphore_info->reference_count == 0)
425 (void) FormatLocaleFile(stderr,
426 "Warning: semaphore lock already unlocked!\n");
427 (void) fflush(stderr);
430 semaphore_info->reference_count--;
432 #if defined(MAGICKCORE_THREAD_SUPPORT)
437 status=pthread_mutex_unlock(&semaphore_info->mutex);
441 ThrowFatalException(ResourceLimitFatalError,"UnableToUnlockSemaphore");
444 #elif defined(MAGICKCORE_HAVE_WINTHREADS)
445 LeaveCriticalSection(&semaphore_info->mutex);