MagickCore 7.1.2-32
Convert, Edit, Or Compose Bitmap Images
Loading...
Searching...
No Matches
thread-private.h
1/*
2 Copyright @ 1999 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/license/
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)
27extern "C" {
28#endif
29
30#define magick_number_threads(source,destination,chunk,factor) \
31 num_threads(GetMagickNumberThreads((source),(destination),(chunk),(factor)))
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 magick_unreferenced(address); \
38 magick_unreferenced(mode); \
39 magick_unreferenced(locality);
40#endif
41
42#if defined(MAGICKCORE_THREAD_SUPPORT)
43 typedef pthread_mutex_t MagickMutexType;
44#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
45 typedef CRITICAL_SECTION MagickMutexType;
46#else
47 typedef size_t MagickMutexType;
48#endif
49
50static inline int GetMagickNumberThreads(const Image *source,
51 const Image *destination,const size_t chunk,const double factor)
52{
53 const CacheType
54 destination_type = (CacheType) GetImagePixelCacheType(destination),
55 source_type = (CacheType) GetImagePixelCacheType(source);
56
57 size_t
58 max_threads = (size_t) GetMagickResourceLimit(ThreadResource),
59 number_threads = 1UL,
60 pixels_per_thread = CastDoubleToSizeT(262144.0*factor);
61
62 if (pixels_per_thread != 0)
63 {
64 const size_t total_pixels = chunk*source->columns;
65 number_threads=(total_pixels < pixels_per_thread) ? 1UL :
66 MagickMin(max_threads,(total_pixels+pixels_per_thread-1)/
67 pixels_per_thread);
68 }
69 if (((source_type != MemoryCache) && (source_type != MapCache)) ||
70 ((destination_type != MemoryCache) && (destination_type != MapCache)))
71 number_threads=MagickMin(number_threads,4UL);
72 return((int) number_threads);
73}
74
75static inline MagickThreadType GetMagickThreadId(void)
76{
77#if defined(MAGICKCORE_THREAD_SUPPORT)
78 return(pthread_self());
79#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
80 return(GetCurrentThreadId());
81#else
82 return(getpid());
83#endif
84}
85
86static inline void GetMagickThreadFilename(const char *filename,
87 char *thread_filename)
88{
89 MagickThreadType
90 id;
91
92 char
93 thread_id[2*sizeof(id)+1];
94
95 ssize_t
96 i;
97
98 unsigned char
99 bytes[sizeof(id)];
100
101 id=GetMagickThreadId();
102 (void) memcpy(bytes,&id,sizeof(id));
103 for (i=0; i < (ssize_t) sizeof(bytes); i++)
104 (void) FormatLocaleString(thread_id+2*i,MagickPathExtent,"%02x",bytes[i]);
105 thread_id[sizeof(thread_id)-1]='\0';
106 (void) FormatLocaleString(thread_filename,MagickPathExtent,"%s|%s",thread_id,
107 filename);
108}
109
110static inline size_t GetMagickThreadSignature(void)
111{
112#if defined(MAGICKCORE_THREAD_SUPPORT)
113 {
114 union
115 {
116 pthread_t
117 id;
118
119 size_t
120 signature;
121 } magick_thread;
122
123 magick_thread.signature=0UL;
124 magick_thread.id=pthread_self();
125 return(magick_thread.signature);
126 }
127#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
128 return((size_t) GetCurrentThreadId());
129#else
130 return((size_t) getpid());
131#endif
132}
133
134static inline MagickBooleanType IsMagickThreadEqual(const MagickThreadType id)
135{
136#if defined(MAGICKCORE_THREAD_SUPPORT)
137 if (pthread_equal(id,pthread_self()) != 0)
138 return(MagickTrue);
139#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
140 if (id == GetCurrentThreadId())
141 return(MagickTrue);
142#else
143 if (id == getpid())
144 return(MagickTrue);
145#endif
146 return(MagickFalse);
147}
148
149/*
150 Lightweight OpenMP methods.
151*/
152static inline size_t GetOpenMPMaximumThreads(void)
153{
154#if defined(MAGICKCORE_OPENMP_SUPPORT)
155 return((size_t) omp_get_max_threads());
156#else
157 return(1);
158#endif
159}
160
161static inline int GetOpenMPThreadId(void)
162{
163#if defined(MAGICKCORE_OPENMP_SUPPORT)
164 return(omp_get_thread_num());
165#else
166 return(0);
167#endif
168}
169
170#if defined(MAGICKCORE_OPENMP_SUPPORT)
171static inline void SetOpenMPMaximumThreads(const int threads)
172{
173 omp_set_num_threads(threads);
174#else
175static inline void SetOpenMPMaximumThreads(const int magick_unused(threads))
176{
177 magick_unreferenced(threads);
178#endif
179}
180
181#if defined(MAGICKCORE_OPENMP_SUPPORT)
182static inline void SetOpenMPMaxActiveLevels(const int value)
183{
184#if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__MINGW32__)
185 omp_set_nested(value);
186#else
187 omp_set_max_active_levels(value ? 2 : 1);
188#endif
189#else
190static inline void SetOpenMPMaxActiveLevels(const int magick_unused(value))
191{
192 magick_unreferenced(value);
193#endif
194}
195
196#if defined(__cplusplus) || defined(c_plusplus)
197}
198#endif
199
200#endif