cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1 | /* |
cristy | 1454be7 | 2011-12-19 01:52:48 +0000 | [diff] [blame] | 2 | Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3 | 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) |
| 22 | extern "C" { |
| 23 | #endif |
| 24 | |
cristy | 4ee2b0c | 2012-05-15 00:30:35 +0000 | [diff] [blame] | 25 | #include <MagickCore/cache.h> |
cristy | 70e8f29 | 2012-05-06 18:34:35 +0000 | [diff] [blame] | 26 | #include <MagickCore/resource_.h> |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 27 | #include <MagickCore/thread_.h> |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 28 | |
cristy | b940c75 | 2012-05-06 18:32:28 +0000 | [diff] [blame] | 29 | /* |
cristy | 9caad06 | 2012-05-07 18:58:56 +0000 | [diff] [blame] | 30 | Single threaded unless workload justifies the threading overhead. |
cristy | b940c75 | 2012-05-06 18:32:28 +0000 | [diff] [blame] | 31 | */ |
cristy | cf25817 | 2012-05-11 11:53:21 +0000 | [diff] [blame] | 32 | #define WorkloadThreshold() (16*GetMagickResourceLimit(ThreadResource)) |
cristy | 4ee2b0c | 2012-05-15 00:30:35 +0000 | [diff] [blame] | 33 | #define dynamic_number_threads(image,columns,rows,expression) \ |
cristy | cf25817 | 2012-05-11 11:53:21 +0000 | [diff] [blame] | 34 | if (((((columns) > WorkloadThreshold()) || \ |
| 35 | ((rows) > WorkloadThreshold()))) && ((MagickSizeType) \ |
| 36 | ((columns)*(rows)) > (WorkloadThreshold()*WorkloadThreshold())) && \ |
cristy | 85cc65a | 2012-10-30 14:17:50 +0000 | [diff] [blame] | 37 | (expression)) \ |
cristy | d4acac9 | 2012-10-30 21:30:14 +0000 | [diff] [blame] | 38 | num_threads(GetMagickResourceLimit(ThreadResource)/ \ |
| 39 | (GetImagePixelCacheType(image) == DiskCache ? 2 : 1)) |
cristy | ccfffb4 | 2012-05-06 01:17:48 +0000 | [diff] [blame] | 40 | |
cristy | 20c26b1 | 2012-08-23 09:21:22 +0000 | [diff] [blame] | 41 | #if (__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ > 10)) |
cristy | ec2c498 | 2010-02-14 23:13:41 +0000 | [diff] [blame] | 42 | #define MagickCachePrefetch(address,mode,locality) \ |
| 43 | __builtin_prefetch(address,mode,locality) |
| 44 | #else |
| 45 | #define MagickCachePrefetch(address,mode,locality) |
| 46 | #endif |
| 47 | |
cristy | 55bf91c | 2010-09-24 00:29:41 +0000 | [diff] [blame] | 48 | #if defined(MAGICKCORE_THREAD_SUPPORT) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 49 | typedef pthread_mutex_t MagickMutexType; |
cristy | 0157aea | 2010-04-24 21:12:18 +0000 | [diff] [blame] | 50 | #elif defined(MAGICKCORE_WINDOWS_SUPPORT) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 51 | typedef CRITICAL_SECTION MagickMutexType; |
| 52 | #else |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 53 | typedef size_t MagickMutexType; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 54 | #endif |
| 55 | |
| 56 | static inline MagickThreadType GetMagickThreadId(void) |
| 57 | { |
cristy | 55bf91c | 2010-09-24 00:29:41 +0000 | [diff] [blame] | 58 | #if defined(MAGICKCORE_THREAD_SUPPORT) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 59 | return(pthread_self()); |
cristy | 0157aea | 2010-04-24 21:12:18 +0000 | [diff] [blame] | 60 | #elif defined(MAGICKCORE_WINDOWS_SUPPORT) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 61 | return(GetCurrentThreadId()); |
| 62 | #else |
| 63 | return(getpid()); |
| 64 | #endif |
| 65 | } |
| 66 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 67 | static inline size_t GetMagickThreadSignature(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 68 | { |
cristy | 55bf91c | 2010-09-24 00:29:41 +0000 | [diff] [blame] | 69 | #if defined(MAGICKCORE_THREAD_SUPPORT) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 70 | { |
| 71 | union |
| 72 | { |
| 73 | pthread_t |
| 74 | id; |
| 75 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 76 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 77 | signature; |
| 78 | } magick_thread; |
| 79 | |
| 80 | magick_thread.signature=0UL; |
| 81 | magick_thread.id=pthread_self(); |
| 82 | return(magick_thread.signature); |
| 83 | } |
cristy | 0157aea | 2010-04-24 21:12:18 +0000 | [diff] [blame] | 84 | #elif defined(MAGICKCORE_WINDOWS_SUPPORT) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 85 | return((size_t) GetCurrentThreadId()); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 86 | #else |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 87 | return((size_t) getpid()); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 88 | #endif |
| 89 | } |
| 90 | |
| 91 | static inline MagickBooleanType IsMagickThreadEqual(const MagickThreadType id) |
| 92 | { |
cristy | 55bf91c | 2010-09-24 00:29:41 +0000 | [diff] [blame] | 93 | #if defined(MAGICKCORE_THREAD_SUPPORT) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 94 | if (pthread_equal(id,pthread_self()) != 0) |
| 95 | return(MagickTrue); |
cristy | 0157aea | 2010-04-24 21:12:18 +0000 | [diff] [blame] | 96 | #elif defined(MAGICKCORE_WINDOWS_SUPPORT) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 97 | if (id == GetCurrentThreadId()) |
| 98 | return(MagickTrue); |
| 99 | #else |
| 100 | if (id == getpid()) |
| 101 | return(MagickTrue); |
| 102 | #endif |
| 103 | return(MagickFalse); |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | Lightweight OpenMP methods. |
| 108 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 109 | static inline size_t GetOpenMPMaximumThreads(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 110 | { |
cristy | 5a62909 | 2012-01-20 14:45:29 +0000 | [diff] [blame] | 111 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 112 | return(omp_get_max_threads()); |
| 113 | #else |
| 114 | return(1); |
cristy | 44a3c23 | 2010-03-24 16:08:12 +0000 | [diff] [blame] | 115 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 116 | } |
| 117 | |
cristy | c3ebda2 | 2010-06-27 17:11:57 +0000 | [diff] [blame] | 118 | static inline int GetOpenMPThreadId(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 119 | { |
cristy | 5a62909 | 2012-01-20 14:45:29 +0000 | [diff] [blame] | 120 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 121 | return(omp_get_thread_num()); |
| 122 | #else |
| 123 | return(0); |
| 124 | #endif |
| 125 | } |
| 126 | |
cristy | eaedf06 | 2010-05-29 22:36:02 +0000 | [diff] [blame] | 127 | static inline void SetOpenMPMaximumThreads(const int threads) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 128 | { |
cristy | 5a62909 | 2012-01-20 14:45:29 +0000 | [diff] [blame] | 129 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 5fa207c | 2011-11-21 18:32:43 +0000 | [diff] [blame] | 130 | omp_set_num_threads(threads); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 131 | #else |
| 132 | (void) threads; |
| 133 | #endif |
| 134 | } |
| 135 | |
cristy | 22ea5d4 | 2009-10-25 04:03:55 +0000 | [diff] [blame] | 136 | static inline void SetOpenMPNested(const int value) |
| 137 | { |
cristy | 5a62909 | 2012-01-20 14:45:29 +0000 | [diff] [blame] | 138 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 22ea5d4 | 2009-10-25 04:03:55 +0000 | [diff] [blame] | 139 | omp_set_nested(value); |
cristy | e27462f | 2009-10-26 13:18:22 +0000 | [diff] [blame] | 140 | #else |
| 141 | (void) value; |
cristy | 22ea5d4 | 2009-10-25 04:03:55 +0000 | [diff] [blame] | 142 | #endif |
| 143 | } |
| 144 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 145 | #if defined(__cplusplus) || defined(c_plusplus) |
| 146 | } |
| 147 | #endif |
| 148 | |
| 149 | #endif |