diff options
author | fadhil riyanto <me@fadev.org> | 2024-09-29 09:15:22 +0700 |
---|---|---|
committer | fadhil riyanto <me@fadev.org> | 2024-09-29 09:15:22 +0700 |
commit | 1ce9aab835719a80aa51dd370674816d29e5ff45 (patch) | |
tree | 8e609fb16b65743ec5626f198dddb5dde5e0db6a | |
parent | dbc07c3542ece924a64ca4afe0d0f10de6ed762b (diff) |
add mutex locking while delete and add fd_sockaddr_list
Signed-off-by: fadhil riyanto <me@fadev.org>
-rw-r--r-- | main.c | 9 |
1 files changed, 8 insertions, 1 deletions
@@ -74,6 +74,7 @@ struct _fd_sockaddr_list { }; struct fd_sockaddr_list { + pthread_mutex_t fd_sockaddr_lock; struct _fd_sockaddr_list *list; int size; }; @@ -278,6 +279,8 @@ static void init_fd_sockaddr(struct fd_sockaddr_list *fdsocklist) static int add_fd_sockaddr(struct fd_sockaddr_list *fdsocklist, int fd, struct sockaddr_in *sockaddr) { + pthread_mutex_lock(&fdsocklist->fd_sockaddr_lock); + /* init mem */ fdsocklist->list[fdsocklist->size - 1].sockaddr = malloc( sizeof(struct sockaddr_in)); @@ -300,6 +303,8 @@ static int add_fd_sockaddr(struct fd_sockaddr_list *fdsocklist, fdsocklist->list = dummymem; + pthread_mutex_unlock(&fdsocklist->fd_sockaddr_lock); + return 0; } @@ -321,7 +326,7 @@ static struct sockaddr_in* del_fd_sockaddr(struct fd_sockaddr_list *fdsocklist, { if (fdsocklist->size != 0) { - + pthread_mutex_lock(&fdsocklist->fd_sockaddr_lock); // fdsocklist->size = fdsocklist->size - 1; struct _fd_sockaddr_list* dummymem = realloc( @@ -349,6 +354,8 @@ static struct sockaddr_in* del_fd_sockaddr(struct fd_sockaddr_list *fdsocklist, fdsocklist->list = dummymem; fdsocklist->size = fdsocklist->size - 1; + pthread_mutex_unlock(&fdsocklist->fd_sockaddr_lock); + } |