MagickCore 6.9.13
Loading...
Searching...
No Matches
thread-private.h
1/*
2 Copyright @ 1999 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. You may
6 obtain a copy of the License at
7
8 https://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/image-private.h"
23#include "magick/resource_.h"
24#include "magick/thread_.h"
25
26#if defined(__cplusplus) || defined(c_plusplus)
27extern "C" {
28#endif
29
30#define magick_number_threads(source,destination,chunk,factor) \
31 num_threads(GetMagickNumberThreads(source,destination,chunk,factor))
32#if defined(__clang__) || (__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ > 10))
33#define MagickCachePrefetch(address,mode,locality) \
34 __builtin_prefetch(address,mode,locality)
35#else
36#define MagickCachePrefetch(address,mode,locality) \
37 magick_unreferenced(address); \
38 magick_unreferenced(mode); \
39 magick_unreferenced(locality);
40#endif
41
42#if defined(MAGICKCORE_THREAD_SUPPORT)
43 typedef pthread_mutex_t MagickMutexType;
44#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
45 typedef CRITICAL_SECTION MagickMutexType;
46#else
47 typedef size_t MagickMutexType;
48#endif
49
50static inline int GetMagickNumberThreads(const Image *source,
51 const Image *destination,const size_t chunk,const int factor)
52{
53 const CacheType
54 destination_type = (CacheType) GetImagePixelCacheType(destination),
55 source_type = (CacheType) GetImagePixelCacheType(source);
56
57 size_t
58 max_threads = (size_t) GetMagickResourceLimit(ThreadResource),
59 number_threads = 1,
60 workload_factor = 64UL << factor;
61
62 /*
63 Determine number of threads based on workload.
64 */
65 number_threads=(chunk <= workload_factor) ? 1 :
66 (chunk >= (workload_factor << 6)) ? max_threads :
67 1+(chunk-workload_factor)*(max_threads-1)/(((workload_factor << 6))-1);
68 /*
69 Limit threads for non-memory or non-map cache sources/destinations.
70 */
71 if (((source_type != MemoryCache) && (source_type != MapCache)) ||
72 ((destination_type != MemoryCache) && (destination_type != MapCache)))
73 number_threads=MagickMin(number_threads,4);
74 return((int) number_threads);
75}
76
77static inline MagickThreadType GetMagickThreadId(void)
78{
79#if defined(MAGICKCORE_THREAD_SUPPORT)
80 return(pthread_self());
81#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
82 return(GetCurrentThreadId());
83#else
84 return(getpid());
85#endif
86}
87
88static inline size_t GetMagickThreadSignature(void)
89{
90#if defined(MAGICKCORE_THREAD_SUPPORT)
91 {
92 union
93 {
94 pthread_t
95 id;
96
97 size_t
98 signature;
99 } magick_thread;
100
101 magick_thread.signature=0UL;
102 magick_thread.id=pthread_self();
103 return(magick_thread.signature);
104 }
105#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
106 return((size_t) GetCurrentThreadId());
107#else
108 return((size_t) getpid());
109#endif
110}
111
112static inline MagickBooleanType IsMagickThreadEqual(const MagickThreadType id)
113{
114#if defined(MAGICKCORE_THREAD_SUPPORT)
115 if (pthread_equal(id,pthread_self()) != 0)
116 return(MagickTrue);
117#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
118 if (id == GetCurrentThreadId())
119 return(MagickTrue);
120#else
121 if (id == getpid())
122 return(MagickTrue);
123#endif
124 return(MagickFalse);
125}
126
127/*
128 Lightweight OpenMP methods.
129*/
130static inline size_t GetOpenMPMaximumThreads(void)
131{
132#if defined(MAGICKCORE_OPENMP_SUPPORT)
133 return((size_t) omp_get_max_threads());
134#else
135 return(1);
136#endif
137}
138
139static inline int GetOpenMPThreadId(void)
140{
141#if defined(MAGICKCORE_OPENMP_SUPPORT)
142 return(omp_get_thread_num());
143#else
144 return(0);
145#endif
146}
147
148#if defined(MAGICKCORE_OPENMP_SUPPORT)
149static inline void SetOpenMPMaximumThreads(const int threads)
150{
151 omp_set_num_threads(threads);
152#else
153static inline void SetOpenMPMaximumThreads(const int magick_unused(threads))
154{
155 magick_unreferenced(threads);
156#endif
157}
158
159#if defined(MAGICKCORE_OPENMP_SUPPORT)
160static inline void SetOpenMPNested(const int value)
161{
162 omp_set_nested(value);
163#else
164static inline void SetOpenMPNested(const int magick_unused(value))
165{
166 magick_unreferenced(value);
167#endif
168}
169
170#if defined(__cplusplus) || defined(c_plusplus)
171}
172#endif
173
174#endif