MagickCore  6.9.9
thread-private.h
Go to the documentation of this file.
1 /*
2  Copyright 1999-2017 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://www.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  Single threaded unless workload justifies the threading overhead.
32 */
33 #define magick_threads(source,destination,chunk,expression) \
34  num_threads((((expression) != 0) && \
35  ((GetImagePixelCacheType(source) == MemoryCache) || \
36  (GetImagePixelCacheType(source) == MapCache)) && \
37  ((GetImagePixelCacheType(destination) == MemoryCache) || \
38  (GetImagePixelCacheType(destination) == MapCache))) ? \
39  MagickMax(1,MagickMin(GetMagickResourceLimit(ThreadResource),(chunk)/16)) : 1)
40 
41 #if defined(__clang__) || (__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ > 10))
42 #define MagickCachePrefetch(address,mode,locality) \
43  __builtin_prefetch(address,mode,locality)
44 #else
45 #define MagickCachePrefetch(address,mode,locality)
46 #endif
47 
48 #if defined(MAGICKCORE_THREAD_SUPPORT)
49  typedef pthread_mutex_t MagickMutexType;
50 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
51  typedef CRITICAL_SECTION MagickMutexType;
52 #else
53  typedef size_t MagickMutexType;
54 #endif
55 
57 {
58 #if defined(MAGICKCORE_THREAD_SUPPORT)
59  return(pthread_self());
60 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
61  return(GetCurrentThreadId());
62 #else
63  return(getpid());
64 #endif
65 }
66 
67 static inline size_t GetMagickThreadSignature(void)
68 {
69 #if defined(MAGICKCORE_THREAD_SUPPORT)
70  {
71  union
72  {
73  pthread_t
74  id;
75 
76  size_t
77  signature;
78  } magick_thread;
79 
80  magick_thread.signature=0UL;
81  magick_thread.id=pthread_self();
82  return(magick_thread.signature);
83  }
84 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
85  return((size_t) GetCurrentThreadId());
86 #else
87  return((size_t) getpid());
88 #endif
89 }
90 
92 {
93 #if defined(MAGICKCORE_THREAD_SUPPORT)
94  if (pthread_equal(id,pthread_self()) != 0)
95  return(MagickTrue);
96 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
97  if (id == GetCurrentThreadId())
98  return(MagickTrue);
99 #else
100  if (id == getpid())
101  return(MagickTrue);
102 #endif
103  return(MagickFalse);
104 }
105 
106 /*
107  Lightweight OpenMP methods.
108 */
109 static inline size_t GetOpenMPMaximumThreads(void)
110 {
111 #if defined(MAGICKCORE_OPENMP_SUPPORT)
112  return(omp_get_max_threads());
113 #else
114  return(1);
115 #endif
116 }
117 
118 static inline int GetOpenMPThreadId(void)
119 {
120 #if defined(MAGICKCORE_OPENMP_SUPPORT)
121  return(omp_get_thread_num());
122 #else
123  return(0);
124 #endif
125 }
126 
127 static inline void SetOpenMPMaximumThreads(const int threads)
128 {
129 #if defined(MAGICKCORE_OPENMP_SUPPORT)
130  omp_set_num_threads(threads);
131 #else
132  (void) threads;
133 #endif
134 }
135 
136 static inline void SetOpenMPNested(const int value)
137 {
138 #if defined(MAGICKCORE_OPENMP_SUPPORT)
139  omp_set_nested(value);
140 #else
141  (void) value;
142 #endif
143 }
144 
145 #if defined(__cplusplus) || defined(c_plusplus)
146 }
147 #endif
148 
149 #endif
static MagickThreadType GetMagickThreadId(void)
Definition: thread-private.h:56
static size_t GetOpenMPMaximumThreads(void)
Definition: thread-private.h:109
#define pthread_self
Definition: vms.h:836
static MagickBooleanType IsMagickThreadEqual(const MagickThreadType id)
Definition: thread-private.h:91
size_t MagickMutexType
Definition: thread-private.h:53
static void SetOpenMPMaximumThreads(const int threads)
Definition: thread-private.h:127
MagickBooleanType
Definition: magick-type.h:189
pid_t MagickThreadType
Definition: thread_.h:34
static int GetOpenMPThreadId(void)
Definition: thread-private.h:118
Definition: magick-type.h:192
static void SetOpenMPNested(const int value)
Definition: thread-private.h:136
Definition: magick-type.h:191
static size_t GetMagickThreadSignature(void)
Definition: thread-private.h:67
#define pthread_equal
Definition: vms.h:802