summaryrefslogtreecommitdiff
path: root/src/cache/threaded_waits.c
blob: 162646633404df38c32169e51d78d4554efbbe5c (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
138
139
140
141
142
#include "threaded_waits.h"

void cch_threaded_waits_init(struct cch_threaded_waits *thw)
{
    thw->disabled = false;
    nn_mutex_init(&thw->lock);
    al_array_init(thw->active);
}

static bool wait_range_satisfied(struct cch_backing *backing, struct cch_wait *wait)
{
    if (wait->end < 0 || (backing->size > 0 && wait->end > backing->size)) {
        return true;
    }
    struct cch_range *range;
    al_array_foreach_ptr(backing->available, i, range) {
        if (wait->start >= range->start && range->end >= wait->end) {
            return true;
        }
    }
    return false;
}

bool cch_threaded_wait_for_range(struct cch_threaded_waits *thw, struct cch_backing *backing,
    struct cch_wait *wait)
{
    off_t size = backing->get_size_if_known(backing);

    // Lock before checking wait_range_satisfied() in case the result changes
    // before wait is possibly added to active.
    nn_mutex_lock(&thw->lock);

    backing->lock(backing);
    bool satisfied = size >= 0 && wait_range_satisfied(backing, wait);
    backing->unlock(backing);

    nn_mutex_lock(&wait->lock);
    bool canceled = thw->disabled || wait->disabled;
    if (!canceled && !satisfied) {
        al_array_push(thw->active, wait);
        // Unlock handler to wait.
        nn_mutex_unlock(&thw->lock);
        nn_cond_wait(&wait->cond, &wait->lock);
        // A successful wait will have been removed before signaling.
        if (wait->disabled) {
            canceled = true;
            // Re-lock to remove.
            nn_mutex_lock(&thw->lock);
            al_array_remove(thw->active, wait);
        }
    }
    nn_mutex_unlock(&wait->lock);

    if (canceled || satisfied) {
        // Else, we already unlocked to wait.
        nn_mutex_unlock(&thw->lock);
    }

    return !canceled;
}

void cch_threaded_waits_signal_anys(struct cch_threaded_waits *thw)
{
    nn_mutex_lock(&thw->lock);
    struct cch_wait *wait;
    al_array_foreach_rev(thw->active, i, wait) {
        if (wait->end < 0) {
            // This will be considered a successful wait.
            al_array_remove_at(thw->active, i);
            nn_mutex_lock(&wait->lock);
            nn_cond_signal(&wait->cond);
            nn_mutex_unlock(&wait->lock);
        }
    }
    nn_mutex_unlock(&thw->lock);
}

void cch_threaded_waits_evaluate(struct cch_threaded_waits *thw, struct cch_backing *backing)
{
    nn_mutex_lock(&thw->lock);
    backing->lock(backing);
    struct cch_wait *wait;
    al_array_foreach_rev(thw->active, i, wait) {
        if (wait_range_satisfied(backing, wait)) {
            al_array_remove_at(thw->active, i);
            nn_mutex_lock(&wait->lock);
            nn_cond_signal(&wait->cond);
            nn_mutex_unlock(&wait->lock);
        }
    }
    backing->unlock(backing);
    nn_mutex_unlock(&thw->lock);
}

void cch_threaded_wait_disable(struct cch_wait *wait)
{
    // If this wait is in active, cond_is_waiting() will be true.
    // So, signaling cond while holding the lock here will ensure it's removed before
    // unlocking. This enables synchronization of successive disable() -> enable() calls.
    nn_mutex_lock(&wait->lock);
    wait->disabled = true;
    if (nn_cond_is_waiting(&wait->cond)) {
        nn_cond_signal(&wait->cond);
    }
    nn_mutex_unlock(&wait->lock);
}

void cch_threaded_wait_enable(struct cch_wait *wait)
{
    nn_mutex_lock(&wait->lock);
    wait->disabled = false;
    nn_mutex_unlock(&wait->lock);
}

void cch_threaded_waits_disable_all(struct cch_threaded_waits *thw)
{
    nn_mutex_lock(&thw->lock);

    if (thw->disabled) {
        nn_mutex_unlock(&thw->lock);
        return;
    }

    // Disallow any further waits.
    thw->disabled = true;

    struct cch_wait *wait;
    al_array_foreach_rev(thw->active, i, wait) {
        nn_mutex_lock(&wait->lock);
        wait->disabled = true;
        nn_cond_signal(&wait->cond);
        nn_mutex_unlock(&wait->lock);
    }

    nn_mutex_unlock(&thw->lock);
}

void cch_threaded_waits_close(struct cch_threaded_waits *thw)
{
    nn_mutex_destroy(&thw->lock);
    al_array_free(thw->active);
}