summaryrefslogtreecommitdiffstats
path: root/src/once.c
blob: 5dc9764eddb109e97efc577af3be63cb95a41631 (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
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <pthread.h>
#include <assert.h>

#include "once.h"
#include "mutex.h"
#include "malloc.h"
#include "macro.h"

static sa_mutex_t *global_mutex; 
static pthread_once_t global_mutex_once = PTHREAD_ONCE_INIT;

static void global_mutex_once_func(void) { 
    global_mutex = sa_mutex_new(0);
} 

int sa_once(sa_once_t *control, sa_once_func_t func) {
    int r;
    
    assert(control); 
    assert(func); 
    
    /* Create the global mutex */
    sa_assert_success(pthread_once(&global_mutex_once, global_mutex_once_func));

    if (!global_mutex)
        return -1;

    r = 0;
    
    /* Create the local mutex */
    sa_mutex_lock(global_mutex); 
    if (!control->mutex) {
        if (!(control->mutex = sa_mutex_new(1)))
            r = -1;
    }
    sa_mutex_unlock(global_mutex);

    if (r)
        return -1;
    
    /* Execute function */
    sa_mutex_lock(control->mutex);
    if (!control->once_value) {
        control->once_value = 1;
        func();
    }
    sa_mutex_unlock(control->mutex); 

    /* Caveat: We have to make sure that the once func has completed
     * before returning, even if the once func is not actually
     * executed by us. Hence the awkward locking. */

    return 0;
}