18#ifndef MAGICKCORE_THREAD_PRIVATE_H
19#define MAGICKCORE_THREAD_PRIVATE_H
21#include "MagickCore/cache.h"
22#include "MagickCore/image-private.h"
23#include "MagickCore/resource_.h"
24#include "MagickCore/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;
50static inline int GetMagickNumberThreads(
const Image *source,
51 const Image *destination,
const size_t chunk,
const double factor)
54 destination_type = (CacheType) GetImagePixelCacheType(destination),
55 source_type = (CacheType) GetImagePixelCacheType(source);
58 max_threads = (size_t) GetMagickResourceLimit(ThreadResource),
60 pixels_per_thread = CastDoubleToSizeT(262144.0*factor);
62 if (pixels_per_thread != 0)
64 const size_t total_pixels = chunk*source->columns;
65 number_threads=(total_pixels < pixels_per_thread) ? 1UL :
66 MagickMin(max_threads,(total_pixels+pixels_per_thread-1)/
69 if (((source_type != MemoryCache) && (source_type != MapCache)) ||
70 ((destination_type != MemoryCache) && (destination_type != MapCache)))
71 number_threads=MagickMin(number_threads,4UL);
72 return((
int) number_threads);
75static inline MagickThreadType GetMagickThreadId(
void)
77#if defined(MAGICKCORE_THREAD_SUPPORT)
78 return(pthread_self());
79#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
80 return(GetCurrentThreadId());
86static inline void GetMagickThreadFilename(
const char *filename,
87 char *thread_filename)
93 thread_id[2*
sizeof(id)+1];
101 id=GetMagickThreadId();
102 (void) memcpy(bytes,&
id,
sizeof(
id));
103 for (i=0; i < (ssize_t)
sizeof(bytes); i++)
104 (
void) FormatLocaleString(thread_id+2*i,MagickPathExtent,
"%02x",bytes[i]);
105 thread_id[
sizeof(thread_id)-1]=
'\0';
106 (void) FormatLocaleString(thread_filename,MagickPathExtent,
"%s|%s",thread_id,
110static inline size_t GetMagickThreadSignature(
void)
112#if defined(MAGICKCORE_THREAD_SUPPORT)
123 magick_thread.signature=0UL;
124 magick_thread.id=pthread_self();
125 return(magick_thread.signature);
127#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
128 return((
size_t) GetCurrentThreadId());
130 return((
size_t) getpid());
134static inline MagickBooleanType IsMagickThreadEqual(
const MagickThreadType
id)
136#if defined(MAGICKCORE_THREAD_SUPPORT)
137 if (pthread_equal(
id,pthread_self()) != 0)
139#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
140 if (
id == GetCurrentThreadId())
152static inline size_t GetOpenMPMaximumThreads(
void)
154#if defined(MAGICKCORE_OPENMP_SUPPORT)
155 return((
size_t) omp_get_max_threads());
161static inline int GetOpenMPThreadId(
void)
163#if defined(MAGICKCORE_OPENMP_SUPPORT)
164 return(omp_get_thread_num());
170#if defined(MAGICKCORE_OPENMP_SUPPORT)
171static inline void SetOpenMPMaximumThreads(
const int threads)
173 omp_set_num_threads(threads);
175static inline void SetOpenMPMaximumThreads(
const int magick_unused(threads))
177 magick_unreferenced(threads);
181#if defined(MAGICKCORE_OPENMP_SUPPORT)
182static inline void SetOpenMPMaxActiveLevels(
const int value)
184#if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__MINGW32__)
185 omp_set_nested(value);
187 omp_set_max_active_levels(value ? 2 : 1);
190static inline void SetOpenMPMaxActiveLevels(
const int magick_unused(value))
192 magick_unreferenced(value);
196#if defined(__cplusplus) || defined(c_plusplus)