diff options
author | Sander Vrijders <[email protected]> | 2016-10-07 15:47:32 +0200 |
---|---|---|
committer | Sander Vrijders <[email protected]> | 2016-10-07 15:47:32 +0200 |
commit | 55d5b9157b78d90d4f6a575d253c58fd50ca531f (patch) | |
tree | 90d6031870d02b557107b0bc2623a129c4b1d074 /src/lib | |
parent | e351576c01e5f705ed21289aa48f23f345d7c7e1 (diff) | |
parent | 71f10f5efab37f3df3d909d324cff2e098d21c85 (diff) | |
download | ouroboros-55d5b9157b78d90d4f6a575d253c58fd50ca531f.tar.gz ouroboros-55d5b9157b78d90d4f6a575d253c58fd50ca531f.zip |
Merged in dstaesse/ouroboros/be-async-dealloc (pull request #257)
lib, dev: Add asynchronous deallocation
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/dev.c | 38 | ||||
-rw-r--r-- | src/lib/sockets.c | 33 |
2 files changed, 47 insertions, 24 deletions
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); } |