53
53
/* A fully qualified no-operation statement */
54
54
#define CAIRO_MUTEX_NOOP do {/*no-op*/} while (0)
55
/* And one that evaluates it's argument once */
56
#define CAIRO_MUTEX_NOOP1(expr) do { if (expr) ; } while (0)
59
/* Cairo mutex implementation:
61
* Any new mutex implementation needs to do the following:
63
* - Condition on the right header or feature. Headers are
64
* preferred as eg. you still can use win32 mutex implementation
65
* on a win32 system even if you do not compile the win32
68
* - typedef cairo_mutex_t to the proper mutex type on your target
69
* system. Note that you may or may not need to use a pointer,
70
* depending on what kinds of initialization your mutex
71
* implementation supports. No trailing semicolon needed.
72
* You should be able to compile the following snippet (don't try
75
* cairo_mutex_t _cairo_some_mutex;
77
* - #define CAIRO_MUTEX_LOCK(mutex) and CAIRO_MUTEX_UNLOCK(mutex) to
78
* proper statement to lock/unlock the mutex object passed in.
79
* You can (and should) assume that the mutex is already
80
* initialized, and is-not-already-locked/is-locked,
81
* respectively. Use the "do { ... } while (0)" idiom if necessary.
82
* No trailing semicolons are needed (in any macro you define here).
83
* You should be able to compile the following snippet:
85
* cairo_mutex_t _cairo_some_mutex;
88
* CAIRO_MUTEX_LOCK (_cairo_some_mutex);
90
* CAIRO_MUTEX_UNLOCK (_cairo_some_mutex);
92
* - #define CAIRO_MUTEX_NIL_INITIALIZER to something that can
93
* initialize the cairo_mutex_t type you defined. Most of the
94
* time one of 0, NULL, or {} works. At this point
95
* you should be able to compile the following snippet:
97
* cairo_mutex_t _cairo_some_mutex = CAIRO_MUTEX_NIL_INITIALIZER;
100
* CAIRO_MUTEX_LOCK (_cairo_some_mutex);
102
* CAIRO_MUTEX_UNLOCK (_cairo_some_mutex);
104
* - If the above code is not enough to initialize a mutex on
105
* your platform, #define CAIRO_MUTEX_INIT(mutex) to statement
106
* to initialize the mutex (allocate resources, etc). Such that
107
* you should be able to compile AND RUN the following snippet:
109
* cairo_mutex_t _cairo_some_mutex = CAIRO_MUTEX_NIL_INITIALIZER;
111
* CAIRO_MUTEX_INIT (_cairo_some_mutex);
114
* CAIRO_MUTEX_LOCK (_cairo_some_mutex);
116
* CAIRO_MUTEX_UNLOCK (_cairo_some_mutex);
118
* - If you define CAIRO_MUTEX_INIT(mutex), cairo will use it to
119
* initialize all static mutex'es. If for any reason that should
120
* not happen (eg. CAIRO_MUTEX_INIT is just a faster way than
121
* what cairo does using CAIRO_MUTEX_NIL_INITIALIZER), then
122
* #define CAIRO_MUTEX_INITIALIZE() CAIRO_MUTEX_NOOP
124
* - If your system supports freeing a mutex object (deallocating
125
* resources, etc), then #define CAIRO_MUTEX_FINI(mutex) to do
128
* - If you define CAIRO_MUTEX_FINI(mutex), cairo will use it to
129
* define a finalizer function to finalize all static mutex'es.
130
* However, it's up to you to call CAIRO_MUTEX_FINALIZE() at
131
* proper places, eg. when the system is unloading the cairo library.
132
* So, if for any reason finalizing static mutex'es is not needed
133
* (eg. you never call CAIRO_MUTEX_FINALIZE), then
134
* #define CAIRO_MUTEX_FINALIZE() CAIRO_MUTEX_NOOP
136
* - That is all. If for any reason you think the above API is
137
* not enough to implement cairo_mutex_t on your system, please
138
* stop and write to the cairo mailing list about it. DO NOT
139
* poke around cairo-mutex-private.h for possible solutions.
58
142
#if CAIRO_NO_MUTEX
73
157
typedef pthread_mutex_t cairo_mutex_t;
75
# define CAIRO_MUTEX_INITIALIZE() CAIRO_MUTEX_NOOP
76
# define CAIRO_MUTEX_LOCK(name) pthread_mutex_lock (&(name))
77
# define CAIRO_MUTEX_UNLOCK(name) pthread_mutex_unlock (&(name))
78
# define CAIRO_MUTEX_FINI(mutex) pthread_mutex_destroy (mutex)
159
# define CAIRO_MUTEX_LOCK(mutex) pthread_mutex_lock (&(mutex))
160
# define CAIRO_MUTEX_UNLOCK(mutex) pthread_mutex_unlock (&(mutex))
161
# define CAIRO_MUTEX_FINI(mutex) pthread_mutex_destroy (&(mutex))
162
# define CAIRO_MUTEX_FINALIZE() CAIRO_MUTEX_NOOP
79
163
# define CAIRO_MUTEX_NIL_INITIALIZER PTHREAD_MUTEX_INITIALIZER
81
165
#elif HAVE_WINDOWS_H /*******************************************************/
85
169
typedef CRITICAL_SECTION cairo_mutex_t;
87
# define CAIRO_MUTEX_LOCK(name) EnterCriticalSection (&(name))
88
# define CAIRO_MUTEX_UNLOCK(name) LeaveCriticalSection (&(name))
89
# define CAIRO_MUTEX_INIT(mutex) InitializeCriticalSection (mutex)
90
# define CAIRO_MUTEX_FINI(mutex) DeleteCriticalSection (mutex)
171
# define CAIRO_MUTEX_LOCK(mutex) EnterCriticalSection (&(mutex))
172
# define CAIRO_MUTEX_UNLOCK(mutex) LeaveCriticalSection (&(mutex))
173
# define CAIRO_MUTEX_INIT(mutex) InitializeCriticalSection (&(mutex))
174
# define CAIRO_MUTEX_FINI(mutex) DeleteCriticalSection (&(mutex))
91
175
# define CAIRO_MUTEX_NIL_INITIALIZER { NULL, 0, 0, NULL, NULL, 0 }
93
177
#elif defined __OS2__ /******************************************************/
99
183
typedef HMTX cairo_mutex_t;
101
# define CAIRO_MUTEX_LOCK(name) DosRequestMutexSem(name, SEM_INDEFINITE_WAIT)
102
# define CAIRO_MUTEX_UNLOCK(name) DosReleaseMutexSem(name)
103
# define CAIRO_MUTEX_INIT(mutex) DosCreateMutexSem (NULL, mutex, 0L, FALSE)
104
# define CAIRO_MUTEX_FINI(mutex) do { \
105
if (0 != (mutex)) { \
106
DosCloseMutexSem (*(mutex)); \
185
# define CAIRO_MUTEX_LOCK(mutex) DosRequestMutexSem(mutex, SEM_INDEFINITE_WAIT)
186
# define CAIRO_MUTEX_UNLOCK(mutex) DosReleaseMutexSem(mutex)
187
# define CAIRO_MUTEX_INIT(mutex) DosCreateMutexSem (NULL, &(mutex), 0L, FALSE)
188
# define CAIRO_MUTEX_FINI(mutex) DosCloseMutexSem (mutex)
110
189
# define CAIRO_MUTEX_NIL_INITIALIZER 0
112
191
#elif CAIRO_HAS_BEOS_SURFACE /***********************************************/
114
193
typedef BLocker* cairo_mutex_t;
116
# define CAIRO_MUTEX_LOCK(name) (name)->Lock()
117
# define CAIRO_MUTEX_UNLOCK(name) (name)->Unlock()
118
# define CAIRO_MUTEX_INIT(mutex) (*(mutex)) = new BLocker()
119
# define CAIRO_MUTEX_FINI(mutex) delete (*(mutex))
195
# define CAIRO_MUTEX_LOCK(mutex) (mutex)->Lock()
196
# define CAIRO_MUTEX_UNLOCK(mutex) (mutex)->Unlock()
197
# define CAIRO_MUTEX_INIT(mutex) (mutex) = new BLocker()
198
# define CAIRO_MUTEX_FINI(mutex) delete (mutex)
120
199
# define CAIRO_MUTEX_NIL_INITIALIZER NULL
122
201
#else /**********************************************************************/
124
# error "XXX: No mutex implementation found. Define CAIRO_NO_MUTEX to 1" \
125
" to compile cairo without thread-safety support."
203
# error "XXX: No mutex implementation found. Cairo will not work with multiple threads. Define CAIRO_NO_MUTEX to 1 to acknowledge and accept this limitation and compile cairo without thread-safety support."