thread does not work the way I thought it did in Odin
This commit is contained in:
24
http.odin
24
http.odin
@@ -204,14 +204,14 @@ server_start :: proc(server: ^Server) -> bool {
|
|||||||
conn_data.conn = conn
|
conn_data.conn = conn
|
||||||
conn_data.source = source
|
conn_data.source = source
|
||||||
|
|
||||||
// Spawn a new thread for this connection
|
// Spawn with self_cleanup, thread resources are freed automatically on exit
|
||||||
t := thread.create(connection_worker_thread)
|
t := thread.create_and_start_with_data(
|
||||||
if t != nil {
|
conn_data,
|
||||||
//t.init_context = context // We dont actually need need to share the main threads context
|
connection_worker,
|
||||||
t.data = conn_data
|
self_cleanup = true,
|
||||||
thread.start(t)
|
)
|
||||||
} else {
|
|
||||||
// Failed to create thread, close connection
|
if t == nil {
|
||||||
net.close(conn)
|
net.close(conn)
|
||||||
free(conn_data, server.allocator)
|
free(conn_data, server.allocator)
|
||||||
}
|
}
|
||||||
@@ -230,11 +230,9 @@ server_stop :: proc(server: ^Server) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Worker thread procedure
|
// Spawn connection worker with rawptr instead of a new thread
|
||||||
connection_worker_thread :: proc(t: ^thread.Thread) {
|
connection_worker :: proc(data: rawptr) {
|
||||||
defer thread.destroy(t)
|
conn_data := cast(^Connection_Task_Data)data
|
||||||
|
|
||||||
conn_data := cast(^Connection_Task_Data)t.data
|
|
||||||
defer free(conn_data, conn_data.server.allocator)
|
defer free(conn_data, conn_data.server.allocator)
|
||||||
|
|
||||||
handle_connection(conn_data.server, conn_data.conn, conn_data.source)
|
handle_connection(conn_data.server, conn_data.conn, conn_data.source)
|
||||||
|
|||||||
Reference in New Issue
Block a user