MagickCore  6.9.10
Convert, Edit, Or Compose Bitmap Images
thread-private.h
Go to the documentation of this file.
1 /*
2  Copyright 1999-2019 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://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 /*
31  Number of threads bounded by the amount of work and any thread resource limit.
32  The limit is 2 if the pixel cache type is not memory or memory-mapped.
33 */
34 #define magick_number_threads(source,destination,chunk,multithreaded) \
35  num_threads((multithreaded) == 0 ? 1 : \
36  ((GetImagePixelCacheType(source) != MemoryCache) && \
37  (GetImagePixelCacheType(source) != MapCache)) || \
38  ((GetImagePixelCacheType(destination) != MemoryCache) && \
39  (GetImagePixelCacheType(destination) != MapCache)) ? \
40  MagickMax(MagickMin(GetMagickResourceLimit(ThreadResource),2),1) : \
41  MagickMax(MagickMin((ssize_t) GetMagickResourceLimit(ThreadResource),(ssize_t) (chunk)/64),1))
42 
43 #if defined(__clang__) || (__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ > 10))
44 #define MagickCachePrefetch(address,mode,locality) \
45  __builtin_prefetch(address,mode,locality)
46 #else
47 #define MagickCachePrefetch(address,mode,locality)
48 #endif
49 
50 #if defined(MAGICKCORE_THREAD_SUPPORT)
51  typedef pthread_mutex_t MagickMutexType;
52 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
53  typedef CRITICAL_SECTION MagickMutexType;
54 #else
55  typedef size_t MagickMutexType;
56 #endif
57 
59 {
60 #if defined(MAGICKCORE_THREAD_SUPPORT)
61  return(pthread_self());
62 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
63  return(GetCurrentThreadId());
64 #else
65  return(getpid());
66 #endif
67 }
68 
69 
70 static inline void GetMagickThreadFilename(const char *filename,
71  char *thread_filename)
72 {
74  id;
75 
76  char
77  thread_id[2*sizeof(id)+1];
78 
79  ssize_t
80  i;
81 
82  unsigned char
83  bytes[sizeof(id)];
84 
85  id=GetMagickThreadId();
86  (void) memcpy(bytes,&id,sizeof(id));
87  for (i=0; i < (ssize_t) sizeof(bytes); i++)
88  (void) FormatLocaleString(thread_id+2*i,MagickPathExtent,"%02x",bytes[i]);
89  thread_id[sizeof(thread_id)-1]='\0';
90  (void) FormatLocaleString(thread_filename,MagickPathExtent,"%s|%s",thread_id,
91  filename);
92 }
93 
94 static inline size_t GetMagickThreadSignature(void)
95 {
96 #if defined(MAGICKCORE_THREAD_SUPPORT)
97  {
98  union
99  {
100  pthread_t
101  id;
102 
103  size_t
104  signature;
105  } magick_thread;
106 
107  magick_thread.signature=0UL;
108  magick_thread.id=pthread_self();
109  return(magick_thread.signature);
110  }
111 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
112  return((size_t) GetCurrentThreadId());
113 #else
114  return((size_t) getpid());
115 #endif
116 }
117 
119 {
120 #if defined(MAGICKCORE_THREAD_SUPPORT)
121  if (pthread_equal(id,pthread_self()) != 0)
122  return(MagickTrue);
123 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
124  if (id == GetCurrentThreadId())
125  return(MagickTrue);
126 #else
127  if (id == getpid())
128  return(MagickTrue);
129 #endif
130  return(MagickFalse);
131 }
132 
133 /*
134  Lightweight OpenMP methods.
135 */
136 static inline size_t GetOpenMPMaximumThreads(void)
137 {
138 #if defined(MAGICKCORE_OPENMP_SUPPORT)
139  return(omp_get_max_threads());
140 #else
141  return(1);
142 #endif
143 }
144 
145 static inline int GetOpenMPThreadId(void)
146 {
147 #if defined(MAGICKCORE_OPENMP_SUPPORT)
148  return(omp_get_thread_num());
149 #else
150  return(0);
151 #endif
152 }
153 
154 static inline void SetOpenMPMaximumThreads(const int threads)
155 {
156 #if defined(MAGICKCORE_OPENMP_SUPPORT)
157  omp_set_num_threads(threads);
158 #else
159  (void) threads;
160 #endif
161 }
162 
163 static inline void SetOpenMPNested(const int value)
164 {
165 #if defined(MAGICKCORE_OPENMP_SUPPORT)
166  omp_set_nested(value);
167 #else
168  (void) value;
169 #endif
170 }
171 
172 #if defined(__cplusplus) || defined(c_plusplus)
173 }
174 #endif
175 
176 #endif
static MagickThreadType GetMagickThreadId(void)
Definition: thread-private.h:58
static size_t GetOpenMPMaximumThreads(void)
Definition: thread-private.h:136
#define pthread_self
Definition: vms.h:836
MagickExport ssize_t FormatLocaleString(char *magick_restrict string, const size_t length, const char *magick_restrict format,...)
Definition: locale.c:502
static MagickBooleanType IsMagickThreadEqual(const MagickThreadType id)
Definition: thread-private.h:118
size_t MagickMutexType
Definition: thread-private.h:55
static void SetOpenMPMaximumThreads(const int threads)
Definition: thread-private.h:154
MagickBooleanType
Definition: magick-type.h:191
pid_t MagickThreadType
Definition: thread_.h:34
static int GetOpenMPThreadId(void)
Definition: thread-private.h:145
Definition: magick-type.h:194
static void SetOpenMPNested(const int value)
Definition: thread-private.h:163
Definition: magick-type.h:193
#define MagickPathExtent
Definition: magick-type.h:31
static size_t GetMagickThreadSignature(void)
Definition: thread-private.h:94
#define pthread_equal
Definition: vms.h:802
static void GetMagickThreadFilename(const char *filename, char *thread_filename)
Definition: thread-private.h:70