MagickCore  7.1.0
Convert, Edit, Or Compose Bitmap Images
thread-private.h
1 /*
2  Copyright @ 2003 ImageMagick Studio LLC, a non-profit organization
3  dedicated to making software imaging solutions freely available.
4 
5  You may not use this file except in compliance with the License. You may
6  obtain a copy of the License at
7 
8  https://imagemagick.org/script/license.php
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 
16  MagickCore private methods for internal threading.
17 */
18 #ifndef MAGICKCORE_THREAD_PRIVATE_H
19 #define MAGICKCORE_THREAD_PRIVATE_H
20 
21 #include "MagickCore/cache.h"
22 #include "MagickCore/image-private.h"
23 #include "MagickCore/resource_.h"
24 #include "MagickCore/thread_.h"
25 
26 #if defined(__cplusplus) || defined(c_plusplus)
27 extern "C" {
28 #endif
29 
30 #define magick_number_threads(source,destination,chunk,multithreaded) \
31  num_threads(GetMagickNumberThreads(source,destination,chunk,multithreaded))
32 #if defined(__clang__) || (__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ > 10))
33 #define MagickCachePrefetch(address,mode,locality) \
34  __builtin_prefetch(address,mode,locality)
35 #else
36 #define MagickCachePrefetch(address,mode,locality) \
37  magick_unreferenced(address); \
38  magick_unreferenced(mode); \
39  magick_unreferenced(locality);
40 #endif
41 
42 #if defined(MAGICKCORE_THREAD_SUPPORT)
43  typedef pthread_mutex_t MagickMutexType;
44 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
45  typedef CRITICAL_SECTION MagickMutexType;
46 #else
47  typedef size_t MagickMutexType;
48 #endif
49 
50 static inline int GetMagickNumberThreads(const Image *source,
51  const Image *destination,const size_t chunk,int multithreaded)
52 {
53  const CacheType
54  destination_type = (CacheType) GetImagePixelCacheType(destination),
55  source_type = (CacheType) GetImagePixelCacheType(source);
56 
57  int
58  number_threads;
59 
60  /*
61  Return number of threads dependent on cache type and work load.
62  */
63  if (multithreaded == 0)
64  return(1);
65  if (((source_type != MemoryCache) && (source_type != MapCache)) ||
66  ((destination_type != MemoryCache) && (destination_type != MapCache)))
67  number_threads=(int) MagickMin(GetMagickResourceLimit(ThreadResource),2);
68  else
69  number_threads=(int) MagickMin((ssize_t)
70  GetMagickResourceLimit(ThreadResource),(ssize_t) (chunk)/64);
71  return(MagickMax(number_threads,1));
72 }
73 
74 static inline MagickThreadType GetMagickThreadId(void)
75 {
76 #if defined(MAGICKCORE_THREAD_SUPPORT)
77  return(pthread_self());
78 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
79  return(GetCurrentThreadId());
80 #else
81  return(getpid());
82 #endif
83 }
84 
85 static inline size_t GetMagickThreadSignature(void)
86 {
87 #if defined(MAGICKCORE_THREAD_SUPPORT)
88  {
89  union
90  {
91  pthread_t
92  id;
93 
94  size_t
95  signature;
96  } magick_thread;
97 
98  magick_thread.signature=0UL;
99  magick_thread.id=pthread_self();
100  return(magick_thread.signature);
101  }
102 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
103  return((size_t) GetCurrentThreadId());
104 #else
105  return((size_t) getpid());
106 #endif
107 }
108 
109 static inline MagickBooleanType IsMagickThreadEqual(const MagickThreadType id)
110 {
111 #if defined(MAGICKCORE_THREAD_SUPPORT)
112  if (pthread_equal(id,pthread_self()) != 0)
113  return(MagickTrue);
114 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
115  if (id == GetCurrentThreadId())
116  return(MagickTrue);
117 #else
118  if (id == getpid())
119  return(MagickTrue);
120 #endif
121  return(MagickFalse);
122 }
123 
124 /*
125  Lightweight OpenMP methods.
126 */
127 static inline size_t GetOpenMPMaximumThreads(void)
128 {
129 #if defined(MAGICKCORE_OPENMP_SUPPORT)
130  return(omp_get_max_threads());
131 #else
132  return(1);
133 #endif
134 }
135 
136 static inline int GetOpenMPThreadId(void)
137 {
138 #if defined(MAGICKCORE_OPENMP_SUPPORT)
139  return(omp_get_thread_num());
140 #else
141  return(0);
142 #endif
143 }
144 
145 static inline void SetOpenMPMaximumThreads(const int threads)
146 {
147 #if defined(MAGICKCORE_OPENMP_SUPPORT)
148  omp_set_num_threads(threads);
149 #else
150  magick_unreferenced(threads);
151 #endif
152 }
153 
154 static inline void SetOpenMPNested(const int value)
155 {
156 #if defined(MAGICKCORE_OPENMP_SUPPORT)
157  omp_set_nested(value);
158 #else
159  magick_unreferenced(value);
160 #endif
161 }
162 
163 #if defined(__cplusplus) || defined(c_plusplus)
164 }
165 #endif
166 
167 #endif
Definition: image.h:152