blob: 2623d521bfd84df73ea82ea4af273482968bfc30 [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
cristy70e8f292012-05-06 18:34:35 +000025#include <MagickCore/resource_.h>
cristy4c08aed2011-07-01 19:47:50 +000026#include <MagickCore/thread_.h>
cristy3ed852e2009-09-05 21:47:34 +000027
cristyb940c752012-05-06 18:32:28 +000028/*
cristy9caad062012-05-07 18:58:56 +000029 Single threaded unless workload justifies the threading overhead.
cristyb940c752012-05-06 18:32:28 +000030*/
cristy9caad062012-05-07 18:58:56 +000031#define MinimumWorkLoad() (32*GetMagickResourceLimit(ThreadResource))
32#define dynamic_num_threads_uno(colors) \
33 if ((colors) > MinimumWorkLoad()) \
cristy0ced47f2012-05-06 01:37:19 +000034 num_threads(GetMagickResourceLimit(ThreadResource))
cristy9caad062012-05-07 18:58:56 +000035#define dynamic_num_threads_dos(columns,rows) \
36 if (((((columns) > MinimumWorkLoad()) || \
37 ((rows) > MinimumWorkLoad()))) && ((MagickSizeType) \
38 ((columns)*(rows)) > (MinimumWorkLoad()*MinimumWorkLoad()))) \
cristy0ced47f2012-05-06 01:37:19 +000039 num_threads(GetMagickResourceLimit(ThreadResource))
cristy9caad062012-05-07 18:58:56 +000040#define dynamic_num_threads_tres(columns,rows,expression) \
41 if (((((columns) > MinimumWorkLoad()) || \
42 ((rows) > MinimumWorkLoad()))) && ((MagickSizeType) \
43 ((columns)*(rows)) > (MinimumWorkLoad()*MinimumWorkLoad())) && \
44 (expression)) \
cristy3545b652012-05-06 01:40:39 +000045 num_threads(GetMagickResourceLimit(ThreadResource))
cristyccfffb42012-05-06 01:17:48 +000046
cristyec2c4982010-02-14 23:13:41 +000047#if (__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR > 10))
48#define MagickCachePrefetch(address,mode,locality) \
49 __builtin_prefetch(address,mode,locality)
50#else
51#define MagickCachePrefetch(address,mode,locality)
52#endif
53
cristy55bf91c2010-09-24 00:29:41 +000054#if defined(MAGICKCORE_THREAD_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000055 typedef pthread_mutex_t MagickMutexType;
cristy0157aea2010-04-24 21:12:18 +000056#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000057 typedef CRITICAL_SECTION MagickMutexType;
58#else
cristybb503372010-05-27 20:51:26 +000059 typedef size_t MagickMutexType;
cristy3ed852e2009-09-05 21:47:34 +000060#endif
61
62static inline MagickThreadType GetMagickThreadId(void)
63{
cristy55bf91c2010-09-24 00:29:41 +000064#if defined(MAGICKCORE_THREAD_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000065 return(pthread_self());
cristy0157aea2010-04-24 21:12:18 +000066#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000067 return(GetCurrentThreadId());
68#else
69 return(getpid());
70#endif
71}
72
cristybb503372010-05-27 20:51:26 +000073static inline size_t GetMagickThreadSignature(void)
cristy3ed852e2009-09-05 21:47:34 +000074{
cristy55bf91c2010-09-24 00:29:41 +000075#if defined(MAGICKCORE_THREAD_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000076 {
77 union
78 {
79 pthread_t
80 id;
81
cristybb503372010-05-27 20:51:26 +000082 size_t
cristy3ed852e2009-09-05 21:47:34 +000083 signature;
84 } magick_thread;
85
86 magick_thread.signature=0UL;
87 magick_thread.id=pthread_self();
88 return(magick_thread.signature);
89 }
cristy0157aea2010-04-24 21:12:18 +000090#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
cristybb503372010-05-27 20:51:26 +000091 return((size_t) GetCurrentThreadId());
cristy3ed852e2009-09-05 21:47:34 +000092#else
cristybb503372010-05-27 20:51:26 +000093 return((size_t) getpid());
cristy3ed852e2009-09-05 21:47:34 +000094#endif
95}
96
97static inline MagickBooleanType IsMagickThreadEqual(const MagickThreadType id)
98{
cristy55bf91c2010-09-24 00:29:41 +000099#if defined(MAGICKCORE_THREAD_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000100 if (pthread_equal(id,pthread_self()) != 0)
101 return(MagickTrue);
cristy0157aea2010-04-24 21:12:18 +0000102#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000103 if (id == GetCurrentThreadId())
104 return(MagickTrue);
105#else
106 if (id == getpid())
107 return(MagickTrue);
108#endif
109 return(MagickFalse);
110}
111
112/*
113 Lightweight OpenMP methods.
114*/
cristybb503372010-05-27 20:51:26 +0000115static inline size_t GetOpenMPMaximumThreads(void)
cristy3ed852e2009-09-05 21:47:34 +0000116{
cristy5a629092012-01-20 14:45:29 +0000117#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000118 return(omp_get_max_threads());
119#else
120 return(1);
cristy44a3c232010-03-24 16:08:12 +0000121#endif
cristy3ed852e2009-09-05 21:47:34 +0000122}
123
cristyc3ebda22010-06-27 17:11:57 +0000124static inline int GetOpenMPThreadId(void)
cristy3ed852e2009-09-05 21:47:34 +0000125{
cristy5a629092012-01-20 14:45:29 +0000126#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000127 return(omp_get_thread_num());
128#else
129 return(0);
130#endif
131}
132
cristyeaedf062010-05-29 22:36:02 +0000133static inline void SetOpenMPMaximumThreads(const int threads)
cristy3ed852e2009-09-05 21:47:34 +0000134{
cristy5a629092012-01-20 14:45:29 +0000135#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy5fa207c2011-11-21 18:32:43 +0000136 omp_set_num_threads(threads);
cristy3ed852e2009-09-05 21:47:34 +0000137#else
138 (void) threads;
139#endif
140}
141
cristy22ea5d42009-10-25 04:03:55 +0000142static inline void SetOpenMPNested(const int value)
143{
cristy5a629092012-01-20 14:45:29 +0000144#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy22ea5d42009-10-25 04:03:55 +0000145 omp_set_nested(value);
cristye27462f2009-10-26 13:18:22 +0000146#else
147 (void) value;
cristy22ea5d42009-10-25 04:03:55 +0000148#endif
149}
150
cristy3ed852e2009-09-05 21:47:34 +0000151#if defined(__cplusplus) || defined(c_plusplus)
152}
153#endif
154
155#endif