| 1 | /* |
|---|
| 2 | Copyright 1999-2012 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 <MagickCore/resource_.h> |
|---|
| 26 | #include <MagickCore/thread_.h> |
|---|
| 27 | |
|---|
| 28 | /* |
|---|
| 29 | Can loop benefit from multi-threads? |
|---|
| 30 | */ |
|---|
| 31 | #define IsConcurrentUno(colors,threshold) \ |
|---|
| 32 | if ((colors) > threshold) \ |
|---|
| 33 | num_threads(GetMagickResourceLimit(ThreadResource)) |
|---|
| 34 | #define IsConcurrentDos(columns,rows,threshold) \ |
|---|
| 35 | if (((((columns) > threshold) || ((rows) > threshold))) && \ |
|---|
| 36 | ((MagickSizeType) (columns*rows) > (threshold*threshold))) \ |
|---|
| 37 | num_threads(GetMagickResourceLimit(ThreadResource)) |
|---|
| 38 | #define IsConcurrentTres(columns,rows,expression,threshold) \ |
|---|
| 39 | if (((((columns) > threshold) || ((rows) > threshold))) && \ |
|---|
| 40 | ((MagickSizeType) (columns*rows) > (threshold*threshold)) && (expression)) \ |
|---|
| 41 | num_threads(GetMagickResourceLimit(ThreadResource)) |
|---|
| 42 | |
|---|
| 43 | #if (__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR > 10)) |
|---|
| 44 | #define MagickCachePrefetch(address,mode,locality) \ |
|---|
| 45 | __builtin_prefetch(address,mode,locality) |
|---|
| 46 | #else |
|---|
| 47 | #define MagickCachePrefetch(address,mode,locality) |
|---|
| 48 | #endif |
|---|
| 49 | |
|---|
| 50 | #if defined(MAGICKCORE_THREAD_SUPPORT) |
|---|
| 51 | typedef pthread_mutex_t MagickMutexType; |
|---|
| 52 | #elif defined(MAGICKCORE_WINDOWS_SUPPORT) |
|---|
| 53 | typedef CRITICAL_SECTION MagickMutexType; |
|---|
| 54 | #else |
|---|
| 55 | typedef size_t MagickMutexType; |
|---|
| 56 | #endif |
|---|
| 57 | |
|---|
| 58 | static inline MagickThreadType GetMagickThreadId(void) |
|---|
| 59 | { |
|---|
| 60 | #if defined(MAGICKCORE_THREAD_SUPPORT) |
|---|
| 61 | return(pthread_self()); |
|---|
| 62 | #elif defined(MAGICKCORE_WINDOWS_SUPPORT) |
|---|
| 63 | return(GetCurrentThreadId()); |
|---|
| 64 | #else |
|---|
| 65 | return(getpid()); |
|---|
| 66 | #endif |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | static inline size_t GetMagickThreadSignature(void) |
|---|
| 70 | { |
|---|
| 71 | #if defined(MAGICKCORE_THREAD_SUPPORT) |
|---|
| 72 | { |
|---|
| 73 | union |
|---|
| 74 | { |
|---|
| 75 | pthread_t |
|---|
| 76 | id; |
|---|
| 77 | |
|---|
| 78 | size_t |
|---|
| 79 | signature; |
|---|
| 80 | } magick_thread; |
|---|
| 81 | |
|---|
| 82 | magick_thread.signature=0UL; |
|---|
| 83 | magick_thread.id=pthread_self(); |
|---|
| 84 | return(magick_thread.signature); |
|---|
| 85 | } |
|---|
| 86 | #elif defined(MAGICKCORE_WINDOWS_SUPPORT) |
|---|
| 87 | return((size_t) GetCurrentThreadId()); |
|---|
| 88 | #else |
|---|
| 89 | return((size_t) getpid()); |
|---|
| 90 | #endif |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | static inline MagickBooleanType IsMagickThreadEqual(const MagickThreadType id) |
|---|
| 94 | { |
|---|
| 95 | #if defined(MAGICKCORE_THREAD_SUPPORT) |
|---|
| 96 | if (pthread_equal(id,pthread_self()) != 0) |
|---|
| 97 | return(MagickTrue); |
|---|
| 98 | #elif defined(MAGICKCORE_WINDOWS_SUPPORT) |
|---|
| 99 | if (id == GetCurrentThreadId()) |
|---|
| 100 | return(MagickTrue); |
|---|
| 101 | #else |
|---|
| 102 | if (id == getpid()) |
|---|
| 103 | return(MagickTrue); |
|---|
| 104 | #endif |
|---|
| 105 | return(MagickFalse); |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | /* |
|---|
| 109 | Lightweight OpenMP methods. |
|---|
| 110 | */ |
|---|
| 111 | static inline size_t GetOpenMPMaximumThreads(void) |
|---|
| 112 | { |
|---|
| 113 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
|---|
| 114 | return(omp_get_max_threads()); |
|---|
| 115 | #else |
|---|
| 116 | return(1); |
|---|
| 117 | #endif |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | static inline int GetOpenMPThreadId(void) |
|---|
| 121 | { |
|---|
| 122 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
|---|
| 123 | return(omp_get_thread_num()); |
|---|
| 124 | #else |
|---|
| 125 | return(0); |
|---|
| 126 | #endif |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | static inline void SetOpenMPMaximumThreads(const int threads) |
|---|
| 130 | { |
|---|
| 131 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
|---|
| 132 | omp_set_num_threads(threads); |
|---|
| 133 | #else |
|---|
| 134 | (void) threads; |
|---|
| 135 | #endif |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | static inline void SetOpenMPNested(const int value) |
|---|
| 139 | { |
|---|
| 140 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
|---|
| 141 | omp_set_nested(value); |
|---|
| 142 | #else |
|---|
| 143 | (void) value; |
|---|
| 144 | #endif |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | #if defined(__cplusplus) || defined(c_plusplus) |
|---|
| 148 | } |
|---|
| 149 | #endif |
|---|
| 150 | |
|---|
| 151 | #endif |
|---|