MagickCore  6.7.5
thread-private.h
Go to the documentation of this file.
00001 /*
00002   Copyright 1999-2012 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.
00006   obtain a copy of the License at
00007 
00008     http://www.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 #if defined(__cplusplus) || defined(c_plusplus)
00022 extern "C" {
00023 #endif
00024 
00025 #include <MagickCore/thread_.h>
00026 
00027 #if (__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR > 10))
00028 #define MagickCachePrefetch(address,mode,locality) \
00029   __builtin_prefetch(address,mode,locality)
00030 #else
00031 #define MagickCachePrefetch(address,mode,locality)
00032 #endif
00033 
00034 #define omp_throttle(factor)  num_threads(omp_get_max_threads() >> \
00035    (factor) == 0 ? 1 : omp_get_max_threads() >> (factor))
00036 
00037 #if defined(MAGICKCORE_THREAD_SUPPORT)
00038   typedef pthread_mutex_t MagickMutexType;
00039 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
00040   typedef CRITICAL_SECTION MagickMutexType;
00041 #else
00042   typedef size_t MagickMutexType;
00043 #endif
00044 
00045 static inline MagickThreadType GetMagickThreadId(void)
00046 {
00047 #if defined(MAGICKCORE_THREAD_SUPPORT)
00048   return(pthread_self());
00049 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
00050   return(GetCurrentThreadId());
00051 #else
00052   return(getpid());
00053 #endif
00054 }
00055 
00056 static inline size_t GetMagickThreadSignature(void)
00057 {
00058 #if defined(MAGICKCORE_THREAD_SUPPORT)
00059   {
00060     union
00061     {
00062       pthread_t
00063         id;
00064 
00065       size_t
00066         signature;
00067     } magick_thread;
00068 
00069     magick_thread.signature=0UL;
00070     magick_thread.id=pthread_self();
00071     return(magick_thread.signature);
00072   }
00073 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
00074   return((size_t) GetCurrentThreadId());
00075 #else
00076   return((size_t) getpid());
00077 #endif
00078 }
00079 
00080 static inline MagickBooleanType IsMagickThreadEqual(const MagickThreadType id)
00081 {
00082 #if defined(MAGICKCORE_THREAD_SUPPORT)
00083   if (pthread_equal(id,pthread_self()) != 0)
00084     return(MagickTrue);
00085 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
00086   if (id == GetCurrentThreadId())
00087     return(MagickTrue);
00088 #else
00089   if (id == getpid())
00090     return(MagickTrue);
00091 #endif
00092   return(MagickFalse);
00093 }
00094 
00095 /*
00096   Lightweight OpenMP methods.
00097 */
00098 static inline size_t GetOpenMPMaximumThreads(void)
00099 {
00100   static size_t
00101     maximum_threads = 1;
00102 
00103 #if defined(MAGICKCORE_OPENMP_SUPPORT)
00104   {
00105     ssize_t
00106       threads;
00107 
00108     threads=omp_get_max_threads();
00109     if (threads > (ssize_t) maximum_threads)
00110       maximum_threads=threads;
00111   }
00112 #endif
00113   return(maximum_threads);
00114 }
00115 
00116 static inline int GetOpenMPThreadId(void)
00117 {
00118 #if defined(MAGICKCORE_OPENMP_SUPPORT)
00119   return(omp_get_thread_num());
00120 #else
00121   return(0);
00122 #endif
00123 }
00124 
00125 static inline void SetOpenMPMaximumThreads(const int threads)
00126 {
00127 #if defined(MAGICKCORE_OPENMP_SUPPORT)
00128   omp_set_num_threads(threads);
00129 #else
00130   (void) threads;
00131 #endif
00132 }
00133 
00134 static inline void SetOpenMPNested(const int value)
00135 {
00136 #if defined(MAGICKCORE_OPENMP_SUPPORT)
00137   omp_set_nested(value);
00138 #else
00139   (void) value;
00140 #endif
00141 }
00142 
00143 #if defined(__cplusplus) || defined(c_plusplus)
00144 }
00145 #endif
00146 
00147 #endif