tomo/examples/http-server/connection-queue.tm

26 lines
717 B
Plaintext
Raw Permalink Normal View History

2025-04-01 18:15:17 -07:00
use pthreads
2025-04-06 13:07:23 -07:00
func _assert_success(name:Text, val:Int32; inline)
2025-04-01 18:15:17 -07:00
fail("$name() failed!") if val < 0
2025-04-06 13:07:23 -07:00
struct ConnectionQueue(_connections:@[Int32]=@[], _mutex=pthread_mutex_t.new(), _cond=pthread_cond_t.new())
func enqueue(queue:ConnectionQueue, connection:Int32)
queue._mutex.lock()
queue._connections.insert(connection)
queue._mutex.unlock()
queue._cond.signal()
2025-04-01 18:15:17 -07:00
2025-04-06 13:07:23 -07:00
func dequeue(queue:ConnectionQueue -> Int32)
conn : Int32?
2025-04-01 18:15:17 -07:00
queue._mutex.lock()
2025-04-01 18:15:17 -07:00
2025-04-06 13:07:23 -07:00
while queue._connections.length == 0
queue._cond.wait(queue._mutex)
2025-04-01 18:15:17 -07:00
conn = queue._connections.pop(1)
queue._mutex.unlock()
queue._cond.signal()
2025-04-01 18:15:17 -07:00
return conn!