tasks.nim 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2021 Nim contributors
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## This module provides basic primitives for creating parallel programs.
  10. ## A `Task` should be only owned by a single Thread, it cannot be shared by threads.
  11. import std/[macros, isolation, typetraits]
  12. import system/ansi_c
  13. when defined(nimPreviewSlimSystem):
  14. import std/assertions
  15. export isolation
  16. when compileOption("threads"):
  17. from std/effecttraits import isGcSafe
  18. #
  19. # proc hello(a: int, b: string) =
  20. # echo $a & b
  21. #
  22. # let literal = "Nim"
  23. # let t = toTask(hello(521, literal))
  24. #
  25. #
  26. # is roughly converted to
  27. #
  28. # type
  29. # ScratchObj_369098780 = object
  30. # a: int
  31. # b: string
  32. #
  33. # let scratch_369098762 = cast[ptr ScratchObj_369098780](c_calloc(csize_t 1,
  34. # csize_t sizeof(ScratchObj_369098780)))
  35. # if scratch_369098762.isNil:
  36. # raise newException(OutOfMemDefect, "Could not allocate memory")
  37. # block:
  38. # var isolate_369098776 = isolate(521)
  39. # scratch_369098762.a = extract(isolate_369098776)
  40. # var isolate_369098778 = isolate(literal)
  41. # scratch_369098762.b = extract(isolate_369098778)
  42. # proc hello_369098781(args`gensym3: pointer) {.nimcall.} =
  43. # let objTemp_369098775 = cast[ptr ScratchObj_369098780](args`gensym3)
  44. # let :tmp_369098777 = objTemp_369098775.a
  45. # let :tmp_369098779 = objTemp_369098775.b
  46. # hello(a = :tmp_369098777, b = :tmp_369098779)
  47. #
  48. # proc destroyScratch_369098782(args`gensym3: pointer) {.nimcall.} =
  49. # let obj_369098783 = cast[ptr ScratchObj_369098780](args`gensym3)
  50. # =destroy(obj_369098783[])
  51. # let t = Task(callback: hello_369098781, args: scratch_369098762, destroy: destroyScratch_369098782)
  52. #
  53. type
  54. Task* = object ## `Task` contains the callback and its arguments.
  55. callback: proc (args: pointer) {.nimcall, gcsafe.}
  56. args: pointer
  57. destroy: proc (args: pointer) {.nimcall, gcsafe.}
  58. proc `=copy`*(x: var Task, y: Task) {.error.}
  59. proc `=destroy`*(t: var Task) {.inline, gcsafe.} =
  60. ## Frees the resources allocated for a `Task`.
  61. if t.args != nil:
  62. if t.destroy != nil:
  63. t.destroy(t.args)
  64. c_free(t.args)
  65. proc invoke*(task: Task) {.inline, gcsafe.} =
  66. ## Invokes the `task`.
  67. assert task.callback != nil
  68. task.callback(task.args)
  69. template checkIsolate(scratchAssignList: seq[NimNode], procParam, scratchDotExpr: NimNode) =
  70. # block:
  71. # var isoTempA = isolate(521)
  72. # scratch.a = extract(isolateA)
  73. # var isoTempB = isolate(literal)
  74. # scratch.b = extract(isolateB)
  75. let isolatedTemp = genSym(nskTemp, "isoTemp")
  76. scratchAssignList.add newVarStmt(isolatedTemp, newCall(newIdentNode("isolate"), procParam))
  77. scratchAssignList.add newAssignment(scratchDotExpr,
  78. newCall(newIdentNode("extract"), isolatedTemp))
  79. template addAllNode(assignParam: NimNode, procParam: NimNode) =
  80. let scratchDotExpr = newDotExpr(scratchIdent, formalParams[i][0])
  81. checkIsolate(scratchAssignList, procParam, scratchDotExpr)
  82. let tempNode = genSym(kind = nskTemp, ident = formalParams[i][0].strVal)
  83. callNode.add nnkExprEqExpr.newTree(formalParams[i][0], tempNode)
  84. tempAssignList.add newLetStmt(tempNode, newDotExpr(objTemp, formalParams[i][0]))
  85. scratchRecList.add newIdentDefs(newIdentNode(formalParams[i][0].strVal), assignParam)
  86. macro toTask*(e: typed{nkCall | nkInfix | nkPrefix | nkPostfix | nkCommand | nkCallStrLit}): Task =
  87. ## Converts the call and its arguments to `Task`.
  88. runnableExamples("--gc:orc"):
  89. proc hello(a: int) = echo a
  90. let b = toTask hello(13)
  91. assert b is Task
  92. if getTypeInst(e).typeKind != ntyVoid:
  93. error("'toTask' cannot accept a call with a return value", e)
  94. when compileOption("threads"):
  95. if not isGcSafe(e[0]):
  96. error("'toTask' takes a GC safe call expression", e)
  97. if hasClosure(e[0]):
  98. error("closure call is not allowed", e)
  99. if e.len > 1:
  100. let scratchIdent = genSym(kind = nskTemp, ident = "scratch")
  101. let impl = e[0].getTypeInst
  102. when defined(nimTasksDebug):
  103. echo impl.treeRepr
  104. echo e.treeRepr
  105. let formalParams = impl[0]
  106. var
  107. scratchRecList = newNimNode(nnkRecList)
  108. scratchAssignList: seq[NimNode]
  109. tempAssignList: seq[NimNode]
  110. callNode: seq[NimNode]
  111. let
  112. objTemp = genSym(nskTemp, ident = "objTemp")
  113. for i in 1 ..< formalParams.len:
  114. var param = formalParams[i][1]
  115. if param.kind == nnkBracketExpr and param[0].eqIdent("sink"):
  116. param = param[0]
  117. if param.typeKind in {ntyExpr, ntyStmt}:
  118. error("'toTask'ed function cannot have a 'typed' or 'untyped' parameter", e)
  119. case param.kind
  120. of nnkVarTy:
  121. error("'toTask'ed function cannot have a 'var' parameter", e)
  122. of nnkBracketExpr:
  123. if param[0].typeKind == ntyTypeDesc:
  124. callNode.add nnkExprEqExpr.newTree(formalParams[i][0], e[i])
  125. elif param[0].typeKind in {ntyVarargs, ntyOpenArray}:
  126. if param[1].typeKind in {ntyExpr, ntyStmt}:
  127. error("'toTask'ed function cannot have a 'typed' or 'untyped' parameter", e)
  128. let
  129. seqType = nnkBracketExpr.newTree(newIdentNode("seq"), param[1])
  130. seqCallNode = newCall("@", e[i])
  131. addAllNode(seqType, seqCallNode)
  132. else:
  133. addAllNode(param, e[i])
  134. of nnkBracket, nnkObjConstr:
  135. # passing by static parameters
  136. # so we pass them directly instead of passing by scratchObj
  137. callNode.add nnkExprEqExpr.newTree(formalParams[i][0], e[i])
  138. of nnkSym, nnkPtrTy:
  139. addAllNode(param, e[i])
  140. of nnkCharLit..nnkNilLit:
  141. callNode.add nnkExprEqExpr.newTree(formalParams[i][0], e[i])
  142. else:
  143. error("'toTask'ed function cannot have a parameter of " & $param.kind & " kind", e)
  144. let scratchObjType = genSym(kind = nskType, ident = "ScratchObj")
  145. let scratchObj = nnkTypeSection.newTree(
  146. nnkTypeDef.newTree(
  147. scratchObjType,
  148. newEmptyNode(),
  149. nnkObjectTy.newTree(
  150. newEmptyNode(),
  151. newEmptyNode(),
  152. scratchRecList
  153. )
  154. )
  155. )
  156. let scratchObjPtrType = quote do:
  157. cast[ptr `scratchObjType`](c_calloc(csize_t 1, csize_t sizeof(`scratchObjType`)))
  158. let scratchLetSection = newLetStmt(
  159. scratchIdent,
  160. scratchObjPtrType
  161. )
  162. let scratchCheck = quote do:
  163. if `scratchIdent`.isNil:
  164. raise newException(OutOfMemDefect, "Could not allocate memory")
  165. var stmtList = newStmtList()
  166. stmtList.add(scratchObj)
  167. stmtList.add(scratchLetSection)
  168. stmtList.add(scratchCheck)
  169. stmtList.add(nnkBlockStmt.newTree(newEmptyNode(), newStmtList(scratchAssignList)))
  170. var functionStmtList = newStmtList()
  171. let funcCall = newCall(e[0], callNode)
  172. functionStmtList.add tempAssignList
  173. functionStmtList.add funcCall
  174. let funcName = genSym(nskProc, e[0].strVal)
  175. let destroyName = genSym(nskProc, "destroyScratch")
  176. let objTemp2 = genSym(ident = "obj")
  177. let tempNode = quote("@") do:
  178. `=destroy`(@objTemp2[])
  179. result = quote do:
  180. `stmtList`
  181. proc `funcName`(args: pointer) {.gcsafe, nimcall.} =
  182. let `objTemp` = cast[ptr `scratchObjType`](args)
  183. `functionStmtList`
  184. proc `destroyName`(args: pointer) {.gcsafe, nimcall.} =
  185. let `objTemp2` = cast[ptr `scratchObjType`](args)
  186. `tempNode`
  187. Task(callback: `funcName`, args: `scratchIdent`, destroy: `destroyName`)
  188. else:
  189. let funcCall = newCall(e[0])
  190. let funcName = genSym(nskProc, e[0].strVal)
  191. result = quote do:
  192. proc `funcName`(args: pointer) {.gcsafe, nimcall.} =
  193. `funcCall`
  194. Task(callback: `funcName`, args: nil)
  195. when defined(nimTasksDebug):
  196. echo result.repr
  197. runnableExamples("--gc:orc"):
  198. block:
  199. var num = 0
  200. proc hello(a: int) = inc num, a
  201. let b = toTask hello(13)
  202. b.invoke()
  203. assert num == 13
  204. # A task can be invoked multiple times
  205. b.invoke()
  206. assert num == 26
  207. block:
  208. type
  209. Runnable = ref object
  210. data: int
  211. var data: int
  212. proc hello(a: Runnable) {.nimcall.} =
  213. a.data += 2
  214. data = a.data
  215. when false:
  216. # the parameters of call must be isolated.
  217. let x = Runnable(data: 12)
  218. let b = toTask hello(x) # error ----> expression cannot be isolated: x
  219. b.invoke()
  220. let b = toTask(hello(Runnable(data: 12)))
  221. b.invoke()
  222. assert data == 14
  223. b.invoke()
  224. assert data == 16