thread-private.h

Go to the documentation of this file.
00001 /*
00002   Copyright 1999-2020 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 "magick/cache.h"
00022 #include "magick/image-private.h"
00023 #include "magick/resource_.h"
00024 #include "magick/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 #endif
00049 
00050 #if defined(MAGICKCORE_THREAD_SUPPORT)
00051   typedef pthread_mutex_t MagickMutexType;
00052 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
00053   typedef CRITICAL_SECTION MagickMutexType;
00054 #else
00055   typedef size_t MagickMutexType;
00056 #endif
00057 
00058 static inline MagickThreadType GetMagickThreadId(void)
00059 {
00060 #if defined(MAGICKCORE_THREAD_SUPPORT)
00061   return(pthread_self());
00062 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
00063   return(GetCurrentThreadId());
00064 #else
00065   return(getpid());
00066 #endif
00067 }
00068 
00069 static inline size_t GetMagickThreadSignature(void)
00070 {
00071 #if defined(MAGICKCORE_THREAD_SUPPORT)
00072   {
00073     union
00074     {
00075       pthread_t
00076         id;
00077 
00078       size_t
00079         signature;
00080     } magick_thread;
00081 
00082     magick_thread.signature=0UL;
00083     magick_thread.id=pthread_self();
00084     return(magick_thread.signature);
00085   }
00086 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
00087   return((size_t) GetCurrentThreadId());
00088 #else
00089   return((size_t) getpid());
00090 #endif
00091 }
00092 
00093 static inline MagickBooleanType IsMagickThreadEqual(const MagickThreadType id)
00094 {
00095 #if defined(MAGICKCORE_THREAD_SUPPORT)
00096   if (pthread_equal(id,pthread_self()) != 0)
00097     return(MagickTrue);
00098 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
00099   if (id == GetCurrentThreadId())
00100     return(MagickTrue);
00101 #else
00102   if (id == getpid())
00103     return(MagickTrue);
00104 #endif
00105   return(MagickFalse);
00106 }
00107 
00108 /*
00109   Lightweight OpenMP methods.
00110 */
00111 static inline size_t GetOpenMPMaximumThreads(void)
00112 {
00113 #if defined(MAGICKCORE_OPENMP_SUPPORT)
00114   return(omp_get_max_threads());
00115 #else
00116   return(1);
00117 #endif
00118 }
00119 
00120 static inline int GetOpenMPThreadId(void)
00121 {
00122 #if defined(MAGICKCORE_OPENMP_SUPPORT)
00123   return(omp_get_thread_num());
00124 #else
00125   return(0);
00126 #endif
00127 }
00128 
00129 static inline void SetOpenMPMaximumThreads(const int threads)
00130 {
00131 #if defined(MAGICKCORE_OPENMP_SUPPORT)
00132   omp_set_num_threads(threads);
00133 #else
00134   (void) threads;
00135 #endif
00136 }
00137 
00138 static inline void SetOpenMPNested(const int value)
00139 {
00140 #if defined(MAGICKCORE_OPENMP_SUPPORT)
00141   omp_set_nested(value);
00142 #else
00143   (void) value;
00144 #endif
00145 }
00146 
00147 #if defined(__cplusplus) || defined(c_plusplus)
00148 }
00149 #endif
00150 
00151 #endif

Generated on 21 Sep 2020 for MagickCore by  doxygen 1.6.1