1 /* Pixman uses some non-standard compiler features. This file ensures
6 * FUNC must be defined to expand to the current function
7 * PIXMAN_EXPORT should be defined to whatever is required to
8 * export functions from a shared library
9 * limits limits for various types must be defined
10 * inline must be defined
11 * force_inline must be defined
13 #if defined (__GNUC__)
14 # define FUNC ((const char*) (__PRETTY_FUNCTION__))
15 #elif defined (__sun) || (defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L)
16 # define FUNC ((const char*) (__func__))
18 # define FUNC ((const char*) ("???"))
22 # define INT16_MIN (-32767-1)
26 # define INT16_MAX (32767)
30 # define INT32_MIN (-2147483647-1)
34 # define INT32_MAX (2147483647)
38 # define UINT32_MIN (0)
42 # define UINT32_MAX (4294967295U)
46 # define M_PI 3.14159265358979323846
50 /* 'inline' is available only in C++ in MSVC */
51 # define inline __inline
52 # define force_inline __forceinline
53 #elif defined __GNUC__ || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590))
54 # define inline __inline__
55 # define force_inline __inline__ __attribute__ ((__always_inline__))
58 # define force_inline inline
63 #if defined(__GNUC__) && __GNUC__ >= 4
64 # define PIXMAN_EXPORT __attribute__ ((visibility("default")))
65 /* Sun Studio 8 visibility */
66 #elif defined(__SUNPRO_C) && (__SUNPRO_C >= 0x550)
67 # define PIXMAN_EXPORT __global
69 # define PIXMAN_EXPORT
73 #if defined(PIXMAN_NO_TLS)
75 # define PIXMAN_DEFINE_THREAD_LOCAL(type, name) \
77 # define PIXMAN_GET_THREAD_LOCAL(name) \
80 #elif defined(TOOLCHAIN_SUPPORTS__THREAD)
82 # define PIXMAN_DEFINE_THREAD_LOCAL(type, name) \
83 static __thread type name
84 # define PIXMAN_GET_THREAD_LOCAL(name) \
87 #elif defined(__MINGW32__) && !defined(__WIN64)
89 /* We can't include <windows.h> as it causes carious clashes with
90 * identifiers in pixman, sigh. So just declare the functions we need
93 extern __stdcall long InterlockedCompareExchange(long volatile *, long, long);
94 #define InterlockedCompareExchangePointer(d,e,c) \
95 (void *)InterlockedCompareExchange((long volatile *)(d),(long)(e),(long)(c))
96 extern __stdcall int TlsAlloc (void);
97 extern __stdcall void *TlsGetValue (unsigned);
98 extern __stdcall int TlsSetValue (unsigned, void *);
99 extern __stdcall void *CreateMutexA(void *, int, char *);
100 extern __stdcall int CloseHandle(void *);
101 extern __stdcall unsigned WaitForSingleObject (void *, unsigned);
102 extern __stdcall int ReleaseMutex (void *);
104 # define PIXMAN_DEFINE_THREAD_LOCAL(type, name) \
105 static volatile int tls_ ## name ## _initialized = 0; \
106 static void *tls_ ## name ## _mutex = NULL; \
107 static unsigned tls_ ## name ## _index; \
110 tls_ ## name ## _alloc (void) \
112 type *value = calloc (1, sizeof (type)); \
114 TlsSetValue (tls_ ## name ## _index, value); \
118 static force_inline type * \
119 tls_ ## name ## _get (void) \
122 if (!tls_ ## name ## _initialized) \
124 if (!tls_ ## name ## _mutex) \
126 void *mutex = CreateMutexA (NULL, 0, NULL); \
127 if (InterlockedCompareExchangePointer ( \
128 &tls_ ## name ## _mutex, mutex, NULL) != NULL) \
130 CloseHandle (mutex); \
133 WaitForSingleObject (tls_ ## name ## _mutex, 0xFFFFFFFF); \
134 if (!tls_ ## name ## _initialized) \
136 tls_ ## name ## _index = TlsAlloc (); \
137 tls_ ## name ## _initialized = 1; \
139 ReleaseMutex (tls_ ## name ## _mutex); \
141 if (tls_ ## name ## _index == 0xFFFFFFFF) \
143 value = TlsGetValue (tls_ ## name ## _index); \
145 value = tls_ ## name ## _alloc (); \
149 # define PIXMAN_GET_THREAD_LOCAL(name) \
150 tls_ ## name ## _get ()
152 #elif defined(_MSC_VER)
154 # define PIXMAN_DEFINE_THREAD_LOCAL(type, name) \
155 static __declspec(thread) type name
156 # define PIXMAN_GET_THREAD_LOCAL(name) \
159 #elif defined(HAVE_PTHREAD_SETSPECIFIC)
163 # define PIXMAN_DEFINE_THREAD_LOCAL(type, name) \
164 static pthread_once_t tls_ ## name ## _once_control = PTHREAD_ONCE_INIT; \
165 static pthread_key_t tls_ ## name ## _key; \
168 tls_ ## name ## _make_key (void) \
170 pthread_key_create (&tls_ ## name ## _key, NULL); \
174 tls_ ## name ## _alloc (void) \
176 type *value = calloc (1, sizeof (type)); \
178 pthread_setspecific (tls_ ## name ## _key, value); \
182 static force_inline type * \
183 tls_ ## name ## _get (void) \
185 type *value = NULL; \
186 if (pthread_once (&tls_ ## name ## _once_control, \
187 tls_ ## name ## _make_key) == 0) \
189 value = pthread_getspecific (tls_ ## name ## _key); \
191 value = tls_ ## name ## _alloc (); \
196 # define PIXMAN_GET_THREAD_LOCAL(name) \
197 tls_ ## name ## _get ()
201 # error "Unknown thread local support for this system. Pixman will not work with multiple threads. Define PIXMAN_NO_TLS to acknowledge and accept this limitation and compile pixman without thread-safety support."