blob: dbe3848388a620a00793039008513e4c58075dc8 [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())) && \
cristy4ee2b0c2012-05-15 00:30:35 +000037 (IsPixelCacheInCore(image) != MagickFalse) && (expression)) \
cristy3545b652012-05-06 01:40:39 +000038 num_threads(GetMagickResourceLimit(ThreadResource))
cristyccfffb42012-05-06 01:17:48 +000039
cristyec2c4982010-02-14 23:13:41 +000040#if (__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR > 10))
41#define MagickCachePrefetch(address,mode,locality) \
42 __builtin_prefetch(address,mode,locality)
43#else
44#define MagickCachePrefetch(address,mode,locality)
45#endif
46
cristy55bf91c2010-09-24 00:29:41 +000047#if defined(MAGICKCORE_THREAD_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000048 typedef pthread_mutex_t MagickMutexType;
cristy0157aea2010-04-24 21:12:18 +000049#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000050 typedef CRITICAL_SECTION MagickMutexType;
51#else
cristybb503372010-05-27 20:51:26 +000052 typedef size_t MagickMutexType;
cristy3ed852e2009-09-05 21:47:34 +000053#endif
54
55static inline MagickThreadType GetMagickThreadId(void)
56{
cristy55bf91c2010-09-24 00:29:41 +000057#if defined(MAGICKCORE_THREAD_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000058 return(pthread_self());
cristy0157aea2010-04-24 21:12:18 +000059#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000060 return(GetCurrentThreadId());
61#else
62 return(getpid());
63#endif
64}
65
cristybb503372010-05-27 20:51:26 +000066static inline size_t GetMagickThreadSignature(void)
cristy3ed852e2009-09-05 21:47:34 +000067{
cristy55bf91c2010-09-24 00:29:41 +000068#if defined(MAGICKCORE_THREAD_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000069 {
70 union
71 {
72 pthread_t
73 id;
74
cristybb503372010-05-27 20:51:26 +000075 size_t
cristy3ed852e2009-09-05 21:47:34 +000076 signature;
77 } magick_thread;
78
79 magick_thread.signature=0UL;
80 magick_thread.id=pthread_self();
81 return(magick_thread.signature);
82 }
cristy0157aea2010-04-24 21:12:18 +000083#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
cristybb503372010-05-27 20:51:26 +000084 return((size_t) GetCurrentThreadId());
cristy3ed852e2009-09-05 21:47:34 +000085#else
cristybb503372010-05-27 20:51:26 +000086 return((size_t) getpid());
cristy3ed852e2009-09-05 21:47:34 +000087#endif
88}
89
90static inline MagickBooleanType IsMagickThreadEqual(const MagickThreadType id)
91{
cristy55bf91c2010-09-24 00:29:41 +000092#if defined(MAGICKCORE_THREAD_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000093 if (pthread_equal(id,pthread_self()) != 0)
94 return(MagickTrue);
cristy0157aea2010-04-24 21:12:18 +000095#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000096 if (id == GetCurrentThreadId())
97 return(MagickTrue);
98#else
99 if (id == getpid())
100 return(MagickTrue);
101#endif
102 return(MagickFalse);
103}
104
105/*
106 Lightweight OpenMP methods.
107*/
cristybb503372010-05-27 20:51:26 +0000108static inline size_t GetOpenMPMaximumThreads(void)
cristy3ed852e2009-09-05 21:47:34 +0000109{
cristy5a629092012-01-20 14:45:29 +0000110#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000111 return(omp_get_max_threads());
112#else
113 return(1);
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{
cristy5a629092012-01-20 14:45:29 +0000119#if defined(MAGICKCORE_OPENMP_SUPPORT)
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{
cristy5a629092012-01-20 14:45:29 +0000128#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy5fa207c2011-11-21 18:32:43 +0000129 omp_set_num_threads(threads);
cristy3ed852e2009-09-05 21:47:34 +0000130#else
131 (void) threads;
132#endif
133}
134
cristy22ea5d42009-10-25 04:03:55 +0000135static inline void SetOpenMPNested(const int value)
136{
cristy5a629092012-01-20 14:45:29 +0000137#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy22ea5d42009-10-25 04:03:55 +0000138 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