]> git.mdlowis.com Git - proto/aos.git/commitdiff
added threading API to the standard lib
authorMichael D. Lowis <mike.lowis@gentex.com>
Thu, 3 Nov 2022 12:50:12 +0000 (08:50 -0400)
committerMichael D. Lowis <mike.lowis@gentex.com>
Thu, 3 Nov 2022 12:50:12 +0000 (08:50 -0400)
bin/mbusd.c
config.mk
inc/liba.h
lib/a/Mutex.c [new file with mode: 0644]
lib/a/Thread.c [new file with mode: 0644]
lib/a/UTF8.c

index b417d746206af9bc8376ebcce5fd15d015a2fa80..3ee86cd1fc879dc130abd71c6aa418319149958b 100644 (file)
@@ -3,24 +3,41 @@
 #define MAX_CONNS 1024
 
 char* Usage = "mbusd DIALSTR";
+
+Thread_T MessagingThread;
+Mutex_T Lock;
 long ConnectionMap[MAX_CONNS / sizeof(long)] = {0};
 
-void OnNewClient(Int cfd)
+void RegisterNewClient(Int cfd)
 {
+    Mutex_Lock(&Lock);
+    // Add fd to connection map
+    Mutex_Unlock(&Lock);
     (void)cfd;
-//    int pid = fork();
-//    if (pid < 0)
-//    {
-//        perror("fork");
-//    }
-//    else if (pid == 0)
-//    {
-////        dup2(cfd, 0);
-////        dup2(cfd, 1);
-////        dup2(cfd, 2);
-////        exit(execvp(Command[0], Command));
-//    }
-//    close(cfd);
+}
+
+Int ProcessMessages(void* arg)
+{
+    (void)arg;
+    for (;;)
+    {
+        Mutex_Lock(&Lock);
+        // for each FD in set
+            // Setup poll struct
+        Mutex_Unlock(&Lock);
+        // poll() for input
+        // for each poll struct
+            // if closed
+                Mutex_Lock(&Lock);
+                // clear it from map
+                // close fd
+                Mutex_Unlock(&Lock);
+            // else if data available
+                // read message
+                // publish message to subscribing clients
+    }
+
+    return 0;
 }
 
 int main(int argc, char** argv)
@@ -31,7 +48,9 @@ int main(int argc, char** argv)
         return 1;
     }
 
-    Net_Serve(argv[0], OnNewClient);
+    Mutex_Init(&Lock);
+    Thread_Create(&MessagingThread, ProcessMessages, NULL);
+    Net_Serve(argv[0], RegisterNewClient);
 
     return 0;
 }
index 02e0cd838ef5a31f87e838af1d620f6618c3cdae..f5e81735025e25d812603f6bbb2789ad3dc1b026 100644 (file)
--- a/config.mk
+++ b/config.mk
@@ -3,7 +3,7 @@ OUTDIR = build
 BINDIR = $(OUTDIR)/bin
 
 CC = cc
-CFLAGS = -O2 --std=c99 -pedantic -Wall -Wextra -Werror
+CFLAGS = -O2 --std=c11 -pedantic -Wall -Wextra -Werror
 CPPFLAGS = -Iinc/ -I/usr/include/freetype2
 
 AR = ar
index 1310d93cd7422cd82cd5163e2506fe153851c722..50fee0a16fd95cb6f610576b542247ccc6b53203 100644 (file)
@@ -3,8 +3,8 @@
 #include <errno.h>
 
 typedef unsigned char Byte;
-typedef long Int;
-typedef unsigned long Uint;
+typedef int Int;
+typedef unsigned int Uint;
 typedef double Real;
 typedef Int Rune;
 typedef _Bool Bool;
@@ -97,11 +97,20 @@ void Net_Serve(char* dialstr, void (*on_client)(Int cfd));
 /*
     Concurrency and Multithreading
 */
-typedef struct Thread Thread_T;
+#include <threads.h>
 
-Int Thread_Create(Thread_T* thr, void* (*func)(void*), void *arg );
+typedef thrd_t Thread_T;
+
+Int Thread_Create(Thread_T* thread, Int (*func)(void*), void *arg);
 Thread_T Thread_Current(void);
 void Thread_Yield(void);
-void Thread_Exit(Int res);
+_Noreturn void Thread_Exit(Int res);
 Int Thread_Detach(Thread_T thread);
-Int Thread_Join(Thread_T thread);
\ No newline at end of file
+Int Thread_Join(Thread_T thread);
+
+typedef mtx_t Mutex_T;
+
+Int Mutex_Init(Mutex_T* mutex);
+Int Mutex_Lock(Mutex_T* mutex);
+Int Mutex_Unlock(Mutex_T* mutex);
+Int Mutex_TryLock(Mutex_T* mutex);
\ No newline at end of file
diff --git a/lib/a/Mutex.c b/lib/a/Mutex.c
new file mode 100644 (file)
index 0000000..c3ae24d
--- /dev/null
@@ -0,0 +1,22 @@
+#include <liba.h>
+#include <threads.h>
+
+Int Mutex_Init(Mutex_T* mutex)
+{
+    return mtx_init(mutex, mtx_plain);
+}
+
+Int Mutex_Lock(Mutex_T* mutex)
+{
+    return mtx_lock(mutex);
+}
+
+Int Mutex_Unlock(Mutex_T* mutex)
+{
+    return mtx_unlock(mutex);
+}
+
+Int Mutex_TryLock(Mutex_T* mutex)
+{
+    return mtx_trylock(mutex);
+}
diff --git a/lib/a/Thread.c b/lib/a/Thread.c
new file mode 100644 (file)
index 0000000..6521313
--- /dev/null
@@ -0,0 +1,34 @@
+#include <liba.h>
+#include <threads.h>
+
+Int Thread_Create(Thread_T* thread, Int (*func)(void*), void *arg)
+{
+    return thrd_create(thread, func, arg);
+}
+
+Thread_T Thread_Current(void)
+{
+    return thrd_current();
+}
+
+void Thread_Yield(void)
+{
+    thrd_yield();
+}
+
+_Noreturn void Thread_Exit(Int res)
+{
+    thrd_exit(res);
+}
+
+Int Thread_Detach(Thread_T thread)
+{
+    return thrd_detach(thread);
+}
+
+Int Thread_Join(Thread_T thread)
+{
+    Int res = 0;
+    Int success = thrd_join(thread, &res);
+    return (success == thrd_success ? res : success);
+}
index 428c0c98bbb2b78024105587e76b9f6624a0a337..852a3ed43fd58321379270fc077d783998a8c5f7 100644 (file)
@@ -6,9 +6,9 @@ static const Byte UTF8_SeqLens[] = { 0x01u, 0x00u, 0x02u, 0x03u, 0x04u, 0x05u, 0
 
 static Bool runevalid(Rune val) {
     return (val <= RUNE_MAX)
-        && ((val & 0xFFFEu) != 0xFFFEu)
-        && ((val < 0xD800u) || (val > 0xDFFFu))
-        && ((val < 0xFDD0u) || (val > 0xFDEFu));
+        && ((val & 0xFFFE) != 0xFFFE)
+        && ((val < 0xD800) || (val > 0xDFFF))
+        && ((val < 0xFDD0) || (val > 0xFDEF));
 }
 
 static Int runelen(Rune rune) {