aboutsummaryrefslogtreecommitdiff
path: root/examples/http-server/connection-queue.tm
blob: 5cf8bb91e00a33b59a233d8a228c2e604b638a9e (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_v1.0

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!