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