Skip to content

Commit

Permalink
fw_fcp: use glibc list instead of GList to maintain transaction entries
Browse files Browse the repository at this point in the history
GList has a slight overhead to allocate entry, while glibc list has no
such overhead. If it is possible to define structure in which the list
pointer is embedded by outselves, glib list is useful.

This commit replaces usage of GList with glibc list.

Signed-off-by: Takashi Sakamoto <[email protected]>
  • Loading branch information
takaswie committed Aug 16, 2023
1 parent 2ddb649 commit ae8cfbb
Showing 1 changed file with 16 additions and 22 deletions.
38 changes: 16 additions & 22 deletions src/fw_fcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <string.h>
#include <errno.h>

#include <sys/queue.h>

/**
* HinawaFwFcp:
* A FCP transaction executor to node in IEEE 1394 bus.
Expand Down Expand Up @@ -62,12 +64,15 @@ enum avc_status {
AVC_STATUS_INTERIM = 0x0f,
};

struct waiter;
LIST_HEAD(waiter_entries, waiter);

typedef struct {
HinawaFwNode *node;
gulong bus_update_handler_id;
guint card_id;

GList *transactions;
struct waiter_entries transactions;
GMutex transactions_mutex;
} HinawaFwFcpPrivate;
G_DEFINE_TYPE_WITH_PRIVATE(HinawaFwFcp, hinawa_fw_fcp, HINAWA_TYPE_FW_RESP)
Expand Down Expand Up @@ -287,21 +292,18 @@ struct waiter {
guint tstamp;
GCond cond;
GMutex mutex;
LIST_ENTRY(waiter) list;
};

static void handle_responded_signal(HinawaFwFcp *self, guint tstamp, const guint8 *frame,
guint frame_size)
{
HinawaFwFcpPrivate *priv = hinawa_fw_fcp_get_instance_private(self);
GList *entry;
struct waiter *w;

g_mutex_lock(&priv->transactions_mutex);

for (entry = g_list_first(priv->transactions);
entry != NULL;
entry = g_list_next(priv->transactions)) {
struct waiter *w = (struct waiter *)entry->data;

LIST_FOREACH(w, &priv->transactions, list) {
g_mutex_lock(&w->mutex);

if (w->frame[1] == frame[1] && w->frame[2] == frame[2]) {
Expand Down Expand Up @@ -388,7 +390,7 @@ gboolean hinawa_fw_fcp_avc_transaction_with_tstamp(HinawaFwFcp *self,
NULL);

g_mutex_lock(&priv->transactions_mutex);
priv->transactions = g_list_append(priv->transactions, &w);
LIST_INSERT_HEAD(&priv->transactions, &w, list);
g_mutex_unlock(&priv->transactions_mutex);

// Finish transaction for command frame.
Expand Down Expand Up @@ -416,7 +418,7 @@ gboolean hinawa_fw_fcp_avc_transaction_with_tstamp(HinawaFwFcp *self,
}

g_mutex_lock(&priv->transactions_mutex);
priv->transactions = g_list_remove(priv->transactions, &w);
LIST_REMOVE(&w, list);
g_mutex_unlock(&priv->transactions_mutex);

g_signal_handler_disconnect(self, handler_id);
Expand Down Expand Up @@ -505,15 +507,11 @@ static HinawaFwRcode handle_requested_signal(HinawaFwResp *resp, HinawaFwTcode t
static void handle_bus_update_signal(HinawaFwNode *node, HinawaFwFcp *self)
{
HinawaFwFcpPrivate *priv = hinawa_fw_fcp_get_instance_private(self);
GList *entry;
struct waiter *w;

g_mutex_lock(&priv->transactions_mutex);

for (entry = g_list_first(priv->transactions);
entry != NULL;
entry = g_list_next(priv->transactions)) {
struct waiter *w = (struct waiter *)entry->data;

LIST_FOREACH(w, &priv->transactions, list) {
g_mutex_lock(&w->mutex);
w->state = WAITER_STATE_ABORTED;
g_cond_signal(&w->cond);
Expand Down Expand Up @@ -547,7 +545,7 @@ gboolean hinawa_fw_fcp_bind(HinawaFwFcp *self, HinawaFwNode *node, GError **erro

if (priv->node == NULL) {
g_mutex_lock(&priv->transactions_mutex);
g_list_free(priv->transactions);
LIST_INIT(&priv->transactions);
g_mutex_unlock(&priv->transactions_mutex);

if (!hinawa_fw_resp_reserve(HINAWA_FW_RESP(self), node, FCP_RESPOND_ADDR,
Expand All @@ -574,7 +572,7 @@ gboolean hinawa_fw_fcp_bind(HinawaFwFcp *self, HinawaFwNode *node, GError **erro
void hinawa_fw_fcp_unbind(HinawaFwFcp *self)
{
HinawaFwFcpPrivate *priv;
GList *entry;
struct waiter *w;

g_return_if_fail(HINAWA_IS_FW_FCP(self));
priv = hinawa_fw_fcp_get_instance_private(self);
Expand All @@ -589,11 +587,7 @@ void hinawa_fw_fcp_unbind(HinawaFwFcp *self)

g_mutex_lock(&priv->transactions_mutex);

for (entry = g_list_first(priv->transactions);
entry != NULL;
entry = g_list_next(priv->transactions)) {
struct waiter *w = (struct waiter *)entry->data;

LIST_FOREACH(w, &priv->transactions, list) {
g_mutex_lock(&w->mutex);

if (w->state == WAITER_STATE_PENDING) {
Expand Down

0 comments on commit ae8cfbb

Please sign in to comment.