MagickCore  6.9.11-60
Convert, Edit, Or Compose Bitmap Images
thread-private.h
Go to the documentation of this file.
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 /*
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 static inline size_t GetMagickThreadSignature(void)
70 {
71 #if defined(MAGICKCORE_THREAD_SUPPORT)
72  {
73  union
74  {
75  pthread_t
76  id;
77 
78  size_t
79  signature;
80  } magick_thread;
81 
82  magick_thread.signature=0UL;
83  magick_thread.id=pthread_self();
84  return(magick_thread.signature);
85  }
86 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
87  return((size_t) GetCurrentThreadId());
88 #else
89  return((size_t) getpid());
90 #endif
91 }
92 
94 {
95 #if defined(MAGICKCORE_THREAD_SUPPORT)
96  if (pthread_equal(id,pthread_self()) != 0)
97  return(MagickTrue);
98 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
99  if (id == GetCurrentThreadId())
100  return(MagickTrue);
101 #else
102  if (id == getpid())
103  return(MagickTrue);
104 #endif
105  return(MagickFalse);
106 }
107 
108 /*
109  Lightweight OpenMP methods.
110 */
111 static inline size_t GetOpenMPMaximumThreads(void)
112 {
113 #if defined(MAGICKCORE_OPENMP_SUPPORT)
114  return(omp_get_max_threads());
115 #else
116  return(1);
117 #endif
118 }
119 
120 static inline int GetOpenMPThreadId(void)
121 {
122 #if defined(MAGICKCORE_OPENMP_SUPPORT)
123  return(omp_get_thread_num());
124 #else
125  return(0);
126 #endif
127 }
128 
129 static inline void SetOpenMPMaximumThreads(const int threads)
130 {
131 #if defined(MAGICKCORE_OPENMP_SUPPORT)
132  omp_set_num_threads(threads);
133 #else
134  (void) threads;
135 #endif
136 }
137 
138 static inline void SetOpenMPNested(const int value)
139 {
140 #if defined(MAGICKCORE_OPENMP_SUPPORT)
141  omp_set_nested(value);
142 #else
143  (void) value;
144 #endif
145 }
146 
147 #if defined(__cplusplus) || defined(c_plusplus)
148 }
149 #endif
150 
151 #endif
MagickBooleanType
Definition: magick-type.h:203
@ MagickFalse
Definition: magick-type.h:204
@ MagickTrue
Definition: magick-type.h:205
static void SetOpenMPNested(const int value)
Definition: thread-private.h:138
static int GetOpenMPThreadId(void)
Definition: thread-private.h:120
static MagickBooleanType IsMagickThreadEqual(const MagickThreadType id)
Definition: thread-private.h:93
static MagickThreadType GetMagickThreadId(void)
Definition: thread-private.h:58
static size_t GetOpenMPMaximumThreads(void)
Definition: thread-private.h:111
size_t MagickMutexType
Definition: thread-private.h:55
static void SetOpenMPMaximumThreads(const int threads)
Definition: thread-private.h:129
static size_t GetMagickThreadSignature(void)
Definition: thread-private.h:69
pid_t MagickThreadType
Definition: thread_.h:34
#define pthread_self
Definition: vms.h:836
#define pthread_equal
Definition: vms.h:802