summaryrefslogtreecommitdiffstats
path: root/src/pulsecore/aupdate.c
blob: 85b6e00e898fcccd8a5ad13b26590e74385da858 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/***
  This file is part of PulseAudio.

  Copyright 2009 Lennart Poettering

  PulseAudio is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License as
  published by the Free Software Foundation; either version 2.1 of the
  License, or (at your option) any later version.

  PulseAudio is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with PulseAudio; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  USA.
***/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <pulse/xmalloc.h>

#include <pulsecore/semaphore.h>
#include <pulsecore/macro.h>
#include <pulsecore/mutex.h>

#include "aupdate.h"

#define MSB (1U << (sizeof(unsigned)*8U-1))
#define WHICH(n) (!!((n) & MSB))
#define COUNTER(n) ((n) & ~MSB)

struct pa_aupdate {
    pa_atomic_t read_lock;
    pa_mutex *write_lock;
    pa_semaphore *semaphore;
    pa_bool_t swapped;
};

pa_aupdate *pa_aupdate_new(void) {
    pa_aupdate *a;

    a = pa_xnew(pa_aupdate, 1);
    pa_atomic_store(&a->read_lock, 0);
    a->write_lock = pa_mutex_new(FALSE, FALSE);
    a->semaphore = pa_semaphore_new(0);

    return a;
}

void pa_aupdate_free(pa_aupdate *a) {
    pa_assert(a);

    pa_mutex_free(a->write_lock);
    pa_semaphore_free(a->semaphore);

    pa_xfree(a);
}

unsigned pa_aupdate_read_begin(pa_aupdate *a) {
    unsigned n;

    pa_assert(a);

    /* Increase the lock counter */
    n = (unsigned) pa_atomic_inc(&a->read_lock);

    /* When n is 0 we have about 2^31 threads running that all try to
     * access the data at the same time, oh my! */
    pa_assert(COUNTER(n)+1 > 0);

    /* The uppermost bit tells us which data to look at */
    return WHICH(n);
}

void pa_aupdate_read_end(pa_aupdate *a) {
    unsigned n;

    pa_assert(a);

    /* Decrease the lock counter */
    n = (unsigned) pa_atomic_dec(&a->read_lock);

    /* Make sure the counter was valid */
    pa_assert(COUNTER(n) > 0);

    /* Post the semaphore */
    pa_semaphore_post(a->semaphore);
}

unsigned pa_aupdate_write_begin(pa_aupdate *a) {
    unsigned n;

    pa_assert(a);

    pa_mutex_lock(a->write_lock);

    n = (unsigned) pa_atomic_load(&a->read_lock);

    a->swapped = FALSE;

    return !WHICH(n);
}

unsigned pa_aupdate_write_swap(pa_aupdate *a) {
    unsigned n;

    pa_assert(a);

    for (;;) {
        n = (unsigned) pa_atomic_load(&a->read_lock);

        /* If the read counter is > 0 wait; if it is 0 try to swap the lists */
        if (COUNTER(n) > 0)
            pa_semaphore_wait(a->semaphore);
        else if (pa_atomic_cmpxchg(&a->read_lock, (int) n, (int) (n ^ MSB)))
            break;
    }

    a->swapped = TRUE;

    return WHICH(n);
}

void pa_aupdate_write_end(pa_aupdate *a) {
    pa_assert(a);

    if (!a->swapped)
        pa_aupdate_write_swap(a);

    pa_mutex_unlock(a->write_lock);
}