blob: 450c1785442d5e21c6e884afa344792398ef4643 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2 Copyright 1999-2009 ImageMagick Studio LLC, a non-profit organization
3 dedicated to making software imaging solutions freely available.
4
5 You may not use this file except in compliance with the License.
6 obtain a copy of the License at
7
8 http://www.imagemagick.org/script/license.php
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15
16 MagickCore private methods for internal threading.
17*/
18#ifndef _MAGICKCORE_THREAD_PRIVATE_H
19#define _MAGICKCORE_THREAD_PRIVATE_H
20
21#if defined(__cplusplus) || defined(c_plusplus)
22extern "C" {
23#endif
24
25#include <magick/thread_.h>
26
27#if defined(MAGICKCORE_HAVE_PTHREAD)
28 typedef pthread_mutex_t MagickMutexType;
29#elif defined(__WINDOWS__)
30 typedef CRITICAL_SECTION MagickMutexType;
31#else
32 typedef unsigned long MagickMutexType;
33#endif
34
35static inline MagickThreadType GetMagickThreadId(void)
36{
37#if defined(MAGICKCORE_HAVE_PTHREAD)
38 return(pthread_self());
39#elif defined(__WINDOWS__)
40 return(GetCurrentThreadId());
41#else
42 return(getpid());
43#endif
44}
45
46static inline unsigned long GetMagickThreadSignature(void)
47{
48#if defined(MAGICKCORE_HAVE_PTHREAD)
49 {
50 union
51 {
52 pthread_t
53 id;
54
55 unsigned long
56 signature;
57 } magick_thread;
58
59 magick_thread.signature=0UL;
60 magick_thread.id=pthread_self();
61 return(magick_thread.signature);
62 }
63#elif defined(__WINDOWS__)
64 return((unsigned long) GetCurrentThreadId());
65#else
66 return((unsigned long) getpid());
67#endif
68}
69
70static inline MagickBooleanType IsMagickThreadEqual(const MagickThreadType id)
71{
72#if defined(MAGICKCORE_HAVE_PTHREAD)
73 if (pthread_equal(id,pthread_self()) != 0)
74 return(MagickTrue);
75#elif defined(__WINDOWS__)
76 if (id == GetCurrentThreadId())
77 return(MagickTrue);
78#else
79 if (id == getpid())
80 return(MagickTrue);
81#endif
82 return(MagickFalse);
83}
84
85/*
86 Lightweight OpenMP methods.
87*/
88static inline unsigned long GetOpenMPMaximumThreads(void)
89{
cristyb28d6472009-10-17 20:13:35 +000090#if defined(MAGICKCORE_OPENMP_SUPPORT) && (_OPENMP >= 200203)
cristy398f9322009-09-11 18:49:48 +000091 {
92 static unsigned long
93 maximum_threads = 1UL;
94
95 if (omp_get_max_threads() > (long) maximum_threads)
96 maximum_threads=omp_get_max_threads();
97 return(maximum_threads);
98 }
cristy3ed852e2009-09-05 21:47:34 +000099#endif
100 return(1UL);
101}
102
103static inline long GetOpenMPThreadId(void)
104{
cristyb28d6472009-10-17 20:13:35 +0000105#if defined(MAGICKCORE_OPENMP_SUPPORT) && (_OPENMP >= 200203)
cristy3ed852e2009-09-05 21:47:34 +0000106 return(omp_get_thread_num());
107#else
108 return(0);
109#endif
110}
111
112static inline void SetOpenMPMaximumThreads(const unsigned long threads)
113{
cristyb28d6472009-10-17 20:13:35 +0000114#if defined(MAGICKCORE_OPENMP_SUPPORT) && (_OPENMP >= 200203)
cristy3ed852e2009-09-05 21:47:34 +0000115 omp_set_num_threads(threads);
116#else
117 (void) threads;
118#endif
119}
120
cristy22ea5d42009-10-25 04:03:55 +0000121static inline void SetOpenMPNested(const int value)
122{
123#if defined(MAGICKCORE_OPENMP_SUPPORT) && (_OPENMP >= 200203)
124 omp_set_nested(value);
125#endif
126}
127
cristy3ed852e2009-09-05 21:47:34 +0000128#if defined(__cplusplus) || defined(c_plusplus)
129}
130#endif
131
132#endif