vendor/routine.nim
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 |
# https://github.com/rogercloud/nim-routine import os, locks, lists, tables, macros, cpuinfo const debug = false proc print[T](data: T) = when debug: echo data # Thread type BreakState* = object isContinue: bool # tell whether this yield need to be continued later isSend: bool # this yield is caused by a send operation msgBoxPtr: pointer # this msgBox's pointer (void*) that makes this yield TaskBody* = (iterator(tl: TaskList, t: ptr Task, arg:pointer): BreakState{.closure.}) Task* = object isRunable: bool # if the task is runnable task: TaskBody arg: pointer watcher: TaskWatcher hasArg: bool TaskList* = ptr TaskListObj TaskListObj = object index: int # for debug lock: Lock # Protect list candiLock: Lock # Protect send and recv candidate list: DoublyLinkedRing[Task] size: int # list's size recvWaiter: Table[pointer, seq[ptr Task]] sendWaiter: Table[pointer, seq[ptr Task]] sendCandidate: seq[pointer] recvCandidate: seq[pointer] TaskWatcher* = ref TaskWatcherObj TaskWatcherObj = object isFinished: bool lock: Lock var threadPoolSize = 4.Natural var taskListPool = newSeq[TaskListObj](threadPoolSize) var threadPool= newSeq[Thread[TaskList]](threadPoolSize) proc isEmpty(tasks: TaskList): bool= result = tasks.list.head == nil proc isEmpty(tasks: TaskListObj): bool= result = tasks.list.head == nil proc run(taskNode: DoublyLinkedNode[Task], tasks: TaskList, t: ptr Task): BreakState {.inline.} = if t.hasArg: result = taskNode.value.task(tasks, t, t.arg) else: result = taskNode.value.task(tasks, t, nil) # Run a task, return false if no runnable task found proc runTask(tasks: TaskList, tracker: var DoublyLinkedNode[Task]): bool {.gcsafe.} = if tracker == nil: tracker = tasks.list.head let start = tracker while not tasks.isEmpty: if tracker.value.isRunable: tasks.lock.release() let ret = tracker.run(tasks, tracker.value.addr) tasks.lock.acquire() if not ret.isContinue: #print("one task finished") let temp = tracker.next if tracker.value.arg != nil: tracker.value.arg.deallocShared() # free task argument tracker.value.watcher.lock.acquire() tracker.value.watcher.isFinished = true tracker.value.watcher.lock.release() tasks.list.remove(tracker) tasks.size -= 1 if tasks.isEmpty: #print("tasks is empty") tracker = nil else: tracker = temp return true else: # tracker.value.isRunable tracker = tracker.next if tracker == start: return false return false proc wakeUp(tasks: TaskList) = tasks.candiLock.acquire() if tasks.sendCandidate.len > 0: for scMsg in tasks.sendCandidate: if tasks.sendWaiter.hasKey(scMsg): for t in tasks.sendWaiter.mget(scMsg): t.isRunable = true tasks.sendWaiter[scMsg] = newSeq[ptr Task]() tasks.sendCandidate = newSeq[pointer]() if tasks.recvCandidate.len > 0: for rcMsg in tasks.recvCandidate: if tasks.recvWaiter.hasKey(rcMsg): for t in tasks.recvWaiter.mget(rcMsg): t.isRunable = true tasks.recvWaiter[rcMsg] = newSeq[ptr Task]() tasks.recvCandidate = newSeq[pointer]() tasks.candiLock.release() proc slave(tasks: TaskList) {.thread, gcsafe.} = var tracker:DoublyLinkedNode[Task] = nil #print($tasks.index & "init ac") tasks.lock.acquire() while true: if not runTask(tasks, tracker): #print($tasks.index & "sleep re") tasks.lock.release() #print("task list is empty:" & $(tasks.isEmpty)) sleep(0) #print($tasks.index & "sleep ac") tasks.lock.acquire() wakeUp(tasks) proc chooseTaskList: int = var minSize = taskListPool[0].size var minIndex = 0 for i, tl in taskListPool: if tl.size < minSize: minSize = tl.size minIndex = i return minIndex proc pRun* (iter: TaskBody): TaskWatcher {.discardable.} = new(result) result.lock.initLock() result.isFinished = false let index = chooseTaskList() taskListPool[index].lock.acquire() taskListPool[index].list.append(Task(isRunable:true, task:iter, arg: nil, watcher: result, hasArg: false)) taskListPool[index].size += 1 taskListPool[index].lock.release() proc pRun* [T](iter: TaskBody, arg: T): TaskWatcher {.discardable.} = new(result) result.lock.initLock() result.isFinished = false let index = chooseTaskList() var p = cast[ptr T](allocShared0(sizeof(T))) p[] = arg taskListPool[index].lock.acquire() taskListPool[index].list.append(Task(isRunable:true, task:iter, arg: cast[pointer](p), watcher: result, hasArg: true)) taskListPool[index].size += 1 taskListPool[index].lock.release() proc initThread(index: int) = taskListPool[index].index = index taskListPool[index].list = initDoublyLinkedRing[Task]() taskListPool[index].lock.initLock() taskListPool[index].candiLock.initLock() taskListPool[index].sendWaiter = initTable[pointer, seq[ptr Task]]() taskListPool[index].recvWaiter = initTable[pointer, seq[ptr Task]]() taskListPool[index].sendCandidate = newSeq[pointer]() taskListPool[index].recvCandidate = newSeq[pointer]() createThread(threadPool[index], slave, taskListPool[index].addr) proc setup = for i in 0..<threadPoolSize: initThread(i) var cpuCount = countProcessors() if cpuCount != 0: threadPoolSize = cpuCount setup() # MsgBox type MsgBox* [T] = ptr MsgBoxObject[T] MsgBoxObject[T] = object cap: int # capability of this MsgBox, if < 0, unlimited size: int # real size of this MsgBox lock: Lock # MsgBox protection lock isSync: bool # sync msgBox flag data: DoublyLinkedList[T] # data holder recvWaiter: seq[TaskList] # recv waiter's TaskList sendWaiter: seq[TaskList] # send waiter's TaskList proc createMsgBox* [T](cap:int = -1): MsgBox[T] = result = cast[MsgBox[T]](allocShared0(sizeof(MsgBoxObject[T]))) result.cap = cap result.size = 0 result.isSync = false result.lock.initLock() result.data = initDoublyLinkedList[T]() result.recvWaiter = newSeq[TaskList]() result.sendWaiter = newSeq[TaskList]() proc createSyncMsgBox* [T]: MsgBox[T] = result = createMsgBox[T](1) result.isSync = true proc deleteMsgBox* [T](msgBox: MsgBox[T]) = msgBox.lock.deinitLock() msgBox.deallocShared() proc registerSend[T](tl: TaskList, msgBox: MsgBox[T], t: ptr Task) = msgBox.sendWaiter.add(tl) let msgBoxPtr = cast[pointer](msgBox) if not tl.sendWaiter.hasKey(msgBoxPtr): tl.sendWaiter[msgBoxPtr] = newSeq[ptr Task]() tl.sendWaiter.mget(msgBoxPtr).add(t) proc registerRecv[T](tl: TaskList, msgBox: MsgBox[T], t: ptr Task) = msgBox.recvWaiter.add(tl) let msgBoxPtr = cast[pointer](msgBox) if not tl.recvWaiter.hasKey(msgBoxPtr): tl.recvWaiter[msgBoxPtr] = newSeq[ptr Task]() tl.recvWaiter.mget(msgBoxPtr).add(t) proc notifySend[T](msgBox: MsgBox[T]) = for tl in msgBox.sendWaiter: tl.candiLock.acquire() tl.sendCandidate.add(cast[pointer](msgBox)) tl.candiLock.release() msgBox.sendWaiter = newSeq[TaskList]() proc notifyRecv[T](msgBox: MsgBox[T]) = for tl in msgBox.recvWaiter: tl.candiLock.acquire() tl.recvCandidate.add(cast[pointer](msgBox)) tl.candiLock.release() msgBox.recvWaiter = newSeq[TaskList]() template sendWaitForMsgBox(tl, msgBox, t: expr):stmt {.immediate.} = registerSend(tl, msgBox, t) t.isRunable = false msgBox.lock.release() yield BreakState(isContinue: true, isSend: true, msgBoxPtr: cast[pointer](msgBox)) msgBox.lock.acquire() template send*(msgBox, msg: expr):stmt {.immediate.}= print("template send acquire") msgBox.lock.acquire() print("template send after acquire") while true: if msgBox.cap < 0 or msgBox.size < msgBox.cap: msgBox.data.append(msg) msgBox.size += 1 print("notifyRecv") notifyRecv(msgBox) if msgBox.isSync: # sync msgBox sendWaitForMsgBox(tl, msgBox, t) break else: sendWaitForMsgBox(tl, msgBox, t) msgBox.lock.release() template recv*(msgBox, msg: expr): stmt {.immediate.} = print("template recv acquire") msgBox.lock.acquire() print("template recv after acquire") while true: if msgBox.size > 0: print("template recv") msg = msgBox.data.head.value msgBox.data.remove(msgBox.data.head) # O(1) msgBox.size -= 1 notifySend(msgBox) break else: #print("recv wait") registerRecv(tl, msgBox, t) t.isRunable = false msgBox.lock.release() yield BreakState(isContinue: true, isSend: false, msgBoxPtr: cast[pointer](msgBox)) msgBox.lock.acquire() msgBox.lock.release() ## Macro proc getName(node: NimNode): string {.compileTime.} = case node.kind of nnkPostfix: return $node[1].ident of nnkIdent: return $node.ident of nnkEmpty: return "anonymous" else: error("Unknown name.") proc routineSingleProc(prc: NimNode): NimNode {.compileTime.} = if prc.kind notin {nnkProcDef, nnkLambda}: error("Cannot transform this node kind into an nim routine." & " Proc definition or lambda node expected.") hint("Processing " & prc[0].getName & " as an nim routine") let returnType = prc[3][0] # Verify that the return type is a void or Empty if returnType.kind != nnkEmpty and not (returnType.kind == nnkIdent and returnType[0].ident == !"void"): error("Expected return type of void got '" & $returnType & "'") else: hint("return type is void or empty") result = newStmtList() var procBody = prc[6] # -> var rArg = (cast[ptr tuple[arg1: T1, arg2: T2, ...]](arg))[] if prc[3].len > 1: var rArgAssignment = newNimNode(nnkVarSection) var tupleList = newNimNode(nnkTupleTy) for i in 1 ..< prc[3].len: let param = prc[3][i] assert(param.kind == nnkIdentDefs) tupleList.add(param) rArgAssignment.add( newIdentDefs( ident("rArg"), newEmptyNode(), newNimNode(nnkBracketExpr).add( newNimNode(nnkPar).add( newNimNode(nnkCast).add( newNimNode(nnkPtrTy).add(tupleList), newIdentNode("arg")))))) # -> var arg1 = rArg.arg1 # -> var arg2 = rArg.arg2 # -> ... for i in 1 ..< prc[3].len: let param = prc[3][i] assert(param.kind == nnkIdentDefs) for j in 0 .. param.len - 3: rArgAssignment.add( newIdentDefs( param[j], newEmptyNode(), newNimNode(nnkDotExpr).add( ident("rArg"), param[j]))) procBody.insert(0, rArgAssignment) var closureIterator = newProc( newIdentNode($prc[0].getName), [ newIdentNode("BreakState"), newIdentDefs(ident("tl"), ident("TaskList")), newIdentDefs(ident("t"), newNimNode(nnkPtrTy).add(newIdentNode("Task"))), newIdentDefs(ident("arg"), ident("pointer")) ], procBody, nnkIteratorDef) closureIterator[4] = newNimNode(nnkPragma).add(newIdentNode("closure")) # Add generic closureIterator[2] = prc[2] result.add(closureIterator) macro routine*(prc: stmt): stmt {.immediate.} = ## Macro which processes async procedures into the appropriate ## iterators and yield statements. if prc.kind == nnkStmtList: result = newStmtList() for oneProc in prc: result.add routineSingleProc(oneProc) else: result = routineSingleProc(prc) proc waitAllRoutine* = var allFinished = true while true: for i in 0 ..< threadPoolSize: if not taskListPool[i].isEmpty: allFinished = false break if allFinished: return allFinished = true sleep(0) proc isDone(watcher: TaskWatcher): bool = watcher.lock.acquire() result = watcher.isFinished watcher.lock.release() proc wait*(watcher: TaskWatcher) = while not watcher.isDone(): sleep(0) |