MagickCore  6.9.13-44
Convert, Edit, Or Compose Bitmap Images
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 "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,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 
50 static inline int GetMagickNumberThreads(const Image *source,
51  const Image *destination,const size_t chunk,const int 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 = 1,
60  workload_factor = 64UL << factor;
61 
62  /*
63  Determine number of threads based on workload.
64  */
65  number_threads=(chunk <= workload_factor) ? 1 :
66  (chunk >= (workload_factor << 6)) ? max_threads :
67  1+(chunk-workload_factor)*(max_threads-1)/(((workload_factor << 6))-1);
68  /*
69  Limit threads for non-memory or non-map cache sources/destinations.
70  */
71  if (((source_type != MemoryCache) && (source_type != MapCache)) ||
72  ((destination_type != MemoryCache) && (destination_type != MapCache)))
73  number_threads=MagickMin(number_threads,4);
74  return((int) number_threads);
75 }
76 
77 static inline MagickThreadType GetMagickThreadId(void)
78 {
79 #if defined(MAGICKCORE_THREAD_SUPPORT)
80  return(pthread_self());
81 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
82  return(GetCurrentThreadId());
83 #else
84  return(getpid());
85 #endif
86 }
87 
88 static inline void GetMagickThreadFilename(const char *filename,
89  char *thread_filename)
90 {
91  MagickThreadType
92  id;
93 
94  char
95  thread_id[2*sizeof(id)+1];
96 
97  ssize_t
98  i;
99 
100  unsigned char
101  bytes[sizeof(id)];
102 
103  id=GetMagickThreadId();
104  (void) memcpy(bytes,&id,sizeof(id));
105  for (i=0; i < (ssize_t) sizeof(bytes); i++)
106  (void) FormatLocaleString(thread_id+2*i,MagickPathExtent,"%02x",bytes[i]);
107  thread_id[sizeof(thread_id)-1]='\0';
108  (void) FormatLocaleString(thread_filename,MagickPathExtent,"%s|%s",thread_id,
109  filename);
110 }
111 
112 static inline size_t GetMagickThreadSignature(void)
113 {
114 #if defined(MAGICKCORE_THREAD_SUPPORT)
115  {
116  union
117  {
118  pthread_t
119  id;
120 
121  size_t
122  signature;
123  } magick_thread;
124 
125  magick_thread.signature=0UL;
126  magick_thread.id=pthread_self();
127  return(magick_thread.signature);
128  }
129 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
130  return((size_t) GetCurrentThreadId());
131 #else
132  return((size_t) getpid());
133 #endif
134 }
135 
136 static inline MagickBooleanType IsMagickThreadEqual(const MagickThreadType id)
137 {
138 #if defined(MAGICKCORE_THREAD_SUPPORT)
139  if (pthread_equal(id,pthread_self()) != 0)
140  return(MagickTrue);
141 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
142  if (id == GetCurrentThreadId())
143  return(MagickTrue);
144 #else
145  if (id == getpid())
146  return(MagickTrue);
147 #endif
148  return(MagickFalse);
149 }
150 
151 /*
152  Lightweight OpenMP methods.
153 */
154 static inline size_t GetOpenMPMaximumThreads(void)
155 {
156 #if defined(MAGICKCORE_OPENMP_SUPPORT)
157  return((size_t) omp_get_max_threads());
158 #else
159  return(1);
160 #endif
161 }
162 
163 static inline int GetOpenMPThreadId(void)
164 {
165 #if defined(MAGICKCORE_OPENMP_SUPPORT)
166  return(omp_get_thread_num());
167 #else
168  return(0);
169 #endif
170 }
171 
172 #if defined(MAGICKCORE_OPENMP_SUPPORT)
173 static inline void SetOpenMPMaximumThreads(const int threads)
174 {
175  omp_set_num_threads(threads);
176 #else
177 static inline void SetOpenMPMaximumThreads(const int magick_unused(threads))
178 {
179  magick_unreferenced(threads);
180 #endif
181 }
182 
183 #if defined(MAGICKCORE_OPENMP_SUPPORT)
184 static inline void SetOpenMPNested(const int value)
185 {
186  omp_set_nested(value);
187 #else
188 static inline void SetOpenMPNested(const int magick_unused(value))
189 {
190  magick_unreferenced(value);
191 #endif
192 }
193 
194 #if defined(__cplusplus) || defined(c_plusplus)
195 }
196 #endif
197 
198 #endif
Definition: image.h:133