blob: 6615f38dd32855e14a7b3cb77e2c9fb5029f76d2 [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/*
29 Can loop benefit from multi-threads?
30*/
cristycc0aa6b2012-05-06 02:05:22 +000031#define IsConcurrentUno(colors,threshold) \
32 if ((colors) > threshold) \
cristy0ced47f2012-05-06 01:37:19 +000033 num_threads(GetMagickResourceLimit(ThreadResource))
cristycc0aa6b2012-05-06 02:05:22 +000034#define IsConcurrentDos(columns,rows,threshold) \
35 if (((((columns) > threshold) || ((rows) > threshold))) && \
36 ((MagickSizeType) (columns*rows) > (threshold*threshold))) \
cristy0ced47f2012-05-06 01:37:19 +000037 num_threads(GetMagickResourceLimit(ThreadResource))
cristycc0aa6b2012-05-06 02:05:22 +000038#define IsConcurrentTres(columns,rows,expression,threshold) \
cristyd94cea52012-05-06 18:36:00 +000039 if (((((columns) > threshold) || ((rows) > threshold))) && ((MagickSizeType) \
40 (columns*rows) > (threshold*threshold)) && (expression)) \
cristy3545b652012-05-06 01:40:39 +000041 num_threads(GetMagickResourceLimit(ThreadResource))
cristyccfffb42012-05-06 01:17:48 +000042
cristyec2c4982010-02-14 23:13:41 +000043#if (__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR > 10))
44#define MagickCachePrefetch(address,mode,locality) \
45 __builtin_prefetch(address,mode,locality)
46#else
47#define MagickCachePrefetch(address,mode,locality)
48#endif
49
cristy55bf91c2010-09-24 00:29:41 +000050#if defined(MAGICKCORE_THREAD_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000051 typedef pthread_mutex_t MagickMutexType;
cristy0157aea2010-04-24 21:12:18 +000052#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000053 typedef CRITICAL_SECTION MagickMutexType;
54#else
cristybb503372010-05-27 20:51:26 +000055 typedef size_t MagickMutexType;
cristy3ed852e2009-09-05 21:47:34 +000056#endif
57
58static inline MagickThreadType GetMagickThreadId(void)
59{
cristy55bf91c2010-09-24 00:29:41 +000060#if defined(MAGICKCORE_THREAD_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000061 return(pthread_self());
cristy0157aea2010-04-24 21:12:18 +000062#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000063 return(GetCurrentThreadId());
64#else
65 return(getpid());
66#endif
67}
68
cristybb503372010-05-27 20:51:26 +000069static inline size_t GetMagickThreadSignature(void)
cristy3ed852e2009-09-05 21:47:34 +000070{
cristy55bf91c2010-09-24 00:29:41 +000071#if defined(MAGICKCORE_THREAD_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000072 {
73 union
74 {
75 pthread_t
76 id;
77
cristybb503372010-05-27 20:51:26 +000078 size_t
cristy3ed852e2009-09-05 21:47:34 +000079 signature;
80 } magick_thread;
81
82 magick_thread.signature=0UL;
83 magick_thread.id=pthread_self();
84 return(magick_thread.signature);
85 }
cristy0157aea2010-04-24 21:12:18 +000086#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
cristybb503372010-05-27 20:51:26 +000087 return((size_t) GetCurrentThreadId());
cristy3ed852e2009-09-05 21:47:34 +000088#else
cristybb503372010-05-27 20:51:26 +000089 return((size_t) getpid());
cristy3ed852e2009-09-05 21:47:34 +000090#endif
91}
92
93static inline MagickBooleanType IsMagickThreadEqual(const MagickThreadType id)
94{
cristy55bf91c2010-09-24 00:29:41 +000095#if defined(MAGICKCORE_THREAD_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000096 if (pthread_equal(id,pthread_self()) != 0)
97 return(MagickTrue);
cristy0157aea2010-04-24 21:12:18 +000098#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000099 if (id == GetCurrentThreadId())
100 return(MagickTrue);
101#else
102 if (id == getpid())
103 return(MagickTrue);
104#endif
105 return(MagickFalse);
106}
107
108/*
109 Lightweight OpenMP methods.
110*/
cristybb503372010-05-27 20:51:26 +0000111static inline size_t GetOpenMPMaximumThreads(void)
cristy3ed852e2009-09-05 21:47:34 +0000112{
cristy5a629092012-01-20 14:45:29 +0000113#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000114 return(omp_get_max_threads());
115#else
116 return(1);
cristy44a3c232010-03-24 16:08:12 +0000117#endif
cristy3ed852e2009-09-05 21:47:34 +0000118}
119
cristyc3ebda22010-06-27 17:11:57 +0000120static inline int GetOpenMPThreadId(void)
cristy3ed852e2009-09-05 21:47:34 +0000121{
cristy5a629092012-01-20 14:45:29 +0000122#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000123 return(omp_get_thread_num());
124#else
125 return(0);
126#endif
127}
128
cristyeaedf062010-05-29 22:36:02 +0000129static inline void SetOpenMPMaximumThreads(const int threads)
cristy3ed852e2009-09-05 21:47:34 +0000130{
cristy5a629092012-01-20 14:45:29 +0000131#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy5fa207c2011-11-21 18:32:43 +0000132 omp_set_num_threads(threads);
cristy3ed852e2009-09-05 21:47:34 +0000133#else
134 (void) threads;
135#endif
136}
137
cristy22ea5d42009-10-25 04:03:55 +0000138static inline void SetOpenMPNested(const int value)
139{
cristy5a629092012-01-20 14:45:29 +0000140#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy22ea5d42009-10-25 04:03:55 +0000141 omp_set_nested(value);
cristye27462f2009-10-26 13:18:22 +0000142#else
143 (void) value;
cristy22ea5d42009-10-25 04:03:55 +0000144#endif
145}
146
cristy3ed852e2009-09-05 21:47:34 +0000147#if defined(__cplusplus) || defined(c_plusplus)
148}
149#endif
150
151#endif