thread-private.h

Go to the documentation of this file.
00001 /*
00002   Copyright 1999-2019 ImageMagick Studio LLC, a non-profit organization
00003   dedicated to making software imaging solutions freely available.
00004 
00005   You may not use this file except in compliance with the License.  You may
00006   obtain a copy of the License at
00007 
00008     https://imagemagick.org/script/license.php
00009 
00010   Unless required by applicable law or agreed to in writing, software
00011   distributed under the License is distributed on an "AS IS" BASIS,
00012   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013   See the License for the specific language governing permissions and
00014   limitations under the License.
00015 
00016   MagickCore private methods for internal threading.
00017 */
00018 #ifndef MAGICKCORE_THREAD_PRIVATE_H
00019 #define MAGICKCORE_THREAD_PRIVATE_H
00020 
00021 #include "MagickCore/cache.h"
00022 #include "MagickCore/image-private.h"
00023 #include "MagickCore/resource_.h"
00024 #include "MagickCore/thread_.h"
00025 
00026 #if defined(__cplusplus) || defined(c_plusplus)
00027 extern "C" {
00028 #endif
00029 
00030 /*
00031   Number of threads bounded by the amount of work and any thread resource limit.
00032   The limit is 2 if the pixel cache type is not memory or memory-mapped.
00033 */
00034 #define magick_number_threads(source,destination,chunk,multithreaded) \
00035   num_threads((multithreaded) == 0 ? 1 : \
00036     ((GetImagePixelCacheType(source) != MemoryCache) && \
00037      (GetImagePixelCacheType(source) != MapCache)) || \
00038     ((GetImagePixelCacheType(destination) != MemoryCache) && \
00039      (GetImagePixelCacheType(destination) != MapCache)) ? \
00040     MagickMax(MagickMin(GetMagickResourceLimit(ThreadResource),2),1) : \
00041     MagickMax(MagickMin((ssize_t) GetMagickResourceLimit(ThreadResource),(ssize_t) (chunk)/64),1))
00042 
00043 #if defined(__clang__) || (__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ > 10))
00044 #define MagickCachePrefetch(address,mode,locality) \
00045   __builtin_prefetch(address,mode,locality)
00046 #else
00047 #define MagickCachePrefetch(address,mode,locality) \
00048   magick_unreferenced(address); \
00049   magick_unreferenced(mode); \
00050   magick_unreferenced(locality);
00051 #endif
00052 
00053 #if defined(MAGICKCORE_THREAD_SUPPORT)
00054   typedef pthread_mutex_t MagickMutexType;
00055 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
00056   typedef CRITICAL_SECTION MagickMutexType;
00057 #else
00058   typedef size_t MagickMutexType;
00059 #endif
00060 
00061 static inline MagickThreadType GetMagickThreadId(void)
00062 {
00063 #if defined(MAGICKCORE_THREAD_SUPPORT)
00064   return(pthread_self());
00065 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
00066   return(GetCurrentThreadId());
00067 #else
00068   return(getpid());
00069 #endif
00070 }
00071 
00072 static inline size_t GetMagickThreadSignature(void)
00073 {
00074 #if defined(MAGICKCORE_THREAD_SUPPORT)
00075   {
00076     union
00077     {
00078       pthread_t
00079         id;
00080 
00081       size_t
00082         signature;
00083     } magick_thread;
00084 
00085     magick_thread.signature=0UL;
00086     magick_thread.id=pthread_self();
00087     return(magick_thread.signature);
00088   }
00089 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
00090   return((size_t) GetCurrentThreadId());
00091 #else
00092   return((size_t) getpid());
00093 #endif
00094 }
00095 
00096 static inline MagickBooleanType IsMagickThreadEqual(const MagickThreadType id)
00097 {
00098 #if defined(MAGICKCORE_THREAD_SUPPORT)
00099   if (pthread_equal(id,pthread_self()) != 0)
00100     return(MagickTrue);
00101 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
00102   if (id == GetCurrentThreadId())
00103     return(MagickTrue);
00104 #else
00105   if (id == getpid())
00106     return(MagickTrue);
00107 #endif
00108   return(MagickFalse);
00109 }
00110 
00111 /*
00112   Lightweight OpenMP methods.
00113 */
00114 static inline size_t GetOpenMPMaximumThreads(void)
00115 {
00116 #if defined(MAGICKCORE_OPENMP_SUPPORT)
00117   return(omp_get_max_threads());
00118 #else
00119   return(1);
00120 #endif
00121 }
00122 
00123 static inline int GetOpenMPThreadId(void)
00124 {
00125 #if defined(MAGICKCORE_OPENMP_SUPPORT)
00126   return(omp_get_thread_num());
00127 #else
00128   return(0);
00129 #endif
00130 }
00131 
00132 static inline void SetOpenMPMaximumThreads(const int threads)
00133 {
00134 #if defined(MAGICKCORE_OPENMP_SUPPORT)
00135   omp_set_num_threads(threads);
00136 #else
00137   (void) threads;
00138 #endif
00139 }
00140 
00141 static inline void SetOpenMPNested(const int value)
00142 {
00143 #if defined(MAGICKCORE_OPENMP_SUPPORT)
00144   omp_set_nested(value);
00145 #else
00146   (void) value;
00147 #endif
00148 }
00149 
00150 #if defined(__cplusplus) || defined(c_plusplus)
00151 }
00152 #endif
00153 
00154 #endif

Generated on 27 Nov 2019 for MagickCore by  doxygen 1.6.1