Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 47 additions & 2 deletions network-windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,16 @@
#include <errno.h>
#include <ws2tcpip.h>
#include <iio/iio-debug.h>
#include <iio/iio-lock.h>

#define close(s) closesocket(s)

struct iiod_client_pdata_os {
struct iio_mutex *event_lock;
int read_waiters;
int write_waiters;
};

int set_blocking_mode(int s, bool blocking)
{
unsigned long nonblock;
Expand Down Expand Up @@ -42,11 +49,33 @@ int setup_cancel(struct iiod_client_pdata *io_ctx)
return -ENOMEM;
}

return 0;
struct iiod_client_pdata_os *os_priv = malloc(sizeof(*os_priv));
if (!os_priv)
goto err_close_events;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think normal libiio coding style is that you don't need {} around one line statements.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, thank you so much for the advices!

os_priv->event_lock = iio_mutex_create();
if (!os_priv->event_lock)
goto err_free_os_priv;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also similar here - it's more like other functions to move the free(os_priv); into the error/return path (at a new err_free_os_priv (or whatever your prefer), right before the existing err_close_events: label, and then in the above - you have a single goto without {}.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, thanks!

os_priv->read_waiters = 0;
os_priv->write_waiters = 0;
io_ctx->os_priv = os_priv;

return 0;
err_free_os_priv:
free(os_priv);
err_close_events:
WSACloseEvent(io_ctx->events[0]);
WSACloseEvent(io_ctx->events[1]);
io_ctx->events[0] = NULL;
io_ctx->events[1] = NULL;
return -ENOMEM;
}

void cleanup_cancel(struct iiod_client_pdata *io_ctx)
{
iio_mutex_destroy(io_ctx->os_priv->event_lock);
free(io_ctx->os_priv);
WSACloseEvent(io_ctx->events[0]);
WSACloseEvent(io_ctx->events[1]);
}
Expand All @@ -59,21 +88,37 @@ void do_cancel(struct iiod_client_pdata *io_ctx)
int wait_cancellable(struct iiod_client_pdata *io_ctx,
bool read, unsigned int timeout_ms)
{
struct iio_mutex *lock = io_ctx->os_priv->event_lock;
long wsa_events = FD_CLOSE;
DWORD ret, timeout = timeout_ms > 0 ? (DWORD) timeout_ms : WSA_INFINITE;

iio_mutex_lock(lock);
if (read)
wsa_events |= FD_READ;
io_ctx->os_priv->read_waiters++;
else
io_ctx->os_priv->write_waiters++;

if (io_ctx->os_priv->read_waiters > 0)
wsa_events |= FD_READ;
if (io_ctx->os_priv->write_waiters > 0)
wsa_events |= FD_WRITE;

WSAEventSelect(io_ctx->fd, NULL, 0);
WSAResetEvent(io_ctx->events[0]);
WSAEventSelect(io_ctx->fd, io_ctx->events[0], wsa_events);

iio_mutex_unlock(lock);

ret = WSAWaitForMultipleEvents(2, io_ctx->events, FALSE,
timeout, FALSE);

iio_mutex_lock(lock);
if (read)
io_ctx->os_priv->read_waiters--;
else
io_ctx->os_priv->write_waiters--;
iio_mutex_unlock(lock);

if (ret == WSA_WAIT_TIMEOUT)
return -ETIMEDOUT;

Expand Down
4 changes: 3 additions & 1 deletion network.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ struct iio_context_params;
struct iio_context_pdata;
struct addrinfo;

struct iiod_client_pdata_os;
struct iiod_client_pdata {
int fd;

/* Platform-specific private data */
struct iiod_client_pdata_os *os_priv;
bool cancelled;
void * events[2];
int cancel_fd[2];
Expand Down
Loading