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