2 Copyright 1999-2018 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 https://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/image-private.h"
23 #include "MagickCore/resource_.h"
24 #include "MagickCore/thread_.h"
26 #if defined(__cplusplus) || defined(c_plusplus)
31 Number of threads bounded by the amount of work and any thread resource limit.
32 The limit is 2 if the pixel cache type is not memory or memory-mapped.
34 #define magick_number_threads(source,destination,chunk,multithreaded) \
35 num_threads((multithreaded) == 0 ? 1 : \
36 ((GetImagePixelCacheType(source) != MemoryCache) && \
37 (GetImagePixelCacheType(source) != MapCache)) || \
38 ((GetImagePixelCacheType(destination) != MemoryCache) && \
39 (GetImagePixelCacheType(destination) != MapCache)) ? \
40 MagickMax(MagickMin(GetMagickResourceLimit(ThreadResource),2),1) : \
41 MagickMax(MagickMin((ssize_t) GetMagickResourceLimit(ThreadResource),(ssize_t) (chunk)/64),1))
43 #if defined(__clang__) || (__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ > 10))
44 #define MagickCachePrefetch(address,mode,locality) \
45 __builtin_prefetch(address,mode,locality)
47 #define MagickCachePrefetch(address,mode,locality) \
48 magick_unreferenced(address); \
49 magick_unreferenced(mode); \
50 magick_unreferenced(locality);
53 #if defined(MAGICKCORE_THREAD_SUPPORT)
54 typedef pthread_mutex_t MagickMutexType;
55 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
56 typedef CRITICAL_SECTION MagickMutexType;
58 typedef size_t MagickMutexType;
61 static inline MagickThreadType GetMagickThreadId(void)
63 #if defined(MAGICKCORE_THREAD_SUPPORT)
64 return(pthread_self());
65 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
66 return(GetCurrentThreadId());
72 static inline size_t GetMagickThreadSignature(void)
74 #if defined(MAGICKCORE_THREAD_SUPPORT)
85 magick_thread.signature=0UL;
86 magick_thread.id=pthread_self();
87 return(magick_thread.signature);
89 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
90 return((size_t) GetCurrentThreadId());
92 return((size_t) getpid());
96 static inline MagickBooleanType IsMagickThreadEqual(const MagickThreadType id)
98 #if defined(MAGICKCORE_THREAD_SUPPORT)
99 if (pthread_equal(id,pthread_self()) != 0)
101 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
102 if (id == GetCurrentThreadId())
112 Lightweight OpenMP methods.
114 static inline size_t GetOpenMPMaximumThreads(void)
116 #if defined(MAGICKCORE_OPENMP_SUPPORT)
117 return(omp_get_max_threads());
123 static inline int GetOpenMPThreadId(void)
125 #if defined(MAGICKCORE_OPENMP_SUPPORT)
126 return(omp_get_thread_num());
132 static inline void SetOpenMPMaximumThreads(const int threads)
134 #if defined(MAGICKCORE_OPENMP_SUPPORT)
135 omp_set_num_threads(threads);
141 static inline void SetOpenMPNested(const int value)
143 #if defined(MAGICKCORE_OPENMP_SUPPORT)
144 omp_set_nested(value);
150 #if defined(__cplusplus) || defined(c_plusplus)