Make thread count adjustable

This commit is contained in:
Hugo Mårdbrink 2025-11-13 16:40:18 +01:00
parent 0bf25b991f
commit 595872f81c
3 changed files with 26 additions and 17 deletions

View file

@ -23,9 +23,11 @@ Server :: struct($Error_Type: typeid) {
http.Response,
Error_Type,
),
shutdown: bool,
running: bool,
router: Router(Error_Type),
thread_pool: thread.Pool,
thread_count: uint,
allocator: runtime.Allocator,
}
@(private)
@ -62,6 +64,7 @@ read_connection :: proc(
server_init :: proc(
server: ^Server($Error_Type),
endpoint: net.Endpoint,
thread_count: uint,
allocator := context.allocator,
) {
server^ = Server(Error_Type) {
@ -80,11 +83,12 @@ server_init :: proc(
)
return http_response, nil
},
shutdown = false,
thread_count = thread_count,
running = false,
allocator = allocator,
}
THREAD_COUNT :: 8
thread.pool_init(&server.thread_pool, allocator, THREAD_COUNT)
thread.pool_init(&server.thread_pool, allocator, int(server.thread_count))
thread.pool_start(&server.thread_pool)
router_init(&server.router, allocator)
}
@ -131,7 +135,7 @@ server_remove_route :: proc(
}
server_shutdown :: proc(server: ^Server($Error_Type)) {
sync.atomic_store(&server.shutdown, true)
sync.atomic_store(&server.running, false)
}
@(private)
@ -192,7 +196,7 @@ request_thread_task :: proc(t: thread.Task) {
serve(task_data.server, task_data.client_socket)
net.close(task_data.client_socket)
free_all(context.temp_allocator)
// context.temp_allocator is freed automatically on thread death.
}
@(private)
@ -206,6 +210,7 @@ spawn_request_thread :: proc(
client_socket = client_socket,
}
// Not sure about sharing the default non temp allocator, have to think about
thread.pool_add_task(
&server.thread_pool,
context.allocator,
@ -219,7 +224,9 @@ server_listen_and_serve :: proc(server: ^Server($Error_Type)) {
log.assert(net_err == nil, "Couldn't create TCP socket")
defer net.close(server_socket)
for !sync.atomic_load(&server.shutdown) {
sync.atomic_store(&server.running, true)
for sync.atomic_load(&server.running) {
client_socket, _, net_err := net.accept_tcp(server_socket)
if net_err != nil {
log.warnf("Failed to accept TCP connection, reason: %s", net_err)