1 /*
2 Copyright 1999-2021 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. You may
6 obtain a copy of the License at
7
8 https://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 "magick/cache.h"
22 #include "magick/image-private.h"
23 #include "magick/resource_.h"
24 #include "magick/thread_.h"
25
26 #if defined(__cplusplus) || defined(c_plusplus)
27 extern "C" {
28 #endif
29
30 #define magick_number_threads(source,destination,chunk,multithreaded) \
31 num_threads(GetMagickNumberThreads(source,destination,chunk,multithreaded))
32 #if defined(__clang__) || (__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ > 10))
33 #define MagickCachePrefetch(address,mode,locality) \
34 __builtin_prefetch(address,mode,locality)
35 #else
36 #define MagickCachePrefetch(address,mode,locality)
37 #endif
38
39 #if defined(MAGICKCORE_THREAD_SUPPORT)
40 typedef pthread_mutex_t MagickMutexType;
41 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
42 typedef CRITICAL_SECTION MagickMutexType;
43 #else
44 typedef size_t MagickMutexType;
45 #endif
46
GetMagickThreadId(void)47 static inline MagickThreadType GetMagickThreadId(void)
48 {
49 #if defined(MAGICKCORE_THREAD_SUPPORT)
50 return(pthread_self());
51 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
52 return(GetCurrentThreadId());
53 #else
54 return(getpid());
55 #endif
56 }
57
GetMagickNumberThreads(const Image * source,const Image * destination,const size_t chunk,int multithreaded)58 static inline int GetMagickNumberThreads(const Image *source,
59 const Image *destination,const size_t chunk,int multithreaded)
60 {
61 const CacheType
62 destination_type = (const CacheType) GetImagePixelCacheType(destination),
63 source_type = (const CacheType) GetImagePixelCacheType(source);
64
65 int
66 number_threads;
67
68 /*
69 Return number of threads dependent on cache type and work load.
70 */
71 if (multithreaded == 0)
72 return(1);
73 if (((source_type != MemoryCache) && (source_type != MapCache)) ||
74 ((destination_type != MemoryCache) && (destination_type != MapCache)))
75 number_threads=(int) MagickMin(GetMagickResourceLimit(ThreadResource),2);
76 else
77 number_threads=(int) MagickMin((ssize_t)
78 GetMagickResourceLimit(ThreadResource),(ssize_t) (chunk)/64);
79 return(MagickMax(number_threads,1));
80 }
81
GetMagickThreadSignature(void)82 static inline size_t GetMagickThreadSignature(void)
83 {
84 #if defined(MAGICKCORE_THREAD_SUPPORT)
85 {
86 union
87 {
88 pthread_t
89 id;
90
91 size_t
92 signature;
93 } magick_thread;
94
95 magick_thread.signature=0UL;
96 magick_thread.id=pthread_self();
97 return(magick_thread.signature);
98 }
99 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
100 return((size_t) GetCurrentThreadId());
101 #else
102 return((size_t) getpid());
103 #endif
104 }
105
IsMagickThreadEqual(const MagickThreadType id)106 static inline MagickBooleanType IsMagickThreadEqual(const MagickThreadType id)
107 {
108 #if defined(MAGICKCORE_THREAD_SUPPORT)
109 if (pthread_equal(id,pthread_self()) != 0)
110 return(MagickTrue);
111 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
112 if (id == GetCurrentThreadId())
113 return(MagickTrue);
114 #else
115 if (id == getpid())
116 return(MagickTrue);
117 #endif
118 return(MagickFalse);
119 }
120
121 /*
122 Lightweight OpenMP methods.
123 */
GetOpenMPMaximumThreads(void)124 static inline size_t GetOpenMPMaximumThreads(void)
125 {
126 #if defined(MAGICKCORE_OPENMP_SUPPORT)
127 return(omp_get_max_threads());
128 #else
129 return(1);
130 #endif
131 }
132
GetOpenMPThreadId(void)133 static inline int GetOpenMPThreadId(void)
134 {
135 #if defined(MAGICKCORE_OPENMP_SUPPORT)
136 return(omp_get_thread_num());
137 #else
138 return(0);
139 #endif
140 }
141
SetOpenMPMaximumThreads(const int threads)142 static inline void SetOpenMPMaximumThreads(const int threads)
143 {
144 #if defined(MAGICKCORE_OPENMP_SUPPORT)
145 omp_set_num_threads(threads);
146 #else
147 (void) threads;
148 #endif
149 }
150
SetOpenMPNested(const int value)151 static inline void SetOpenMPNested(const int value)
152 {
153 #if defined(MAGICKCORE_OPENMP_SUPPORT)
154 omp_set_nested(value);
155 #else
156 (void) value;
157 #endif
158 }
159
160 #if defined(__cplusplus) || defined(c_plusplus)
161 }
162 #endif
163
164 #endif
165