summaryrefslogtreecommitdiffstats
path: root/server/spice_timer_queue.c
blob: c4f2f6e6d49e242b167a9f8b9b9b1718a2984dc4 (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
   Copyright (C) 2013 Red Hat, Inc.

   This library 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.

   This library 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <pthread.h>
#include "red_common.h"
#include "spice_timer_queue.h"
#include "common/ring.h"

static Ring timer_queue_list;
static int queue_count = 0;
static pthread_mutex_t queue_list_lock = PTHREAD_MUTEX_INITIALIZER;

static void spice_timer_queue_init(void)
{
    ring_init(&timer_queue_list);
}

struct SpiceTimer {
    RingItem link;
    RingItem active_link;

    SpiceTimerFunc func;
    void *opaque;

    SpiceTimerQueue *queue;

    int is_active;
    uint32_t ms;
    uint64_t expiry_time;
};

struct SpiceTimerQueue {
    RingItem link;
    pthread_t thread;
    Ring timers;
    Ring active_timers;
};

static SpiceTimerQueue *spice_timer_queue_find(void)
{
    pthread_t self = pthread_self();
    RingItem *queue_item;

    RING_FOREACH(queue_item, &timer_queue_list) {
         SpiceTimerQueue *queue = SPICE_CONTAINEROF(queue_item, SpiceTimerQueue, link);

         if (pthread_equal(self, queue->thread) != 0) {
            return queue;
         }
    }

    return NULL;
}

static SpiceTimerQueue *spice_timer_queue_find_with_lock(void)
{
    SpiceTimerQueue *queue;

    pthread_mutex_lock(&queue_list_lock);
    queue = spice_timer_queue_find();
    pthread_mutex_unlock(&queue_list_lock);
    return queue;
}

int spice_timer_queue_create(void)
{
    SpiceTimerQueue *queue;

    pthread_mutex_lock(&queue_list_lock);
    if (queue_count == 0) {
        spice_timer_queue_init();
    }

    if (spice_timer_queue_find() != NULL) {
        spice_printerr("timer queue was already created for the thread");
        return FALSE;
    }

    queue = spice_new0(SpiceTimerQueue, 1);
    queue->thread = pthread_self();
    ring_init(&queue->timers);
    ring_init(&queue->active_timers);

    ring_add(&timer_queue_list, &queue->link);
    queue_count++;

    pthread_mutex_unlock(&queue_list_lock);

    return TRUE;
}

void spice_timer_queue_destroy(void)
{
    RingItem *item;
    SpiceTimerQueue *queue;

    pthread_mutex_lock(&queue_list_lock);
    queue = spice_timer_queue_find();

    spice_assert(queue != NULL);

    while ((item = ring_get_head(&queue->timers))) {
        SpiceTimer *timer;

        timer = SPICE_CONTAINEROF(item, SpiceTimer, link);
        spice_timer_remove(timer);
    }

    ring_remove(&queue->link);
    free(queue);
    queue_count--;

    pthread_mutex_unlock(&queue_list_lock);
}

SpiceTimer *spice_timer_queue_add(SpiceTimerFunc func, void *opaque)
{
    SpiceTimer *timer = spice_new0(SpiceTimer, 1);
    SpiceTimerQueue *queue = spice_timer_queue_find_with_lock();

    spice_assert(queue != NULL);

    ring_item_init(&timer->link);
    ring_item_init(&timer->active_link);

    timer->opaque = opaque;
    timer->func = func;
    timer->queue = queue;

    ring_add(&queue->timers, &timer->link);

    return timer;
}

static void _spice_timer_set(SpiceTimer *timer, uint32_t ms, uint64_t now)
{
    RingItem *next_item;
    SpiceTimerQueue *queue;

    if (timer->is_active) {
        spice_timer_cancel(timer);
    }

    queue = timer->queue;
    timer->expiry_time = now + ms;
    timer->ms = ms;

    RING_FOREACH(next_item, &queue->active_timers) {
        SpiceTimer *next_timer = SPICE_CONTAINEROF(next_item, SpiceTimer, active_link);

        if (timer->expiry_time <= next_timer->expiry_time) {
            break;
        }
    }

    if (next_item) {
        ring_add_before(&timer->active_link, next_item);
    } else {
        ring_add_before(&timer->active_link, &queue->active_timers);
    }
    timer->is_active = TRUE;
}

void spice_timer_set(SpiceTimer *timer, uint32_t ms)
{
    struct timespec now;

    spice_assert(pthread_equal(timer->queue->thread, pthread_self()) != 0);

    clock_gettime(CLOCK_MONOTONIC, &now);
    _spice_timer_set(timer, ms,
                     (uint64_t)now.tv_sec * 1000 + (now.tv_nsec / 1000 / 1000));
}

void spice_timer_cancel(SpiceTimer *timer)
{
    spice_assert(pthread_equal(timer->queue->thread, pthread_self()) != 0);

    if (!ring_item_is_linked(&timer->active_link)) {
        spice_assert(!timer->is_active);
        return;
    }

    spice_assert(timer->is_active);
    ring_remove(&timer->active_link);
    timer->is_active = FALSE;
}

void spice_timer_remove(SpiceTimer *timer)
{
    spice_assert(timer->queue);
    spice_assert(ring_item_is_linked(&timer->link));
    spice_assert(pthread_equal(timer->queue->thread, pthread_self()) != 0);

    if (timer->is_active) {
        spice_assert(ring_item_is_linked(&timer->active_link));
        ring_remove(&timer->active_link);
    }
    ring_remove(&timer->link);
    free(timer);
}

unsigned int spice_timer_queue_get_timeout_ms(void)
{
    struct timespec now;
    int64_t now_ms;
    RingItem *head;
    SpiceTimer *head_timer;
    SpiceTimerQueue *queue = spice_timer_queue_find_with_lock();

    spice_assert(queue != NULL);

    if (ring_is_empty(&queue->active_timers)) {
        return -1;
    }

    head = ring_get_head(&queue->active_timers);
    head_timer = SPICE_CONTAINEROF(head, SpiceTimer, active_link);

    clock_gettime(CLOCK_MONOTONIC, &now);
    now_ms = ((int64_t)now.tv_sec * 1000) + (now.tv_nsec / 1000 / 1000);

    return MAX(0, ((int64_t)head_timer->expiry_time - now_ms));
}


void spice_timer_queue_cb(void)
{
    struct timespec now;
    uint64_t now_ms;
    RingItem *head;
    SpiceTimerQueue *queue = spice_timer_queue_find_with_lock();

    spice_assert(queue != NULL);

    if (ring_is_empty(&queue->active_timers)) {
        return;
    }

    clock_gettime(CLOCK_MONOTONIC, &now);
    now_ms = ((uint64_t)now.tv_sec * 1000) + (now.tv_nsec / 1000 / 1000);

    while ((head = ring_get_head(&queue->active_timers))) {
        SpiceTimer *timer = SPICE_CONTAINEROF(head, SpiceTimer, active_link);

        if (timer->expiry_time > now_ms) {
            break;
        } else {
            /* Remove active timer before calling the timer function.
             * Timer function could delete the timer making the timer
             * pointer point to freed data.
             */
            spice_timer_cancel(timer);
            timer->func(timer->opaque);
            /* timer could now be invalid ! */
        }
    }
}