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