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