cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | Copyright 1999-2009 ImageMagick Studio LLC, a non-profit organization |
| 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 | |
| 27 | #if defined(MAGICKCORE_HAVE_PTHREAD) |
| 28 | typedef pthread_mutex_t MagickMutexType; |
| 29 | #elif defined(__WINDOWS__) |
| 30 | typedef CRITICAL_SECTION MagickMutexType; |
| 31 | #else |
| 32 | typedef unsigned long MagickMutexType; |
| 33 | #endif |
| 34 | |
| 35 | static inline MagickThreadType GetMagickThreadId(void) |
| 36 | { |
| 37 | #if defined(MAGICKCORE_HAVE_PTHREAD) |
| 38 | return(pthread_self()); |
| 39 | #elif defined(__WINDOWS__) |
| 40 | return(GetCurrentThreadId()); |
| 41 | #else |
| 42 | return(getpid()); |
| 43 | #endif |
| 44 | } |
| 45 | |
| 46 | static inline unsigned long GetMagickThreadSignature(void) |
| 47 | { |
| 48 | #if defined(MAGICKCORE_HAVE_PTHREAD) |
| 49 | { |
| 50 | union |
| 51 | { |
| 52 | pthread_t |
| 53 | id; |
| 54 | |
| 55 | unsigned long |
| 56 | signature; |
| 57 | } magick_thread; |
| 58 | |
| 59 | magick_thread.signature=0UL; |
| 60 | magick_thread.id=pthread_self(); |
| 61 | return(magick_thread.signature); |
| 62 | } |
| 63 | #elif defined(__WINDOWS__) |
| 64 | return((unsigned long) GetCurrentThreadId()); |
| 65 | #else |
| 66 | return((unsigned long) getpid()); |
| 67 | #endif |
| 68 | } |
| 69 | |
| 70 | static inline MagickBooleanType IsMagickThreadEqual(const MagickThreadType id) |
| 71 | { |
| 72 | #if defined(MAGICKCORE_HAVE_PTHREAD) |
| 73 | if (pthread_equal(id,pthread_self()) != 0) |
| 74 | return(MagickTrue); |
| 75 | #elif defined(__WINDOWS__) |
| 76 | if (id == GetCurrentThreadId()) |
| 77 | return(MagickTrue); |
| 78 | #else |
| 79 | if (id == getpid()) |
| 80 | return(MagickTrue); |
| 81 | #endif |
| 82 | return(MagickFalse); |
| 83 | } |
| 84 | |
| 85 | /* |
| 86 | Lightweight OpenMP methods. |
| 87 | */ |
| 88 | static inline unsigned long GetOpenMPMaximumThreads(void) |
| 89 | { |
cristy | b28d647 | 2009-10-17 20:13:35 +0000 | [diff] [blame] | 90 | #if defined(MAGICKCORE_OPENMP_SUPPORT) && (_OPENMP >= 200203) |
cristy | 398f932 | 2009-09-11 18:49:48 +0000 | [diff] [blame] | 91 | { |
| 92 | static unsigned long |
| 93 | maximum_threads = 1UL; |
| 94 | |
| 95 | if (omp_get_max_threads() > (long) maximum_threads) |
| 96 | maximum_threads=omp_get_max_threads(); |
| 97 | return(maximum_threads); |
| 98 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 99 | #endif |
| 100 | return(1UL); |
| 101 | } |
| 102 | |
| 103 | static inline long GetOpenMPThreadId(void) |
| 104 | { |
cristy | b28d647 | 2009-10-17 20:13:35 +0000 | [diff] [blame] | 105 | #if defined(MAGICKCORE_OPENMP_SUPPORT) && (_OPENMP >= 200203) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 106 | return(omp_get_thread_num()); |
| 107 | #else |
| 108 | return(0); |
| 109 | #endif |
| 110 | } |
| 111 | |
| 112 | static inline void SetOpenMPMaximumThreads(const unsigned long threads) |
| 113 | { |
cristy | b28d647 | 2009-10-17 20:13:35 +0000 | [diff] [blame] | 114 | #if defined(MAGICKCORE_OPENMP_SUPPORT) && (_OPENMP >= 200203) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 115 | omp_set_num_threads(threads); |
| 116 | #else |
| 117 | (void) threads; |
| 118 | #endif |
| 119 | } |
| 120 | |
| 121 | #if defined(__cplusplus) || defined(c_plusplus) |
| 122 | } |
| 123 | #endif |
| 124 | |
| 125 | #endif |