Remove destructors from Nim 1.2

This commit is contained in:
Mamy Ratsimbazafy 2021-09-29 11:20:02 +02:00
parent e1fa0ec097
commit d0fc9cb4ab
2 changed files with 15 additions and 5 deletions

View File

@ -61,12 +61,13 @@ type
Task* = object ## `Task` contains the callback and its arguments. Task* = object ## `Task` contains the callback and its arguments.
callback: proc (args: pointer) {.nimcall, gcsafe.} callback: proc (args: pointer) {.nimcall, gcsafe.}
args: pointer 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`. ## Frees the resources allocated for a `Task`.
if t.args != nil: if t.args != nil:
if t.destroy != nil: if t.destroy != nil:
@ -221,7 +222,12 @@ macro toTask*(e: typed{nkCall | nkInfix | nkPrefix | nkPostfix | nkCommand | nkC
let destroyName = genSym(nskProc, "destroyScratch") let destroyName = genSym(nskProc, "destroyScratch")
let objTemp2 = genSym(ident = "obj") let objTemp2 = genSym(ident = "obj")
let tempNode = quote("@") do: 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: result = quote do:
`stmtList` `stmtList`

View File

@ -193,7 +193,11 @@ proc new(T: type TaskNode, parent: TaskNode, task: sink Task): T =
proc runTask(tn: var TaskNode) {.raises:[Exception], inline.} = proc runTask(tn: var TaskNode) {.raises:[Exception], inline.} =
## Run a task and consumes the taskNode ## Run a task and consumes the taskNode
tn.task.invoke() tn.task.invoke()
when (NimMajor,NimMinor,NimPatch) >= (1,6,0):
{.gcsafe.}: # Upstream missing tagging `=destroy` as gcsafe
tn.task.`=destroy`() tn.task.`=destroy`()
else:
tn.task.shim_destroy()
tn.c_free() tn.c_free()
proc schedule(ctx: WorkerContext, tn: sink TaskNode) {.inline.} = proc schedule(ctx: WorkerContext, tn: sink TaskNode) {.inline.} =