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