- Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathtasks.nim
312 lines (255 loc) · 9.55 KB
/
tasks.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
#
#
# Nim's Runtime Library
# (c) Copyright 2021 Nim contributors
#
# See the file "copying.txt", included in this
# distribution, for details about the copyright.
#
## This module provides basic primitives for creating parallel programs.
## A `Task` should be only owned by a single Thread, it cannot be shared by threads.
import std/[macros, isolation, typetraits]
whendefined(nimPreviewSlimSystem):
import std/assertions
export isolation
whencompileOption("threads"):
from std/effecttraits import isGcSafe
#
# proc hello(a: int, b: string) =
# echo $a & b
#
# let literal = "Nim"
# let t = toTask(hello(521, literal))
#
#
# is roughly converted to
#
# type
# ScratchObj_369098780 = object
# a: int
# b: string
#
# let scratch_369098762 = cast[ptr ScratchObj_369098780](c_calloc(csize_t 1,
# csize_t sizeof(ScratchObj_369098780)))
# if scratch_369098762.isNil:
# raise newException(OutOfMemDefect, "Could not allocate memory")
# block:
# var isolate_369098776 = isolate(521)
# scratch_369098762.a = extract(isolate_369098776)
# var isolate_369098778 = isolate(literal)
# scratch_369098762.b = extract(isolate_369098778)
# proc hello_369098781(args`gensym3: pointer) {.nimcall.} =
# let objTemp_369098775 = cast[ptr ScratchObj_369098780](args`gensym3)
# let :tmp_369098777 = objTemp_369098775.a
# let :tmp_369098779 = objTemp_369098775.b
# hello(a = :tmp_369098777, b = :tmp_369098779)
#
# proc destroyScratch_369098782(args`gensym3: pointer) {.nimcall.} =
# let obj_369098783 = cast[ptr ScratchObj_369098780](args`gensym3)
# =destroy(obj_369098783[])
# let t = Task(callback: hello_369098781, args: scratch_369098762, destroy: destroyScratch_369098782)
#
type
Task*=object## `Task` contains the callback and its arguments.
callback: proc (args, res: pointer) {.nimcall, gcsafe.}
args: pointer
destroy: proc (args: pointer) {.nimcall, gcsafe.}
proc`=copy`*(x: varTask, y: Task) {.error.}
const arcLike =defined(gcArc) ordefined(gcAtomicArc) ordefined(gcOrc)
whendefined(nimAllowNonVarDestructor) and arcLike:
proc`=destroy`*(t: Task) {.inline, gcsafe.} =
## Frees the resources allocated for a `Task`.
if t.args !=nil:
if t.destroy !=nil:
t.destroy(t.args)
deallocShared(t.args)
else:
proc`=destroy`*(t: varTask) {.inline, gcsafe.} =
## Frees the resources allocated for a `Task`.
if t.args !=nil:
if t.destroy !=nil:
t.destroy(t.args)
deallocShared(t.args)
procinvoke*(task: Task; res: pointer=nil) {.inline, gcsafe.} =
## Invokes the `task`.
assert task.callback !=nil
task.callback(task.args, res)
templatecheckIsolate(scratchAssignList: seq[NimNode], procParam, scratchDotExpr: NimNode) =
# block:
# var isoTempA = isolate(521)
# scratch.a = extract(isolateA)
# var isoTempB = isolate(literal)
# scratch.b = extract(isolateB)
let isolatedTemp =genSym(nskTemp, "isoTemp")
scratchAssignList.addnewVarStmt(isolatedTemp, newCall(newIdentNode("isolate"), procParam))
scratchAssignList.addnewAssignment(scratchDotExpr,
newCall(newIdentNode("extract"), isolatedTemp))
templateaddAllNode(assignParam: NimNode, procParam: NimNode) =
let scratchDotExpr =newDotExpr(scratchIdent, formalParams[i][0])
checkIsolate(scratchAssignList, procParam, scratchDotExpr)
let tempNode =genSym(kind = nskTemp, ident = formalParams[i][0].strVal)
callNode.add nnkExprEqExpr.newTree(formalParams[i][0], tempNode)
tempAssignList.addnewLetStmt(tempNode, newDotExpr(objTemp, formalParams[i][0]))
scratchRecList.addnewIdentDefs(newIdentNode(formalParams[i][0].strVal), assignParam)
procanalyseRootSym(s: NimNode): NimNode=
result= s
whiletrue:
caseresult.kind
of nnkBracketExpr, nnkDerefExpr, nnkHiddenDeref,
nnkAddr, nnkHiddenAddr,
nnkObjDownConv, nnkObjUpConv:
result=result[0]
of nnkDotExpr, nnkCheckedFieldExpr, nnkHiddenStdConv, nnkHiddenSubConv:
result=result[1]
else:
break
macrotoTask*(e: typed{nkCall | nkInfix | nkPrefix | nkPostfix | nkCommand | nkCallStrLit}): Task=
## Converts the call and its arguments to `Task`.
runnableExamples:
prochello(a: int) =echo a
let b =toTaskhello(13)
assert b isTask
let retType =getTypeInst(e)
let returnsVoid = retType.typeKind == ntyVoid
let rootSym =analyseRootSym(e[0])
expectKind rootSym, nnkSym
whencompileOption("threads"):
ifnotisGcSafe(rootSym):
error("'toTask' takes a GC safe call expression", e)
ifhasClosure(rootSym):
error("closure call is not allowed", e)
if e.len >1:
let scratchIdent =genSym(kind = nskTemp, ident ="scratch")
let impl = e[0].getTypeInst
whendefined(nimTasksDebug):
echo impl.treeRepr
echo e.treeRepr
let formalParams = impl[0]
var
scratchRecList =newNimNode(nnkRecList)
scratchAssignList: seq[NimNode]
tempAssignList: seq[NimNode]
callNode: seq[NimNode]
let
objTemp =genSym(nskTemp, ident ="objTemp")
for i in1..< formalParams.len:
var param = formalParams[i][1]
if param.kind == nnkBracketExpr and param[0].eqIdent("sink"):
param = param[0]
if param.typeKind in {ntyExpr, ntyStmt}:
error("'toTask'ed function cannot have a 'typed' or 'untyped' parameter", e)
case param.kind
of nnkVarTy:
error("'toTask'ed function cannot have a 'var' parameter", e)
of nnkBracketExpr:
if param[0].typeKind == ntyTypeDesc:
callNode.add nnkExprEqExpr.newTree(formalParams[i][0], e[i])
elif param[0].typeKind in {ntyVarargs, ntyOpenArray}:
if param[1].typeKind in {ntyExpr, ntyStmt}:
error("'toTask'ed function cannot have a 'typed' or 'untyped' parameter", e)
let
seqType = nnkBracketExpr.newTree(newIdentNode("seq"), param[1])
seqCallNode =newCall("@", e[i])
addAllNode(seqType, seqCallNode)
else:
addAllNode(param, e[i])
of nnkBracket, nnkObjConstr:
# passing by static parameters
# so we pass them directly instead of passing by scratchObj
callNode.add nnkExprEqExpr.newTree(formalParams[i][0], e[i])
of nnkSym, nnkPtrTy, nnkProcTy, nnkTupleTy, nnkTupleConstr:
addAllNode(param, e[i])
of nnkCharLit..nnkNilLit:
callNode.add nnkExprEqExpr.newTree(formalParams[i][0], e[i])
else:
error("'toTask'ed function cannot have a parameter of "&$param.kind &" kind", e)
let scratchObjType =genSym(kind = nskType, ident ="ScratchObj")
let scratchObj = nnkTypeSection.newTree(
nnkTypeDef.newTree(
scratchObjType,
newEmptyNode(),
nnkObjectTy.newTree(
newEmptyNode(),
newEmptyNode(),
scratchRecList
)
)
)
let scratchObjPtrType =quotedo:
cast[ptr `scratchObjType`](allocShared0(sizeof(`scratchObjType`)))
let scratchLetSection =newLetStmt(scratchIdent, scratchObjPtrType)
var stmtList =newStmtList()
stmtList.add(scratchObj)
stmtList.add(scratchLetSection)
stmtList.add(nnkBlockStmt.newTree(newEmptyNode(), newStmtList(scratchAssignList)))
var functionStmtList =newStmtList()
let funcCall =newCall(e[0], callNode)
functionStmtList.add tempAssignList
let funcName =genSym(nskProc, rootSym.strVal)
let destroyName =genSym(nskProc, "destroyScratch")
let objTemp2 =genSym(ident ="obj")
let tempNode =quote("@") do:
`=destroy`(@objTemp2[])
var funcDecl: NimNode
if returnsVoid:
funcDecl =quotedo:
proc`funcName`(args, res: pointer) {.gcsafe, nimcall.} =
let `objTemp` =cast[ptr `scratchObjType`](args)
`functionStmtList`
`funcCall`
else:
funcDecl =quotedo:
proc`funcName`(args, res: pointer) {.gcsafe, nimcall.} =
let `objTemp` =cast[ptr `scratchObjType`](args)
`functionStmtList`
cast[ptr `retType`](res)[] = `funcCall`
result=quotedo:
`stmtList`
`funcDecl`
proc`destroyName`(args: pointer) {.gcsafe, nimcall.} =
let `objTemp2` =cast[ptr `scratchObjType`](args)
`tempNode`
Task(callback: `funcName`, args: `scratchIdent`, destroy: `destroyName`)
else:
let funcCall =newCall(e[0])
let funcName =genSym(nskProc, rootSym.strVal)
if returnsVoid:
result=quotedo:
proc`funcName`(args, res: pointer) {.gcsafe, nimcall.} =
`funcCall`
Task(callback: `funcName`, args: nil)
else:
result=quotedo:
proc`funcName`(args, res: pointer) {.gcsafe, nimcall.} =
cast[ptr `retType`](res)[] = `funcCall`
Task(callback: `funcName`, args: nil)
whendefined(nimTasksDebug):
echoresult.repr
runnableExamples:
block:
var num =0
prochello(a: int) =inc num, a
let b =toTaskhello(13)
b.invoke()
assert num ==13
# A task can be invoked multiple times
b.invoke()
assert num ==26
block:
type
Runnable=refobject
data: int
var data: int
prochello(a: Runnable) {.nimcall.} =
a.data +=2
data = a.data
whenfalse:
# the parameters of call must be isolated.
let x =Runnable(data: 12)
let b =toTaskhello(x) # error ----> expression cannot be isolated: x
b.invoke()
let b =toTask(hello(Runnable(data: 12)))
b.invoke()
assert data ==14
b.invoke()
assert data ==16