diff options
author | Takashi Sakamoto <o-takashi@sakamocchi.jp> | 2022-05-03 07:57:47 +0900 |
---|---|---|
committer | 坂本 貴史 <o-takashi@sakamocchi.jp> | 2022-05-03 08:31:29 +0900 |
commit | 54da6fd071b365350a4728947a12d7c6e8e3ff8c (patch) | |
tree | bddbbe0b857633ea4661334c12ac7bb51f9e0b9a | |
parent | 85e59ec314c2610bfdbf9d50d19ad759d6f558a3 (diff) | |
download | libhinoko-54da6fd071b365350a4728947a12d7c6e8e3ff8c.tar.gz |
fw_iso_resource: code refactoring to dispatch iso_event
The "allocated" and "deallocated" signals are used by derived object of
Hinoko.FwIsoResource.
This commit adds private macro to share the same name string.
Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
-rw-r--r-- | src/fw_iso_resource.c | 83 | ||||
-rw-r--r-- | src/fw_iso_resource_auto.c | 63 | ||||
-rw-r--r-- | src/fw_iso_resource_once.c | 12 | ||||
-rw-r--r-- | src/fw_iso_resource_private.h | 11 |
4 files changed, 91 insertions, 78 deletions
diff --git a/src/fw_iso_resource.c b/src/fw_iso_resource.c index 6ce7324..7f601b9 100644 --- a/src/fw_iso_resource.c +++ b/src/fw_iso_resource.c @@ -61,15 +61,10 @@ typedef struct { gsize len; guint8 *buf; int fd; + void (*handle_event)(HinokoFwIsoResource *self, const char *signal_name, guint channel, + guint bandwidth, const GError *error); } FwIsoResourceSource; -enum fw_iso_resource_sig_type { - FW_ISO_RESOURCE_SIG_ALLOCATED = 0, - FW_ISO_RESOURCE_SIG_DEALLOCATED, - FW_ISO_RESOURCE_SIG_COUNT, -}; -static guint fw_iso_resource_sigs[FW_ISO_RESOURCE_SIG_COUNT] = { 0 }; - static void fw_iso_resource_finalize(GObject *obj) { HinokoFwIsoResource *self = HINOKO_FW_ISO_RESOURCE(obj); @@ -82,6 +77,12 @@ static void fw_iso_resource_finalize(GObject *obj) G_OBJECT_CLASS(hinoko_fw_iso_resource_parent_class)->finalize(obj); } +static void fw_iso_resource_handle_event(HinokoFwIsoResource *inst, const char *signal_name, + guint channel, guint bandwidth, const GError *error) +{ + g_signal_emit_by_name(inst, signal_name, channel, bandwidth, error); +} + static void hinoko_fw_iso_resource_class_init(HinokoFwIsoResourceClass *klass) { GObjectClass *gobject_class = G_OBJECT_CLASS(klass); @@ -98,15 +99,14 @@ static void hinoko_fw_iso_resource_class_init(HinokoFwIsoResourceClass *klass) * * Emitted when allocation of isochronous resource finishes. */ - fw_iso_resource_sigs[FW_ISO_RESOURCE_SIG_ALLOCATED] = - g_signal_new("allocated", - G_OBJECT_CLASS_TYPE(klass), - G_SIGNAL_RUN_LAST, - G_STRUCT_OFFSET(HinokoFwIsoResourceClass, allocated), - NULL, NULL, - hinoko_sigs_marshal_VOID__UINT_UINT_BOXED, - G_TYPE_NONE, - 3, G_TYPE_UINT, G_TYPE_UINT, G_TYPE_ERROR); + g_signal_new(ALLOCATED_SIGNAL_NAME, + G_OBJECT_CLASS_TYPE(klass), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET(HinokoFwIsoResourceClass, allocated), + NULL, NULL, + hinoko_sigs_marshal_VOID__UINT_UINT_BOXED, + G_TYPE_NONE, + 3, G_TYPE_UINT, G_TYPE_UINT, G_TYPE_ERROR); /** * HinokoFwIsoResource::deallocated: @@ -118,15 +118,14 @@ static void hinoko_fw_iso_resource_class_init(HinokoFwIsoResourceClass *klass) * * Emitted when deallocation of isochronous resource finishes. */ - fw_iso_resource_sigs[FW_ISO_RESOURCE_SIG_DEALLOCATED] = - g_signal_new("deallocated", - G_OBJECT_CLASS_TYPE(klass), - G_SIGNAL_RUN_LAST, - G_STRUCT_OFFSET(HinokoFwIsoResourceClass, deallocated), - NULL, NULL, - hinoko_sigs_marshal_VOID__UINT_UINT_BOXED, - G_TYPE_NONE, - 3, G_TYPE_UINT, G_TYPE_UINT, G_TYPE_ERROR); + g_signal_new(DEALLOCATED_SIGNAL_NAME, + G_OBJECT_CLASS_TYPE(klass), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET(HinokoFwIsoResourceClass, deallocated), + NULL, NULL, + hinoko_sigs_marshal_VOID__UINT_UINT_BOXED, + G_TYPE_NONE, + 3, G_TYPE_UINT, G_TYPE_UINT, G_TYPE_ERROR); } static void hinoko_fw_iso_resource_init(HinokoFwIsoResource *self) @@ -265,12 +264,12 @@ void hinoko_fw_iso_resource_ioctl(HinokoFwIsoResource *self, } } -static void handle_fw_iso_resource_event(HinokoFwIsoResource *self, - const struct fw_cdev_event_iso_resource *ev) +static void handle_iso_resource_event(FwIsoResourceSource *src, + const struct fw_cdev_event_iso_resource *ev) { guint channel; guint bandwidth; - enum fw_iso_resource_sig_type signal_type; + const char *signal_name; GError *error = NULL; if (ev->channel >= 0) { @@ -283,24 +282,11 @@ static void handle_fw_iso_resource_event(HinokoFwIsoResource *self, } if (ev->type == FW_CDEV_EVENT_ISO_RESOURCE_ALLOCATED) - signal_type = FW_ISO_RESOURCE_SIG_ALLOCATED; + signal_name = ALLOCATED_SIGNAL_NAME; else - signal_type = FW_ISO_RESOURCE_SIG_DEALLOCATED; - - // To change state machine for auto mode. - if (HINOKO_IS_FW_ISO_RESOURCE_AUTO(self)) { - const char *signal_name; + signal_name = DEALLOCATED_SIGNAL_NAME; - if (signal_type == FW_ISO_RESOURCE_SIG_ALLOCATED) - signal_name = "allocated"; - else - signal_name = "deallocated"; - - hinoko_fw_iso_resource_auto_handle_event(HINOKO_FW_ISO_RESOURCE_AUTO(self), - channel, bandwidth, signal_name, error); - } - - g_signal_emit(self, fw_iso_resource_sigs[signal_type], 0, channel, bandwidth, error); + src->handle_event(src->self, signal_name, channel, bandwidth, error); if (error != NULL) g_clear_error(&error); @@ -343,7 +329,7 @@ static gboolean dispatch_src(GSource *source, GSourceFunc cb, gpointer user_data switch (ev->common.type) { case FW_CDEV_EVENT_ISO_RESOURCE_ALLOCATED: case FW_CDEV_EVENT_ISO_RESOURCE_DEALLOCATED: - handle_fw_iso_resource_event(src->self, &ev->iso_resource); + handle_iso_resource_event(src, &ev->iso_resource); break; default: break; @@ -401,6 +387,13 @@ void hinoko_fw_iso_resource_create_source(HinokoFwIsoResource *self, src->tag = g_source_add_unix_fd(*source, priv->fd, G_IO_IN); src->fd = priv->fd; src->self = g_object_ref(self); + + if (HINOKO_IS_FW_ISO_RESOURCE_AUTO(self)) + src->handle_event = fw_iso_resource_auto_handle_event; + else if (HINOKO_IS_FW_ISO_RESOURCE_ONCE(self)) + src->handle_event = fw_iso_resource_once_handle_event; + else + src->handle_event = fw_iso_resource_handle_event; } /** diff --git a/src/fw_iso_resource_auto.c b/src/fw_iso_resource_auto.c index 67bd7fb..6d4429f 100644 --- a/src/fw_iso_resource_auto.c +++ b/src/fw_iso_resource_auto.c @@ -111,6 +111,37 @@ static void hinoko_fw_iso_resource_auto_init(HinokoFwIsoResourceAuto *self) g_mutex_init(&priv->mutex); } +void fw_iso_resource_auto_handle_event(HinokoFwIsoResource *inst, const char *signal_name, + guint channel, guint bandwidth, const GError *error) +{ + HinokoFwIsoResourceAuto *self; + HinokoFwIsoResourceAutoPrivate *priv; + + g_return_if_fail(HINOKO_IS_FW_ISO_RESOURCE_AUTO(inst)); + self = HINOKO_FW_ISO_RESOURCE_AUTO(inst); + priv = hinoko_fw_iso_resource_auto_get_instance_private(self); + + if (!strcmp(signal_name, ALLOCATED_SIGNAL_NAME)) { + if (error == NULL) { + g_mutex_lock(&priv->mutex); + priv->channel = channel; + priv->bandwidth = bandwidth; + priv->is_allocated = TRUE; + g_mutex_unlock(&priv->mutex); + } + } else { + if (error == NULL) { + g_mutex_lock(&priv->mutex); + priv->channel = 0; + priv->bandwidth -= bandwidth; + priv->is_allocated = FALSE; + g_mutex_unlock(&priv->mutex); + } + } + + g_signal_emit_by_name(self, signal_name, channel, bandwidth, error); +} + /** * hinoko_fw_iso_resource_auto_new: * @@ -242,7 +273,8 @@ void hinoko_fw_iso_resource_auto_allocate_sync(HinokoFwIsoResourceAuto *self, g_return_if_fail(HINOKO_IS_FW_ISO_RESOURCE_AUTO(self)); g_return_if_fail(error == NULL || *error == NULL); - fw_iso_resource_waiter_init(HINOKO_FW_ISO_RESOURCE(self), &w, "allocated", timeout_ms); + fw_iso_resource_waiter_init(HINOKO_FW_ISO_RESOURCE(self), &w, ALLOCATED_SIGNAL_NAME, + timeout_ms); hinoko_fw_iso_resource_auto_allocate_async(self, channel_candidates, channel_candidates_count, @@ -271,35 +303,10 @@ void hinoko_fw_iso_resource_auto_deallocate_sync(HinokoFwIsoResourceAuto *self, g_return_if_fail(HINOKO_IS_FW_ISO_RESOURCE_AUTO(self)); g_return_if_fail(error == NULL || *error == NULL); - fw_iso_resource_waiter_init(HINOKO_FW_ISO_RESOURCE(self), &w, "deallocated", timeout_ms); + fw_iso_resource_waiter_init(HINOKO_FW_ISO_RESOURCE(self), &w, DEALLOCATED_SIGNAL_NAME, + timeout_ms); hinoko_fw_iso_resource_auto_deallocate_async(self, error); fw_iso_resource_waiter_wait(HINOKO_FW_ISO_RESOURCE(self), &w, error); } - -void hinoko_fw_iso_resource_auto_handle_event(HinokoFwIsoResourceAuto *self, guint channel, - guint bandwidth, const char *signal_name, - const GError *error) -{ - HinokoFwIsoResourceAutoPrivate *priv = - hinoko_fw_iso_resource_auto_get_instance_private(self); - - if (!strcmp(signal_name, "allocated")) { - if (error == NULL) { - g_mutex_lock(&priv->mutex); - priv->channel = channel; - priv->bandwidth = bandwidth; - priv->is_allocated = TRUE; - g_mutex_unlock(&priv->mutex); - } - } else if (!strcmp(signal_name, "deallocated")) { - if (error == NULL) { - g_mutex_lock(&priv->mutex); - priv->channel = 0; - priv->bandwidth -= bandwidth; - priv->is_allocated = FALSE; - g_mutex_unlock(&priv->mutex); - } - } -} diff --git a/src/fw_iso_resource_once.c b/src/fw_iso_resource_once.c index d377885..6f897a8 100644 --- a/src/fw_iso_resource_once.c +++ b/src/fw_iso_resource_once.c @@ -22,6 +22,12 @@ static void hinoko_fw_iso_resource_once_init(HinokoFwIsoResourceOnce *self) return; } +void fw_iso_resource_once_handle_event(HinokoFwIsoResource *inst, const char *signal_name, + guint channel, guint bandwidth, const GError *error) +{ + g_signal_emit_by_name(inst, signal_name, channel, bandwidth, error); +} + /** * hinoko_fw_iso_resource_once_new: * @@ -132,7 +138,8 @@ void hinoko_fw_iso_resource_once_allocate_sync(HinokoFwIsoResourceOnce *self, g_return_if_fail(HINOKO_IS_FW_ISO_RESOURCE_ONCE(self)); g_return_if_fail(error == NULL || *error == NULL); - fw_iso_resource_waiter_init(HINOKO_FW_ISO_RESOURCE(self), &w, "allocated", timeout_ms); + fw_iso_resource_waiter_init(HINOKO_FW_ISO_RESOURCE(self), &w, ALLOCATED_SIGNAL_NAME, + timeout_ms); hinoko_fw_iso_resource_once_allocate_async(self, channel_candidates, channel_candidates_count, bandwidth, error); @@ -162,7 +169,8 @@ void hinoko_fw_iso_resource_once_deallocate_sync(HinokoFwIsoResourceOnce *self, g_return_if_fail(HINOKO_IS_FW_ISO_RESOURCE_ONCE(self)); g_return_if_fail(error == NULL || *error == NULL); - fw_iso_resource_waiter_init(HINOKO_FW_ISO_RESOURCE(self), &w, "deallocated", timeout_ms); + fw_iso_resource_waiter_init(HINOKO_FW_ISO_RESOURCE(self), &w, DEALLOCATED_SIGNAL_NAME, + timeout_ms); hinoko_fw_iso_resource_once_deallocate_async(self, channel, bandwidth, error); diff --git a/src/fw_iso_resource_private.h b/src/fw_iso_resource_private.h index 51a2b91..d8ff66e 100644 --- a/src/fw_iso_resource_private.h +++ b/src/fw_iso_resource_private.h @@ -7,13 +7,18 @@ #include <unistd.h> #include <errno.h> +#define ALLOCATED_SIGNAL_NAME "allocated" +#define DEALLOCATED_SIGNAL_NAME "deallocated" + void hinoko_fw_iso_resource_ioctl(HinokoFwIsoResource *self, unsigned long request, void *argp, GError **error); -void hinoko_fw_iso_resource_auto_handle_event(HinokoFwIsoResourceAuto *self, guint channel, - guint bandwidth, const char *signal_name, - const GError *error); +void fw_iso_resource_auto_handle_event(HinokoFwIsoResource *inst, const char *signal_name, + guint channel, guint bandwidth, const GError *error); + +void fw_iso_resource_once_handle_event(HinokoFwIsoResource *inst, const char *signal_name, + guint channel, guint bandwidth, const GError *error); struct fw_iso_resource_waiter { GMutex mutex; |