aboutsummaryrefslogtreecommitdiff
path: root/examples/http-server/connection-queue.tm
blob: c56069e1a9bda256c752b2a4856edf44cbfa428b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
use pthreads

func _assert_success(name:Text, val:Int32; inline)
    fail("$name() failed!") if val < 0

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()


    func dequeue(queue:ConnectionQueue -> Int32)
        conn : Int32?

        queue._mutex.lock()

        while queue._connections.length == 0
            queue._cond.wait(queue._mutex)

        conn = queue._connections.pop(1)
        queue._mutex.unlock()
        queue._cond.signal()
        return conn!