Remove core_public and core_interface_adapter globals usage

Avoid not constant globals.

We started encapsulating all global state into RedsState however
there are still some global variable. This patch remove the
core_public global variable.

To implement this a new SpiceCoreInterface *public_interface
field is added to SpiceCoreInterfaceInternal and the
SpiceCoreInterfaceInternal* argument is passed to callbacks to
understand which external interface to use.

Signed-off-by: Frediano Ziglio <fziglio@redhat.com>
Acked-by: Jonathon Jongsma <jjongsma@redhat.com>
This commit is contained in:
Frediano Ziglio 2016-11-15 16:49:09 +00:00
parent de27dc034d
commit 1b4e8855ea
7 changed files with 124 additions and 90 deletions

View File

@ -61,7 +61,8 @@ dummy_channel_init(DummyChannel *self)
}
// TODO: red_worker can use this one
static void dummy_watch_update_mask(SpiceWatch *watch, int event_mask)
static void dummy_watch_update_mask(const SpiceCoreInterfaceInternal *iface,
SpiceWatch *watch, int event_mask)
{
}
@ -71,7 +72,7 @@ static SpiceWatch *dummy_watch_add(const SpiceCoreInterfaceInternal *iface,
return NULL; // apparently allowed?
}
static void dummy_watch_remove(SpiceWatch *watch)
static void dummy_watch_remove(const SpiceCoreInterfaceInternal *iface, SpiceWatch *watch)
{
}

View File

@ -56,7 +56,8 @@ static gboolean timer_func(gpointer user_data)
return FALSE;
}
static void timer_cancel(SpiceTimer *timer)
static void timer_cancel(const SpiceCoreInterfaceInternal *iface,
SpiceTimer *timer)
{
if (timer->source) {
g_source_destroy(timer->source);
@ -65,9 +66,10 @@ static void timer_cancel(SpiceTimer *timer)
}
}
static void timer_start(SpiceTimer *timer, uint32_t ms)
static void timer_start(const SpiceCoreInterfaceInternal *iface,
SpiceTimer *timer, uint32_t ms)
{
timer_cancel(timer);
timer_cancel(iface, timer);
timer->source = g_timeout_source_new(ms);
spice_assert(timer->source != NULL);
@ -77,9 +79,10 @@ static void timer_start(SpiceTimer *timer, uint32_t ms)
g_source_attach(timer->source, timer->context);
}
static void timer_remove(SpiceTimer *timer)
static void timer_remove(const SpiceCoreInterfaceInternal *iface,
SpiceTimer *timer)
{
timer_cancel(timer);
timer_cancel(iface, timer);
spice_assert(timer->source == NULL);
free(timer);
}
@ -127,7 +130,8 @@ static gboolean watch_func(GIOChannel *source, GIOCondition condition,
return TRUE;
}
static void watch_update_mask(SpiceWatch *watch, int event_mask)
static void watch_update_mask(const SpiceCoreInterfaceInternal *iface,
SpiceWatch *watch, int event_mask)
{
if (watch->source) {
g_source_destroy(watch->source);
@ -157,14 +161,15 @@ static SpiceWatch *watch_add(const SpiceCoreInterfaceInternal *iface,
watch->func = func;
watch->opaque = opaque;
watch_update_mask(watch, event_mask);
watch_update_mask(iface, watch, event_mask);
return watch;
}
static void watch_remove(SpiceWatch *watch)
static void watch_remove(const SpiceCoreInterfaceInternal *iface,
SpiceWatch *watch)
{
watch_update_mask(watch, 0);
watch_update_mask(iface, watch, 0);
spice_assert(watch->source == NULL);
g_io_channel_unref(watch->channel);

View File

@ -104,7 +104,7 @@ static void red_channel_client_start_ping_timer(RedChannelClient *rcc, uint32_t
rcc->priv->latency_monitor.state = PING_STATE_TIMER;
core = red_channel_get_core_interface(rcc->priv->channel);
core->timer_start(rcc->priv->latency_monitor.timer, timeout);
core->timer_start(core, rcc->priv->latency_monitor.timer, timeout);
}
static void red_channel_client_cancel_ping_timer(RedChannelClient *rcc)
@ -119,7 +119,7 @@ static void red_channel_client_cancel_ping_timer(RedChannelClient *rcc)
}
core = red_channel_get_core_interface(rcc->priv->channel);
core->timer_cancel(rcc->priv->latency_monitor.timer);
core->timer_cancel(core, rcc->priv->latency_monitor.timer);
rcc->priv->latency_monitor.state = PING_STATE_NONE;
}
@ -413,7 +413,7 @@ void red_channel_client_on_out_block(void *opaque)
rcc->priv->send_data.blocked = TRUE;
core = red_channel_get_core_interface(rcc->priv->channel);
core->watch_update_mask(rcc->priv->stream->watch,
core->watch_update_mask(core, rcc->priv->stream->watch,
SPICE_WATCH_EVENT_READ |
SPICE_WATCH_EVENT_WRITE);
}
@ -579,7 +579,7 @@ void red_channel_client_on_out_msg_done(void *opaque)
if (rcc->priv->send_data.blocked) {
SpiceCoreInterfaceInternal *core = red_channel_get_core_interface(rcc->priv->channel);
rcc->priv->send_data.blocked = FALSE;
core->watch_update_mask(rcc->priv->stream->watch,
core->watch_update_mask(core, rcc->priv->stream->watch,
SPICE_WATCH_EVENT_READ);
}
@ -723,8 +723,8 @@ static void red_channel_client_connectivity_timer(void *opaque)
} else {
monitor->state = CONNECTIVITY_STATE_CONNECTED;
}
core->timer_start(rcc->priv->connectivity_monitor.timer,
rcc->priv->connectivity_monitor.timeout);
core->timer_start(core, rcc->priv->connectivity_monitor.timer,
rcc->priv->connectivity_monitor.timeout);
} else {
uint32_t type, id;
g_object_get(rcc->priv->channel,
@ -766,7 +766,7 @@ void red_channel_client_start_connectivity_monitoring(RedChannelClient *rcc, uin
core, red_channel_client_connectivity_timer, rcc);
rcc->priv->connectivity_monitor.timeout = timeout_ms;
if (!red_client_during_migrate_at_target(rcc->priv->client)) {
core->timer_start(rcc->priv->connectivity_monitor.timer,
core->timer_start(core, rcc->priv->connectivity_monitor.timer,
rcc->priv->connectivity_monitor.timeout);
}
}
@ -957,7 +957,7 @@ static void red_channel_client_seamless_migration_done(RedChannelClient *rcc)
}
if (rcc->priv->connectivity_monitor.timer) {
SpiceCoreInterfaceInternal *core = red_channel_get_core_interface(rcc->priv->channel);
core->timer_start(rcc->priv->connectivity_monitor.timer,
core->timer_start(core, rcc->priv->connectivity_monitor.timer,
rcc->priv->connectivity_monitor.timeout);
}
}
@ -980,11 +980,11 @@ void red_channel_client_default_migrate(RedChannelClient *rcc)
SpiceCoreInterfaceInternal *core = red_channel_get_core_interface(rcc->priv->channel);
if (rcc->priv->latency_monitor.timer) {
red_channel_client_cancel_ping_timer(rcc);
core->timer_remove(rcc->priv->latency_monitor.timer);
core->timer_remove(core, rcc->priv->latency_monitor.timer);
rcc->priv->latency_monitor.timer = NULL;
}
if (rcc->priv->connectivity_monitor.timer) {
core->timer_remove(rcc->priv->connectivity_monitor.timer);
core->timer_remove(core, rcc->priv->connectivity_monitor.timer);
rcc->priv->connectivity_monitor.timer = NULL;
}
red_channel_client_pipe_add_type(rcc, RED_PIPE_ITEM_TYPE_MIGRATE);
@ -1002,7 +1002,7 @@ void red_channel_client_shutdown(RedChannelClient *rcc)
{
if (rcc->priv->stream && !rcc->priv->stream->shutdown) {
SpiceCoreInterfaceInternal *core = red_channel_get_core_interface(rcc->priv->channel);
core->watch_remove(rcc->priv->stream->watch);
core->watch_remove(core, rcc->priv->stream->watch);
rcc->priv->stream->watch = NULL;
shutdown(rcc->priv->stream->socket, SHUT_RDWR);
rcc->priv->stream->shutdown = TRUE;
@ -1235,7 +1235,7 @@ void red_channel_client_push(RedChannelClient *rcc)
&& rcc->priv->stream->watch) {
SpiceCoreInterfaceInternal *core;
core = red_channel_get_core_interface(rcc->priv->channel);
core->watch_update_mask(rcc->priv->stream->watch,
core->watch_update_mask(core, rcc->priv->stream->watch,
SPICE_WATCH_EVENT_READ);
}
rcc->priv->during_send = FALSE;
@ -1463,7 +1463,7 @@ static inline gboolean prepare_pipe_add(RedChannelClient *rcc, RedPipeItem *item
if (g_queue_is_empty(&rcc->priv->pipe) && rcc->priv->stream->watch) {
SpiceCoreInterfaceInternal *core;
core = red_channel_get_core_interface(rcc->priv->channel);
core->watch_update_mask(rcc->priv->stream->watch,
core->watch_update_mask(core, rcc->priv->stream->watch,
SPICE_WATCH_EVENT_READ | SPICE_WATCH_EVENT_WRITE);
}
return TRUE;
@ -1638,15 +1638,15 @@ static void red_channel_client_default_disconnect(RedChannelClient *rcc)
type, id);
red_channel_client_pipe_clear(rcc);
if (rcc->priv->stream->watch) {
core->watch_remove(rcc->priv->stream->watch);
core->watch_remove(core, rcc->priv->stream->watch);
rcc->priv->stream->watch = NULL;
}
if (rcc->priv->latency_monitor.timer) {
core->timer_remove(rcc->priv->latency_monitor.timer);
core->timer_remove(core, rcc->priv->latency_monitor.timer);
rcc->priv->latency_monitor.timer = NULL;
}
if (rcc->priv->connectivity_monitor.timer) {
core->timer_remove(rcc->priv->connectivity_monitor.timer);
core->timer_remove(core, rcc->priv->connectivity_monitor.timer);
rcc->priv->connectivity_monitor.timer = NULL;
}
red_channel_remove_client(channel, rcc);

View File

@ -45,17 +45,30 @@ typedef struct SpiceCoreInterfaceInternal SpiceCoreInterfaceInternal;
struct SpiceCoreInterfaceInternal {
SpiceTimer *(*timer_add)(const SpiceCoreInterfaceInternal *iface, SpiceTimerFunc func, void *opaque);
void (*timer_start)(SpiceTimer *timer, uint32_t ms);
void (*timer_cancel)(SpiceTimer *timer);
void (*timer_remove)(SpiceTimer *timer);
void (*timer_start)(const SpiceCoreInterfaceInternal *iface, SpiceTimer *timer, uint32_t ms);
void (*timer_cancel)(const SpiceCoreInterfaceInternal *iface, SpiceTimer *timer);
void (*timer_remove)(const SpiceCoreInterfaceInternal *iface, SpiceTimer *timer);
SpiceWatch *(*watch_add)(const SpiceCoreInterfaceInternal *iface, int fd, int event_mask, SpiceWatchFunc func, void *opaque);
void (*watch_update_mask)(SpiceWatch *watch, int event_mask);
void (*watch_remove)(SpiceWatch *watch);
void (*watch_update_mask)(const SpiceCoreInterfaceInternal *iface, SpiceWatch *watch, int event_mask);
void (*watch_remove)(const SpiceCoreInterfaceInternal *iface, SpiceWatch *watch);
void (*channel_event)(int event, SpiceChannelEventInfo *info);
void (*channel_event)(const SpiceCoreInterfaceInternal *iface, int event, SpiceChannelEventInfo *info);
GMainContext *main_context;
/* This structure is an adapter that allows us to use the same API to
* implement the core interface in a couple different ways. The first
* method is to use a public SpiceCoreInterface provided to us by the
* library user (for example, qemu). The second method is to implement the
* core interface functions using the glib event loop. In order to avoid
* global variables, each method needs to store additional data in this
* adapter structure. Instead of using a generic void* data parameter, we
* provide a bit more type-safety by using a union to store the type of
* data needed by each implementation.
*/
union {
GMainContext *main_context;
SpiceCoreInterface *public_interface;
};
};
extern const SpiceCoreInterfaceInternal event_loop_core;

View File

@ -140,7 +140,7 @@ struct RedsState {
SpiceCharDeviceInstance *vdagent;
SpiceMigrateInstance *migration_interface;
SpiceCoreInterfaceInternal *core;
SpiceCoreInterfaceInternal core;
GList *qxl_instances;
MainDispatcher *main_dispatcher;
};

View File

@ -79,52 +79,49 @@
static void reds_client_monitors_config(RedsState *reds, VDAgentMonitorsConfig *monitors_config);
static gboolean reds_use_client_monitors_config(RedsState *reds);
static SpiceCoreInterface *core_public = NULL;
static SpiceTimer *adapter_timer_add(const SpiceCoreInterfaceInternal *iface, SpiceTimerFunc func, void *opaque)
{
return core_public->timer_add(func, opaque);
return iface->public_interface->timer_add(func, opaque);
}
static void adapter_timer_start(SpiceTimer *timer, uint32_t ms)
static void adapter_timer_start(const SpiceCoreInterfaceInternal *iface, SpiceTimer *timer, uint32_t ms)
{
core_public->timer_start(timer, ms);
iface->public_interface->timer_start(timer, ms);
}
static void adapter_timer_cancel(SpiceTimer *timer)
static void adapter_timer_cancel(const SpiceCoreInterfaceInternal *iface, SpiceTimer *timer)
{
core_public->timer_cancel(timer);
iface->public_interface->timer_cancel(timer);
}
static void adapter_timer_remove(SpiceTimer *timer)
static void adapter_timer_remove(const SpiceCoreInterfaceInternal *iface, SpiceTimer *timer)
{
core_public->timer_remove(timer);
iface->public_interface->timer_remove(timer);
}
static SpiceWatch *adapter_watch_add(const SpiceCoreInterfaceInternal *iface,
int fd, int event_mask, SpiceWatchFunc func, void *opaque)
{
return core_public->watch_add(fd, event_mask, func, opaque);
return iface->public_interface->watch_add(fd, event_mask, func, opaque);
}
static void adapter_watch_update_mask(SpiceWatch *watch, int event_mask)
static void adapter_watch_update_mask(const SpiceCoreInterfaceInternal *iface, SpiceWatch *watch, int event_mask)
{
core_public->watch_update_mask(watch, event_mask);
iface->public_interface->watch_update_mask(watch, event_mask);
}
static void adapter_watch_remove(SpiceWatch *watch)
static void adapter_watch_remove(const SpiceCoreInterfaceInternal *iface, SpiceWatch *watch)
{
core_public->watch_remove(watch);
iface->public_interface->watch_remove(watch);
}
static void adapter_channel_event(int event, SpiceChannelEventInfo *info)
static void adapter_channel_event(const SpiceCoreInterfaceInternal *iface, int event, SpiceChannelEventInfo *info)
{
if (core_public->base.minor_version >= 3 && core_public->channel_event != NULL)
core_public->channel_event(event, info);
if (iface->public_interface->base.minor_version >= 3 && iface->public_interface->channel_event != NULL)
iface->public_interface->channel_event(event, info);
}
static SpiceCoreInterfaceInternal core_interface_adapter = {
static const SpiceCoreInterfaceInternal core_interface_adapter = {
.timer_add = adapter_timer_add,
.timer_start = adapter_timer_start,
.timer_cancel = adapter_timer_cancel,
@ -323,7 +320,7 @@ static ChannelSecurityOptions *reds_find_channel_security(RedsState *reds, int i
void reds_handle_channel_event(RedsState *reds, int event, SpiceChannelEventInfo *info)
{
reds->core->channel_event(event, info);
reds->core.channel_event(&reds->core, event, info);
if (event == SPICE_CHANNEL_EVENT_DISCONNECTED) {
free(info);
@ -502,7 +499,7 @@ static void reds_mig_cleanup(RedsState *reds)
reds->mig_inprogress = FALSE;
reds->mig_wait_connect = FALSE;
reds->mig_wait_disconnect = FALSE;
reds->core->timer_cancel(reds->mig_timer);
reds_core_timer_cancel(reds, reds->mig_timer);
reds_mig_cleanup_wait_disconnect(reds);
}
}
@ -2980,7 +2977,7 @@ static void reds_mig_started(RedsState *reds)
reds->mig_inprogress = TRUE;
reds->mig_wait_connect = TRUE;
reds->core->timer_start(reds->mig_timer, MIGRATE_TIMEOUT);
reds_core_timer_start(reds, reds->mig_timer, MIGRATE_TIMEOUT);
}
static void reds_mig_fill_wait_disconnect(RedsState *reds)
@ -2996,7 +2993,7 @@ static void reds_mig_fill_wait_disconnect(RedsState *reds)
}
reds->mig_wait_connect = FALSE;
reds->mig_wait_disconnect = TRUE;
reds->core->timer_start(reds->mig_timer, MIGRATE_TIMEOUT);
reds_core_timer_start(reds, reds->mig_timer, MIGRATE_TIMEOUT);
}
static void reds_mig_cleanup_wait_disconnect(RedsState *reds)
@ -3426,21 +3423,21 @@ static int do_spice_init(RedsState *reds, SpiceCoreInterface *core_interface)
spice_warning("bad core interface version");
goto err;
}
core_public = core_interface;
reds->core = &core_interface_adapter;
reds->core = core_interface_adapter;
reds->core.public_interface = core_interface;
reds->listen_socket = -1;
reds->secure_listen_socket = -1;
reds->agent_dev = red_char_device_vdi_port_new(reds);
reds_update_agent_properties(reds);
reds->clients = NULL;
reds->main_dispatcher = main_dispatcher_new(reds, reds->core);
reds->main_dispatcher = main_dispatcher_new(reds, &reds->core);
reds->channels = NULL;
reds->mig_target_clients = NULL;
reds->char_devices = NULL;
reds->mig_wait_disconnect_clients = NULL;
reds->vm_running = TRUE; /* for backward compatibility */
if (!(reds->mig_timer = reds->core->timer_add(reds->core, migrate_timeout, reds))) {
if (!(reds->mig_timer = reds->core.timer_add(&reds->core, migrate_timeout, reds))) {
spice_error("migration timer create failed");
}
@ -4222,7 +4219,7 @@ spice_wan_compression_t reds_get_zlib_glz_state(const RedsState *reds)
SpiceCoreInterfaceInternal* reds_get_core_interface(RedsState *reds)
{
return reds->core;
return &reds->core;
}
SpiceWatch *reds_core_watch_add(RedsState *reds,
@ -4231,10 +4228,9 @@ SpiceWatch *reds_core_watch_add(RedsState *reds,
void *opaque)
{
g_return_val_if_fail(reds != NULL, NULL);
g_return_val_if_fail(reds->core != NULL, NULL);
g_return_val_if_fail(reds->core->watch_add != NULL, NULL);
g_return_val_if_fail(reds->core.watch_add != NULL, NULL);
return reds->core->watch_add(reds->core, fd, event_mask, func, opaque);
return reds->core.watch_add(&reds->core, fd, event_mask, func, opaque);
}
void reds_core_watch_update_mask(RedsState *reds,
@ -4242,19 +4238,17 @@ void reds_core_watch_update_mask(RedsState *reds,
int event_mask)
{
g_return_if_fail(reds != NULL);
g_return_if_fail(reds->core != NULL);
g_return_if_fail(reds->core->watch_update_mask != NULL);
g_return_if_fail(reds->core.watch_update_mask != NULL);
reds->core->watch_update_mask(watch, event_mask);
reds->core.watch_update_mask(&reds->core, watch, event_mask);
}
void reds_core_watch_remove(RedsState *reds, SpiceWatch *watch)
{
g_return_if_fail(reds != NULL);
g_return_if_fail(reds->core != NULL);
g_return_if_fail(reds->core->watch_remove != NULL);
g_return_if_fail(reds->core.watch_remove != NULL);
reds->core->watch_remove(watch);
reds->core.watch_remove(&reds->core, watch);
}
SpiceTimer *reds_core_timer_add(RedsState *reds,
@ -4262,10 +4256,9 @@ SpiceTimer *reds_core_timer_add(RedsState *reds,
void *opaque)
{
g_return_val_if_fail(reds != NULL, NULL);
g_return_val_if_fail(reds->core != NULL, NULL);
g_return_val_if_fail(reds->core->timer_add != NULL, NULL);
g_return_val_if_fail(reds->core.timer_add != NULL, NULL);
return reds->core->timer_add(reds->core, func, opaque);
return reds->core.timer_add(&reds->core, func, opaque);
}
@ -4274,30 +4267,27 @@ void reds_core_timer_start(RedsState *reds,
uint32_t ms)
{
g_return_if_fail(reds != NULL);
g_return_if_fail(reds->core != NULL);
g_return_if_fail(reds->core->timer_start != NULL);
g_return_if_fail(reds->core.timer_start != NULL);
return reds->core->timer_start(timer, ms);
return reds->core.timer_start(&reds->core, timer, ms);
}
void reds_core_timer_cancel(RedsState *reds,
SpiceTimer *timer)
{
g_return_if_fail(reds != NULL);
g_return_if_fail(reds->core != NULL);
g_return_if_fail(reds->core->timer_cancel != NULL);
g_return_if_fail(reds->core.timer_cancel != NULL);
return reds->core->timer_cancel(timer);
return reds->core.timer_cancel(&reds->core, timer);
}
void reds_core_timer_remove(RedsState *reds,
SpiceTimer *timer)
{
g_return_if_fail(reds != NULL);
g_return_if_fail(reds->core != NULL);
g_return_if_fail(reds->core->timer_remove != NULL);
g_return_if_fail(reds->core.timer_remove != NULL);
return reds->core->timer_remove(timer);
return reds->core.timer_remove(&reds->core, timer);
}
void reds_update_client_mouse_allowed(RedsState *reds)

View File

@ -73,18 +73,49 @@ static SpiceTimer* base_timer_add(SpiceTimerFunc func, void *opaque)
return base_core_interface.timer_add(&base_core_interface, func, opaque);
}
static void base_timer_start(SpiceTimer *timer, uint32_t ms)
{
base_core_interface.timer_start(&base_core_interface, timer, ms);
}
static void base_timer_cancel(SpiceTimer *timer)
{
base_core_interface.timer_cancel(&base_core_interface, timer);
}
static void base_timer_remove(SpiceTimer *timer)
{
base_core_interface.timer_remove(&base_core_interface, timer);
}
static SpiceWatch *base_watch_add(int fd, int event_mask, SpiceWatchFunc func, void *opaque)
{
return base_core_interface.watch_add(&base_core_interface, fd, event_mask, func, opaque);
}
static void base_watch_update_mask(SpiceWatch *watch, int event_mask)
{
base_core_interface.watch_update_mask(&base_core_interface, watch, event_mask);
}
static void base_watch_remove(SpiceWatch *watch)
{
base_core_interface.watch_remove(&base_core_interface, watch);
}
static SpiceCoreInterface core = {
.base = {
.major_version = SPICE_INTERFACE_CORE_MAJOR,
.minor_version = SPICE_INTERFACE_CORE_MINOR,
},
.timer_add = base_timer_add,
.timer_start = base_timer_start,
.timer_cancel = base_timer_cancel,
.timer_remove = base_timer_remove,
.watch_add = base_watch_add,
.watch_update_mask = base_watch_update_mask,
.watch_remove = base_watch_remove,
.channel_event = event_loop_channel_event,
};
SpiceCoreInterface *basic_event_loop_init(void)
@ -93,12 +124,6 @@ SpiceCoreInterface *basic_event_loop_init(void)
spice_assert(main_context == NULL);
main_context = g_main_context_new();
base_core_interface = event_loop_core;
core.timer_start = base_core_interface.timer_start;
core.timer_cancel = base_core_interface.timer_cancel;
core.timer_remove = base_core_interface.timer_remove;
core.watch_update_mask = base_core_interface.watch_update_mask;
core.watch_remove = base_core_interface.watch_remove;
base_core_interface.channel_event = core.channel_event = event_loop_channel_event;
base_core_interface.main_context = main_context;
return &core;