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