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