commit 87c298a53ab948952a0800352fdcb96edc4e3dfd
parent bf4f22e29ae75a01e73d47c0ed8ee407727d493b
Author: quantumish <freifeld.david@gmail.com>
Date: Sun, 16 Apr 2023 02:35:02 -0700
Continue process of implementing thread communication
Diffstat:
5 files changed, 46 insertions(+), 8 deletions(-)
diff --git a/http.c b/http.c
@@ -1,3 +1,5 @@
+#define _GNU_SOURCE
+
#include <bits/time.h>
#include <stdlib.h>
#include <stdio.h>
@@ -151,8 +153,18 @@ double diff_timespec(const struct timespec *time1, const struct timespec *time0)
+ (time1->tv_nsec - time0->tv_nsec) / 1000000000.0;
}
+struct handle_conn_ctxt {
+ int ns;
+ channel_t* chan;
+};
+
void* handle_conn(void* ctxt) {
- int ns = *(int*)ctxt;
+ channel_t* chan = ((struct handle_conn_ctxt *)ctxt)->chan;
+ int ns = ((struct handle_conn_ctxt *)ctxt)->ns;
+
+ pid_t tid = gettid();
+ channel_push(chan, &tid);
+
char msgbuf[1024] = {0};
while(true) {
struct timespec before, after, tdiff;
@@ -201,7 +213,6 @@ void* listen_for_conns(void* ctxt) {
int s = *(int*)ctxt;
int namelen;
struct sockaddr_in client;
- log_info("Hi!");
while (true) {
listen(s, 1);
int ns = accept(s, (struct sockaddr*)&client, (socklen_t*)&namelen);
@@ -241,11 +252,24 @@ int main() {
pthread_t lthread;
pthread_create(&lthread, NULL, listen_for_conns, &s);
+ shitvec_t channels = shitvec_new(sizeof(channel_t));
+
+ int* ns;
+ pid_t* tid;
while (true) {
- int ns = *(int*)channel_recv(&listen_chan);
-
- pthread_t thread;
- pthread_create(&thread, NULL, handle_conn, &ns);
+ if ((ns = channel_try_recv(&listen_chan))) {
+ channel_t chan = channel_new(sizeof(pid_t));
+ shitvec_push(&channels, &chan);
+ channel_t* chanp = shitvec_last(&channels);
+ struct handle_conn_ctxt ctxt = { .chan = chanp, .ns = *ns };
+ pthread_t thread;
+ pthread_create(&thread, NULL, handle_conn, &ctxt);
+ }
+ for (size_t i = 0; i < channels.vec_sz; i++) {
+ if ((tid = channel_try_recv(shitvec_get(&channels, i)))) {
+ log_debug("tid %d", *tid);
+ }
+ }
}
}
diff --git a/utils/shitvec.c b/utils/shitvec.c
@@ -19,6 +19,10 @@ void* shitvec_get(shitvec_t* sv, size_t index) {
return sv->arr+(sv->e_sz * index);
}
+void* shitvec_last(shitvec_t* sv) {
+ return shitvec_get(sv, sv->vec_sz-1);
+}
+
void shitvec_push(shitvec_t* sv, void* item) {
// FIXME sketchy af
if ((sv->arr+(2 * sv->e_sz * sv->vec_sz)) > sv->arr+sv->alloc_sz) {
diff --git a/utils/shitvec.h b/utils/shitvec.h
@@ -17,6 +17,7 @@ typedef int(*sv_cmp_t)(void*, void*);
shitvec_t shitvec_new(size_t e_sz);
void* shitvec_get(shitvec_t* sv, size_t index);
+void* shitvec_last(shitvec_t* sv);
void shitvec_push(shitvec_t* sv, void* item);
void shitvec_subpush(shitvec_t* sv, void* item, size_t sz);
bool shitvec_check(shitvec_t* sv, void* item, sv_cmp_t cmp);
diff --git a/utils/sync.c b/utils/sync.c
@@ -28,7 +28,15 @@ void* channel_pop(channel_t* chan) {
return out;
}
-void* channel_recv(channel_t * chan) {
+void* channel_recv(channel_t* chan) {
while(__atomic_load_n(&chan->sz, __ATOMIC_RELAXED) == 0);
return channel_pop(chan);
}
+
+void* channel_try_recv(channel_t* chan) {
+ if (__atomic_load_n(&chan->sz, __ATOMIC_RELAXED) > 0) {
+ return channel_pop(chan);
+ }
+ return 0x0;
+}
+
diff --git a/utils/sync.h b/utils/sync.h
@@ -14,6 +14,7 @@ typedef struct channel {
channel_t channel_new(size_t msg_sz);
void channel_push(channel_t* chan, void* msg);
void* channel_pop(channel_t* chan);
-void* channel_recv(channel_t * chan);
+void* channel_recv(channel_t* chan);
+void* channel_try_recv(channel_t* chan);
#endif