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/mutex.h"
50#include "MagickCore/semaphore.h"
51#include "MagickCore/semaphore-private.h"
52#include "MagickCore/string_.h"
53#include "MagickCore/thread_.h"
54#include "MagickCore/thread-private.h"
55#include "MagickCore/utility-private.h"
98MagickExport
void ActivateSemaphoreInfo(
SemaphoreInfo **semaphore_info)
103 *semaphore_info=AcquireSemaphoreInfo();
126static void *AcquireSemaphoreMemory(
const size_t count,
const size_t quantum)
128#define AlignedExtent(size,alignment) \
129 (((size)+((alignment)-1)) & (size_t) ~((alignment)-1))
140 if ((count == 0) || (quantum != (size/count)))
143 return((
void *) NULL);
146 alignment=CACHE_LINE_SIZE;
147 extent=AlignedExtent(size,CACHE_LINE_SIZE);
148 if ((size == 0) || (alignment <
sizeof(
void *)) || (extent < size))
149 return((
void *) NULL);
150#if defined(MAGICKCORE_HAVE_POSIX_MEMALIGN)
151 if (posix_memalign(&memory,alignment,extent) != 0)
153#elif defined(MAGICKCORE_HAVE__ALIGNED_MALLOC)
154 memory=_aligned_malloc(extent,alignment);
160 extent=(size+alignment-1)+
sizeof(
void *);
166 memory=(
void *) AlignedExtent((
size_t) p+
sizeof(
void *),alignment);
167 *((
void **) memory-1)=p;
175static void *RelinquishSemaphoreMemory(
void *memory)
177 if (memory == (
void *) NULL)
178 return((
void *) NULL);
179#if defined(MAGICKCORE_HAVE_POSIX_MEMALIGN)
181#elif defined(MAGICKCORE_HAVE__ALIGNED_MALLOC)
182 _aligned_free(memory);
184 free(*((
void **) memory-1));
198 sizeof(*semaphore_info));
200 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
205#if defined(MAGICKCORE_OPENMP_SUPPORT)
206 omp_init_lock((omp_lock_t *) &semaphore_info->mutex);
207#elif defined(MAGICKCORE_THREAD_SUPPORT)
215 status=pthread_mutexattr_init(&mutex_info);
219 perror(
"unable to initialize mutex attributes");
222#if defined(MAGICKCORE_DEBUG)
223#if defined(PTHREAD_MUTEX_ERRORCHECK)
224 status=pthread_mutexattr_settype(&mutex_info,PTHREAD_MUTEX_ERRORCHECK);
228 perror(
"unable to set mutex type");
233 status=pthread_mutex_init(&semaphore_info->mutex,&mutex_info);
237 perror(
"unable to initialize mutex");
240 status=pthread_mutexattr_destroy(&mutex_info);
244 perror(
"unable to destroy mutex attributes");
248#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
253 status=InitializeCriticalSectionAndSpinCount(&semaphore_info->mutex,0x0400);
256 perror(
"unable to initialize critical section");
261 semaphore_info->id=GetMagickThreadId();
262 semaphore_info->reference_count=0;
263 semaphore_info->signature=MagickCoreSignature;
264 return(semaphore_info);
289MagickExport
void LockSemaphoreInfo(
SemaphoreInfo *semaphore_info)
292 assert(semaphore_info->signature == MagickCoreSignature);
293#if defined(MAGICKCORE_DEBUG)
294 if ((semaphore_info->reference_count > 0) &&
295 (IsMagickThreadEqual(semaphore_info->id) != MagickFalse))
297 (void) FormatLocaleFile(stderr,
"Warning: unexpected recursive lock!\n");
298 (void) fflush(stderr);
301#if defined(MAGICKCORE_OPENMP_SUPPORT)
302 omp_set_lock((omp_lock_t *) &semaphore_info->mutex);
303#elif defined(MAGICKCORE_THREAD_SUPPORT)
308 status=pthread_mutex_lock(&semaphore_info->mutex);
312 perror(
"unable to lock mutex");
316#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
317 EnterCriticalSection(&semaphore_info->mutex);
319#if defined(MAGICKCORE_DEBUG)
320 semaphore_info->id=GetMagickThreadId();
321 semaphore_info->reference_count++;
347MagickExport
void RelinquishSemaphoreInfo(
SemaphoreInfo **semaphore_info)
353 assert((*semaphore_info)->signature == MagickCoreSignature);
354#if defined(MAGICKCORE_OPENMP_SUPPORT)
355 omp_destroy_lock((omp_lock_t *) &(*semaphore_info)->mutex);
356#elif defined(MAGICKCORE_THREAD_SUPPORT)
361 status=pthread_mutex_destroy(&(*semaphore_info)->mutex);
365 perror(
"unable to destroy mutex");
369#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
370 DeleteCriticalSection(&(*semaphore_info)->mutex);
372 (*semaphore_info)->signature=(~MagickCoreSignature);
397MagickPrivate MagickBooleanType SemaphoreComponentGenesis(
void)
399 InitializeMagickMutex();
421MagickPrivate
void SemaphoreComponentTerminus(
void)
423 DestroyMagickMutex();
448MagickExport
void UnlockSemaphoreInfo(
SemaphoreInfo *semaphore_info)
451 assert(semaphore_info->signature == MagickCoreSignature);
452#if defined(MAGICKCORE_DEBUG)
453 assert(IsMagickThreadEqual(semaphore_info->id) != MagickFalse);
454 if (semaphore_info->reference_count == 0)
456 (void) FormatLocaleFile(stderr,
457 "Warning: semaphore lock already unlocked!\n");
458 (void) fflush(stderr);
461 semaphore_info->reference_count--;
463#if defined(MAGICKCORE_OPENMP_SUPPORT)
464 omp_unset_lock((omp_lock_t *) &semaphore_info->mutex);
465#elif defined(MAGICKCORE_THREAD_SUPPORT)
470 status=pthread_mutex_unlock(&semaphore_info->mutex);
474 perror(
"unable to unlock mutex");
478#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
479 LeaveCriticalSection(&semaphore_info->mutex);