blob: 24e66a57a838f29674363958fafe3347e32c8446 [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
cristy4ee2b0c2012-05-15 00:30:35 +000025#include <MagickCore/cache.h>
cristy70e8f292012-05-06 18:34:35 +000026#include <MagickCore/resource_.h>
cristy4c08aed2011-07-01 19:47:50 +000027#include <MagickCore/thread_.h>
cristy3ed852e2009-09-05 21:47:34 +000028
cristyb940c752012-05-06 18:32:28 +000029/*
cristy9caad062012-05-07 18:58:56 +000030 Single threaded unless workload justifies the threading overhead.
cristyb940c752012-05-06 18:32:28 +000031*/
cristycf258172012-05-11 11:53:21 +000032#define WorkloadThreshold() (16*GetMagickResourceLimit(ThreadResource))
cristy4ee2b0c2012-05-15 00:30:35 +000033#define dynamic_number_threads(image,columns,rows,expression) \
cristycf258172012-05-11 11:53:21 +000034 if (((((columns) > WorkloadThreshold()) || \
35 ((rows) > WorkloadThreshold()))) && ((MagickSizeType) \
36 ((columns)*(rows)) > (WorkloadThreshold()*WorkloadThreshold())) && \
cristy85cc65a2012-10-30 14:17:50 +000037 (expression)) \
cristyd4acac92012-10-30 21:30:14 +000038 num_threads(GetMagickResourceLimit(ThreadResource)/ \
39 (GetImagePixelCacheType(image) == DiskCache ? 2 : 1))
cristyccfffb42012-05-06 01:17:48 +000040
cristy20c26b12012-08-23 09:21:22 +000041#if (__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ > 10))
cristyec2c4982010-02-14 23:13:41 +000042#define MagickCachePrefetch(address,mode,locality) \
43 __builtin_prefetch(address,mode,locality)
44#else
45#define MagickCachePrefetch(address,mode,locality)
46#endif
47
cristy55bf91c2010-09-24 00:29:41 +000048#if defined(MAGICKCORE_THREAD_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000049 typedef pthread_mutex_t MagickMutexType;
cristy0157aea2010-04-24 21:12:18 +000050#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000051 typedef CRITICAL_SECTION MagickMutexType;
52#else
cristybb503372010-05-27 20:51:26 +000053 typedef size_t MagickMutexType;
cristy3ed852e2009-09-05 21:47:34 +000054#endif
55
56static inline MagickThreadType GetMagickThreadId(void)
57{
cristy55bf91c2010-09-24 00:29:41 +000058#if defined(MAGICKCORE_THREAD_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000059 return(pthread_self());
cristy0157aea2010-04-24 21:12:18 +000060#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000061 return(GetCurrentThreadId());
62#else
63 return(getpid());
64#endif
65}
66
cristybb503372010-05-27 20:51:26 +000067static inline size_t GetMagickThreadSignature(void)
cristy3ed852e2009-09-05 21:47:34 +000068{
cristy55bf91c2010-09-24 00:29:41 +000069#if defined(MAGICKCORE_THREAD_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000070 {
71 union
72 {
73 pthread_t
74 id;
75
cristybb503372010-05-27 20:51:26 +000076 size_t
cristy3ed852e2009-09-05 21:47:34 +000077 signature;
78 } magick_thread;
79
80 magick_thread.signature=0UL;
81 magick_thread.id=pthread_self();
82 return(magick_thread.signature);
83 }
cristy0157aea2010-04-24 21:12:18 +000084#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
cristybb503372010-05-27 20:51:26 +000085 return((size_t) GetCurrentThreadId());
cristy3ed852e2009-09-05 21:47:34 +000086#else
cristybb503372010-05-27 20:51:26 +000087 return((size_t) getpid());
cristy3ed852e2009-09-05 21:47:34 +000088#endif
89}
90
91static inline MagickBooleanType IsMagickThreadEqual(const MagickThreadType id)
92{
cristy55bf91c2010-09-24 00:29:41 +000093#if defined(MAGICKCORE_THREAD_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000094 if (pthread_equal(id,pthread_self()) != 0)
95 return(MagickTrue);
cristy0157aea2010-04-24 21:12:18 +000096#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000097 if (id == GetCurrentThreadId())
98 return(MagickTrue);
99#else
100 if (id == getpid())
101 return(MagickTrue);
102#endif
103 return(MagickFalse);
104}
105
106/*
107 Lightweight OpenMP methods.
108*/
cristybb503372010-05-27 20:51:26 +0000109static inline size_t GetOpenMPMaximumThreads(void)
cristy3ed852e2009-09-05 21:47:34 +0000110{
cristy5a629092012-01-20 14:45:29 +0000111#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000112 return(omp_get_max_threads());
113#else
114 return(1);
cristy44a3c232010-03-24 16:08:12 +0000115#endif
cristy3ed852e2009-09-05 21:47:34 +0000116}
117
cristyc3ebda22010-06-27 17:11:57 +0000118static inline int GetOpenMPThreadId(void)
cristy3ed852e2009-09-05 21:47:34 +0000119{
cristy5a629092012-01-20 14:45:29 +0000120#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000121 return(omp_get_thread_num());
122#else
123 return(0);
124#endif
125}
126
cristyeaedf062010-05-29 22:36:02 +0000127static inline void SetOpenMPMaximumThreads(const int threads)
cristy3ed852e2009-09-05 21:47:34 +0000128{
cristy5a629092012-01-20 14:45:29 +0000129#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy5fa207c2011-11-21 18:32:43 +0000130 omp_set_num_threads(threads);
cristy3ed852e2009-09-05 21:47:34 +0000131#else
132 (void) threads;
133#endif
134}
135
cristy22ea5d42009-10-25 04:03:55 +0000136static inline void SetOpenMPNested(const int value)
137{
cristy5a629092012-01-20 14:45:29 +0000138#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy22ea5d42009-10-25 04:03:55 +0000139 omp_set_nested(value);
cristye27462f2009-10-26 13:18:22 +0000140#else
141 (void) value;
cristy22ea5d42009-10-25 04:03:55 +0000142#endif
143}
144
cristy3ed852e2009-09-05 21:47:34 +0000145#if defined(__cplusplus) || defined(c_plusplus)
146}
147#endif
148
149#endif