]> granicus.if.org Git - imagemagick/blob - MagickCore/thread-private.h
(no commit message)
[imagemagick] / MagickCore / thread-private.h
1 /*
2   Copyright 1999-2011 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)
22 extern "C" {
23 #endif
24
25 #include <MagickCore/thread_.h>
26
27 #if (__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR > 10))
28 #define MagickCachePrefetch(address,mode,locality) \
29   __builtin_prefetch(address,mode,locality)
30 #else
31 #define MagickCachePrefetch(address,mode,locality)
32 #endif
33
34 #define omp_throttle(factor)  num_threads(omp_get_max_threads() >> \
35    (factor) == 0 ? 1 : omp_get_max_threads() >> (factor))
36
37 #if defined(MAGICKCORE_THREAD_SUPPORT)
38   typedef pthread_mutex_t MagickMutexType;
39 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
40   typedef CRITICAL_SECTION MagickMutexType;
41 #else
42   typedef size_t MagickMutexType;
43 #endif
44
45 static inline MagickThreadType GetMagickThreadId(void)
46 {
47 #if defined(MAGICKCORE_THREAD_SUPPORT)
48   return(pthread_self());
49 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
50   return(GetCurrentThreadId());
51 #else
52   return(getpid());
53 #endif
54 }
55
56 static inline size_t GetMagickThreadSignature(void)
57 {
58 #if defined(MAGICKCORE_THREAD_SUPPORT)
59   {
60     union
61     {
62       pthread_t
63         id;
64
65       size_t
66         signature;
67     } magick_thread;
68
69     magick_thread.signature=0UL;
70     magick_thread.id=pthread_self();
71     return(magick_thread.signature);
72   }
73 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
74   return((size_t) GetCurrentThreadId());
75 #else
76   return((size_t) getpid());
77 #endif
78 }
79
80 static inline MagickBooleanType IsMagickThreadEqual(const MagickThreadType id)
81 {
82 #if defined(MAGICKCORE_THREAD_SUPPORT)
83   if (pthread_equal(id,pthread_self()) != 0)
84     return(MagickTrue);
85 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
86   if (id == GetCurrentThreadId())
87     return(MagickTrue);
88 #else
89   if (id == getpid())
90     return(MagickTrue);
91 #endif
92   return(MagickFalse);
93 }
94
95 /*
96   Lightweight OpenMP methods.
97 */
98 static inline size_t GetOpenMPMaximumThreads(void)
99 {
100   static size_t
101     maximum_threads = 1;
102
103 #if defined(MAGICKCORE_OPENMP_SUPPORT) && (_OPENMP >= 200203)
104   {
105     ssize_t
106       threads;
107
108     threads=omp_get_max_threads();
109     if (threads > (ssize_t) maximum_threads)
110       maximum_threads=threads;
111   }
112 #endif
113   return(maximum_threads);
114 }
115
116 static inline int GetOpenMPThreadId(void)
117 {
118 #if defined(MAGICKCORE_OPENMP_SUPPORT) && (_OPENMP >= 200203)
119   return(omp_get_thread_num());
120 #else
121   return(0);
122 #endif
123 }
124
125 static inline void SetOpenMPMaximumThreads(const int threads)
126 {
127 #if defined(MAGICKCORE_OPENMP_SUPPORT) && (_OPENMP >= 200203)
128   omp_set_num_threads(threads);
129 #else
130   (void) threads;
131 #endif
132 }
133
134 static inline void SetOpenMPNested(const int value)
135 {
136 #if defined(MAGICKCORE_OPENMP_SUPPORT) && (_OPENMP >= 200203)
137   omp_set_nested(value);
138 #else
139   (void) value;
140 #endif
141 }
142
143 #if defined(__cplusplus) || defined(c_plusplus)
144 }
145 #endif
146
147 #endif