MagickCore 7.0.10
thread-private.h
Go to the documentation of this file.
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.
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 WizardCore private methods for internal threading.
17*/
18#ifndef _WIZARDSTOOLKIT_THREAD_PRIVATE_H
19#define _WIZARDSTOOLKIT_THREAD_PRIVATE_H
20
21#include <wizard/thread_.h>
22
23#if defined(__cplusplus) || defined(c_plusplus)
24extern "C" {
25#endif
26
27#if defined(__clang__) || (__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ > 10))
28#define WizardCachePrefetch(address,mode,locality) \
29 __builtin_prefetch(address,mode,locality)
30#else
31#define WizardCachePrefetch(address,mode,locality)
32#endif
33
34#define omp_throttle(factor) num_threads(omp_get_max_threads() >> \
35 (factor) == 0 ? 1 : omp_get_max_threads() >> (factor))
36
37#if defined(WIZARDSTOOLKIT_THREAD_SUPPORT)
38 typedef pthread_mutex_t WizardMutexType;
39#elif defined(WIZARDSTOOLKIT_WINDOWS_SUPPORT)
40 typedef CRITICAL_SECTION WizardMutexType;
41#else
42 typedef size_t WizardMutexType;
43#endif
44
46{
47#if defined(WIZARDSTOOLKIT_THREAD_SUPPORT)
48 return(pthread_self());
49#elif defined(WIZARDSTOOLKIT_WINDOWS_SUPPORT)
50 return(GetCurrentThreadId());
51#else
52 return(getpid());
53#endif
54}
55
56static inline size_t GetWizardThreadSignature(void)
57{
58#if defined(WIZARDSTOOLKIT_THREAD_SUPPORT)
59 {
60 union
61 {
62 pthread_t
63 id;
64
65 size_t
66 signature;
67 } wizard_thread;
68
69 wizard_thread.signature=0UL;
70 wizard_thread.id=pthread_self();
71 return(wizard_thread.signature);
72 }
73#elif defined(WIZARDSTOOLKIT_WINDOWS_SUPPORT)
74 return((size_t) GetCurrentThreadId());
75#else
76 return((size_t) getpid());
77#endif
78}
79
81{
82#if defined(WIZARDSTOOLKIT_THREAD_SUPPORT)
83 if (pthread_equal(id,pthread_self()) != 0)
84 return(WizardTrue);
85#elif defined(WIZARDSTOOLKIT_WINDOWS_SUPPORT)
86 if (id == GetCurrentThreadId())
87 return(WizardTrue);
88#else
89 if (id == getpid())
90 return(WizardTrue);
91#endif
92 return(WizardFalse);
93}
94
95/*
96 Lightweight OpenMP methods.
97*/
98static inline size_t GetOpenMPMaximumThreads(void)
99{
100 static size_t
101 maximum_threads = 1;
102
103#if defined(WIZARDSTOOLKIT_OPENMP_SUPPORT)
104 if (omp_get_max_threads() > (ssize_t) maximum_threads)
105 maximum_threads=omp_get_max_threads();
106#endif
107 return(maximum_threads);
108}
109
110static inline ssize_t GetOpenMPThreadId(void)
111{
112#if defined(WIZARDSTOOLKIT_OPENMP_SUPPORT)
113 return(omp_get_thread_num());
114#else
115 return(0);
116#endif
117}
118
119static inline void SetOpenMPMaximumThreads(const size_t threads)
120{
121#if defined(WIZARDSTOOLKIT_OPENMP_SUPPORT)
122 omp_set_num_threads(threads);
123#else
124 (void) threads;
125#endif
126}
127
128static inline void SetOpenMPNested(const int value)
129{
130#if defined(WIZARDSTOOLKIT_OPENMP_SUPPORT) && (_OPENMP >= 200203)
131 omp_set_nested(value);
132#else
133 (void) value;
134#endif
135}
136
137#if defined(__cplusplus) || defined(c_plusplus)
138}
139#endif
140
141#endif
static void SetOpenMPNested(const int value)
static ssize_t GetOpenMPThreadId(void)
static WizardThreadType GetWizardThreadId(void)
static void SetOpenMPMaximumThreads(const size_t threads)
static size_t GetWizardThreadSignature(void)
static size_t GetOpenMPMaximumThreads(void)
static WizardBooleanType IsWizardThreadEqual(const WizardThreadType id)
size_t WizardMutexType
pid_t WizardThreadType
Definition thread_.h:30
WizardBooleanType
Definition wizard-type.h:26
@ WizardTrue
Definition wizard-type.h:28
@ WizardFalse
Definition wizard-type.h:27