]> 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 WorkloadThreshold()  (16*GetMagickResourceLimit(ThreadResource))
33 #define dynamic_number_threads(image,columns,rows,expression) \
34   if (((((columns) > WorkloadThreshold()) || \
35       ((rows) > WorkloadThreshold()))) && ((MagickSizeType) \
36       ((columns)*(rows)) > (WorkloadThreshold()*WorkloadThreshold())) && \
37       (expression)) \
38     num_threads(GetMagickResourceLimit(ThreadResource)/ \
39       (GetImagePixelCacheType(image) == DiskCache ? 2 : 1))
40
41 #if (__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ > 10))
42 #define MagickCachePrefetch(address,mode,locality) \
43   __builtin_prefetch(address,mode,locality)
44 #else
45 #define MagickCachePrefetch(address,mode,locality)
46 #endif
47
48 #if defined(MAGICKCORE_THREAD_SUPPORT)
49   typedef pthread_mutex_t MagickMutexType;
50 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
51   typedef CRITICAL_SECTION MagickMutexType;
52 #else
53   typedef size_t MagickMutexType;
54 #endif
55
56 static inline MagickThreadType GetMagickThreadId(void)
57 {
58 #if defined(MAGICKCORE_THREAD_SUPPORT)
59   return(pthread_self());
60 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
61   return(GetCurrentThreadId());
62 #else
63   return(getpid());
64 #endif
65 }
66
67 static inline size_t GetMagickThreadSignature(void)
68 {
69 #if defined(MAGICKCORE_THREAD_SUPPORT)
70   {
71     union
72     {
73       pthread_t
74         id;
75
76       size_t
77         signature;
78     } magick_thread;
79
80     magick_thread.signature=0UL;
81     magick_thread.id=pthread_self();
82     return(magick_thread.signature);
83   }
84 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
85   return((size_t) GetCurrentThreadId());
86 #else
87   return((size_t) getpid());
88 #endif
89 }
90
91 static inline MagickBooleanType IsMagickThreadEqual(const MagickThreadType id)
92 {
93 #if defined(MAGICKCORE_THREAD_SUPPORT)
94   if (pthread_equal(id,pthread_self()) != 0)
95     return(MagickTrue);
96 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
97   if (id == GetCurrentThreadId())
98     return(MagickTrue);
99 #else
100   if (id == getpid())
101     return(MagickTrue);
102 #endif
103   return(MagickFalse);
104 }
105
106 /*
107   Lightweight OpenMP methods.
108 */
109 static inline size_t GetOpenMPMaximumThreads(void)
110 {
111 #if defined(MAGICKCORE_OPENMP_SUPPORT)
112   return(omp_get_max_threads());
113 #else
114   return(1);
115 #endif
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