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