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