tasks.nim 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. doAssert getTypeInst(e).typeKind == ntyVoid
  93. when compileOption("threads"):
  94. if not isGcSafe(e[0]):
  95. error("'toTask' takes a GC safe call expression", e)
  96. if hasClosure(e[0]):
  97. error("closure call is not allowed", e)
  98. if e.len > 1:
  99. let scratchIdent = genSym(kind = nskTemp, ident = "scratch")
  100. let impl = e[0].getTypeInst
  101. when defined(nimTasksDebug):
  102. echo impl.treeRepr
  103. echo e.treeRepr
  104. let formalParams = impl[0]
  105. var
  106. scratchRecList = newNimNode(nnkRecList)
  107. scratchAssignList: seq[NimNode]
  108. tempAssignList: seq[NimNode]
  109. callNode: seq[NimNode]
  110. let
  111. objTemp = genSym(nskTemp, ident = "objTemp")
  112. for i in 1 ..< formalParams.len:
  113. var param = formalParams[i][1]
  114. if param.kind == nnkBracketExpr and param[0].eqIdent("sink"):
  115. param = param[0]
  116. if param.typeKind in {ntyExpr, ntyStmt}:
  117. error("'toTask'ed function cannot have a 'typed' or 'untyped' parameter", e)
  118. case param.kind
  119. of nnkVarTy:
  120. error("'toTask'ed function cannot have a 'var' parameter", e)
  121. of nnkBracketExpr:
  122. if param[0].typeKind == ntyTypeDesc:
  123. callNode.add nnkExprEqExpr.newTree(formalParams[i][0], e[i])
  124. elif param[0].typeKind in {ntyVarargs, ntyOpenArray}:
  125. if param[1].typeKind in {ntyExpr, ntyStmt}:
  126. error("'toTask'ed function cannot have a 'typed' or 'untyped' parameter", e)
  127. let
  128. seqType = nnkBracketExpr.newTree(newIdentNode("seq"), param[1])
  129. seqCallNode = newCall("@", e[i])
  130. addAllNode(seqType, seqCallNode)
  131. else:
  132. addAllNode(param, e[i])
  133. of nnkBracket, nnkObjConstr:
  134. # passing by static parameters
  135. # so we pass them directly instead of passing by scratchObj
  136. callNode.add nnkExprEqExpr.newTree(formalParams[i][0], e[i])
  137. of nnkSym, nnkPtrTy:
  138. addAllNode(param, e[i])
  139. of nnkCharLit..nnkNilLit:
  140. callNode.add nnkExprEqExpr.newTree(formalParams[i][0], e[i])
  141. else:
  142. error("'toTask'ed function cannot have a parameter of " & $param.kind & " kind", e)
  143. let scratchObjType = genSym(kind = nskType, ident = "ScratchObj")
  144. let scratchObj = nnkTypeSection.newTree(
  145. nnkTypeDef.newTree(
  146. scratchObjType,
  147. newEmptyNode(),
  148. nnkObjectTy.newTree(
  149. newEmptyNode(),
  150. newEmptyNode(),
  151. scratchRecList
  152. )
  153. )
  154. )
  155. let scratchObjPtrType = quote do:
  156. cast[ptr `scratchObjType`](c_calloc(csize_t 1, csize_t sizeof(`scratchObjType`)))
  157. let scratchLetSection = newLetStmt(
  158. scratchIdent,
  159. scratchObjPtrType
  160. )
  161. let scratchCheck = quote do:
  162. if `scratchIdent`.isNil:
  163. raise newException(OutOfMemDefect, "Could not allocate memory")
  164. var stmtList = newStmtList()
  165. stmtList.add(scratchObj)
  166. stmtList.add(scratchLetSection)
  167. stmtList.add(scratchCheck)
  168. stmtList.add(nnkBlockStmt.newTree(newEmptyNode(), newStmtList(scratchAssignList)))
  169. var functionStmtList = newStmtList()
  170. let funcCall = newCall(e[0], callNode)
  171. functionStmtList.add tempAssignList
  172. functionStmtList.add funcCall
  173. let funcName = genSym(nskProc, e[0].strVal)
  174. let destroyName = genSym(nskProc, "destroyScratch")
  175. let objTemp2 = genSym(ident = "obj")
  176. let tempNode = quote("@") do:
  177. `=destroy`(@objTemp2[])
  178. result = quote do:
  179. `stmtList`
  180. proc `funcName`(args: pointer) {.gcsafe, nimcall.} =
  181. let `objTemp` = cast[ptr `scratchObjType`](args)
  182. `functionStmtList`
  183. proc `destroyName`(args: pointer) {.gcsafe, nimcall.} =
  184. let `objTemp2` = cast[ptr `scratchObjType`](args)
  185. `tempNode`
  186. Task(callback: `funcName`, args: `scratchIdent`, destroy: `destroyName`)
  187. else:
  188. let funcCall = newCall(e[0])
  189. let funcName = genSym(nskProc, e[0].strVal)
  190. result = quote do:
  191. proc `funcName`(args: pointer) {.gcsafe, nimcall.} =
  192. `funcCall`
  193. Task(callback: `funcName`, args: nil)
  194. when defined(nimTasksDebug):
  195. echo result.repr
  196. runnableExamples("--gc:orc"):
  197. block:
  198. var num = 0
  199. proc hello(a: int) = inc num, a
  200. let b = toTask hello(13)
  201. b.invoke()
  202. assert num == 13
  203. # A task can be invoked multiple times
  204. b.invoke()
  205. assert num == 26
  206. block:
  207. type
  208. Runnable = ref object
  209. data: int
  210. var data: int
  211. proc hello(a: Runnable) {.nimcall.} =
  212. a.data += 2
  213. data = a.data
  214. when false:
  215. # the parameters of call must be isolated.
  216. let x = Runnable(data: 12)
  217. let b = toTask hello(x) # error ----> expression cannot be isolated: x
  218. b.invoke()
  219. let b = toTask(hello(Runnable(data: 12)))
  220. b.invoke()
  221. assert data == 14
  222. b.invoke()
  223. assert data == 16