add thread protection
authorMazet Laurent <laurent.mazet@thalesgroup.com>
Thu, 12 Jun 2025 09:43:22 +0000 (11:43 +0200)
committerMazet Laurent <laurent.mazet@thalesgroup.com>
Thu, 12 Jun 2025 09:43:22 +0000 (11:43 +0200)
morep.c

diff --git a/morep.c b/morep.c
index b9be600502e82593a4befa24bcc499797e19898e..254428e3000f829721fdfb7e4c48bad01a8128c3 100644 (file)
--- a/morep.c
+++ b/morep.c
@@ -278,9 +278,13 @@ int find_unused_morep (int sock)
     VERBOSE (morep, TRACE, PRINTF ("find_unused_morep\n"));
 
     for (int i = 0; i < MAX_MOREP_NUMBER; i++) {
-        // FIXIT: not thread safe
+#if 0
+        i// FIXIT: not thread safe
         if (MOREP_list[i]->sock < 0) {
             MOREP_list[i]->sock = sock;
+#else
+        if (__sync_bool_compare_and_swap (&MOREP_list[i]->sock, -1, sock)) {
+#endif
             VERBOSE (morep, DEBUG, PRINTF ("find morep: %d\n", i));
             return i;
         }
@@ -351,6 +355,10 @@ int MOREP_Connect (char *url)
         memcpy (MOREP_list[index]->tx_buffer + 6, sock_addr.sll_addr, 6);
         MOREP_list[index]->n_etype = htons (addr.ether_type);
         memcpy (MOREP_list[index]->tx_buffer + 12, (void *) &(MOREP_list[index]->n_etype), 2);
+    } else {
+        close (sock);
+        VERBOSE (morep, ERROR, PRINTF ("can't store MOREP for socket %d\n", sock));
+        return -1;
     }
 
     return sock;