blob: 97a783a41f885170d6bd0e33d32b025824eff8b5 [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
cristyccfffb42012-05-06 01:17:48 +000027#define ThreadThreshold 64
28#define IsConcurrent(columns,rows) \
29 if (((((columns) > ThreadThreshold) || ((rows) > ThreadThreshold))) && \
30 ((MagickSizeType) (columns*rows) > (ThreadThreshold*ThreadThreshold))) \
31 num_threads(GetMagickResourceLimit(ThreadResource))
cristyb389cdc2012-05-06 01:28:57 +000032#define IsConcurrentExp(columns,rows,expression) \
33 if (((((columns) > ThreadThreshold) || ((rows) > ThreadThreshold))) && \
34 ((MagickSizeType) (columns*rows) > (ThreadThreshold*ThreadThreshold)) && \
35 (expression)) \
36 num_threads(GetMagickResourceLimit(ThreadResource))
cristyccfffb42012-05-06 01:17:48 +000037
cristyec2c4982010-02-14 23:13:41 +000038#if (__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR > 10))
39#define MagickCachePrefetch(address,mode,locality) \
40 __builtin_prefetch(address,mode,locality)
41#else
42#define MagickCachePrefetch(address,mode,locality)
43#endif
44
cristy55bf91c2010-09-24 00:29:41 +000045#if defined(MAGICKCORE_THREAD_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000046 typedef pthread_mutex_t MagickMutexType;
cristy0157aea2010-04-24 21:12:18 +000047#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000048 typedef CRITICAL_SECTION MagickMutexType;
49#else
cristybb503372010-05-27 20:51:26 +000050 typedef size_t MagickMutexType;
cristy3ed852e2009-09-05 21:47:34 +000051#endif
52
53static inline MagickThreadType GetMagickThreadId(void)
54{
cristy55bf91c2010-09-24 00:29:41 +000055#if defined(MAGICKCORE_THREAD_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000056 return(pthread_self());
cristy0157aea2010-04-24 21:12:18 +000057#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000058 return(GetCurrentThreadId());
59#else
60 return(getpid());
61#endif
62}
63
cristybb503372010-05-27 20:51:26 +000064static inline size_t GetMagickThreadSignature(void)
cristy3ed852e2009-09-05 21:47:34 +000065{
cristy55bf91c2010-09-24 00:29:41 +000066#if defined(MAGICKCORE_THREAD_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000067 {
68 union
69 {
70 pthread_t
71 id;
72
cristybb503372010-05-27 20:51:26 +000073 size_t
cristy3ed852e2009-09-05 21:47:34 +000074 signature;
75 } magick_thread;
76
77 magick_thread.signature=0UL;
78 magick_thread.id=pthread_self();
79 return(magick_thread.signature);
80 }
cristy0157aea2010-04-24 21:12:18 +000081#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
cristybb503372010-05-27 20:51:26 +000082 return((size_t) GetCurrentThreadId());
cristy3ed852e2009-09-05 21:47:34 +000083#else
cristybb503372010-05-27 20:51:26 +000084 return((size_t) getpid());
cristy3ed852e2009-09-05 21:47:34 +000085#endif
86}
87
88static inline MagickBooleanType IsMagickThreadEqual(const MagickThreadType id)
89{
cristy55bf91c2010-09-24 00:29:41 +000090#if defined(MAGICKCORE_THREAD_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000091 if (pthread_equal(id,pthread_self()) != 0)
92 return(MagickTrue);
cristy0157aea2010-04-24 21:12:18 +000093#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +000094 if (id == GetCurrentThreadId())
95 return(MagickTrue);
96#else
97 if (id == getpid())
98 return(MagickTrue);
99#endif
100 return(MagickFalse);
101}
102
103/*
104 Lightweight OpenMP methods.
105*/
cristybb503372010-05-27 20:51:26 +0000106static inline size_t GetOpenMPMaximumThreads(void)
cristy3ed852e2009-09-05 21:47:34 +0000107{
cristy5a629092012-01-20 14:45:29 +0000108#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000109 return(omp_get_max_threads());
110#else
111 return(1);
cristy44a3c232010-03-24 16:08:12 +0000112#endif
cristy3ed852e2009-09-05 21:47:34 +0000113}
114
cristyc3ebda22010-06-27 17:11:57 +0000115static inline int GetOpenMPThreadId(void)
cristy3ed852e2009-09-05 21:47:34 +0000116{
cristy5a629092012-01-20 14:45:29 +0000117#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000118 return(omp_get_thread_num());
119#else
120 return(0);
121#endif
122}
123
cristyeaedf062010-05-29 22:36:02 +0000124static inline void SetOpenMPMaximumThreads(const int threads)
cristy3ed852e2009-09-05 21:47:34 +0000125{
cristy5a629092012-01-20 14:45:29 +0000126#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy5fa207c2011-11-21 18:32:43 +0000127 omp_set_num_threads(threads);
cristy3ed852e2009-09-05 21:47:34 +0000128#else
129 (void) threads;
130#endif
131}
132
cristy22ea5d42009-10-25 04:03:55 +0000133static inline void SetOpenMPNested(const int value)
134{
cristy5a629092012-01-20 14:45:29 +0000135#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy22ea5d42009-10-25 04:03:55 +0000136 omp_set_nested(value);
cristye27462f2009-10-26 13:18:22 +0000137#else
138 (void) value;
cristy22ea5d42009-10-25 04:03:55 +0000139#endif
140}
141
cristy3ed852e2009-09-05 21:47:34 +0000142#if defined(__cplusplus) || defined(c_plusplus)
143}
144#endif
145
146#endif