blob: 9c5b89ac3999d825606d31c1522dc7c7df9c67a1 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
cristy7e41fe82010-12-04 23:12:08 +00002 Copyright 1999-2011 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
25#include <magick/thread_.h>
26
cristy09d81172010-10-21 16:15:05 +000027#define omp_throttle(factor) num_threads(omp_get_max_threads() >> (factor))
28
cristyec2c4982010-02-14 23:13:41 +000029#if (__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR > 10))
30#define MagickCachePrefetch(address,mode,locality) \
31 __builtin_prefetch(address,mode,locality)
32#else
33#define MagickCachePrefetch(address,mode,locality)
34#endif
35
cristy55bf91c2010-09-24 00:29:41 +000036#if defined(MAGICKCORE_THREAD_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000037 typedef pthread_mutex_t MagickMutexType;
cristy0157aea2010-04-24 21:12:18 +000038#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000039 typedef CRITICAL_SECTION MagickMutexType;
40#else
cristybb503372010-05-27 20:51:26 +000041 typedef size_t MagickMutexType;
cristy3ed852e2009-09-05 21:47:34 +000042#endif
43
44static inline MagickThreadType GetMagickThreadId(void)
45{
cristy55bf91c2010-09-24 00:29:41 +000046#if defined(MAGICKCORE_THREAD_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000047 return(pthread_self());
cristy0157aea2010-04-24 21:12:18 +000048#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000049 return(GetCurrentThreadId());
50#else
51 return(getpid());
52#endif
53}
54
cristybb503372010-05-27 20:51:26 +000055static inline size_t GetMagickThreadSignature(void)
cristy3ed852e2009-09-05 21:47:34 +000056{
cristy55bf91c2010-09-24 00:29:41 +000057#if defined(MAGICKCORE_THREAD_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000058 {
59 union
60 {
61 pthread_t
62 id;
63
cristybb503372010-05-27 20:51:26 +000064 size_t
cristy3ed852e2009-09-05 21:47:34 +000065 signature;
66 } magick_thread;
67
68 magick_thread.signature=0UL;
69 magick_thread.id=pthread_self();
70 return(magick_thread.signature);
71 }
cristy0157aea2010-04-24 21:12:18 +000072#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
cristybb503372010-05-27 20:51:26 +000073 return((size_t) GetCurrentThreadId());
cristy3ed852e2009-09-05 21:47:34 +000074#else
cristybb503372010-05-27 20:51:26 +000075 return((size_t) getpid());
cristy3ed852e2009-09-05 21:47:34 +000076#endif
77}
78
79static inline MagickBooleanType IsMagickThreadEqual(const MagickThreadType id)
80{
cristy55bf91c2010-09-24 00:29:41 +000081#if defined(MAGICKCORE_THREAD_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000082 if (pthread_equal(id,pthread_self()) != 0)
83 return(MagickTrue);
cristy0157aea2010-04-24 21:12:18 +000084#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000085 if (id == GetCurrentThreadId())
86 return(MagickTrue);
87#else
88 if (id == getpid())
89 return(MagickTrue);
90#endif
91 return(MagickFalse);
92}
93
94/*
95 Lightweight OpenMP methods.
96*/
cristybb503372010-05-27 20:51:26 +000097static inline size_t GetOpenMPMaximumThreads(void)
cristy3ed852e2009-09-05 21:47:34 +000098{
cristyb28d6472009-10-17 20:13:35 +000099#if defined(MAGICKCORE_OPENMP_SUPPORT) && (_OPENMP >= 200203)
cristy398f9322009-09-11 18:49:48 +0000100 {
cristy45431442010-09-12 19:53:17 +0000101 ssize_t
102 threads;
103
cristybb503372010-05-27 20:51:26 +0000104 static size_t
cristy398f9322009-09-11 18:49:48 +0000105 maximum_threads = 1UL;
106
cristy45431442010-09-12 19:53:17 +0000107 threads=omp_get_max_threads();
108 if (threads > (ssize_t) maximum_threads)
109 maximum_threads=threads;
cristy398f9322009-09-11 18:49:48 +0000110 return(maximum_threads);
111 }
cristy44a3c232010-03-24 16:08:12 +0000112#else
cristy3ed852e2009-09-05 21:47:34 +0000113 return(1UL);
cristy44a3c232010-03-24 16:08:12 +0000114#endif
cristy3ed852e2009-09-05 21:47:34 +0000115}
116
cristyc3ebda22010-06-27 17:11:57 +0000117static inline int GetOpenMPThreadId(void)
cristy3ed852e2009-09-05 21:47:34 +0000118{
cristyb28d6472009-10-17 20:13:35 +0000119#if defined(MAGICKCORE_OPENMP_SUPPORT) && (_OPENMP >= 200203)
cristy3ed852e2009-09-05 21:47:34 +0000120 return(omp_get_thread_num());
121#else
122 return(0);
123#endif
124}
125
cristyeaedf062010-05-29 22:36:02 +0000126static inline void SetOpenMPMaximumThreads(const int threads)
cristy3ed852e2009-09-05 21:47:34 +0000127{
cristyb28d6472009-10-17 20:13:35 +0000128#if defined(MAGICKCORE_OPENMP_SUPPORT) && (_OPENMP >= 200203)
cristy3ed852e2009-09-05 21:47:34 +0000129 omp_set_num_threads(threads);
130#else
131 (void) threads;
132#endif
133}
134
cristy22ea5d42009-10-25 04:03:55 +0000135static inline void SetOpenMPNested(const int value)
136{
137#if defined(MAGICKCORE_OPENMP_SUPPORT) && (_OPENMP >= 200203)
138 omp_set_nested(value);
cristye27462f2009-10-26 13:18:22 +0000139#else
140 (void) value;
cristy22ea5d42009-10-25 04:03:55 +0000141#endif
142}
143
cristy3ed852e2009-09-05 21:47:34 +0000144#if defined(__cplusplus) || defined(c_plusplus)
145}
146#endif
147
148#endif