From 71f10f5efab37f3df3d909d324cff2e098d21c85 Mon Sep 17 00:00:00 2001 From: dimitri staessens Date: Fri, 7 Oct 2016 15:25:22 +0200 Subject: lib, dev: Add asynchronous deallocation Flow deallocation from the application will immediately return (void call). The IRMd will not send a reply message. --- src/lib/dev.c | 38 ++++++++++++++------------------------ src/lib/sockets.c | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 24 deletions(-) (limited to 'src/lib') diff --git a/src/lib/dev.c b/src/lib/dev.c index 8556d6e2..d36764ed 100644 --- a/src/lib/dev.c +++ b/src/lib/dev.c @@ -130,7 +130,7 @@ struct flow { pid_t api; - struct timespec timeout; + struct timespec * timeout; }; struct { @@ -220,8 +220,7 @@ int ap_init(char * ap_name) ai.flows[i].port_id = -1; ai.flows[i].oflags = 0; ai.flows[i].api = -1; - ai.flows[i].timeout.tv_sec = 0; - ai.flows[i].timeout.tv_nsec = 0; + ai.flows[i].timeout = NULL; } ai.ports = malloc(sizeof(*ai.ports) * IRMD_MAX_FLOWS); @@ -270,9 +269,12 @@ void ap_fini() pthread_rwlock_rdlock(&ai.flows_lock); - for (i = 0; i < AP_MAX_FLOWS; ++i) + for (i = 0; i < AP_MAX_FLOWS; ++i) { if (ai.flows[i].rb != NULL) shm_ap_rbuff_close(ai.flows[i].rb); + if (ai.flows[i].timeout != NULL) + free(ai.flows[i].timeout); + } for (i = 0; i < IRMD_MAX_FLOWS; ++i) { ai.ports[i].state = PORT_NULL; @@ -527,8 +529,6 @@ int flow_alloc_res(int fd) int flow_dealloc(int fd) { irm_msg_t msg = IRM_MSG__INIT; - irm_msg_t * recv_msg = NULL; - int ret = -1; msg.code = IRM_MSG_CODE__IRM_FLOW_DEALLOC; msg.has_port_id = true; @@ -552,30 +552,20 @@ int flow_dealloc(int fd) shm_ap_rbuff_close(ai.flows[fd].rb); ai.flows[fd].rb = NULL; ai.flows[fd].api = -1; + if (ai.flows[fd].timeout != NULL) { + free(ai.flows[fd].timeout); + ai.flows[fd].timeout = NULL; + } bmp_release(ai.fds, fd); pthread_rwlock_unlock(&ai.flows_lock); - recv_msg = send_recv_irm_msg(&msg); - if (recv_msg == NULL) { - pthread_rwlock_unlock(&ai.data_lock); - return -1; - } - - if (!recv_msg->has_result) { - pthread_rwlock_unlock(&ai.data_lock); - irm_msg__free_unpacked(recv_msg, NULL); - return -1; - } - - ret = recv_msg->result; + send_irm_msg(&msg); pthread_rwlock_unlock(&ai.data_lock); - irm_msg__free_unpacked(recv_msg, NULL); - - return ret; + return 0; } int flow_cntl(int fd, int cmd, int oflags) @@ -708,10 +698,10 @@ ssize_t flow_read(int fd, void * buf, size_t count) } else { struct shm_ap_rbuff * rb = ai.rb; int port_id = ai.flows[fd].port_id; - struct timespec timeout = ai.flows[fd].timeout; + struct timespec * timeout = ai.flows[fd].timeout; pthread_rwlock_unlock(&ai.flows_lock); pthread_rwlock_unlock(&ai.data_lock); - idx = shm_ap_rbuff_read_port_b(rb, port_id, &timeout); + idx = shm_ap_rbuff_read_port_b(rb, port_id, timeout); pthread_rwlock_rdlock(&ai.data_lock); } diff --git a/src/lib/sockets.c b/src/lib/sockets.c index 408e79e7..c8375c22 100644 --- a/src/lib/sockets.c +++ b/src/lib/sockets.c @@ -154,6 +154,39 @@ static irm_msg_t * send_recv_irm_msg_timed(irm_msg_t * msg, bool timed) return recv_msg; } +void send_irm_msg(irm_msg_t * msg) +{ + int sockfd; + buffer_t buf; + + sockfd = client_socket_open(IRM_SOCK_PATH); + if (sockfd < 0) + return; + + buf.len = irm_msg__get_packed_size(msg); + if (buf.len == 0) { + close(sockfd); + return; + } + + buf.data = malloc(buf.len); + if (buf.data == NULL) { + close(sockfd); + return; + } + + pthread_cleanup_push(close_ptr, &sockfd); + pthread_cleanup_push((void (*)(void *)) free, (void *) buf.data); + + irm_msg__pack(msg, buf.data); + + if (write(sockfd, buf.data, buf.len) < 0) + return; + + pthread_cleanup_pop(true); + pthread_cleanup_pop(true); +} + irm_msg_t * send_recv_irm_msg(irm_msg_t * msg) { return send_recv_irm_msg_timed(msg, true); } -- cgit v1.2.3