@@ -110,7 +110,7 @@ when isMainModule:
110
110
while not chan.tryRecv(data):
111
111
body
112
112
113
- const NumVals = 10
113
+ const NumVals = 100000
114
114
const Padding = 10 * NumVals # Pad with a 0 so that iteration 10 of thread 3 is 3010 with 99 max iters
115
115
116
116
type
@@ -151,16 +151,19 @@ when isMainModule:
151
151
152
152
# I think the createShared/freeShared allocators have a race condition
153
153
proc valAlloc(): Val =
154
- when not defined(debugNimAlloc):
155
- cast[Val](c_malloc(sizeof(ValObj)))
156
- else:
154
+ when defined(debugNimalloc):
157
155
createShared(ValObj)
156
+ else:
157
+ cast[Val](c_malloc(sizeof(ValObj)))
158
158
159
159
proc valFree(val: Val) =
160
- when not defined(debugNimAlloc):
161
- c_free(val)
162
- else:
160
+ ## Note: it seems like freeing memory
161
+ ## is confusing the allocators
162
+ ## The test pass if memory is not freed
163
+ when defined(debugNimalloc):
163
164
freeShared(val)
165
+ else:
166
+ c_free(val)
164
167
165
168
proc thread_func(args: ThreadArgs) =
166
169
@@ -182,23 +185,25 @@ when isMainModule:
182
185
args.chan[].recvLoop(val):
183
186
# Busy loop, in prod we might want to yield the core/thread timeslice
184
187
discard
185
- echo "Receiver got: ", val.val, " at address 0x", toLowerASCII toHex cast[ByteAddress](val)
188
+ # echo "Receiver got: ", val.val, " at address 0x", toLowerASCII toHex cast[ByteAddress](val)
186
189
let sender = WorkerKind(val.val div Padding)
187
190
doAssert val.val == counts[sender] + ord(sender) * Padding, "Incorrect value: " & $val.val
188
191
inc counts[sender]
189
- valFree(val)
192
+ # valFree(val) # Don't free memory for testing the queue, allocators break
193
+
190
194
191
195
Worker(Sender1..Sender15):
192
196
for j in 0 ..< NumVals:
193
197
let val = valAlloc()
194
198
val.val = ord(args.ID) * Padding + j
199
+
200
+ # const pad = spaces(8)
201
+ # echo pad.repeat(ord(args.ID)), 'S', $ord(args.ID), ": ", val.val
202
+
195
203
args.chan[].sendLoop(val):
196
204
# Busy loop, in prod we might want to yield the core/thread timeslice
197
205
discard
198
206
199
- const pad = spaces(8)
200
- echo pad.repeat(ord(args.ID)), 'S', $ord(args.ID), ": ", val.val
201
-
202
207
proc main() =
203
208
echo "Testing if 15 threads can send data to 1 consumer"
204
209
echo "------------------------------------------------------------------------"
0 commit comments