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 int factor)
53#define WorkLoadFactor (64UL << factor)
56 destination_type = (CacheType) GetImagePixelCacheType(destination),
57 source_type = (CacheType) GetImagePixelCacheType(source);
65 number_threads=(int) MagickMax(MagickMin(chunk/WorkLoadFactor,
66 GetMagickResourceLimit(ThreadResource)),1);
67 if (((source_type != MemoryCache) && (source_type != MapCache)) ||
68 ((destination_type != MemoryCache) && (destination_type != MapCache)))
69 number_threads=MagickMin(number_threads,2);
70 return(number_threads);
73static inline MagickThreadType GetMagickThreadId(
void)
75#if defined(MAGICKCORE_THREAD_SUPPORT)
76 return(pthread_self());
77#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
78 return(GetCurrentThreadId());
84static inline void GetMagickThreadFilename(
const char *filename,
85 char *thread_filename)
91 thread_id[2*
sizeof(id)+1];
99 id=GetMagickThreadId();
100 (void) memcpy(bytes,&
id,
sizeof(
id));
101 for (i=0; i < (ssize_t)
sizeof(bytes); i++)
102 (
void) FormatLocaleString(thread_id+2*i,MagickPathExtent,
"%02x",bytes[i]);
103 thread_id[
sizeof(thread_id)-1]=
'\0';
104 (void) FormatLocaleString(thread_filename,MagickPathExtent,
"%s|%s",thread_id,
108static inline size_t GetMagickThreadSignature(
void)
110#if defined(MAGICKCORE_THREAD_SUPPORT)
121 magick_thread.signature=0UL;
122 magick_thread.id=pthread_self();
123 return(magick_thread.signature);
125#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
126 return((
size_t) GetCurrentThreadId());
128 return((
size_t) getpid());
132static inline MagickBooleanType IsMagickThreadEqual(
const MagickThreadType
id)
134#if defined(MAGICKCORE_THREAD_SUPPORT)
135 if (pthread_equal(
id,pthread_self()) != 0)
137#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
138 if (
id == GetCurrentThreadId())
150static inline size_t GetOpenMPMaximumThreads(
void)
152#if defined(MAGICKCORE_OPENMP_SUPPORT)
153 return((
size_t) omp_get_max_threads());
159static inline int GetOpenMPThreadId(
void)
161#if defined(MAGICKCORE_OPENMP_SUPPORT)
162 return(omp_get_thread_num());
168#if defined(MAGICKCORE_OPENMP_SUPPORT)
169static inline void SetOpenMPMaximumThreads(
const int threads)
171 omp_set_num_threads(threads);
173static inline void SetOpenMPMaximumThreads(
const int magick_unused(threads))
175 magick_unreferenced(threads);
179#if defined(MAGICKCORE_OPENMP_SUPPORT)
180static inline void SetOpenMPNested(
const int value)
182 omp_set_nested(value);
184static inline void SetOpenMPNested(
const int magick_unused(value))
186 magick_unreferenced(value);
190#if defined(__cplusplus) || defined(c_plusplus)