• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2   Copyright 1999-2016 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 "MagickCore/cache.h"
22 #include "MagickCore/resource_.h"
23 #include "MagickCore/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) == MemoryCache) || \
36       (GetImagePixelCacheType(source) == MapCache)) && \
37      ((GetImagePixelCacheType(destination) == MemoryCache) || \
38       (GetImagePixelCacheType(destination) == MapCache)) ? \
39       GetMagickResourceLimit(ThreadResource) : \
40       GetMagickResourceLimit(ThreadResource) < 2 ? 1 : 2)
41 
42 #if defined(__clang__) || (__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ > 10))
43 #define MagickCachePrefetch(address,mode,locality) \
44   __builtin_prefetch(address,mode,locality)
45 #else
46 #define MagickCachePrefetch(address,mode,locality) \
47   magick_unreferenced(address); \
48   magick_unreferenced(mode); \
49   magick_unreferenced(locality);
50 #endif
51 
52 #if defined(MAGICKCORE_THREAD_SUPPORT)
53   typedef pthread_mutex_t MagickMutexType;
54 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
55   typedef CRITICAL_SECTION MagickMutexType;
56 #else
57   typedef size_t MagickMutexType;
58 #endif
59 
GetMagickThreadId(void)60 static inline MagickThreadType GetMagickThreadId(void)
61 {
62 #if defined(MAGICKCORE_THREAD_SUPPORT)
63   return(pthread_self());
64 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
65   return(GetCurrentThreadId());
66 #else
67   return(getpid());
68 #endif
69 }
70 
GetMagickThreadSignature(void)71 static inline size_t GetMagickThreadSignature(void)
72 {
73 #if defined(MAGICKCORE_THREAD_SUPPORT)
74   {
75     union
76     {
77       pthread_t
78         id;
79 
80       size_t
81         signature;
82     } magick_thread;
83 
84     magick_thread.signature=0UL;
85     magick_thread.id=pthread_self();
86     return(magick_thread.signature);
87   }
88 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
89   return((size_t) GetCurrentThreadId());
90 #else
91   return((size_t) getpid());
92 #endif
93 }
94 
IsMagickThreadEqual(const MagickThreadType id)95 static inline MagickBooleanType IsMagickThreadEqual(const MagickThreadType id)
96 {
97 #if defined(MAGICKCORE_THREAD_SUPPORT)
98   if (pthread_equal(id,pthread_self()) != 0)
99     return(MagickTrue);
100 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
101   if (id == GetCurrentThreadId())
102     return(MagickTrue);
103 #else
104   if (id == getpid())
105     return(MagickTrue);
106 #endif
107   return(MagickFalse);
108 }
109 
110 /*
111   Lightweight OpenMP methods.
112 */
GetOpenMPMaximumThreads(void)113 static inline size_t GetOpenMPMaximumThreads(void)
114 {
115 #if defined(MAGICKCORE_OPENMP_SUPPORT)
116   return(omp_get_max_threads());
117 #else
118   return(1);
119 #endif
120 }
121 
GetOpenMPThreadId(void)122 static inline int GetOpenMPThreadId(void)
123 {
124 #if defined(MAGICKCORE_OPENMP_SUPPORT)
125   return(omp_get_thread_num());
126 #else
127   return(0);
128 #endif
129 }
130 
SetOpenMPMaximumThreads(const int threads)131 static inline void SetOpenMPMaximumThreads(const int threads)
132 {
133 #if defined(MAGICKCORE_OPENMP_SUPPORT)
134   omp_set_num_threads(threads);
135 #else
136   (void) threads;
137 #endif
138 }
139 
SetOpenMPNested(const int value)140 static inline void SetOpenMPNested(const int value)
141 {
142 #if defined(MAGICKCORE_OPENMP_SUPPORT)
143   omp_set_nested(value);
144 #else
145   (void) value;
146 #endif
147 }
148 
149 #if defined(__cplusplus) || defined(c_plusplus)
150 }
151 #endif
152 
153 #endif
154