summaryrefslogtreecommitdiff
path: root/docs/thread-primitives
blob: 160bef34a4fbfd4fdd9d421bb24197d0493678ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
There are only two types of systems we care about: Windows, and
variations of pthreads. Other systems can be dealt with by people who
care about them.

Windows
-------

On Windows, mutexes cannot be initialized statically, but on the other
hand, we are guaranteed to have atomic primitives available without a
mutex. This means mutexes can be initialized dynamically with
something like this:

#define PIXMAN_DEFINE_LOCK(name)					\
    static CRITICAL_SECTION  __ ## name ## _critical_section;		\
    static CRITICAL_SECTION *__ ## name ## _ptr;

#define PIXMAN_LOCK(name)						\
    if (!PIXMAN_ATOMIC_POINTER_GET ((void **)&__ ## name ## _ptr))	\
    {									\
	_pixman_win32_initialize_critical_section (			\
	    &__ ## name ## _ptr,					\
	    &__ ## name ## _critical_section);				\
    }									\
									\
    EnterCriticalSection(&__ ## name ## _critical_section);

#define PIXMAN_UNLOCK(name)						\
    LeaveCriticalSection(&__ ## name ## _critical_section);

Where _pixman_win32_initialize_critical_section() would be responsible
for initializing the variables. (And not doing anything if they are
already set).


Pthreads
--------

On pthreads, we may or may not have atomic primitives, but mutexes can
always be initialized statically. That means we can fake atomic
primitives with mutexes if necessary.

Ie,. something like

#define PIXMAN_DEFINE_LOCK(name)					\
    pthread_mutex_t __ ## name ## __lock = pthread_mutex_initializer;

#ifdef BUILTIN_ATOMICS
    ...
#else
    /* fake atomic primitives with mutexes */
#endif

Therefore, we can guarantee availability of the following macros:

PIXMAN_ATOMIC_POINTER_GET(void **)
PIXMAN_ATOMIC_POINTER_SET(void **, void *value)
PIXMAN_ATOMIC_POINTER_CMPXCHG()
PIXMAN_ATOMIC_INT_GET(atomic *a)
PIXMAN_ATOMIC_INT_SET(atomic *a, int value)
PIXMAN_ATOMIC_INT_DEC_AND_TEST(atomic *a)
PIXMAN_ATOMIC_INT_CMPXCHG(atomic, ...)
PIXMAN_MUTEX_DEFINE_STATIC(name)
PIXMAN_MUTEX_LOCK(name)
PIXMAN_MUTEX_UNLOCK(name)