annotate seobeo/os/s_macos_edge.c @ 201:6cdee35a7ba9

[MrJuneJune] notes
author MrJuneJune <me@mrjunejune.com>
date Sun, 15 Feb 2026 07:07:50 -0800
parents a8976a008a9d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
1 #include <sys/event.h>
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
2 #include "seobeo/seobeo.h"
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
3
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
4
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
5 void *Seobeo_Web_Edge_Worker(void *vargs)
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
6 {
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
7 WorkerArgs *args = vargs;
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
8 struct kevent evlist[64];
62
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 33
diff changeset
9
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 33
diff changeset
10 // Each thread creates its own kqueue to avoid race conditions
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 33
diff changeset
11 int kq = kqueue();
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 33
diff changeset
12 if (kq < 0) {
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 33
diff changeset
13 perror("kqueue");
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 33
diff changeset
14 return NULL;
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 33
diff changeset
15 }
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 33
diff changeset
16
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 33
diff changeset
17 // Add server socket to this thread's kqueue
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 33
diff changeset
18 struct kevent kev = {
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 33
diff changeset
19 .ident = args->srv->socket,
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 33
diff changeset
20 .filter = EVFILT_READ,
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 33
diff changeset
21 .flags = EV_ADD,
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 33
diff changeset
22 .udata = args->srv
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 33
diff changeset
23 };
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 33
diff changeset
24 kevent(kq, &kev, 1, NULL, 0, NULL);
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 33
diff changeset
25
33
c0f6c8c7829f [Seobeo] Linux epoll. Set the client socket to be nonblocking so that it doesn't stop loading when two different threads handle different client calls. I might have problem with socket not being cleaned up properly so need to check that.
MrJuneJune <me@mrjunejune.com>
parents: 19
diff changeset
26 while (1)
c0f6c8c7829f [Seobeo] Linux epoll. Set the client socket to be nonblocking so that it doesn't stop loading when two different threads handle different client calls. I might have problem with socket not being cleaned up properly so need to check that.
MrJuneJune <me@mrjunejune.com>
parents: 19
diff changeset
27 {
62
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 33
diff changeset
28 int ne = kevent(kq, NULL, 0, evlist, 64, NULL);
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 33
diff changeset
29 if (ne < 0) {
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 33
diff changeset
30 if (errno == EINTR) continue;
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 33
diff changeset
31 perror("kevent");
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 33
diff changeset
32 continue;
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 33
diff changeset
33 }
33
c0f6c8c7829f [Seobeo] Linux epoll. Set the client socket to be nonblocking so that it doesn't stop loading when two different threads handle different client calls. I might have problem with socket not being cleaned up properly so need to check that.
MrJuneJune <me@mrjunejune.com>
parents: 19
diff changeset
34
c0f6c8c7829f [Seobeo] Linux epoll. Set the client socket to be nonblocking so that it doesn't stop loading when two different threads handle different client calls. I might have problem with socket not being cleaned up properly so need to check that.
MrJuneJune <me@mrjunejune.com>
parents: 19
diff changeset
35 for (int i = 0; i < ne; i++)
c0f6c8c7829f [Seobeo] Linux epoll. Set the client socket to be nonblocking so that it doesn't stop loading when two different threads handle different client calls. I might have problem with socket not being cleaned up properly so need to check that.
MrJuneJune <me@mrjunejune.com>
parents: 19
diff changeset
36 {
66
a0f0ad5e42eb [Misc] taking out capital P stuff.
June Park <parkjune1995@gmail.com>
parents: 65
diff changeset
37 Seobeo_Handle *h = evlist[i].udata;
33
c0f6c8c7829f [Seobeo] Linux epoll. Set the client socket to be nonblocking so that it doesn't stop loading when two different threads handle different client calls. I might have problem with socket not being cleaned up properly so need to check that.
MrJuneJune <me@mrjunejune.com>
parents: 19
diff changeset
38 if (h == args->srv)
c0f6c8c7829f [Seobeo] Linux epoll. Set the client socket to be nonblocking so that it doesn't stop loading when two different threads handle different client calls. I might have problem with socket not being cleaned up properly so need to check that.
MrJuneJune <me@mrjunejune.com>
parents: 19
diff changeset
39 {
62
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 33
diff changeset
40 // Accept new connections in a loop (for edge-triggered behavior)
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 33
diff changeset
41 while (1) {
66
a0f0ad5e42eb [Misc] taking out capital P stuff.
June Park <parkjune1995@gmail.com>
parents: 65
diff changeset
42 Seobeo_Handle *cli = Seobeo_Stream_Handle_Server_Accept(args->srv);
62
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 33
diff changeset
43 if (!cli) break;
33
c0f6c8c7829f [Seobeo] Linux epoll. Set the client socket to be nonblocking so that it doesn't stop loading when two different threads handle different client calls. I might have problem with socket not being cleaned up properly so need to check that.
MrJuneJune <me@mrjunejune.com>
parents: 19
diff changeset
44
62
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 33
diff changeset
45 struct kevent client_kev = {
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 33
diff changeset
46 .ident = cli->socket,
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 33
diff changeset
47 .filter = EVFILT_READ,
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 33
diff changeset
48 .flags = EV_ADD | EV_ONESHOT,
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 33
diff changeset
49 .udata = cli
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 33
diff changeset
50 };
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 33
diff changeset
51 kevent(kq, &client_kev, 1, NULL, 0, NULL);
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 33
diff changeset
52 }
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 33
diff changeset
53 } else {
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 33
diff changeset
54 // Remove from kqueue first
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 33
diff changeset
55 struct kevent del_kev = {
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 33
diff changeset
56 .ident = h->socket,
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
57 .filter = EVFILT_READ,
62
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 33
diff changeset
58 .flags = EV_DELETE,
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
59 };
62
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 33
diff changeset
60 kevent(kq, &del_kev, 1, NULL, 0, NULL);
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 33
diff changeset
61
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 33
diff changeset
62 // Handle request (this function destroys the handle internally)
183
a8976a008a9d [BenchMark] Added bun bench mark to test seoboe vs other popular benchmarks.
MrJuneJune <me@mrjunejune.com>
parents: 71
diff changeset
63 // TODO: Add keep-alive support for macOS like Linux version
a8976a008a9d [BenchMark] Added bun bench mark to test seoboe vs other popular benchmarks.
MrJuneJune <me@mrjunejune.com>
parents: 71
diff changeset
64 Seobeo_Web_ClientHandle_Request(h, args->cache, FALSE);
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
65 }
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
66 }
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
67 }
62
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 33
diff changeset
68
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 33
diff changeset
69 close(kq);
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
70 return NULL;
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
71 }
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
72
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
73 void Seobeo_Web_Edge(
66
a0f0ad5e42eb [Misc] taking out capital P stuff.
June Park <parkjune1995@gmail.com>
parents: 65
diff changeset
74 Seobeo_Handle *p_server_handle,
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
75 int thread_count,
71
75de5903355c Giagantic changes that update Dowa library to be more align with stb style array and hashmap. Updated Seobeo to be caching on server side instead of file level caching. Deleted bunch of things I don't really use.
June Park <parkjune1995@gmail.com>
parents: 66
diff changeset
76 Seobeo_Cache_Entry *p_html_cache)
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
77 {
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
78 pthread_attr_t attr;
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
79 pthread_attr_init(&attr);
33
c0f6c8c7829f [Seobeo] Linux epoll. Set the client socket to be nonblocking so that it doesn't stop loading when two different threads handle different client calls. I might have problem with socket not being cleaned up properly so need to check that.
MrJuneJune <me@mrjunejune.com>
parents: 19
diff changeset
80 pthread_attr_setstacksize(&attr, 5 * 1024 * 1024); // 5 MB
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
81
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
82 pthread_t threads[thread_count];
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
83 for (int i = 0; i < thread_count; i++)
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
84 {
18
fa2b8af609d9 [Seobeo] Fixed a bug with pathing. Support SSL.
June Park <parkjune1995@gmail.com>
parents: 17
diff changeset
85 WorkerArgs *args = malloc(sizeof(WorkerArgs));
62
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 33
diff changeset
86 *args = (WorkerArgs){ p_server_handle, p_html_cache };
18
fa2b8af609d9 [Seobeo] Fixed a bug with pathing. Support SSL.
June Park <parkjune1995@gmail.com>
parents: 17
diff changeset
87
62
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 33
diff changeset
88 pthread_create(&threads[i], &attr, Seobeo_Web_Edge_Worker, args);
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
89 }
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
90 for (int i = 0; i < thread_count; i++)
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
91 {
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
92 pthread_join(threads[i], NULL);
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
93 }
62
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 33
diff changeset
94
ea9ef388ab97 [Seobeo] Fixed issues with epoll or kqeue in different threads. Initizlied the event looop inside of the thread itself.
June Park <parkjune1995@gmail.com>
parents: 33
diff changeset
95 pthread_attr_destroy(&attr);
7
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
96 return;
114cad94008f [Seobeo] Updated to support thread and edge server calls.
June Park <parkjune1995@gmail.com>
parents:
diff changeset
97 }