MagickCore  6.8.5
thread-private.h
Go to the documentation of this file.
1 /*
2  Copyright 1999-2013 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 #if defined(__cplusplus) || defined(c_plusplus)
22 extern "C" {
23 #endif
24 
25 #include "magick/cache.h"
26 #include "magick/resource_.h"
27 #include "magick/thread_.h"
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) > (16*GetMagickResourceLimit(ThreadResource))) && \
35  (GetImagePixelCacheType(source) != DiskCache)) && \
36  (GetImagePixelCacheType(destination) != DiskCache) ? \
37  GetMagickResourceLimit(ThreadResource) : \
38  GetMagickResourceLimit(ThreadResource) < 2 ? 1 : 2)
39 
40 #if (__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 #endif
113  return(1);
114 }
115 
116 static inline int GetOpenMPThreadId(void)
117 {
118 #if defined(MAGICKCORE_OPENMP_SUPPORT)
119  return(omp_get_thread_num());
120 #else
121  return(0);
122 #endif
123 }
124 
125 static inline void SetOpenMPMaximumThreads(const int threads)
126 {
127 #if defined(MAGICKCORE_OPENMP_SUPPORT)
128  omp_set_num_threads(threads);
129 #else
130  (void) threads;
131 #endif
132 }
133 
134 static inline void SetOpenMPNested(const int value)
135 {
136 #if defined(MAGICKCORE_OPENMP_SUPPORT)
137  omp_set_nested(value);
138 #else
139  (void) value;
140 #endif
141 }
142 
143 #if defined(__cplusplus) || defined(c_plusplus)
144 }
145 #endif
146 
147 #endif