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