MagickCore 6.9.12-20
Convert, Edit, Or Compose Bitmap Images
thread-private.h
Go to the documentation of this file.
1/*
2 Copyright 1999-2021 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"
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,multithreaded) \
31 num_threads(GetMagickNumberThreads(source,destination,chunk,multithreaded))
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#endif
38
39#if defined(MAGICKCORE_THREAD_SUPPORT)
40 typedef pthread_mutex_t MagickMutexType;
41#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
42 typedef CRITICAL_SECTION MagickMutexType;
43#else
44 typedef size_t MagickMutexType;
45#endif
46
48{
49#if defined(MAGICKCORE_THREAD_SUPPORT)
50 return(pthread_self());
51#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
52 return(GetCurrentThreadId());
53#else
54 return(getpid());
55#endif
56}
57
58static inline int GetMagickNumberThreads(const Image *source,
59 const Image *destination,const size_t chunk,int multithreaded)
60{
61 /*
62 Number of threads bounded by the amount of work and any thread resource
63 limit. The limit is 2 if the pixel cache type is not memory or
64 memory-mapped.
65 */
66 if (multithreaded == 0)
67 return(1);
68 if (((GetImagePixelCacheType(source) != MemoryCache) &&
69 (GetImagePixelCacheType(source) != MapCache)) ||
70 ((GetImagePixelCacheType(destination) != MemoryCache) &&
71 (GetImagePixelCacheType(destination) != MapCache)))
73 return((int) MagickMax(MagickMin((ssize_t) GetMagickResourceLimit(
74 ThreadResource),(ssize_t) (chunk)/64),1));
75}
76
77static inline size_t GetMagickThreadSignature(void)
78{
79#if defined(MAGICKCORE_THREAD_SUPPORT)
80 {
81 union
82 {
83 pthread_t
84 id;
85
86 size_t
87 signature;
88 } magick_thread;
89
90 magick_thread.signature=0UL;
91 magick_thread.id=pthread_self();
92 return(magick_thread.signature);
93 }
94#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
95 return((size_t) GetCurrentThreadId());
96#else
97 return((size_t) getpid());
98#endif
99}
100
102{
103#if defined(MAGICKCORE_THREAD_SUPPORT)
104 if (pthread_equal(id,pthread_self()) != 0)
105 return(MagickTrue);
106#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
107 if (id == GetCurrentThreadId())
108 return(MagickTrue);
109#else
110 if (id == getpid())
111 return(MagickTrue);
112#endif
113 return(MagickFalse);
114}
115
116/*
117 Lightweight OpenMP methods.
118*/
119static inline size_t GetOpenMPMaximumThreads(void)
120{
121#if defined(MAGICKCORE_OPENMP_SUPPORT)
122 return(omp_get_max_threads());
123#else
124 return(1);
125#endif
126}
127
128static inline int GetOpenMPThreadId(void)
129{
130#if defined(MAGICKCORE_OPENMP_SUPPORT)
131 return(omp_get_thread_num());
132#else
133 return(0);
134#endif
135}
136
137static inline void SetOpenMPMaximumThreads(const int threads)
138{
139#if defined(MAGICKCORE_OPENMP_SUPPORT)
140 omp_set_num_threads(threads);
141#else
142 (void) threads;
143#endif
144}
145
146static inline void SetOpenMPNested(const int value)
147{
148#if defined(MAGICKCORE_OPENMP_SUPPORT)
149 omp_set_nested(value);
150#else
151 (void) value;
152#endif
153}
154
155#if defined(__cplusplus) || defined(c_plusplus)
156}
157#endif
158
159#endif
MagickExport CacheType GetImagePixelCacheType(const Image *image)
Definition: cache.c:1977
@ MemoryCache
Definition: cache.h:30
@ MapCache
Definition: cache.h:31
#define MagickMin(x, y)
Definition: image-private.h:37
#define MagickMax(x, y)
Definition: image-private.h:36
MagickBooleanType
Definition: magick-type.h:199
@ MagickFalse
Definition: magick-type.h:200
@ MagickTrue
Definition: magick-type.h:201
MagickExport MagickSizeType GetMagickResourceLimit(const ResourceType type)
Definition: resource.c:842
@ ThreadResource
Definition: resource_.h:33
Definition: image.h:153
static void SetOpenMPNested(const int value)
Definition: thread-private.h:146
static int GetOpenMPThreadId(void)
Definition: thread-private.h:128
static MagickBooleanType IsMagickThreadEqual(const MagickThreadType id)
Definition: thread-private.h:101
static MagickThreadType GetMagickThreadId(void)
Definition: thread-private.h:47
static size_t GetOpenMPMaximumThreads(void)
Definition: thread-private.h:119
size_t MagickMutexType
Definition: thread-private.h:44
static int GetMagickNumberThreads(const Image *source, const Image *destination, const size_t chunk, int multithreaded)
Definition: thread-private.h:58
static void SetOpenMPMaximumThreads(const int threads)
Definition: thread-private.h:137
static size_t GetMagickThreadSignature(void)
Definition: thread-private.h:77
pid_t MagickThreadType
Definition: thread_.h:34
#define pthread_self
Definition: vms.h:836
#define pthread_equal
Definition: vms.h:802