blob: fea3330089f5880f087e32e1556af693cf5987c7 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
cristy45ef08f2012-12-07 13:13:34 +00002 Copyright 1999-2013 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))
cristy5e6b2592012-12-19 14:08:11 +000033#define magick_threads(source,destination,rows,expression) \
cristy26b64912012-12-16 18:20:09 +000034 if (((rows) > WorkloadThreshold()) && (expression)) \
35 num_threads((source) != (destination) ? \
36 GetMagickResourceLimit(ThreadResource) : \
cristy2032fc92012-12-30 22:17:37 +000037 GetImagePixelCacheType(source) != DiskCache ? \
38 GetMagickResourceLimit(ThreadResource) : \
cristy42454122013-01-06 15:37:16 +000039 GetMagickResourceLimit(ThreadResource) < 2 ? \
40 GetMagickResourceLimit(ThreadResource) : 2)
cristy5e6b2592012-12-19 14:08:11 +000041#define magick_schedule(type,chunk) \
42 schedule(type,(chunk) < 1 ? 1 : (chunk))
cristyccfffb42012-05-06 01:17:48 +000043
cristy20c26b12012-08-23 09:21:22 +000044#if (__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ > 10))
cristyec2c4982010-02-14 23:13:41 +000045#define MagickCachePrefetch(address,mode,locality) \
46 __builtin_prefetch(address,mode,locality)
47#else
48#define MagickCachePrefetch(address,mode,locality)
49#endif
50
cristy55bf91c2010-09-24 00:29:41 +000051#if defined(MAGICKCORE_THREAD_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000052 typedef pthread_mutex_t MagickMutexType;
cristy0157aea2010-04-24 21:12:18 +000053#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000054 typedef CRITICAL_SECTION MagickMutexType;
55#else
cristybb503372010-05-27 20:51:26 +000056 typedef size_t MagickMutexType;
cristy3ed852e2009-09-05 21:47:34 +000057#endif
58
59static inline MagickThreadType GetMagickThreadId(void)
60{
cristy55bf91c2010-09-24 00:29:41 +000061#if defined(MAGICKCORE_THREAD_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000062 return(pthread_self());
cristy0157aea2010-04-24 21:12:18 +000063#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000064 return(GetCurrentThreadId());
65#else
66 return(getpid());
67#endif
68}
69
cristybb503372010-05-27 20:51:26 +000070static inline size_t GetMagickThreadSignature(void)
cristy3ed852e2009-09-05 21:47:34 +000071{
cristy55bf91c2010-09-24 00:29:41 +000072#if defined(MAGICKCORE_THREAD_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000073 {
74 union
75 {
76 pthread_t
77 id;
78
cristybb503372010-05-27 20:51:26 +000079 size_t
cristy3ed852e2009-09-05 21:47:34 +000080 signature;
81 } magick_thread;
82
83 magick_thread.signature=0UL;
84 magick_thread.id=pthread_self();
85 return(magick_thread.signature);
86 }
cristy0157aea2010-04-24 21:12:18 +000087#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
cristybb503372010-05-27 20:51:26 +000088 return((size_t) GetCurrentThreadId());
cristy3ed852e2009-09-05 21:47:34 +000089#else
cristybb503372010-05-27 20:51:26 +000090 return((size_t) getpid());
cristy3ed852e2009-09-05 21:47:34 +000091#endif
92}
93
94static inline MagickBooleanType IsMagickThreadEqual(const MagickThreadType id)
95{
cristy55bf91c2010-09-24 00:29:41 +000096#if defined(MAGICKCORE_THREAD_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000097 if (pthread_equal(id,pthread_self()) != 0)
98 return(MagickTrue);
cristy0157aea2010-04-24 21:12:18 +000099#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000100 if (id == GetCurrentThreadId())
101 return(MagickTrue);
102#else
103 if (id == getpid())
104 return(MagickTrue);
105#endif
106 return(MagickFalse);
107}
108
109/*
110 Lightweight OpenMP methods.
111*/
cristybb503372010-05-27 20:51:26 +0000112static inline size_t GetOpenMPMaximumThreads(void)
cristy3ed852e2009-09-05 21:47:34 +0000113{
cristy5a629092012-01-20 14:45:29 +0000114#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000115 return(omp_get_max_threads());
116#else
117 return(1);
cristy44a3c232010-03-24 16:08:12 +0000118#endif
cristy3ed852e2009-09-05 21:47:34 +0000119}
120
cristyc3ebda22010-06-27 17:11:57 +0000121static inline int GetOpenMPThreadId(void)
cristy3ed852e2009-09-05 21:47:34 +0000122{
cristy5a629092012-01-20 14:45:29 +0000123#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000124 return(omp_get_thread_num());
125#else
126 return(0);
127#endif
128}
129
cristyeaedf062010-05-29 22:36:02 +0000130static inline void SetOpenMPMaximumThreads(const int threads)
cristy3ed852e2009-09-05 21:47:34 +0000131{
cristy5a629092012-01-20 14:45:29 +0000132#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy5fa207c2011-11-21 18:32:43 +0000133 omp_set_num_threads(threads);
cristy3ed852e2009-09-05 21:47:34 +0000134#else
135 (void) threads;
136#endif
137}
138
cristy22ea5d42009-10-25 04:03:55 +0000139static inline void SetOpenMPNested(const int value)
140{
cristy5a629092012-01-20 14:45:29 +0000141#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy22ea5d42009-10-25 04:03:55 +0000142 omp_set_nested(value);
cristye27462f2009-10-26 13:18:22 +0000143#else
144 (void) value;
cristy22ea5d42009-10-25 04:03:55 +0000145#endif
146}
147
cristy3ed852e2009-09-05 21:47:34 +0000148#if defined(__cplusplus) || defined(c_plusplus)
149}
150#endif
151
152#endif