2 Copyright 1999-2017 ImageMagick Studio LLC, a non-profit organization
3 dedicated to making software imaging solutions freely available.
5 You may not use this file except in compliance with the License.
6 obtain a copy of the License at
8 http://www.imagemagick.org/script/license.php
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.
16 MagickCore private methods for internal threading.
18 #ifndef MAGICKCORE_THREAD_PRIVATE_H
19 #define MAGICKCORE_THREAD_PRIVATE_H
21 #include "MagickCore/cache.h"
22 #include "MagickCore/resource_.h"
23 #include "MagickCore/thread_.h"
25 #if defined(__cplusplus) || defined(c_plusplus)
30 Single threaded unless workload justifies the threading overhead.
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)
42 #if defined(__clang__) || (__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ > 10))
43 #define MagickCachePrefetch(address,mode,locality) \
44 __builtin_prefetch(address,mode,locality)
46 #define MagickCachePrefetch(address,mode,locality) \
47 magick_unreferenced(address); \
48 magick_unreferenced(mode); \
49 magick_unreferenced(locality);
52 #if defined(MAGICKCORE_THREAD_SUPPORT)
53 typedef pthread_mutex_t MagickMutexType;
54 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
55 typedef CRITICAL_SECTION MagickMutexType;
57 typedef size_t MagickMutexType;
60 static inline MagickThreadType GetMagickThreadId(void)
62 #if defined(MAGICKCORE_THREAD_SUPPORT)
63 return(pthread_self());
64 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
65 return(GetCurrentThreadId());
71 static inline size_t GetMagickThreadSignature(void)
73 #if defined(MAGICKCORE_THREAD_SUPPORT)
84 magick_thread.signature=0UL;
85 magick_thread.id=pthread_self();
86 return(magick_thread.signature);
88 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
89 return((size_t) GetCurrentThreadId());
91 return((size_t) getpid());
95 static inline MagickBooleanType IsMagickThreadEqual(const MagickThreadType id)
97 #if defined(MAGICKCORE_THREAD_SUPPORT)
98 if (pthread_equal(id,pthread_self()) != 0)
100 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
101 if (id == GetCurrentThreadId())
111 Lightweight OpenMP methods.
113 static inline size_t GetOpenMPMaximumThreads(void)
115 #if defined(MAGICKCORE_OPENMP_SUPPORT)
116 return(omp_get_max_threads());
122 static inline int GetOpenMPThreadId(void)
124 #if defined(MAGICKCORE_OPENMP_SUPPORT)
125 return(omp_get_thread_num());
131 static inline void SetOpenMPMaximumThreads(const int threads)
133 #if defined(MAGICKCORE_OPENMP_SUPPORT)
134 omp_set_num_threads(threads);
140 static inline void SetOpenMPNested(const int value)
142 #if defined(MAGICKCORE_OPENMP_SUPPORT)
143 omp_set_nested(value);
149 #if defined(__cplusplus) || defined(c_plusplus)