|
| 1 | +use taskBackend "kiana/taskBackend" |
| 2 | +use request "kiana/request" |
| 3 | +use response "kiana/response" |
| 4 | +use socket "net/socket" |
| 5 | +use threading "threading" |
| 6 | +use vec "vec" |
| 7 | +use lang "builtin" |
| 8 | + |
| 9 | +struct PoolTask { |
| 10 | + sock: socket.AbstractSocket, |
| 11 | + resp_func: func() : response.Response, |
| 12 | + constructor(sock: socket.AbstractSocket, resp_func: func() : response.Response) |
| 13 | +} |
| 14 | + |
| 15 | +impl PoolTask { |
| 16 | + constructor(sock: socket.AbstractSocket, resp_func: func() : response.Response) { |
| 17 | + this.sock = sock |
| 18 | + this.resp_func = resp_func |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +struct TaskPoolBackend { |
| 23 | + thread_count: int, |
| 24 | + tasks: vec.Vec<PoolTask>, |
| 25 | + head: int, |
| 26 | + mutex: threading.Mutex, |
| 27 | + condition: threading.Condition, |
| 28 | + workers: vec.Vec<threading.Thread>, |
| 29 | + stop_flag: bool, |
| 30 | + constructor(), |
| 31 | + constructor(thread_count: int), |
| 32 | + worker_loop() : none |
| 33 | +} |
| 34 | + |
| 35 | +impl TaskPoolBackend { |
| 36 | + constructor() { |
| 37 | + this.thread_count = threading.hardware_concurrency() |
| 38 | + this.tasks = vec.Vec<PoolTask>() |
| 39 | + this.head = 0 |
| 40 | + this.mutex = threading.Mutex() |
| 41 | + this.condition = threading.Condition(this.mutex) |
| 42 | + this.workers = vec.Vec<threading.Thread>() |
| 43 | + this.stop_flag = false |
| 44 | + }, |
| 45 | + constructor(thread_count: int) { |
| 46 | + this.thread_count = thread_count |
| 47 | + this.tasks = vec.Vec<PoolTask>() |
| 48 | + this.head = 0 |
| 49 | + this.mutex = threading.Mutex() |
| 50 | + this.condition = threading.Condition(this.mutex) |
| 51 | + this.workers = vec.Vec<threading.Thread>() |
| 52 | + this.stop_flag = false |
| 53 | + }, |
| 54 | + worker_loop() : none { |
| 55 | + while (true) { |
| 56 | + let task_opt : PoolTask = null |
| 57 | + |
| 58 | + this.mutex.lock() |
| 59 | + while (this.stop_flag == false && this.head == this.tasks.length) { |
| 60 | + this.condition.wait() |
| 61 | + } |
| 62 | + |
| 63 | + if (this.stop_flag && this.head == this.tasks.length) { |
| 64 | + this.mutex.unlock() |
| 65 | + return |
| 66 | + } |
| 67 | + |
| 68 | + task_opt = this.tasks[this.head] |
| 69 | + this.head += 1 |
| 70 | + |
| 71 | + if (this.head == this.tasks.length) { |
| 72 | + this.tasks.length = 0 |
| 73 | + this.head = 0 |
| 74 | + } |
| 75 | + |
| 76 | + this.mutex.unlock() |
| 77 | + |
| 78 | + // Execute task |
| 79 | + if (task_opt != null) { |
| 80 | + task_opt.resp_func().write_to(task_opt.sock) |
| 81 | + task_opt.sock.close() |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +impl TaskPoolBackend : taskBackend.TaskBackend { |
| 88 | + init() : bool { |
| 89 | + for (let i = 0; i < this.thread_count; i += 1) { |
| 90 | + let thread = threading.Thread(func[this]() : none { |
| 91 | + this.this.worker_loop() |
| 92 | + }) |
| 93 | + if (thread.start().is_err()) { |
| 94 | + return false |
| 95 | + } |
| 96 | + this.workers.push(thread) |
| 97 | + } |
| 98 | + return true |
| 99 | + }, |
| 100 | + |
| 101 | + new_task(sock: socket.AbstractSocket, resp : func() : response.Response) : none { |
| 102 | + this.mutex.lock() |
| 103 | + this.tasks.push(PoolTask(sock, resp)) |
| 104 | + this.condition.signal() |
| 105 | + this.mutex.unlock() |
| 106 | + }, |
| 107 | + |
| 108 | + stop() : none { |
| 109 | + this.mutex.lock() |
| 110 | + this.stop_flag = true |
| 111 | + this.condition.signal() // Might need notify_all if kiana supported it, but signal in a loop/multiple times |
| 112 | + this.mutex.unlock() |
| 113 | + |
| 114 | + // Signal everyone |
| 115 | + for (let i = 0; i < this.workers.length; i += 1) { |
| 116 | + this.mutex.lock() |
| 117 | + this.condition.signal() |
| 118 | + this.mutex.unlock() |
| 119 | + } |
| 120 | + |
| 121 | + for (let i = 0; i < this.workers.length; i += 1) { |
| 122 | + this.workers[i].join() |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments