Avoid not constant globals. Signed-off-by: Frediano Ziglio <fziglio@xxxxxxxxxx> --- server/dummy-channel.c | 5 ++- server/event-loop.c | 23 ++++++----- server/red-channel-client.c | 32 +++++++-------- server/red-common.h | 17 ++++---- server/reds-private.h | 2 +- server/reds.c | 88 ++++++++++++++++++----------------------- server/tests/basic_event_loop.c | 37 ++++++++++++++--- 7 files changed, 114 insertions(+), 90 deletions(-) diff --git a/server/dummy-channel.c b/server/dummy-channel.c index f13c8f8..aecd3a6 100644 --- a/server/dummy-channel.c +++ b/server/dummy-channel.c @@ -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) { } diff --git a/server/event-loop.c b/server/event-loop.c index 80add0b..7f2db0f 100644 --- a/server/event-loop.c +++ b/server/event-loop.c @@ -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); diff --git a/server/red-channel-client.c b/server/red-channel-client.c index ad4a545..24b2fb0 100644 --- a/server/red-channel-client.c +++ b/server/red-channel-client.c @@ -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); diff --git a/server/red-common.h b/server/red-common.h index 190fd9c..da68fc1 100644 --- a/server/red-common.h +++ b/server/red-common.h @@ -45,17 +45,20 @@ 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; + union { + GMainContext *main_context; + SpiceCoreInterface *public_interface; + }; }; extern const SpiceCoreInterfaceInternal event_loop_core; diff --git a/server/reds-private.h b/server/reds-private.h index ce78945..8198a8d 100644 --- a/server/reds-private.h +++ b/server/reds-private.h @@ -140,7 +140,7 @@ struct RedsState { SpiceCharDeviceInstance *vdagent; SpiceMigrateInstance *migration_interface; - SpiceCoreInterfaceInternal *core; + SpiceCoreInterfaceInternal core; GList *qxl_instances; MainDispatcher *main_dispatcher; }; diff --git a/server/reds.c b/server/reds.c index 30b9165..024dc69 100644 --- a/server/reds.c +++ b/server/reds.c @@ -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); } } @@ -2977,7 +2974,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) @@ -2993,7 +2990,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) @@ -3423,21 +3420,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"); } @@ -4219,7 +4216,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, @@ -4228,10 +4225,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, @@ -4239,19 +4235,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, @@ -4259,10 +4253,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); } @@ -4271,30 +4264,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) diff --git a/server/tests/basic_event_loop.c b/server/tests/basic_event_loop.c index 4820387..2029424 100644 --- a/server/tests/basic_event_loop.c +++ b/server/tests/basic_event_loop.c @@ -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; -- 2.7.4 _______________________________________________ Spice-devel mailing list Spice-devel@xxxxxxxxxxxxxxxxxxxxx https://lists.freedesktop.org/mailman/listinfo/spice-devel