Avoid not constant globals. Signed-off-by: Frediano Ziglio <fziglio@xxxxxxxxxx> --- server/event-loop.c | 23 +++++++++------- server/red-channel.c | 59 ++++++++++++++++++++++++----------------- server/red-common.h | 12 ++++----- server/reds-private.h | 2 +- server/reds.c | 40 ++++++++++++++-------------- server/tests/basic_event_loop.c | 37 +++++++++++++++++++++----- 6 files changed, 106 insertions(+), 67 deletions(-) diff --git a/server/event-loop.c b/server/event-loop.c index d16aca1..f628a8f 100644 --- a/server/event-loop.c +++ b/server/event-loop.c @@ -53,7 +53,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); @@ -62,9 +63,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); @@ -74,9 +76,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); } @@ -124,7 +127,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); @@ -154,14 +158,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.c b/server/red-channel.c index a02c51a..fffbe24 100644 --- a/server/red-channel.c +++ b/server/red-channel.c @@ -431,9 +431,10 @@ static void red_channel_client_on_out_block(void *opaque) RedChannelClient *rcc = (RedChannelClient *)opaque; rcc->send_data.blocked = TRUE; - rcc->channel->core->watch_update_mask(rcc->stream->watch, - SPICE_WATCH_EVENT_READ | - SPICE_WATCH_EVENT_WRITE); + rcc->channel->core->watch_update_mask(rcc->channel->core, + rcc->stream->watch, + SPICE_WATCH_EVENT_READ | + SPICE_WATCH_EVENT_WRITE); } static inline int red_channel_client_urgent_marshaller_is_active(RedChannelClient *rcc) @@ -611,8 +612,9 @@ static void red_channel_client_on_out_msg_done(void *opaque) red_channel_client_release_sent_item(rcc); if (rcc->send_data.blocked) { rcc->send_data.blocked = FALSE; - rcc->channel->core->watch_update_mask(rcc->stream->watch, - SPICE_WATCH_EVENT_READ); + rcc->channel->core->watch_update_mask(rcc->channel->core, + rcc->stream->watch, + SPICE_WATCH_EVENT_READ); } if (red_channel_client_urgent_marshaller_is_active(rcc)) { @@ -795,7 +797,8 @@ static void red_channel_client_connectivity_timer(void *opaque) } else { monitor->state = CONNECTIVITY_STATE_CONNECTED; } - rcc->channel->core->timer_start(rcc->connectivity_monitor.timer, + rcc->channel->core->timer_start(rcc->channel->core, + rcc->connectivity_monitor.timer, rcc->connectivity_monitor.timeout); } else { monitor->state = CONNECTIVITY_STATE_DISCONNECTED; @@ -832,7 +835,8 @@ void red_channel_client_start_connectivity_monitoring(RedChannelClient *rcc, uin rcc->channel->core, red_channel_client_connectivity_timer, rcc); rcc->connectivity_monitor.timeout = timeout_ms; if (!red_client_during_migrate_at_target(rcc->client)) { - rcc->channel->core->timer_start(rcc->connectivity_monitor.timer, + rcc->channel->core->timer_start(rcc->channel->core, + rcc->connectivity_monitor.timer, rcc->connectivity_monitor.timeout); } } @@ -895,9 +899,9 @@ RedChannelClient *red_channel_client_create(int size, RedChannel *channel, RedCl rcc->pipe_size = 0; stream->watch = channel->core->watch_add(channel->core, - stream->socket, - SPICE_WATCH_EVENT_READ, - red_channel_client_event, rcc); + stream->socket, + SPICE_WATCH_EVENT_READ, + red_channel_client_event, rcc); rcc->id = g_list_length(channel->clients); red_channel_add_client(channel, rcc); red_client_add_channel(client, rcc); @@ -953,7 +957,8 @@ static void red_channel_client_seamless_migration_done(RedChannelClient *rcc) red_channel_client_start_ping_timer(rcc, PING_TEST_IDLE_NET_TIMEOUT_MS); } if (rcc->connectivity_monitor.timer) { - rcc->channel->core->timer_start(rcc->connectivity_monitor.timer, + rcc->channel->core->timer_start(rcc->channel->core, + rcc->connectivity_monitor.timer, rcc->connectivity_monitor.timeout); } } @@ -999,11 +1004,11 @@ void red_channel_client_default_migrate(RedChannelClient *rcc) { if (rcc->latency_monitor.timer) { red_channel_client_cancel_ping_timer(rcc); - rcc->channel->core->timer_remove(rcc->latency_monitor.timer); + rcc->channel->core->timer_remove(rcc->channel->core, rcc->latency_monitor.timer); rcc->latency_monitor.timer = NULL; } if (rcc->connectivity_monitor.timer) { - rcc->channel->core->timer_remove(rcc->connectivity_monitor.timer); + rcc->channel->core->timer_remove(rcc->channel->core, rcc->connectivity_monitor.timer); rcc->connectivity_monitor.timer = NULL; } red_channel_client_pipe_add_type(rcc, RED_PIPE_ITEM_TYPE_MIGRATE); @@ -1069,7 +1074,8 @@ RedChannel *red_channel_create(int size, } // 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) { } @@ -1079,7 +1085,8 @@ 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) { } @@ -1279,7 +1286,7 @@ void red_channel_destroy(RedChannel *channel) void red_channel_client_shutdown(RedChannelClient *rcc) { if (rcc->stream && !rcc->stream->shutdown) { - rcc->channel->core->watch_remove(rcc->stream->watch); + rcc->channel->core->watch_remove(rcc->channel->core, rcc->stream->watch); rcc->stream->watch = NULL; shutdown(rcc->stream->socket, SHUT_RDWR); rcc->stream->shutdown = TRUE; @@ -1341,7 +1348,8 @@ void red_channel_client_push(RedChannelClient *rcc) } if (red_channel_client_no_item_being_sent(rcc) && ring_is_empty(&rcc->pipe) && rcc->stream->watch) { - rcc->channel->core->watch_update_mask(rcc->stream->watch, + rcc->channel->core->watch_update_mask(rcc->channel->core, + rcc->stream->watch, SPICE_WATCH_EVENT_READ); } rcc->during_send = FALSE; @@ -1436,7 +1444,7 @@ static void red_channel_client_start_ping_timer(RedChannelClient *rcc, uint32_t return; } rcc->latency_monitor.state = PING_STATE_TIMER; - rcc->channel->core->timer_start(rcc->latency_monitor.timer, timeout); + rcc->channel->core->timer_start(rcc->channel->core, rcc->latency_monitor.timer, timeout); } static void red_channel_client_cancel_ping_timer(RedChannelClient *rcc) @@ -1448,7 +1456,7 @@ static void red_channel_client_cancel_ping_timer(RedChannelClient *rcc) return; } - rcc->channel->core->timer_cancel(rcc->latency_monitor.timer); + rcc->channel->core->timer_cancel(rcc->channel->core, rcc->latency_monitor.timer); rcc->latency_monitor.state = PING_STATE_NONE; } @@ -1635,9 +1643,10 @@ static inline gboolean client_pipe_add(RedChannelClient *rcc, RedPipeItem *item, return FALSE; } if (ring_is_empty(&rcc->pipe) && rcc->stream->watch) { - rcc->channel->core->watch_update_mask(rcc->stream->watch, - SPICE_WATCH_EVENT_READ | - SPICE_WATCH_EVENT_WRITE); + rcc->channel->core->watch_update_mask(rcc->channel->core, + rcc->stream->watch, + SPICE_WATCH_EVENT_READ | + SPICE_WATCH_EVENT_WRITE); } rcc->pipe_size++; ring_add(pos, &item->link); @@ -1823,15 +1832,15 @@ void red_channel_client_disconnect(RedChannelClient *rcc) rcc->channel->type, rcc->channel->id); red_channel_client_pipe_clear(rcc); if (rcc->stream->watch) { - rcc->channel->core->watch_remove(rcc->stream->watch); + rcc->channel->core->watch_remove(rcc->channel->core, rcc->stream->watch); rcc->stream->watch = NULL; } if (rcc->latency_monitor.timer) { - rcc->channel->core->timer_remove(rcc->latency_monitor.timer); + rcc->channel->core->timer_remove(rcc->channel->core, rcc->latency_monitor.timer); rcc->latency_monitor.timer = NULL; } if (rcc->connectivity_monitor.timer) { - rcc->channel->core->timer_remove(rcc->connectivity_monitor.timer); + rcc->channel->core->timer_remove(rcc->channel->core, rcc->connectivity_monitor.timer); rcc->connectivity_monitor.timer = NULL; } red_channel_remove_client(rcc); diff --git a/server/red-common.h b/server/red-common.h index 7ab7e15..a0f6e55 100644 --- a/server/red-common.h +++ b/server/red-common.h @@ -45,15 +45,15 @@ 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; }; diff --git a/server/reds-private.h b/server/reds-private.h index 0408f25..c5cd816 100644 --- a/server/reds-private.h +++ b/server/reds-private.h @@ -153,7 +153,7 @@ struct RedsState { SpiceCharDeviceInstance *vdagent; SpiceMigrateInstance *migration_interface; - SpiceCoreInterfaceInternal *core; + SpiceCoreInterfaceInternal core[1]; GList *qxl_instances; MainDispatcher *main_dispatcher; }; diff --git a/server/reds.c b/server/reds.c index c4ffee1..14d86ec 100644 --- a/server/reds.c +++ b/server/reds.c @@ -76,24 +76,24 @@ 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; +#define core_public ((SpiceCoreInterface *) iface->main_context) static SpiceTimer *adapter_timer_add(const SpiceCoreInterfaceInternal *iface, SpiceTimerFunc func, void *opaque) { return core_public->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); } -static void adapter_timer_cancel(SpiceTimer *timer) +static void adapter_timer_cancel(const SpiceCoreInterfaceInternal *iface, SpiceTimer *timer) { core_public->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); } @@ -104,24 +104,24 @@ static SpiceWatch *adapter_watch_add(const SpiceCoreInterfaceInternal *iface, return core_public->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); } -static void adapter_watch_remove(SpiceWatch *watch) +static void adapter_watch_remove(const SpiceCoreInterfaceInternal *iface, SpiceWatch *watch) { core_public->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); } +#undef core_public - -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, @@ -321,7 +321,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); @@ -513,7 +513,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); } } @@ -2961,7 +2961,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) @@ -2977,7 +2977,7 @@ static void reds_mig_fill_wait_disconnect(RedsState *reds) reds->mig_wait_disconnect_clients = g_list_append(reds->mig_wait_disconnect_clients, client); } 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) @@ -3404,8 +3404,8 @@ 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->main_context = (GMainContext *) core_interface; reds->listen_socket = -1; reds->secure_listen_socket = -1; reds->agent_dev = red_char_device_vdi_port_new(reds); @@ -4221,7 +4221,7 @@ void reds_core_watch_update_mask(RedsState *reds, g_return_if_fail(reds->core != 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) @@ -4230,7 +4230,7 @@ void reds_core_watch_remove(RedsState *reds, SpiceWatch *watch) g_return_if_fail(reds->core != 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, @@ -4253,7 +4253,7 @@ void reds_core_timer_start(RedsState *reds, g_return_if_fail(reds->core != 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, @@ -4263,7 +4263,7 @@ void reds_core_timer_cancel(RedsState *reds, g_return_if_fail(reds->core != 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, @@ -4273,7 +4273,7 @@ void reds_core_timer_remove(RedsState *reds, g_return_if_fail(reds->core != 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