Skip to content

Commit

Permalink
Remove destructors from Nim 1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
mratsim committed Sep 29, 2021
1 parent e1fa0ec commit d0fc9cb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
14 changes: 10 additions & 4 deletions taskpools/shims_pre_1_6/tasks.nim
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,13 @@ type
Task* = object ## `Task` contains the callback and its arguments.
callback: proc (args: pointer) {.nimcall, gcsafe.}
args: pointer
destroy: proc (args: pointer) {.nimcall.}
destroy: proc (args: pointer) {.nimcall, gcsafe.}

# XXX: ⚠️ No destructors for 1.2 due to unreliable codegen

proc `=copy`*(x: var Task, y: Task) {.error.}
# proc `=copy`*(x: var Task, y: Task) {.error.}

proc `=destroy`*(t: var Task) {.inline.} =
proc shim_destroy*(t: var Task) {.inline, gcsafe.} =
## Frees the resources allocated for a `Task`.
if t.args != nil:
if t.destroy != nil:
Expand Down Expand Up @@ -221,7 +222,12 @@ macro toTask*(e: typed{nkCall | nkInfix | nkPrefix | nkPostfix | nkCommand | nkC
let destroyName = genSym(nskProc, "destroyScratch")
let objTemp2 = genSym(ident = "obj")
let tempNode = quote("@") do:
`=destroy`(@objTemp2[])
# XXX:
# We avoid destructors for Nim 1.2 due to bad codegen
# For taskpool there are no destructor to run.
# We ensure that by checking that we only operate on plain old data
static: doAssert supportsCopyMem(@scratchObjType)
# `=destroy`(@objTemp2[])

result = quote do:
`stmtList`
Expand Down
6 changes: 5 additions & 1 deletion taskpools/taskpools.nim
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,11 @@ proc new(T: type TaskNode, parent: TaskNode, task: sink Task): T =
proc runTask(tn: var TaskNode) {.raises:[Exception], inline.} =
## Run a task and consumes the taskNode
tn.task.invoke()
tn.task.`=destroy`()
when (NimMajor,NimMinor,NimPatch) >= (1,6,0):
{.gcsafe.}: # Upstream missing tagging `=destroy` as gcsafe
tn.task.`=destroy`()
else:
tn.task.shim_destroy()
tn.c_free()

proc schedule(ctx: WorkerContext, tn: sink TaskNode) {.inline.} =
Expand Down

0 comments on commit d0fc9cb

Please sign in to comment.