spice/server/spice_timer_queue.h
Frediano Ziglio ba175f9be1 channel: add interface parameters to SpiceCoreInterfaceInternal
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>
2016-01-11 16:40:17 +00:00

45 lines
1.7 KiB
C

/*
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/>.
*/
#ifndef _H_SPICE_TIMER_QUEUE
#define _H_SPICE_TIMER_QUEUE
#include <stdint.h>
#include "spice.h"
typedef struct SpiceTimerQueue SpiceTimerQueue;
/* create/destroy a timer queue for the current thread.
* In order to execute the timers functions, spice_timer_queue_cb should be called
* periodically, according to spice_timer_queue_get_timeout_ms */
int spice_timer_queue_create(void);
void spice_timer_queue_destroy(void);
SpiceTimer *spice_timer_queue_add(const SpiceCoreInterfaceInternal *iface,
SpiceTimerFunc func, void *opaque);
void spice_timer_set(SpiceTimer *timer, uint32_t ms);
void spice_timer_cancel(SpiceTimer *timer);
void spice_timer_remove(SpiceTimer *timer);
/* returns the time left till the earliest timer in the queue expires.
* returns (unsigned)-1 if there are no active timers */
unsigned int spice_timer_queue_get_timeout_ms(void);
/* call the timeout callbacks of all the expired timers */
void spice_timer_queue_cb(void);
#endif