1 /* cairo - a vector graphics library with display and print output
3 * Copyright © 2002 University of Southern California
4 * Copyright © 2005,2007 Red Hat, Inc.
5 * Copyright © 2007 Mathias Hasselmann
7 * This library is free software; you can redistribute it and/or
8 * modify it either under the terms of the GNU Lesser General Public
9 * License version 2.1 as published by the Free Software Foundation
10 * (the "LGPL") or, at your option, under the terms of the Mozilla
11 * Public License Version 1.1 (the "MPL"). If you do not alter this
12 * notice, a recipient may use your version of this file under either
13 * the MPL or the LGPL.
15 * You should have received a copy of the LGPL along with this library
16 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * You should have received a copy of the MPL along with this library
19 * in the file COPYING-MPL-1.1
21 * The contents of this file are subject to the Mozilla Public License
22 * Version 1.1 (the "License"); you may not use this file except in
23 * compliance with the License. You may obtain a copy of the License at
24 * http://www.mozilla.org/MPL/
26 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
27 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
28 * the specific language governing rights and limitations.
30 * The Original Code is the cairo graphics library.
32 * The Initial Developer of the Original Code is University of Southern
36 * Carl D. Worth <cworth@cworth.org>
37 * Mathias Hasselmann <mathias.hasselmann@gmx.de>
38 * Behdad Esfahbod <behdad@behdad.org>
41 #ifndef CAIRO_MUTEX_IMPL_PRIVATE_H
42 #define CAIRO_MUTEX_IMPL_PRIVATE_H
54 /* A fully qualified no-operation statement */
55 #define CAIRO_MUTEX_IMPL_NOOP do {/*no-op*/} while (0)
56 /* And one that evaluates it's argument once */
57 #define CAIRO_MUTEX_IMPL_NOOP1(expr) do { if (expr) ; } while (0)
60 /* Cairo mutex implementation:
62 * Any new mutex implementation needs to do the following:
64 * - Condition on the right header or feature. Headers are
65 * preferred as eg. you still can use win32 mutex implementation
66 * on a win32 system even if you do not compile the win32
69 * - typedef #cairo_mutex_impl_t to the proper mutex type on your target
70 * system. Note that you may or may not need to use a pointer,
71 * depending on what kinds of initialization your mutex
72 * implementation supports. No trailing semicolon needed.
73 * You should be able to compile the following snippet (don't try
77 * cairo_mutex_impl_t _cairo_some_mutex;
80 * - #define %CAIRO_MUTEX_IMPL_<NAME> 1 with suitable name for your platform. You
81 * can later use this symbol in cairo-system.c.
83 * - #define CAIRO_MUTEX_IMPL_LOCK(mutex) and CAIRO_MUTEX_IMPL_UNLOCK(mutex) to
84 * proper statement to lock/unlock the mutex object passed in.
85 * You can (and should) assume that the mutex is already
86 * initialized, and is-not-already-locked/is-locked,
87 * respectively. Use the "do { ... } while (0)" idiom if necessary.
88 * No trailing semicolons are needed (in any macro you define here).
89 * You should be able to compile the following snippet:
92 * cairo_mutex_impl_t _cairo_some_mutex;
95 * CAIRO_MUTEX_IMPL_LOCK (_cairo_some_mutex);
97 * CAIRO_MUTEX_IMPL_UNLOCK (_cairo_some_mutex);
100 * - #define %CAIRO_MUTEX_IMPL_NIL_INITIALIZER to something that can
101 * initialize the #cairo_mutex_impl_t type you defined. Most of the
102 * time one of 0, %NULL, or {} works. At this point
103 * you should be able to compile the following snippet:
106 * cairo_mutex_impl_t _cairo_some_mutex = CAIRO_MUTEX_IMPL_NIL_INITIALIZER;
109 * CAIRO_MUTEX_IMPL_LOCK (_cairo_some_mutex);
111 * CAIRO_MUTEX_IMPL_UNLOCK (_cairo_some_mutex);
114 * - If the above code is not enough to initialize a mutex on
115 * your platform, #define CAIRO_MUTEX_IMPL_INIT(mutex) to statement
116 * to initialize the mutex (allocate resources, etc). Such that
117 * you should be able to compile AND RUN the following snippet:
120 * cairo_mutex_impl_t _cairo_some_mutex = CAIRO_MUTEX_IMPL_NIL_INITIALIZER;
122 * CAIRO_MUTEX_IMPL_INIT (_cairo_some_mutex);
125 * CAIRO_MUTEX_IMPL_LOCK (_cairo_some_mutex);
127 * CAIRO_MUTEX_IMPL_UNLOCK (_cairo_some_mutex);
130 * - If you define CAIRO_MUTEX_IMPL_INIT(mutex), cairo will use it to
131 * initialize all static mutex'es. If for any reason that should
132 * not happen (eg. %CAIRO_MUTEX_IMPL_INIT is just a faster way than
133 * what cairo does using %CAIRO_MUTEX_IMPL_NIL_INITIALIZER), then
135 * #define CAIRO_MUTEX_IMPL_INITIALIZE() CAIRO_MUTEX_IMPL_NOOP
138 * - If your system supports freeing a mutex object (deallocating
139 * resources, etc), then #define CAIRO_MUTEX_IMPL_FINI(mutex) to do
142 * - If you define CAIRO_MUTEX_IMPL_FINI(mutex), cairo will use it to
143 * define a finalizer function to finalize all static mutex'es.
144 * However, it's up to you to call CAIRO_MUTEX_IMPL_FINALIZE() at
145 * proper places, eg. when the system is unloading the cairo library.
146 * So, if for any reason finalizing static mutex'es is not needed
147 * (eg. you never call CAIRO_MUTEX_IMPL_FINALIZE()), then
149 * #define CAIRO_MUTEX_IMPL_FINALIZE() CAIRO_MUTEX_IMPL_NOOP
152 * - That is all. If for any reason you think the above API is
153 * not enough to implement #cairo_mutex_impl_t on your system, please
154 * stop and write to the cairo mailing list about it. DO NOT
155 * poke around cairo-mutex-private.h for possible solutions.
162 typedef int cairo_mutex_impl_t
;
164 # define CAIRO_MUTEX_IMPL_NO 1
165 # define CAIRO_MUTEX_IMPL_INITIALIZE() CAIRO_MUTEX_IMPL_NOOP
166 # define CAIRO_MUTEX_IMPL_LOCK(mutex) CAIRO_MUTEX_IMPL_NOOP1(mutex)
167 # define CAIRO_MUTEX_IMPL_UNLOCK(mutex) CAIRO_MUTEX_IMPL_NOOP1(mutex)
168 # define CAIRO_MUTEX_IMPL_NIL_INITIALIZER 0
170 #elif HAVE_PTHREAD_H /*******************************************************/
172 # include <pthread.h>
174 typedef pthread_mutex_t cairo_mutex_impl_t
;
176 # define CAIRO_MUTEX_IMPL_PTHREAD 1
178 /* expose all mutexes to the validator */
179 # define CAIRO_MUTEX_IMPL_INIT(mutex) pthread_mutex_init (&(mutex), NULL)
181 # define CAIRO_MUTEX_IMPL_LOCK(mutex) pthread_mutex_lock (&(mutex))
182 # define CAIRO_MUTEX_IMPL_UNLOCK(mutex) pthread_mutex_unlock (&(mutex))
184 # define CAIRO_MUTEX_IS_LOCKED(mutex) LOCKDEP_IS_LOCKED (&(mutex))
185 # define CAIRO_MUTEX_IS_UNLOCKED(mutex) LOCKDEP_IS_UNLOCKED (&(mutex))
187 # define CAIRO_MUTEX_IMPL_FINI(mutex) pthread_mutex_destroy (&(mutex))
189 # define CAIRO_MUTEX_IMPL_FINALIZE() CAIRO_MUTEX_IMPL_NOOP
191 # define CAIRO_MUTEX_IMPL_NIL_INITIALIZER PTHREAD_MUTEX_INITIALIZER
193 #elif defined(HAVE_WINDOWS_H) || defined(_MSC_VER) /*************************/
195 # include <windows.h>
197 typedef CRITICAL_SECTION cairo_mutex_impl_t
;
199 # define CAIRO_MUTEX_IMPL_WIN32 1
200 # define CAIRO_MUTEX_IMPL_LOCK(mutex) EnterCriticalSection (&(mutex))
201 # define CAIRO_MUTEX_IMPL_UNLOCK(mutex) LeaveCriticalSection (&(mutex))
202 # define CAIRO_MUTEX_IMPL_INIT(mutex) InitializeCriticalSection (&(mutex))
203 # define CAIRO_MUTEX_IMPL_FINI(mutex) DeleteCriticalSection (&(mutex))
204 # define CAIRO_MUTEX_IMPL_NIL_INITIALIZER { NULL, 0, 0, NULL, NULL, 0 }
206 #elif defined __OS2__ /******************************************************/
212 typedef HMTX cairo_mutex_impl_t
;
214 # define CAIRO_MUTEX_IMPL_OS2 1
215 # define CAIRO_MUTEX_IMPL_LOCK(mutex) DosRequestMutexSem(mutex, SEM_INDEFINITE_WAIT)
216 # define CAIRO_MUTEX_IMPL_UNLOCK(mutex) DosReleaseMutexSem(mutex)
217 # define CAIRO_MUTEX_IMPL_INIT(mutex) DosCreateMutexSem (NULL, &(mutex), 0L, FALSE)
218 # define CAIRO_MUTEX_IMPL_FINI(mutex) DosCloseMutexSem (mutex)
219 # define CAIRO_MUTEX_IMPL_NIL_INITIALIZER 0
221 #elif CAIRO_HAS_BEOS_SURFACE /***********************************************/
223 typedef BLocker
* cairo_mutex_impl_t
;
225 # define CAIRO_MUTEX_IMPL_BEOS 1
226 # define CAIRO_MUTEX_IMPL_LOCK(mutex) (mutex)->Lock()
227 # define CAIRO_MUTEX_IMPL_UNLOCK(mutex) (mutex)->Unlock()
228 # define CAIRO_MUTEX_IMPL_INIT(mutex) (mutex) = new BLocker()
229 # define CAIRO_MUTEX_IMPL_FINI(mutex) delete (mutex)
230 # define CAIRO_MUTEX_IMPL_NIL_INITIALIZER NULL
232 #else /**********************************************************************/
234 # 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."