mirror of
https://gitlab.uni-freiburg.de/opensourcevdi/spice
synced 2025-12-26 14:41:25 +00:00
This patch and previous ones want to solve the problem of not having a context in SpiceCoreInterface. SpiceCoreInterface defines a set of callbacks to handle events in spice-server. These callbacks allow to handle timers, watch for file descriptors and send channel events. All these callbacks do not accept a context (usually in C passed as a void* parameter) so it is hard for them to differentiate the interface specified. Unfortunately this structure is used even internally from different contexts for instance every RedWorker thread has a different context. To solve this issue some workarounds are used. Currently for timers a variable depending on the current thread is used while for watches the opaque parameter to pass to the event callback is used as it currently points just to RedChannelClient structure. This however imposes some implicit maintainance problem in the future. What happens for instance if for some reason a timer is registered during worker initialization, run in another thread? What if we decide to register a file descriptor callback for something not a RedChannelClient? Could be that the program will run without any issue till some bytes change and weird things could happen. The implementation of this solution is done implementing an internal "core" interface that has context specific and use it to differentiate the context instead of relying on some other, hard to maintain, detail. Then an adapter structure (name inpired to the adapter pattern) will provide the internal core interface using the external, public, definition (in the future this technique can be used to extend the external interface without breaking the ABI). Signed-off-by: Frediano Ziglio <fziglio@redhat.com> Acked-by: Christophe Fergeau <cfergeau@redhat.com>
268 lines
6.6 KiB
C
268 lines
6.6 KiB
C
/* -*- 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"
|
|
#include "utils.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(const SpiceCoreInterfaceInternal *iface,
|
|
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)
|
|
{
|
|
spice_assert(pthread_equal(timer->queue->thread, pthread_self()) != 0);
|
|
|
|
_spice_timer_set(timer, ms, spice_get_monotonic_time_ms());
|
|
}
|
|
|
|
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)
|
|
{
|
|
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);
|
|
|
|
now_ms = spice_get_monotonic_time_ms();
|
|
|
|
return MAX(0, ((int64_t)head_timer->expiry_time - now_ms));
|
|
}
|
|
|
|
|
|
void spice_timer_queue_cb(void)
|
|
{
|
|
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;
|
|
}
|
|
|
|
now_ms = spice_get_monotonic_time_ms();
|
|
|
|
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 ! */
|
|
}
|
|
}
|
|
}
|