blob: 07d6991cdaa0071e1d2d83b011c05dc021bb5896 [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
cristy4c08aed2011-07-01 19:47:50 +000025#include <MagickCore/thread_.h>
cristy3ed852e2009-09-05 21:47:34 +000026
cristyb940c752012-05-06 18:32:28 +000027/*
28 Can loop benefit from multi-threads?
29*/
cristycc0aa6b2012-05-06 02:05:22 +000030#define IsConcurrentUno(colors,threshold) \
31 if ((colors) > threshold) \
cristy0ced47f2012-05-06 01:37:19 +000032 num_threads(GetMagickResourceLimit(ThreadResource))
cristycc0aa6b2012-05-06 02:05:22 +000033#define IsConcurrentDos(columns,rows,threshold) \
34 if (((((columns) > threshold) || ((rows) > threshold))) && \
35 ((MagickSizeType) (columns*rows) > (threshold*threshold))) \
cristy0ced47f2012-05-06 01:37:19 +000036 num_threads(GetMagickResourceLimit(ThreadResource))
cristycc0aa6b2012-05-06 02:05:22 +000037#define IsConcurrentTres(columns,rows,expression,threshold) \
38 if (((((columns) > threshold) || ((rows) > threshold))) && \
39 ((MagickSizeType) (columns*rows) > (threshold*threshold)) && (expression)) \
cristy3545b652012-05-06 01:40:39 +000040 num_threads(GetMagickResourceLimit(ThreadResource))
cristyccfffb42012-05-06 01:17:48 +000041
cristyec2c4982010-02-14 23:13:41 +000042#if (__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR > 10))
43#define MagickCachePrefetch(address,mode,locality) \
44 __builtin_prefetch(address,mode,locality)
45#else
46#define MagickCachePrefetch(address,mode,locality)
47#endif
48
cristy55bf91c2010-09-24 00:29:41 +000049#if defined(MAGICKCORE_THREAD_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000050 typedef pthread_mutex_t MagickMutexType;
cristy0157aea2010-04-24 21:12:18 +000051#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000052 typedef CRITICAL_SECTION MagickMutexType;
53#else
cristybb503372010-05-27 20:51:26 +000054 typedef size_t MagickMutexType;
cristy3ed852e2009-09-05 21:47:34 +000055#endif
56
57static inline MagickThreadType GetMagickThreadId(void)
58{
cristy55bf91c2010-09-24 00:29:41 +000059#if defined(MAGICKCORE_THREAD_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000060 return(pthread_self());
cristy0157aea2010-04-24 21:12:18 +000061#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000062 return(GetCurrentThreadId());
63#else
64 return(getpid());
65#endif
66}
67
cristybb503372010-05-27 20:51:26 +000068static inline size_t GetMagickThreadSignature(void)
cristy3ed852e2009-09-05 21:47:34 +000069{
cristy55bf91c2010-09-24 00:29:41 +000070#if defined(MAGICKCORE_THREAD_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000071 {
72 union
73 {
74 pthread_t
75 id;
76
cristybb503372010-05-27 20:51:26 +000077 size_t
cristy3ed852e2009-09-05 21:47:34 +000078 signature;
79 } magick_thread;
80
81 magick_thread.signature=0UL;
82 magick_thread.id=pthread_self();
83 return(magick_thread.signature);
84 }
cristy0157aea2010-04-24 21:12:18 +000085#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
cristybb503372010-05-27 20:51:26 +000086 return((size_t) GetCurrentThreadId());
cristy3ed852e2009-09-05 21:47:34 +000087#else
cristybb503372010-05-27 20:51:26 +000088 return((size_t) getpid());
cristy3ed852e2009-09-05 21:47:34 +000089#endif
90}
91
92static inline MagickBooleanType IsMagickThreadEqual(const MagickThreadType id)
93{
cristy55bf91c2010-09-24 00:29:41 +000094#if defined(MAGICKCORE_THREAD_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000095 if (pthread_equal(id,pthread_self()) != 0)
96 return(MagickTrue);
cristy0157aea2010-04-24 21:12:18 +000097#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000098 if (id == GetCurrentThreadId())
99 return(MagickTrue);
100#else
101 if (id == getpid())
102 return(MagickTrue);
103#endif
104 return(MagickFalse);
105}
106
107/*
108 Lightweight OpenMP methods.
109*/
cristybb503372010-05-27 20:51:26 +0000110static inline size_t GetOpenMPMaximumThreads(void)
cristy3ed852e2009-09-05 21:47:34 +0000111{
cristy5a629092012-01-20 14:45:29 +0000112#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000113 return(omp_get_max_threads());
114#else
115 return(1);
cristy44a3c232010-03-24 16:08:12 +0000116#endif
cristy3ed852e2009-09-05 21:47:34 +0000117}
118
cristyc3ebda22010-06-27 17:11:57 +0000119static inline int GetOpenMPThreadId(void)
cristy3ed852e2009-09-05 21:47:34 +0000120{
cristy5a629092012-01-20 14:45:29 +0000121#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000122 return(omp_get_thread_num());
123#else
124 return(0);
125#endif
126}
127
cristyeaedf062010-05-29 22:36:02 +0000128static inline void SetOpenMPMaximumThreads(const int threads)
cristy3ed852e2009-09-05 21:47:34 +0000129{
cristy5a629092012-01-20 14:45:29 +0000130#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy5fa207c2011-11-21 18:32:43 +0000131 omp_set_num_threads(threads);
cristy3ed852e2009-09-05 21:47:34 +0000132#else
133 (void) threads;
134#endif
135}
136
cristy22ea5d42009-10-25 04:03:55 +0000137static inline void SetOpenMPNested(const int value)
138{
cristy5a629092012-01-20 14:45:29 +0000139#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy22ea5d42009-10-25 04:03:55 +0000140 omp_set_nested(value);
cristye27462f2009-10-26 13:18:22 +0000141#else
142 (void) value;
cristy22ea5d42009-10-25 04:03:55 +0000143#endif
144}
145
cristy3ed852e2009-09-05 21:47:34 +0000146#if defined(__cplusplus) || defined(c_plusplus)
147}
148#endif
149
150#endif