18 #ifndef MAGICKCORE_THREAD_PRIVATE_H 19 #define MAGICKCORE_THREAD_PRIVATE_H 21 #include "magick/cache.h" 22 #include "magick/image-private.h" 23 #include "magick/resource_.h" 24 #include "magick/thread_.h" 26 #if defined(__cplusplus) || defined(c_plusplus) 30 #define magick_number_threads(source,destination,chunk,factor) \ 31 num_threads(GetMagickNumberThreads(source,destination,chunk,factor)) 32 #if defined(__clang__) || (__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ > 10)) 33 #define MagickCachePrefetch(address,mode,locality) \ 34 __builtin_prefetch(address,mode,locality) 36 #define MagickCachePrefetch(address,mode,locality) \ 37 magick_unreferenced(address); \ 38 magick_unreferenced(mode); \ 39 magick_unreferenced(locality); 42 #if defined(MAGICKCORE_THREAD_SUPPORT) 43 typedef pthread_mutex_t MagickMutexType;
44 #elif defined(MAGICKCORE_WINDOWS_SUPPORT) 45 typedef CRITICAL_SECTION MagickMutexType;
47 typedef size_t MagickMutexType;
50 static inline int GetMagickNumberThreads(
const Image *source,
51 const Image *destination,
const size_t chunk,
const int factor)
54 destination_type = (CacheType) GetImagePixelCacheType(destination),
55 source_type = (CacheType) GetImagePixelCacheType(source);
58 max_threads = (size_t) GetMagickResourceLimit(ThreadResource),
60 workload_factor = 64UL << factor;
65 number_threads=(chunk <= workload_factor) ? 1 :
66 (chunk >= (workload_factor << 6)) ? max_threads :
67 1+(chunk-workload_factor)*(max_threads-1)/(((workload_factor << 6))-1);
71 if (((source_type != MemoryCache) && (source_type != MapCache)) ||
72 ((destination_type != MemoryCache) && (destination_type != MapCache)))
73 number_threads=MagickMin(number_threads,4);
74 return((
int) number_threads);
77 static inline MagickThreadType GetMagickThreadId(
void)
79 #if defined(MAGICKCORE_THREAD_SUPPORT) 80 return(pthread_self());
81 #elif defined(MAGICKCORE_WINDOWS_SUPPORT) 82 return(GetCurrentThreadId());
88 static inline void GetMagickThreadFilename(
const char *filename,
89 char *thread_filename)
95 thread_id[2*
sizeof(id)+1];
103 id=GetMagickThreadId();
104 (void) memcpy(bytes,&
id,
sizeof(
id));
105 for (i=0; i < (ssize_t)
sizeof(bytes); i++)
106 (
void) FormatLocaleString(thread_id+2*i,MagickPathExtent,
"%02x",bytes[i]);
107 thread_id[
sizeof(thread_id)-1]=
'\0';
108 (void) FormatLocaleString(thread_filename,MagickPathExtent,
"%s|%s",thread_id,
112 static inline size_t GetMagickThreadSignature(
void)
114 #if defined(MAGICKCORE_THREAD_SUPPORT) 125 magick_thread.signature=0UL;
126 magick_thread.id=pthread_self();
127 return(magick_thread.signature);
129 #elif defined(MAGICKCORE_WINDOWS_SUPPORT) 130 return((
size_t) GetCurrentThreadId());
132 return((
size_t) getpid());
136 static inline MagickBooleanType IsMagickThreadEqual(
const MagickThreadType
id)
138 #if defined(MAGICKCORE_THREAD_SUPPORT) 139 if (pthread_equal(
id,pthread_self()) != 0)
141 #elif defined(MAGICKCORE_WINDOWS_SUPPORT) 142 if (
id == GetCurrentThreadId())
154 static inline size_t GetOpenMPMaximumThreads(
void)
156 #if defined(MAGICKCORE_OPENMP_SUPPORT) 157 return((
size_t) omp_get_max_threads());
163 static inline int GetOpenMPThreadId(
void)
165 #if defined(MAGICKCORE_OPENMP_SUPPORT) 166 return(omp_get_thread_num());
172 #if defined(MAGICKCORE_OPENMP_SUPPORT) 173 static inline void SetOpenMPMaximumThreads(
const int threads)
175 omp_set_num_threads(threads);
177 static inline void SetOpenMPMaximumThreads(
const int magick_unused(threads))
179 magick_unreferenced(threads);
183 #if defined(MAGICKCORE_OPENMP_SUPPORT) 184 static inline void SetOpenMPMaxActiveLevels(
const int value)
186 #if defined(MAGICKCORE_WINDOWS_SUPPORT) 187 omp_set_nested(value);
189 omp_set_max_active_levels(value ? 2 : 1);
192 static inline void SetOpenMPMaxActiveLevels(
const int magick_unused(value))
194 magick_unreferenced(value);
198 #if defined(__cplusplus) || defined(c_plusplus)