]> code.delx.au - pulseaudio/blob - src/pulsecore/aupdate.c
memtrap,aupdate: split atomic update from memtrap into seperate aupdate framework
[pulseaudio] / src / pulsecore / aupdate.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2009 Lennart Poettering
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
10
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <pulse/xmalloc.h>
27
28 #include <pulsecore/semaphore.h>
29 #include <pulsecore/macro.h>
30 #include <pulsecore/mutex.h>
31
32 #include "aupdate.h"
33
34 #define MSB (1U << (sizeof(unsigned)*8U-1))
35 #define WHICH(n) (!!((n) & MSB))
36 #define COUNTER(n) ((n) & ~MSB)
37
38 struct pa_aupdate {
39 pa_atomic_t read_lock;
40 pa_mutex *write_lock;
41 pa_semaphore *semaphore;
42 };
43
44 pa_aupdate *pa_aupdate_new(void) {
45 pa_aupdate *a;
46
47 a = pa_xnew(pa_aupdate, 1);
48 pa_atomic_store(&a->read_lock, 0);
49 a->write_lock = pa_mutex_new(FALSE, FALSE);
50 a->semaphore = pa_semaphore_new(0);
51
52 return a;
53 }
54
55 void pa_aupdate_free(pa_aupdate *a) {
56 pa_assert(a);
57
58 pa_mutex_free(a->write_lock);
59 pa_semaphore_free(a->semaphore);
60
61 pa_xfree(a);
62 }
63
64 unsigned pa_aupdate_read_begin(pa_aupdate *a) {
65 unsigned n;
66
67 pa_assert(a);
68
69 /* Increase the lock counter */
70 n = (unsigned) pa_atomic_inc(&a->read_lock);
71
72 /* When n is 0 we have about 2^31 threads running that all try to
73 * access the data at the same time, oh my! */
74 pa_assert(COUNTER(n)+1 > 0);
75
76 /* The uppermost bit tells us which data to look at */
77 return WHICH(n);
78 }
79
80 void pa_aupdate_read_end(pa_aupdate *a) {
81 unsigned n;
82
83 pa_assert(a);
84
85 /* Decrease the lock counter */
86 n = (unsigned) pa_atomic_dec(&a->read_lock);
87
88 /* Make sure the counter was valid */
89 pa_assert(COUNTER(n) > 0);
90
91 /* Post the semaphore */
92 pa_semaphore_post(a->semaphore);
93 }
94
95 unsigned pa_aupdate_write_begin(pa_aupdate *a) {
96 unsigned n;
97
98 pa_assert(a);
99
100 pa_mutex_lock(a->write_lock);
101
102 n = (unsigned) pa_atomic_load(&a->read_lock);
103
104 return !WHICH(n);
105 }
106
107 unsigned pa_aupdate_write_swap(pa_aupdate *a) {
108 unsigned n;
109
110 pa_assert(a);
111
112 for (;;) {
113 n = (unsigned) pa_atomic_load(&a->read_lock);
114
115 /* If the read counter is > 0 wait; if it is 0 try to swap the lists */
116 if (COUNTER(n) > 0)
117 pa_semaphore_wait(a->semaphore);
118 else if (pa_atomic_cmpxchg(&a->read_lock, (int) n, (int) (n ^ MSB)))
119 break;
120 }
121
122 return WHICH(n);
123 }
124
125 void pa_aupdate_write_end(pa_aupdate *a) {
126 pa_assert(a);
127
128 pa_mutex_unlock(a->write_lock);
129 }