MagickCore 7.1.2-37
Convert, Edit, Or Compose Bitmap Images
Loading...
Searching...
No Matches
semaphore.c
1/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
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 %
11% %
12% %
13% MagickCore Semaphore Methods %
14% %
15% Software Design %
16% William Radcliffe %
17% Cristy %
18% June 2000 %
19% %
20% %
21% Copyright @ 1999 ImageMagick Studio LLC, a non-profit organization %
22% dedicated to making software imaging solutions freely available. %
23% %
24% You may not use this file except in compliance with the License. You may %
25% obtain a copy of the License at %
26% %
27% https://imagemagick.org/license/ %
28% %
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. %
34% %
35%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36%
37%
38%
39*/
40␌
41/*
42 Include declarations.
43*/
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"
56␌
57/*
58 Struct declarations.
59*/
61{
62 MagickMutexType
63 mutex;
64
65 MagickThreadType
66 id;
67
68 ssize_t
69 reference_count;
70
71 size_t
72 signature;
73};
74␌
75/*
76%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
77% %
78% %
79% %
80% A c t i v a t e S e m a p h o r e I n f o %
81% %
82% %
83% %
84%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
85%
86% ActivateSemaphoreInfo() activates a semaphore under protection of a mutex
87% to ensure only one thread allocates the semaphore.
88%
89% The format of the ActivateSemaphoreInfo method is:
90%
91% void ActivateSemaphoreInfo(SemaphoreInfo **semaphore_info)
92%
93% A description of each parameter follows:
94%
95% o semaphore_info: Specifies a pointer to an SemaphoreInfo structure.
96%
97*/
98MagickExport void ActivateSemaphoreInfo(SemaphoreInfo **semaphore_info)
99{
100 assert(semaphore_info != (SemaphoreInfo **) NULL);
101 LockMagickMutex();
102 if (*semaphore_info == (SemaphoreInfo *) NULL)
103 *semaphore_info=AcquireSemaphoreInfo();
104 UnlockMagickMutex();
105}
106␌
107/*
108%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
109% %
110% %
111% %
112% A c q u i r e S e m a p h o r e I n f o %
113% %
114% %
115% %
116%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
117%
118% AcquireSemaphoreInfo() initializes the SemaphoreInfo structure.
119%
120% The format of the AcquireSemaphoreInfo method is:
121%
122% SemaphoreInfo *AcquireSemaphoreInfo(void)
123%
124*/
125
126static void *AcquireSemaphoreMemory(const size_t count,const size_t quantum)
127{
128#define AlignedExtent(size,alignment) \
129 (((size)+((alignment)-1)) & (size_t) ~((alignment)-1))
130
131 size_t
132 alignment,
133 extent,
134 size;
135
136 void
137 *memory;
138
139 size=count*quantum;
140 if ((count == 0) || (quantum != (size/count)))
141 {
142 errno=ENOMEM;
143 return((void *) NULL);
144 }
145 memory=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)
152 memory=NULL;
153#elif defined(MAGICKCORE_HAVE__ALIGNED_MALLOC)
154 memory=_aligned_malloc(extent,alignment);
155#else
156 {
157 void
158 *p;
159
160 extent=(size+alignment-1)+sizeof(void *);
161 if (extent > size)
162 {
163 p=malloc(extent);
164 if (p != NULL)
165 {
166 memory=(void *) AlignedExtent((size_t) p+sizeof(void *),alignment);
167 *((void **) memory-1)=p;
168 }
169 }
170 }
171#endif
172 return(memory);
173}
174
175static void *RelinquishSemaphoreMemory(void *memory)
176{
177 if (memory == (void *) NULL)
178 return((void *) NULL);
179#if defined(MAGICKCORE_HAVE_POSIX_MEMALIGN)
180 free(memory);
181#elif defined(MAGICKCORE_HAVE__ALIGNED_MALLOC)
182 _aligned_free(memory);
183#else
184 free(*((void **) memory-1));
185#endif
186 return(NULL);
187}
188
189MagickExport SemaphoreInfo *AcquireSemaphoreInfo(void)
190{
192 *semaphore_info;
193
194 /*
195 Acquire semaphore.
196 */
197 semaphore_info=(SemaphoreInfo *) AcquireSemaphoreMemory(1,
198 sizeof(*semaphore_info));
199 if (semaphore_info == (SemaphoreInfo *) NULL)
200 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
201 (void) memset(semaphore_info,0,sizeof(SemaphoreInfo));
202 /*
203 Initialize the semaphore.
204 */
205#if defined(MAGICKCORE_OPENMP_SUPPORT)
206 omp_init_lock((omp_lock_t *) &semaphore_info->mutex);
207#elif defined(MAGICKCORE_THREAD_SUPPORT)
208 {
209 int
210 status;
211
212 pthread_mutexattr_t
213 mutex_info;
214
215 status=pthread_mutexattr_init(&mutex_info);
216 if (status != 0)
217 {
218 errno=status;
219 perror("unable to initialize mutex attributes");
220 _exit(1);
221 }
222#if defined(MAGICKCORE_DEBUG)
223#if defined(PTHREAD_MUTEX_ERRORCHECK)
224 status=pthread_mutexattr_settype(&mutex_info,PTHREAD_MUTEX_ERRORCHECK);
225 if (status != 0)
226 {
227 errno=status;
228 perror("unable to set mutex type");
229 _exit(1);
230 }
231#endif
232#endif
233 status=pthread_mutex_init(&semaphore_info->mutex,&mutex_info);
234 if (status != 0)
235 {
236 errno=status;
237 perror("unable to initialize mutex");
238 _exit(1);
239 }
240 status=pthread_mutexattr_destroy(&mutex_info);
241 if (status != 0)
242 {
243 errno=status;
244 perror("unable to destroy mutex attributes");
245 _exit(1);
246 }
247 }
248#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
249 {
250 int
251 status;
252
253 status=InitializeCriticalSectionAndSpinCount(&semaphore_info->mutex,0x0400);
254 if (status == 0)
255 {
256 perror("unable to initialize critical section");
257 _exit(1);
258 }
259 }
260#endif
261 semaphore_info->id=GetMagickThreadId();
262 semaphore_info->reference_count=0;
263 semaphore_info->signature=MagickCoreSignature;
264 return(semaphore_info);
265}
266␌
267/*
268%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
269% %
270% %
271% %
272% L o c k S e m a p h o r e I n f o %
273% %
274% %
275% %
276%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
277%
278% LockSemaphoreInfo() locks a semaphore.
279%
280% The format of the LockSemaphoreInfo method is:
281%
282% void LockSemaphoreInfo(SemaphoreInfo *semaphore_info)
283%
284% A description of each parameter follows:
285%
286% o semaphore_info: Specifies a pointer to an SemaphoreInfo structure.
287%
288*/
289MagickExport void LockSemaphoreInfo(SemaphoreInfo *semaphore_info)
290{
291 assert(semaphore_info != (SemaphoreInfo *) NULL);
292 assert(semaphore_info->signature == MagickCoreSignature);
293#if defined(MAGICKCORE_DEBUG)
294 if ((semaphore_info->reference_count > 0) &&
295 (IsMagickThreadEqual(semaphore_info->id) != MagickFalse))
296 {
297 (void) FormatLocaleFile(stderr,"Warning: unexpected recursive lock!\n");
298 (void) fflush(stderr);
299 }
300#endif
301#if defined(MAGICKCORE_OPENMP_SUPPORT)
302 omp_set_lock((omp_lock_t *) &semaphore_info->mutex);
303#elif defined(MAGICKCORE_THREAD_SUPPORT)
304 {
305 int
306 status;
307
308 status=pthread_mutex_lock(&semaphore_info->mutex);
309 if (status != 0)
310 {
311 errno=status;
312 perror("unable to lock mutex");
313 _exit(1);
314 }
315 }
316#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
317 EnterCriticalSection(&semaphore_info->mutex);
318#endif
319#if defined(MAGICKCORE_DEBUG)
320 semaphore_info->id=GetMagickThreadId();
321 semaphore_info->reference_count++;
322#endif
323}
324␌
325/*
326%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
327% %
328% %
329% %
330% R e l i n q u i s h S e m a p h o r e I n f o %
331% %
332% %
333% %
334%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
335%
336% RelinquishSemaphoreInfo() destroys a semaphore.
337%
338% The format of the RelinquishSemaphoreInfo method is:
339%
340% void RelinquishSemaphoreInfo(SemaphoreInfo **semaphore_info)
341%
342% A description of each parameter follows:
343%
344% o semaphore_info: Specifies a pointer to an SemaphoreInfo structure.
345%
346*/
347MagickExport void RelinquishSemaphoreInfo(SemaphoreInfo **semaphore_info)
348{
349 assert(semaphore_info != (SemaphoreInfo **) NULL);
350 LockMagickMutex();
351 if (*semaphore_info != (SemaphoreInfo *) NULL)
352 {
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)
357 {
358 int
359 status;
360
361 status=pthread_mutex_destroy(&(*semaphore_info)->mutex);
362 if (status != 0)
363 {
364 errno=status;
365 perror("unable to destroy mutex");
366 _exit(1);
367 }
368 }
369#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
370 DeleteCriticalSection(&(*semaphore_info)->mutex);
371#endif
372 (*semaphore_info)->signature=(~MagickCoreSignature);
373 *semaphore_info=(SemaphoreInfo *) RelinquishSemaphoreMemory(
374 *semaphore_info);
375 }
376 UnlockMagickMutex();
377}
378␌
379/*
380%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
381% %
382% %
383% %
384% 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 %
385% %
386% %
387% %
388%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
389%
390% SemaphoreComponentGenesis() instantiates the semaphore environment.
391%
392% The format of the SemaphoreComponentGenesis method is:
393%
394% MagickBooleanType SemaphoreComponentGenesis(void)
395%
396*/
397MagickPrivate MagickBooleanType SemaphoreComponentGenesis(void)
398{
399 InitializeMagickMutex();
400 return(MagickTrue);
401}
402␌
403/*
404%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
405% %
406% %
407% %
408% 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 %
409% %
410% %
411% %
412%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
413%
414% SemaphoreComponentTerminus() destroys the semaphore component.
415%
416% The format of the SemaphoreComponentTerminus method is:
417%
418% SemaphoreComponentTerminus(void)
419%
420*/
421MagickPrivate void SemaphoreComponentTerminus(void)
422{
423 DestroyMagickMutex();
424}
425␌
426/*
427%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
428% %
429% %
430% %
431% U n l o c k S e m a p h o r e I n f o %
432% %
433% %
434% %
435%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
436%
437% UnlockSemaphoreInfo() unlocks a semaphore.
438%
439% The format of the UnlockSemaphoreInfo method is:
440%
441% void UnlockSemaphoreInfo(SemaphoreInfo *semaphore_info)
442%
443% A description of each parameter follows:
444%
445% o semaphore_info: Specifies a pointer to an SemaphoreInfo structure.
446%
447*/
448MagickExport void UnlockSemaphoreInfo(SemaphoreInfo *semaphore_info)
449{
450 assert(semaphore_info != (SemaphoreInfo *) NULL);
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)
455 {
456 (void) FormatLocaleFile(stderr,
457 "Warning: semaphore lock already unlocked!\n");
458 (void) fflush(stderr);
459 return;
460 }
461 semaphore_info->reference_count--;
462#endif
463#if defined(MAGICKCORE_OPENMP_SUPPORT)
464 omp_unset_lock((omp_lock_t *) &semaphore_info->mutex);
465#elif defined(MAGICKCORE_THREAD_SUPPORT)
466 {
467 int
468 status;
469
470 status=pthread_mutex_unlock(&semaphore_info->mutex);
471 if (status != 0)
472 {
473 errno=status;
474 perror("unable to unlock mutex");
475 _exit(1);
476 }
477 }
478#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
479 LeaveCriticalSection(&semaphore_info->mutex);
480#endif
481}