blob: d27e7c576d8aa6b8b39965416b78a871ecd5a803 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
cristy1454be72011-12-19 01:52:48 +00002 Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization
cristy3ed852e2009-09-05 21:47:34 +00003 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 http://www.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#if defined(__cplusplus) || defined(c_plusplus)
22extern "C" {
23#endif
24
cristy4c08aed2011-07-01 19:47:50 +000025#include <MagickCore/thread_.h>
cristy3ed852e2009-09-05 21:47:34 +000026
cristyec2c4982010-02-14 23:13:41 +000027#if (__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR > 10))
28#define MagickCachePrefetch(address,mode,locality) \
29 __builtin_prefetch(address,mode,locality)
30#else
31#define MagickCachePrefetch(address,mode,locality)
32#endif
33
cristy5fa207c2011-11-21 18:32:43 +000034#define omp_throttle(factor) num_threads(omp_get_max_threads() >> \
35 (factor) == 0 ? 1 : omp_get_max_threads() >> (factor))
36
cristy55bf91c2010-09-24 00:29:41 +000037#if defined(MAGICKCORE_THREAD_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000038 typedef pthread_mutex_t MagickMutexType;
cristy0157aea2010-04-24 21:12:18 +000039#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000040 typedef CRITICAL_SECTION MagickMutexType;
41#else
cristybb503372010-05-27 20:51:26 +000042 typedef size_t MagickMutexType;
cristy3ed852e2009-09-05 21:47:34 +000043#endif
44
45static inline MagickThreadType GetMagickThreadId(void)
46{
cristy55bf91c2010-09-24 00:29:41 +000047#if defined(MAGICKCORE_THREAD_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000048 return(pthread_self());
cristy0157aea2010-04-24 21:12:18 +000049#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000050 return(GetCurrentThreadId());
51#else
52 return(getpid());
53#endif
54}
55
cristybb503372010-05-27 20:51:26 +000056static inline size_t GetMagickThreadSignature(void)
cristy3ed852e2009-09-05 21:47:34 +000057{
cristy55bf91c2010-09-24 00:29:41 +000058#if defined(MAGICKCORE_THREAD_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000059 {
60 union
61 {
62 pthread_t
63 id;
64
cristybb503372010-05-27 20:51:26 +000065 size_t
cristy3ed852e2009-09-05 21:47:34 +000066 signature;
67 } magick_thread;
68
69 magick_thread.signature=0UL;
70 magick_thread.id=pthread_self();
71 return(magick_thread.signature);
72 }
cristy0157aea2010-04-24 21:12:18 +000073#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
cristybb503372010-05-27 20:51:26 +000074 return((size_t) GetCurrentThreadId());
cristy3ed852e2009-09-05 21:47:34 +000075#else
cristybb503372010-05-27 20:51:26 +000076 return((size_t) getpid());
cristy3ed852e2009-09-05 21:47:34 +000077#endif
78}
79
80static inline MagickBooleanType IsMagickThreadEqual(const MagickThreadType id)
81{
cristy55bf91c2010-09-24 00:29:41 +000082#if defined(MAGICKCORE_THREAD_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000083 if (pthread_equal(id,pthread_self()) != 0)
84 return(MagickTrue);
cristy0157aea2010-04-24 21:12:18 +000085#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000086 if (id == GetCurrentThreadId())
87 return(MagickTrue);
88#else
89 if (id == getpid())
90 return(MagickTrue);
91#endif
92 return(MagickFalse);
93}
94
95/*
96 Lightweight OpenMP methods.
97*/
cristybb503372010-05-27 20:51:26 +000098static inline size_t GetOpenMPMaximumThreads(void)
cristy3ed852e2009-09-05 21:47:34 +000099{
cristyf53f26f2011-05-16 00:56:05 +0000100 static size_t
101 maximum_threads = 1;
102
cristyb28d6472009-10-17 20:13:35 +0000103#if defined(MAGICKCORE_OPENMP_SUPPORT) && (_OPENMP >= 200203)
cristy398f9322009-09-11 18:49:48 +0000104 {
cristy45431442010-09-12 19:53:17 +0000105 ssize_t
106 threads;
107
cristy45431442010-09-12 19:53:17 +0000108 threads=omp_get_max_threads();
109 if (threads > (ssize_t) maximum_threads)
110 maximum_threads=threads;
cristy398f9322009-09-11 18:49:48 +0000111 }
cristy44a3c232010-03-24 16:08:12 +0000112#endif
cristyf53f26f2011-05-16 00:56:05 +0000113 return(maximum_threads);
cristy3ed852e2009-09-05 21:47:34 +0000114}
115
cristyc3ebda22010-06-27 17:11:57 +0000116static inline int GetOpenMPThreadId(void)
cristy3ed852e2009-09-05 21:47:34 +0000117{
cristyb28d6472009-10-17 20:13:35 +0000118#if defined(MAGICKCORE_OPENMP_SUPPORT) && (_OPENMP >= 200203)
cristy3ed852e2009-09-05 21:47:34 +0000119 return(omp_get_thread_num());
120#else
121 return(0);
122#endif
123}
124
cristyeaedf062010-05-29 22:36:02 +0000125static inline void SetOpenMPMaximumThreads(const int threads)
cristy3ed852e2009-09-05 21:47:34 +0000126{
cristyb28d6472009-10-17 20:13:35 +0000127#if defined(MAGICKCORE_OPENMP_SUPPORT) && (_OPENMP >= 200203)
cristy5fa207c2011-11-21 18:32:43 +0000128 omp_set_num_threads(threads);
cristy3ed852e2009-09-05 21:47:34 +0000129#else
130 (void) threads;
131#endif
132}
133
cristy22ea5d42009-10-25 04:03:55 +0000134static inline void SetOpenMPNested(const int value)
135{
136#if defined(MAGICKCORE_OPENMP_SUPPORT) && (_OPENMP >= 200203)
137 omp_set_nested(value);
cristye27462f2009-10-26 13:18:22 +0000138#else
139 (void) value;
cristy22ea5d42009-10-25 04:03:55 +0000140#endif
141}
142
cristy3ed852e2009-09-05 21:47:34 +0000143#if defined(__cplusplus) || defined(c_plusplus)
144}
145#endif
146
147#endif