Skip to content

Commit

Permalink
iiod-client: move context lock inside the iiod_client object
Browse files Browse the repository at this point in the history
All the iiod-client (type) backends create a context lock and pass it to
the iio_client object via iiod_client_new().

This looks wonky, since the iio_client routines do a lot of things with
that lock.

The serial backend uses it quite extensively. The USB doesn't (explicitly
lock it at all), and the network backend only uses it during
network_close() (which may be un-required).

For the cases where this lock is still used, a iiod_client_mutex_{un}lock()
function pair is created to access the lock. We may find that we can hide
the lock entirely, but until then this change shouldn't change any current
behavior.

Signed-off-by: Alexandru Ardelean <[email protected]>
  • Loading branch information
commodo committed Oct 13, 2020
1 parent 53ad0a5 commit a56f583
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 59 deletions.
24 changes: 22 additions & 2 deletions iiod-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ struct iiod_client {
struct iio_mutex *lock;
};

void iiod_client_mutex_lock(struct iiod_client *client)
{
iio_mutex_lock(client->lock);
}

void iiod_client_mutex_unlock(struct iiod_client *client)
{
iio_mutex_unlock(client->lock);
}

static ssize_t iiod_client_read_integer(struct iiod_client *client,
void *desc, int *val)
{
Expand Down Expand Up @@ -138,7 +148,7 @@ static ssize_t iiod_client_read_all(struct iiod_client *client,
}

struct iiod_client * iiod_client_new(struct iio_context_pdata *pdata,
struct iio_mutex *lock, const struct iiod_client_ops *ops)
const struct iiod_client_ops *ops)
{
struct iiod_client *client;

Expand All @@ -148,14 +158,24 @@ struct iiod_client * iiod_client_new(struct iio_context_pdata *pdata,
return NULL;
}

client->lock = lock;
client->lock = iio_mutex_create();
if (!client->lock) {
errno = ENOMEM;
goto err_free_client;
}

client->pdata = pdata;
client->ops = ops;
return client;

err_free_client:
free(client);
return NULL;
}

void iiod_client_destroy(struct iiod_client *client)
{
iio_mutex_destroy(client->lock);
free(client);
}

Expand Down
5 changes: 4 additions & 1 deletion iiod-client.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ struct iiod_client_ops {
void *desc, char *dst, size_t len);
};

void iiod_client_mutex_lock(struct iiod_client *client);
void iiod_client_mutex_unlock(struct iiod_client *client);

struct iiod_client * iiod_client_new(struct iio_context_pdata *pdata,
struct iio_mutex *lock, const struct iiod_client_ops *ops);
const struct iiod_client_ops *ops);
void iiod_client_destroy(struct iiod_client *client);

int iiod_client_get_version(struct iiod_client *client, void *desc,
Expand Down
28 changes: 9 additions & 19 deletions network.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ struct iio_network_io_context {
struct iio_context_pdata {
struct iio_network_io_context io_ctx;
struct addrinfo *addrinfo;
struct iio_mutex *lock;
struct iiod_client *iiod_client;
bool msg_trunc_supported;
};
Expand Down Expand Up @@ -1080,10 +1079,10 @@ static void network_shutdown(struct iio_context *ctx)
struct iio_context_pdata *pdata = ctx->pdata;
unsigned int i;

iio_mutex_lock(pdata->lock);
iiod_client_mutex_lock(pdata->iiod_client);
write_command(&pdata->io_ctx, "\r\nEXIT\r\n");
close(pdata->io_ctx.fd);
iio_mutex_unlock(pdata->lock);
iiod_client_mutex_unlock(pdata->iiod_client);

for (i = 0; i < iio_context_get_devices_count(ctx); i++) {
struct iio_device *dev = iio_context_get_device(ctx, i);
Expand All @@ -1097,7 +1096,6 @@ static void network_shutdown(struct iio_context *ctx)
}

iiod_client_destroy(pdata->iiod_client);
iio_mutex_destroy(pdata->lock);
freeaddrinfo(pdata->addrinfo);
free(pdata);
}
Expand Down Expand Up @@ -1293,6 +1291,7 @@ struct iio_context * network_create_context(const char *host)
{
struct addrinfo hints, *res;
struct iio_context *ctx;
struct iiod_client *iiod_client;
struct iio_context_pdata *pdata;
size_t i, len, uri_len;
int fd, ret;
Expand Down Expand Up @@ -1361,28 +1360,21 @@ struct iio_context * network_create_context(const char *host)
goto err_close_socket;
}

iiod_client = iiod_client_new(pdata, &network_iiod_client_ops);
if (!iiod_client)
goto err_free_pdata;

pdata->iiod_client = iiod_client;
pdata->io_ctx.fd = fd;
pdata->addrinfo = res;
pdata->io_ctx.timeout_ms = DEFAULT_TIMEOUT_MS;

pdata->lock = iio_mutex_create();
if (!pdata->lock) {
errno = ENOMEM;
goto err_free_pdata;
}

pdata->iiod_client = iiod_client_new(pdata, pdata->lock,
&network_iiod_client_ops);

pdata->msg_trunc_supported = msg_trunc_supported(&pdata->io_ctx);
if (pdata->msg_trunc_supported)
IIO_DEBUG("MSG_TRUNC is supported\n");
else
IIO_DEBUG("MSG_TRUNC is NOT supported\n");

if (!pdata->iiod_client)
goto err_destroy_mutex;

IIO_DEBUG("Creating context...\n");
ctx = iiod_client_create_context(pdata->iiod_client, &pdata->io_ctx);
if (!ctx)
Expand Down Expand Up @@ -1517,9 +1509,7 @@ struct iio_context * network_create_context(const char *host)
return NULL;

err_destroy_iiod_client:
iiod_client_destroy(pdata->iiod_client);
err_destroy_mutex:
iio_mutex_destroy(pdata->lock);
iiod_client_destroy(iiod_client);
err_free_pdata:
free(pdata);
err_close_socket:
Expand Down
31 changes: 10 additions & 21 deletions serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@

struct iio_context_pdata {
struct sp_port *port;
struct iio_mutex *lock;
struct iiod_client *iiod_client;

unsigned int timeout_ms;
Expand Down Expand Up @@ -124,7 +123,7 @@ static int serial_open(const struct iio_device *dev,
struct iio_device_pdata *pdata = dev->pdata;
int ret = -EBUSY;

iio_mutex_lock(ctx_pdata->lock);
iiod_client_mutex_lock(ctx_pdata->iiod_client);
if (pdata->opened)
goto out_unlock;

Expand All @@ -134,7 +133,7 @@ static int serial_open(const struct iio_device *dev,
pdata->opened = !ret;

out_unlock:
iio_mutex_unlock(ctx_pdata->lock);
iiod_client_mutex_unlock(ctx_pdata->iiod_client);
return ret;
}

Expand All @@ -145,15 +144,15 @@ static int serial_close(const struct iio_device *dev)
struct iio_device_pdata *pdata = dev->pdata;
int ret = -EBADF;

iio_mutex_lock(ctx_pdata->lock);
iiod_client_mutex_lock(ctx_pdata->iiod_client);
if (!pdata->opened)
goto out_unlock;

ret = iiod_client_close_unlocked(ctx_pdata->iiod_client, NULL, dev);
pdata->opened = false;

out_unlock:
iio_mutex_unlock(ctx_pdata->lock);
iiod_client_mutex_unlock(ctx_pdata->iiod_client);
return ret;
}

Expand All @@ -164,10 +163,10 @@ static ssize_t serial_read(const struct iio_device *dev, void *dst, size_t len,
struct iio_context_pdata *pdata = ctx->pdata;
ssize_t ret;

iio_mutex_lock(pdata->lock);
iiod_client_mutex_lock(pdata->iiod_client);
ret = iiod_client_read_unlocked(pdata->iiod_client, NULL,
dev, dst, len, mask, words);
iio_mutex_unlock(pdata->lock);
iiod_client_mutex_unlock(pdata->iiod_client);

return ret;
}
Expand All @@ -179,9 +178,9 @@ static ssize_t serial_write(const struct iio_device *dev,
struct iio_context_pdata *pdata = ctx->pdata;
ssize_t ret;

iio_mutex_lock(pdata->lock);
iiod_client_mutex_lock(pdata->iiod_client);
ret = iiod_client_write_unlocked(pdata->iiod_client, NULL, dev, src, len);
iio_mutex_unlock(pdata->lock);
iiod_client_mutex_unlock(pdata->iiod_client);

return ret;
}
Expand Down Expand Up @@ -297,7 +296,6 @@ static void serial_shutdown(struct iio_context *ctx)
unsigned int i;

iiod_client_destroy(ctx_pdata->iiod_client);
iio_mutex_destroy(ctx_pdata->lock);
sp_close(ctx_pdata->port);
sp_free_port(ctx_pdata->port);

Expand Down Expand Up @@ -425,16 +423,9 @@ static struct iio_context * serial_create_context(const char *port_name,
pdata->port = port;
pdata->timeout_ms = DEFAULT_TIMEOUT_MS;

pdata->lock = iio_mutex_create();
if (!pdata->lock) {
errno = ENOMEM;
goto err_free_pdata;
}

pdata->iiod_client = iiod_client_new(pdata, pdata->lock,
&serial_iiod_client_ops);
pdata->iiod_client = iiod_client_new(pdata, &serial_iiod_client_ops);
if (!pdata->iiod_client)
goto err_destroy_mutex;
goto err_free_pdata;

ctx = iiod_client_create_context(pdata->iiod_client, NULL);
if (!ctx)
Expand Down Expand Up @@ -473,8 +464,6 @@ static struct iio_context * serial_create_context(const char *port_name,

err_destroy_iiod_client:
iiod_client_destroy(pdata->iiod_client);
err_destroy_mutex:
iio_mutex_destroy(pdata->lock);
err_free_pdata:
free(pdata);
err_free_description:
Expand Down
18 changes: 2 additions & 16 deletions usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,6 @@ struct iio_context_pdata {

struct iiod_client *iiod_client;

/* Lock for non-streaming operations */
struct iio_mutex *lock;

/* Lock for endpoint reservation */
struct iio_mutex *ep_lock;

Expand Down Expand Up @@ -457,7 +454,6 @@ static void usb_shutdown(struct iio_context *ctx)
for (i = 0; i < nb_devices; i++)
usb_close(iio_context_get_device(ctx, i));

iio_mutex_destroy(ctx->pdata->lock);
iio_mutex_destroy(ctx->pdata->ep_lock);

for (i = 0; i < ctx->pdata->nb_ep_couples; i++)
Expand Down Expand Up @@ -840,22 +836,14 @@ struct iio_context * usb_create_context(unsigned int bus,
goto err_set_errno;
}

pdata->lock = iio_mutex_create();
if (!pdata->lock) {
IIO_ERROR("Unable to create mutex\n");
ret = -ENOMEM;
goto err_free_pdata;
}

pdata->ep_lock = iio_mutex_create();
if (!pdata->ep_lock) {
IIO_ERROR("Unable to create mutex\n");
ret = -ENOMEM;
goto err_destroy_mutex;
goto err_free_pdata;
}

pdata->iiod_client = iiod_client_new(pdata, pdata->lock,
&usb_iiod_client_ops);
pdata->iiod_client = iiod_client_new(pdata, &usb_iiod_client_ops);
if (!pdata->iiod_client) {
IIO_ERROR("Unable to create IIOD client\n");
ret = -errno;
Expand Down Expand Up @@ -1062,8 +1050,6 @@ struct iio_context * usb_create_context(unsigned int bus,
iiod_client_destroy(pdata->iiod_client);
err_destroy_ep_mutex:
iio_mutex_destroy(pdata->ep_lock);
err_destroy_mutex:
iio_mutex_destroy(pdata->lock);
err_free_pdata:
free(pdata);
err_set_errno:
Expand Down

0 comments on commit a56f583

Please sign in to comment.