blob: 2588eeeb5e7a6c97f0a5ac4e9a2bd08564c97d78 [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{
cristyf53f26f2011-05-16 00:56:05 +000099 static size_t
100 maximum_threads = 1;
101
cristyb28d6472009-10-17 20:13:35 +0000102#if defined(MAGICKCORE_OPENMP_SUPPORT) && (_OPENMP >= 200203)
cristy398f9322009-09-11 18:49:48 +0000103 {
cristy45431442010-09-12 19:53:17 +0000104 ssize_t
105 threads;
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 }
cristy44a3c232010-03-24 16:08:12 +0000111#endif
cristyf53f26f2011-05-16 00:56:05 +0000112 return(maximum_threads);
cristy3ed852e2009-09-05 21:47:34 +0000113}
114
cristyc3ebda22010-06-27 17:11:57 +0000115static inline int GetOpenMPThreadId(void)
cristy3ed852e2009-09-05 21:47:34 +0000116{
cristyb28d6472009-10-17 20:13:35 +0000117#if defined(MAGICKCORE_OPENMP_SUPPORT) && (_OPENMP >= 200203)
cristy3ed852e2009-09-05 21:47:34 +0000118 return(omp_get_thread_num());
119#else
120 return(0);
121#endif
122}
123
cristyeaedf062010-05-29 22:36:02 +0000124static inline void SetOpenMPMaximumThreads(const int threads)
cristy3ed852e2009-09-05 21:47:34 +0000125{
cristyb28d6472009-10-17 20:13:35 +0000126#if defined(MAGICKCORE_OPENMP_SUPPORT) && (_OPENMP >= 200203)
cristy3ed852e2009-09-05 21:47:34 +0000127 omp_set_num_threads(threads);
128#else
129 (void) threads;
130#endif
131}
132
cristy22ea5d42009-10-25 04:03:55 +0000133static inline void SetOpenMPNested(const int value)
134{
135#if defined(MAGICKCORE_OPENMP_SUPPORT) && (_OPENMP >= 200203)
136 omp_set_nested(value);
cristye27462f2009-10-26 13:18:22 +0000137#else
138 (void) value;
cristy22ea5d42009-10-25 04:03:55 +0000139#endif
140}
141
cristy3ed852e2009-09-05 21:47:34 +0000142#if defined(__cplusplus) || defined(c_plusplus)
143}
144#endif
145
146#endif