MagickCore  6.9.13-12
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/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,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 #define WorkLoadFactor (64UL << factor)
54 
55  const CacheType
56  destination_type = (CacheType) GetImagePixelCacheType(destination),
57  source_type = (CacheType) GetImagePixelCacheType(source);
58 
59  int
60  number_threads;
61 
62  /*
63  Return number of threads dependent on cache type and work load.
64  */
65  number_threads=(int) MagickMax(MagickMin(chunk/WorkLoadFactor,
66  GetMagickResourceLimit(ThreadResource)),1);
67  if (((source_type != MemoryCache) && (source_type != MapCache)) ||
68  ((destination_type != MemoryCache) && (destination_type != MapCache)))
69  number_threads=MagickMin(number_threads,2);
70  return(number_threads);
71 }
72 
73 static inline MagickThreadType GetMagickThreadId(void)
74 {
75 #if defined(MAGICKCORE_THREAD_SUPPORT)
76  return(pthread_self());
77 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
78  return(GetCurrentThreadId());
79 #else
80  return(getpid());
81 #endif
82 }
83 
84 static inline size_t GetMagickThreadSignature(void)
85 {
86 #if defined(MAGICKCORE_THREAD_SUPPORT)
87  {
88  union
89  {
90  pthread_t
91  id;
92 
93  size_t
94  signature;
95  } magick_thread;
96 
97  magick_thread.signature=0UL;
98  magick_thread.id=pthread_self();
99  return(magick_thread.signature);
100  }
101 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
102  return((size_t) GetCurrentThreadId());
103 #else
104  return((size_t) getpid());
105 #endif
106 }
107 
108 static inline MagickBooleanType IsMagickThreadEqual(const MagickThreadType id)
109 {
110 #if defined(MAGICKCORE_THREAD_SUPPORT)
111  if (pthread_equal(id,pthread_self()) != 0)
112  return(MagickTrue);
113 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
114  if (id == GetCurrentThreadId())
115  return(MagickTrue);
116 #else
117  if (id == getpid())
118  return(MagickTrue);
119 #endif
120  return(MagickFalse);
121 }
122 
123 /*
124  Lightweight OpenMP methods.
125 */
126 static inline size_t GetOpenMPMaximumThreads(void)
127 {
128 #if defined(MAGICKCORE_OPENMP_SUPPORT)
129  return((size_t) omp_get_max_threads());
130 #else
131  return(1);
132 #endif
133 }
134 
135 static inline int GetOpenMPThreadId(void)
136 {
137 #if defined(MAGICKCORE_OPENMP_SUPPORT)
138  return(omp_get_thread_num());
139 #else
140  return(0);
141 #endif
142 }
143 
144 #if defined(MAGICKCORE_OPENMP_SUPPORT)
145 static inline void SetOpenMPMaximumThreads(const int threads)
146 {
147  omp_set_num_threads(threads);
148 #else
149 static inline void SetOpenMPMaximumThreads(const int magick_unused(threads))
150 {
151  magick_unreferenced(threads);
152 #endif
153 }
154 
155 #if defined(MAGICKCORE_OPENMP_SUPPORT)
156 static inline void SetOpenMPNested(const int value)
157 {
158  omp_set_nested(value);
159 #else
160 static inline void SetOpenMPNested(const int magick_unused(value))
161 {
162  magick_unreferenced(value);
163 #endif
164 }
165 
166 #if defined(__cplusplus) || defined(c_plusplus)
167 }
168 #endif
169 
170 #endif
Definition: image.h:134