blob: 7496eeb7f1eee251f81f83a1f69536df58e6dfc6 [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*/
cristy7f5bb552012-05-08 13:34:21 +000031#define MinimumWorkLoad() (16*GetMagickResourceLimit(ThreadResource))
cristyddacdd12012-05-07 23:08:14 +000032#define dynamic_number_threads(columns,rows,expression) \
cristy9caad062012-05-07 18:58:56 +000033 if (((((columns) > MinimumWorkLoad()) || \
34 ((rows) > MinimumWorkLoad()))) && ((MagickSizeType) \
35 ((columns)*(rows)) > (MinimumWorkLoad()*MinimumWorkLoad())) && \
36 (expression)) \
cristy3545b652012-05-06 01:40:39 +000037 num_threads(GetMagickResourceLimit(ThreadResource))
cristyccfffb42012-05-06 01:17:48 +000038
cristyec2c4982010-02-14 23:13:41 +000039#if (__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR > 10))
40#define MagickCachePrefetch(address,mode,locality) \
41 __builtin_prefetch(address,mode,locality)
42#else
43#define MagickCachePrefetch(address,mode,locality)
44#endif
45
cristy55bf91c2010-09-24 00:29:41 +000046#if defined(MAGICKCORE_THREAD_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000047 typedef pthread_mutex_t MagickMutexType;
cristy0157aea2010-04-24 21:12:18 +000048#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000049 typedef CRITICAL_SECTION MagickMutexType;
50#else
cristybb503372010-05-27 20:51:26 +000051 typedef size_t MagickMutexType;
cristy3ed852e2009-09-05 21:47:34 +000052#endif
53
54static inline MagickThreadType GetMagickThreadId(void)
55{
cristy55bf91c2010-09-24 00:29:41 +000056#if defined(MAGICKCORE_THREAD_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000057 return(pthread_self());
cristy0157aea2010-04-24 21:12:18 +000058#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000059 return(GetCurrentThreadId());
60#else
61 return(getpid());
62#endif
63}
64
cristybb503372010-05-27 20:51:26 +000065static inline size_t GetMagickThreadSignature(void)
cristy3ed852e2009-09-05 21:47:34 +000066{
cristy55bf91c2010-09-24 00:29:41 +000067#if defined(MAGICKCORE_THREAD_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000068 {
69 union
70 {
71 pthread_t
72 id;
73
cristybb503372010-05-27 20:51:26 +000074 size_t
cristy3ed852e2009-09-05 21:47:34 +000075 signature;
76 } magick_thread;
77
78 magick_thread.signature=0UL;
79 magick_thread.id=pthread_self();
80 return(magick_thread.signature);
81 }
cristy0157aea2010-04-24 21:12:18 +000082#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
cristybb503372010-05-27 20:51:26 +000083 return((size_t) GetCurrentThreadId());
cristy3ed852e2009-09-05 21:47:34 +000084#else
cristybb503372010-05-27 20:51:26 +000085 return((size_t) getpid());
cristy3ed852e2009-09-05 21:47:34 +000086#endif
87}
88
89static inline MagickBooleanType IsMagickThreadEqual(const MagickThreadType id)
90{
cristy55bf91c2010-09-24 00:29:41 +000091#if defined(MAGICKCORE_THREAD_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000092 if (pthread_equal(id,pthread_self()) != 0)
93 return(MagickTrue);
cristy0157aea2010-04-24 21:12:18 +000094#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000095 if (id == GetCurrentThreadId())
96 return(MagickTrue);
97#else
98 if (id == getpid())
99 return(MagickTrue);
100#endif
101 return(MagickFalse);
102}
103
104/*
105 Lightweight OpenMP methods.
106*/
cristybb503372010-05-27 20:51:26 +0000107static inline size_t GetOpenMPMaximumThreads(void)
cristy3ed852e2009-09-05 21:47:34 +0000108{
cristy5a629092012-01-20 14:45:29 +0000109#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000110 return(omp_get_max_threads());
111#else
112 return(1);
cristy44a3c232010-03-24 16:08:12 +0000113#endif
cristy3ed852e2009-09-05 21:47:34 +0000114}
115
cristyc3ebda22010-06-27 17:11:57 +0000116static inline int GetOpenMPThreadId(void)
cristy3ed852e2009-09-05 21:47:34 +0000117{
cristy5a629092012-01-20 14:45:29 +0000118#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000119 return(omp_get_thread_num());
120#else
121 return(0);
122#endif
123}
124
cristyeaedf062010-05-29 22:36:02 +0000125static inline void SetOpenMPMaximumThreads(const int threads)
cristy3ed852e2009-09-05 21:47:34 +0000126{
cristy5a629092012-01-20 14:45:29 +0000127#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy5fa207c2011-11-21 18:32:43 +0000128 omp_set_num_threads(threads);
cristy3ed852e2009-09-05 21:47:34 +0000129#else
130 (void) threads;
131#endif
132}
133
cristy22ea5d42009-10-25 04:03:55 +0000134static inline void SetOpenMPNested(const int value)
135{
cristy5a629092012-01-20 14:45:29 +0000136#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy22ea5d42009-10-25 04:03:55 +0000137 omp_set_nested(value);
cristye27462f2009-10-26 13:18:22 +0000138#else
139 (void) value;
cristy22ea5d42009-10-25 04:03:55 +0000140#endif
141}
142
cristy3ed852e2009-09-05 21:47:34 +0000143#if defined(__cplusplus) || defined(c_plusplus)
144}
145#endif
146
147#endif