]> git.feebdaed.xyz Git - socialize.git/commitdiff
add: rehashable concurrent map
authorseantywork <seantywork@gmail.com>
Wed, 12 Mar 2025 11:04:31 +0000 (11:04 +0000)
committerseantywork <seantywork@gmail.com>
Wed, 12 Mar 2025 11:04:31 +0000 (11:04 +0000)
include/socialize/core.h
include/socialize/ctl.h
src/ctl.c
src/sock/sock.c
src/utils.c

index a70e09c0652677dbc91182fec37cd0326a4a3f37..00c99b78f5b804d6874a3fddcbb9a735a60f4f12 100644 (file)
@@ -10,6 +10,7 @@
 #include <stdint.h>
 #include <sys/types.h>
 #include <sys/socket.h>  
+#include <sys/random.h>
 #include <unistd.h> 
 #include <time.h>
 #include <endian.h>
 
 
 
-
+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;
index a59d38796a5b32a4ef61935b97543389e93d1f7b..0391971a0462f953e8762149cc3328ebd07cf804 100644 (file)
@@ -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
index e8c62f0892be1f9c81e82dcca4a8f0b87dc2d7fd..2f9cca8388bb43dc85d35f615e7803d437b24af1 100644 (file)
--- 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);
+}
index 5c591aa57b13651b894d8ee406a4670488c9614a..5e7ddfd293451ccb36751a98615c84009d2dd4db 100644 (file)
@@ -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);
 
index 8509963b8023cf3a7caa958ce5935d8edfb16009..07798031655ac2d9a60ad811819797d31ddbf1d9 100644 (file)
@@ -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;