From: seantywork Date: Wed, 12 Mar 2025 11:04:31 +0000 (+0000) Subject: add: rehashable concurrent map X-Git-Url: https://git.feebdaed.xyz/?a=commitdiff_plain;h=3314dded3d17320dbf0fd3f0ef74025fba185d28;p=socialize.git add: rehashable concurrent map --- diff --git a/include/socialize/core.h b/include/socialize/core.h index a70e09c..00c99b7 100644 --- a/include/socialize/core.h +++ b/include/socialize/core.h @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -177,7 +178,9 @@ - +struct spinlock { + int locked; +}; struct user { char name[MAX_USER_NAME]; @@ -209,12 +212,20 @@ struct SOCK_CONTEXT { int auth; char id[MAX_ID_LEN]; int chan_idx; - pthread_mutex_t lock; struct SOCK_CONTEXT *next; }; +struct SOCK_CONTEXT_LOCK { + + pthread_mutex_t lock; +}; +struct SOCK_CTL { + struct spinlock slock; + struct SOCK_CONTEXT** SOCK_CTX; + struct SOCK_CONTEXT_LOCK** SOCK_CTX_LOCK; +}; struct HUB_PACKET { @@ -261,8 +272,7 @@ extern struct user USER; extern struct CHANNEL_CONTEXT CHAN_CTX[MAX_CONN]; -extern struct SOCK_CONTEXT SOCK_CTX[MAX_CONN]; - +extern struct SOCK_CTL SOCK_CTL; extern FILE* LOGFP; diff --git a/include/socialize/ctl.h b/include/socialize/ctl.h index a59d387..0391971 100644 --- a/include/socialize/ctl.h +++ b/include/socialize/ctl.h @@ -63,6 +63,11 @@ void ctx_write_packet(struct HUB_PACKET* hp); void ctx_read_packet(struct HUB_PACKET* hp); +void spinlock_init(struct spinlock* spinlock); + +void spinlock_lock(struct spinlock* spinlock); + +void spinlock_unlock(struct spinlock* spinlock); #endif \ No newline at end of file diff --git a/src/ctl.c b/src/ctl.c index e8c62f0..2f9cca8 100644 --- a/src/ctl.c +++ b/src/ctl.c @@ -1,9 +1,9 @@ #include "socialize/ctl.h" #include "socialize/utils.h" -struct CHANNEL_CONTEXT CHAN_CTX[MAX_CONN]; -struct SOCK_CONTEXT SOCK_CTX[MAX_CONN]; +// TODO: +// rehashing int make_socket_non_blocking (int sfd){ @@ -254,15 +254,23 @@ struct SOCK_CONTEXT* get_sockctx_by_fd(int fd){ struct SOCK_CONTEXT* ctx; - pthread_mutex_lock(&SOCK_CTX[i].lock); + struct SOCK_CONTEXT_LOCK* ctxlock; - ctx = &SOCK_CTX[i]; + spinlock_lock(&SOCK_CTL.slock); + + ctx = SOCK_CTL.SOCK_CTX[i]; + + ctxlock = SOCK_CTL.SOCK_CTX_LOCK[i]; + + spinlock_unlock(&SOCK_CTL.slock); + + pthread_mutex_lock(&ctxlock->lock); while(ctx != NULL){ if(ctx->sockfd == fd){ - pthread_mutex_unlock(&SOCK_CTX[i].lock); + pthread_mutex_unlock(&ctxlock->lock); return ctx; @@ -272,7 +280,8 @@ struct SOCK_CONTEXT* get_sockctx_by_fd(int fd){ } - pthread_mutex_unlock(&SOCK_CTX[i].lock); + pthread_mutex_unlock(&ctxlock->lock); + //free(ctx); @@ -494,9 +503,17 @@ int calloc_sockctx(int fd){ struct SOCK_CONTEXT* ctx = NULL; - pthread_mutex_lock(&SOCK_CTX[i].lock); + struct SOCK_CONTEXT_LOCK* ctxlock = NULL; + + spinlock_lock(&SOCK_CTL.slock); + + ctx = SOCK_CTL.SOCK_CTX[i]; - ctx = &SOCK_CTX[i]; + ctxlock = SOCK_CTL.SOCK_CTX_LOCK[i]; + + spinlock_unlock(&SOCK_CTL.slock); + + pthread_mutex_lock(&ctxlock->lock); while(ctx != NULL){ @@ -508,7 +525,7 @@ int calloc_sockctx(int fd){ ctx->chan_idx = -1; ctx->allocated = 1; - pthread_mutex_unlock(&SOCK_CTX[i].lock); + pthread_mutex_unlock(&ctxlock->lock); return i; @@ -518,7 +535,7 @@ int calloc_sockctx(int fd){ } - pthread_mutex_unlock(&SOCK_CTX[i].lock); + pthread_mutex_unlock(&ctxlock->lock); return -1; } @@ -532,9 +549,17 @@ int free_sockctx(int fd, int memfree){ struct SOCK_CONTEXT* ctx = NULL; - pthread_mutex_lock(&SOCK_CTX[i].lock); + struct SOCK_CONTEXT_LOCK* ctxlock = NULL; + + spinlock_lock(&SOCK_CTL.slock); + + ctx = SOCK_CTL.SOCK_CTX[i]; - ctx = &SOCK_CTX[i]; + ctxlock = SOCK_CTL.SOCK_CTX_LOCK[i]; + + spinlock_unlock(&SOCK_CTL.slock); + + pthread_mutex_lock(&ctxlock->lock); while(ctx != NULL){ @@ -556,7 +581,7 @@ int free_sockctx(int fd, int memfree){ ctx->sockfd = 0; ctx->allocated = 0; - pthread_mutex_unlock(&SOCK_CTX[i].lock); + pthread_mutex_unlock(&ctxlock->lock); return 0; } @@ -565,7 +590,7 @@ int free_sockctx(int fd, int memfree){ } - pthread_mutex_unlock(&SOCK_CTX[i].lock); + pthread_mutex_unlock(&ctxlock->lock); return -1; } @@ -868,3 +893,30 @@ void ctx_read_packet(struct HUB_PACKET* hp){ + +static inline bool atomic_compare_exchange(int* ptr, int compare, int exchange) { + return __atomic_compare_exchange_n(ptr, &compare, exchange, + 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST); +} + +static inline void atomic_store(int* ptr, int value) { + __atomic_store_n(ptr, 0, __ATOMIC_SEQ_CST); +} + +static inline int atomic_add_fetch(int* ptr, int d) { + return __atomic_add_fetch(ptr, d, __ATOMIC_SEQ_CST); +} + + +void spinlock_init(struct spinlock* spinlock) { + atomic_store(&spinlock->locked, 0); +} + +void spinlock_lock(struct spinlock* spinlock) { + while (!atomic_compare_exchange(&spinlock->locked, 0, 1)) { + } +} + +void spinlock_unlock(struct spinlock* spinlock) { + atomic_store(&spinlock->locked, 0); +} diff --git a/src/sock/sock.c b/src/sock/sock.c index 5c591aa..5e7ddfd 100644 --- a/src/sock/sock.c +++ b/src/sock/sock.c @@ -14,19 +14,41 @@ char CA_PRIV[MAX_PW_LEN] = {0}; char CA_PUB[MAX_PW_LEN] = {0}; -int init_all(){ +struct CHANNEL_CONTEXT CHAN_CTX[MAX_CONN]; + +// struct SOCK_CONTEXT SOCK_CTX[MAX_CONN]; - for(int i = 0 ; i < MAX_CONN; i++){ +struct SOCK_CTL SOCK_CTL; + +int init_all(){ - pthread_mutex_init(&SOCK_CTX[i].lock, NULL); + for(int i = 0 ; i < MAX_CONN;i ++){ + + CHAN_CTX[i].ssl = NULL; + CHAN_CTX[i].ctx = NULL; - SOCK_CTX[i].next = NULL; } - // TODO: - // init bucket + + spinlock_init(&SOCK_CTL.slock); + + SOCK_CTL.SOCK_CTX = (struct SOCK_CONTEXT**)malloc(MAX_CONN * sizeof(struct SOCK_CONTEXT*)); + + SOCK_CTL.SOCK_CTX_LOCK = (struct SOCK_CONTEXT_LOCK**)malloc(MAX_CONN * sizeof(struct SOCK_CONTEXT_LOCK*)); + + for(int i = 0; i < MAX_CONN; i++){ + + SOCK_CTL.SOCK_CTX[i] = (struct SOCK_CONTEXT*)malloc(sizeof(struct SOCK_CONTEXT)); + + SOCK_CTL.SOCK_CTX_LOCK[i] = (struct SOCK_CONTEXT_LOCK*)malloc(sizeof(struct SOCK_CONTEXT_LOCK)); + + memset(SOCK_CTL.SOCK_CTX[i], 0, sizeof(struct SOCK_CONTEXT)); + + pthread_mutex_init(&SOCK_CTL.SOCK_CTX_LOCK[i]->lock, NULL); + + } return 0; } @@ -73,14 +95,6 @@ void sock_listen_and_serve(void* varg){ struct sockaddr_in SERVADDR; - for(int i = 0 ; i < MAX_CONN;i ++){ - - CHAN_CTX[i].ssl = NULL; - CHAN_CTX[i].ctx = NULL; - - - } - //signal(SIGPIPE, SIG_IGN); diff --git a/src/utils.c b/src/utils.c index 8509963..0779803 100644 --- a/src/utils.c +++ b/src/utils.c @@ -48,8 +48,11 @@ int gen_random_bytestream(uint8_t* bytes, size_t num_bytes){ for (i = 0; i < num_bytes; i++){ - bytes[i] = rand(); - + if (0 >getrandom(bytes + i, 1, 0)){ + + return -2; + } + } return 0;