threadpool.nim 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2015 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## Implements Nim's `spawn <manual_experimental.html#parallel-amp-spawn>`_.
  10. ##
  11. ## **See also:**
  12. ## * `threads module <threads.html>`_
  13. ## * `channels module <channels.html>`_
  14. ## * `locks module <locks.html>`_
  15. ## * `asyncdispatch module <asyncdispatch.html>`_
  16. ##
  17. ## Unstable API.
  18. when not compileOption("threads"):
  19. {.error: "Threadpool requires --threads:on option.".}
  20. import cpuinfo, cpuload, locks, os
  21. {.push stackTrace:off.}
  22. type
  23. Semaphore = object
  24. c: Cond
  25. L: Lock
  26. counter: int
  27. proc initSemaphore(cv: var Semaphore) =
  28. initCond(cv.c)
  29. initLock(cv.L)
  30. proc destroySemaphore(cv: var Semaphore) {.inline.} =
  31. deinitCond(cv.c)
  32. deinitLock(cv.L)
  33. proc blockUntil(cv: var Semaphore) =
  34. acquire(cv.L)
  35. while cv.counter <= 0:
  36. wait(cv.c, cv.L)
  37. dec cv.counter
  38. release(cv.L)
  39. proc signal(cv: var Semaphore) =
  40. acquire(cv.L)
  41. inc cv.counter
  42. release(cv.L)
  43. signal(cv.c)
  44. const CacheLineSize = 32 # true for most archs
  45. type
  46. Barrier {.compilerProc.} = object
  47. entered: int
  48. cv: Semaphore # Semaphore takes 3 words at least
  49. when sizeof(int) < 8:
  50. cacheAlign: array[CacheLineSize-4*sizeof(int), byte]
  51. left: int
  52. cacheAlign2: array[CacheLineSize-sizeof(int), byte]
  53. interest: bool # whether the master is interested in the "all done" event
  54. proc barrierEnter(b: ptr Barrier) {.compilerProc, inline.} =
  55. # due to the signaling between threads, it is ensured we are the only
  56. # one with access to 'entered' so we don't need 'atomicInc' here:
  57. inc b.entered
  58. # also we need no 'fence' instructions here as soon 'nimArgsPassingDone'
  59. # will be called which already will perform a fence for us.
  60. proc barrierLeave(b: ptr Barrier) {.compilerProc, inline.} =
  61. atomicInc b.left
  62. when not defined(x86): fence()
  63. # We may not have seen the final value of b.entered yet,
  64. # so we need to check for >= instead of ==.
  65. if b.interest and b.left >= b.entered: signal(b.cv)
  66. proc openBarrier(b: ptr Barrier) {.compilerProc, inline.} =
  67. b.entered = 0
  68. b.left = 0
  69. b.interest = false
  70. proc closeBarrier(b: ptr Barrier) {.compilerProc.} =
  71. fence()
  72. if b.left != b.entered:
  73. b.cv.initSemaphore()
  74. fence()
  75. b.interest = true
  76. fence()
  77. while b.left != b.entered: blockUntil(b.cv)
  78. destroySemaphore(b.cv)
  79. {.pop.}
  80. # ----------------------------------------------------------------------------
  81. type
  82. AwaitInfo = object
  83. cv: Semaphore
  84. idx: int
  85. FlowVarBase* = ref FlowVarBaseObj ## Untyped base class for ``FlowVar[T]``.
  86. FlowVarBaseObj = object of RootObj
  87. ready, usesSemaphore, awaited: bool
  88. cv: Semaphore # for 'blockUntilAny' support
  89. ai: ptr AwaitInfo
  90. idx: int
  91. data: pointer # we incRef and unref it to keep it alive; note this MUST NOT
  92. # be RootRef here otherwise the wrong GC keeps track of it!
  93. owner: pointer # ptr Worker
  94. FlowVarObj[T] = object of FlowVarBaseObj
  95. blob: T
  96. FlowVar*{.compilerProc.}[T] = ref FlowVarObj[T] ## A data flow variable.
  97. ToFreeQueue = object
  98. len: int
  99. lock: Lock
  100. empty: Semaphore
  101. data: array[128, pointer]
  102. WorkerProc = proc (thread, args: pointer) {.nimcall, gcsafe.}
  103. Worker = object
  104. taskArrived: Semaphore
  105. taskStarted: Semaphore #\
  106. # task data:
  107. f: WorkerProc
  108. data: pointer
  109. ready: bool # put it here for correct alignment!
  110. initialized: bool # whether it has even been initialized
  111. shutdown: bool # the pool requests to shut down this worker thread
  112. q: ToFreeQueue
  113. readyForTask: Semaphore
  114. const threadpoolWaitMs {.intdefine.}: int = 100
  115. proc blockUntil*(fv: FlowVarBase) =
  116. ## Waits until the value for the ``fv`` arrives.
  117. ##
  118. ## Usually it is not necessary to call this explicitly.
  119. if fv.usesSemaphore and not fv.awaited:
  120. fv.awaited = true
  121. blockUntil(fv.cv)
  122. destroySemaphore(fv.cv)
  123. proc selectWorker(w: ptr Worker; fn: WorkerProc; data: pointer): bool =
  124. if cas(addr w.ready, true, false):
  125. w.data = data
  126. w.f = fn
  127. signal(w.taskArrived)
  128. blockUntil(w.taskStarted)
  129. result = true
  130. proc cleanFlowVars(w: ptr Worker) =
  131. let q = addr(w.q)
  132. acquire(q.lock)
  133. for i in 0 ..< q.len:
  134. GC_unref(cast[RootRef](q.data[i]))
  135. #echo "GC_unref"
  136. q.len = 0
  137. release(q.lock)
  138. proc wakeupWorkerToProcessQueue(w: ptr Worker) =
  139. # we have to ensure it's us who wakes up the owning thread.
  140. # This is quite horrible code, but it runs so rarely that it doesn't matter:
  141. while not cas(addr w.ready, true, false):
  142. cpuRelax()
  143. discard
  144. w.data = nil
  145. w.f = proc (w, a: pointer) {.nimcall.} =
  146. let w = cast[ptr Worker](w)
  147. cleanFlowVars(w)
  148. signal(w.q.empty)
  149. signal(w.taskArrived)
  150. proc attach(fv: FlowVarBase; i: int): bool =
  151. acquire(fv.cv.L)
  152. if fv.cv.counter <= 0:
  153. fv.idx = i
  154. result = true
  155. else:
  156. result = false
  157. release(fv.cv.L)
  158. proc finished(fv: FlowVarBase) =
  159. doAssert fv.ai.isNil, "flowVar is still attached to an 'blockUntilAny'"
  160. # we have to protect against the rare cases where the owner of the flowVar
  161. # simply disregards the flowVar and yet the "flowVar" has not yet written
  162. # anything to it:
  163. blockUntil(fv)
  164. if fv.data.isNil: return
  165. let owner = cast[ptr Worker](fv.owner)
  166. let q = addr(owner.q)
  167. acquire(q.lock)
  168. while not (q.len < q.data.len):
  169. #echo "EXHAUSTED!"
  170. release(q.lock)
  171. wakeupWorkerToProcessQueue(owner)
  172. blockUntil(q.empty)
  173. acquire(q.lock)
  174. q.data[q.len] = cast[pointer](fv.data)
  175. inc q.len
  176. release(q.lock)
  177. fv.data = nil
  178. # the worker thread waits for "data" to be set to nil before shutting down
  179. owner.data = nil
  180. proc fvFinalizer[T](fv: FlowVar[T]) = finished(fv)
  181. proc nimCreateFlowVar[T](): FlowVar[T] {.compilerProc.} =
  182. new(result, fvFinalizer)
  183. proc nimFlowVarCreateSemaphore(fv: FlowVarBase) {.compilerProc.} =
  184. fv.cv.initSemaphore()
  185. fv.usesSemaphore = true
  186. proc nimFlowVarSignal(fv: FlowVarBase) {.compilerProc.} =
  187. if fv.ai != nil:
  188. acquire(fv.ai.cv.L)
  189. fv.ai.idx = fv.idx
  190. inc fv.ai.cv.counter
  191. release(fv.ai.cv.L)
  192. signal(fv.ai.cv.c)
  193. if fv.usesSemaphore:
  194. signal(fv.cv)
  195. proc awaitAndThen*[T](fv: FlowVar[T]; action: proc (x: T) {.closure.}) =
  196. ## Blocks until the ``fv`` is available and then passes its value
  197. ## to ``action``.
  198. ##
  199. ## Note that due to Nim's parameter passing semantics this
  200. ## means that ``T`` doesn't need to be copied so ``awaitAndThen`` can
  201. ## sometimes be more efficient than `^ proc <#^,FlowVar[T]>`_.
  202. blockUntil(fv)
  203. when T is string or T is seq:
  204. action(cast[T](fv.data))
  205. elif T is ref:
  206. {.error: "'awaitAndThen' not available for FlowVar[ref]".}
  207. else:
  208. action(fv.blob)
  209. finished(fv)
  210. proc unsafeRead*[T](fv: FlowVar[ref T]): ptr T =
  211. ## Blocks until the value is available and then returns this value.
  212. blockUntil(fv)
  213. result = cast[ptr T](fv.data)
  214. finished(fv)
  215. proc `^`*[T](fv: FlowVar[ref T]): ref T =
  216. ## Blocks until the value is available and then returns this value.
  217. blockUntil(fv)
  218. let src = cast[ref T](fv.data)
  219. when defined(nimV2):
  220. result = src
  221. else:
  222. deepCopy result, src
  223. finished(fv)
  224. proc `^`*[T](fv: FlowVar[T]): T =
  225. ## Blocks until the value is available and then returns this value.
  226. blockUntil(fv)
  227. when T is string or T is seq:
  228. let src = cast[T](fv.data)
  229. when defined(nimV2):
  230. result = src
  231. else:
  232. deepCopy result, src
  233. else:
  234. result = fv.blob
  235. finished(fv)
  236. proc blockUntilAny*(flowVars: openArray[FlowVarBase]): int =
  237. ## Awaits any of the given ``flowVars``. Returns the index of one ``flowVar``
  238. ## for which a value arrived.
  239. ##
  240. ## A ``flowVar`` only supports one call to ``blockUntilAny`` at the same time.
  241. ## That means if you ``blockUntilAny([a,b])`` and ``blockUntilAny([b,c])``
  242. ## the second call will only block until ``c``. If there is no ``flowVar`` left
  243. ## to be able to wait on, -1 is returned.
  244. ##
  245. ## **Note**: This results in non-deterministic behaviour and should be avoided.
  246. var ai: AwaitInfo
  247. ai.cv.initSemaphore()
  248. var conflicts = 0
  249. result = -1
  250. for i in 0 .. flowVars.high:
  251. if cas(addr flowVars[i].ai, nil, addr ai):
  252. if not attach(flowVars[i], i):
  253. result = i
  254. break
  255. else:
  256. inc conflicts
  257. if conflicts < flowVars.len:
  258. if result < 0:
  259. blockUntil(ai.cv)
  260. result = ai.idx
  261. for i in 0 .. flowVars.high:
  262. discard cas(addr flowVars[i].ai, addr ai, nil)
  263. destroySemaphore(ai.cv)
  264. proc isReady*(fv: FlowVarBase): bool =
  265. ## Determines whether the specified ``FlowVarBase``'s value is available.
  266. ##
  267. ## If ``true``, awaiting ``fv`` will not block.
  268. if fv.usesSemaphore and not fv.awaited:
  269. acquire(fv.cv.L)
  270. result = fv.cv.counter > 0
  271. release(fv.cv.L)
  272. else:
  273. result = true
  274. proc nimArgsPassingDone(p: pointer) {.compilerProc.} =
  275. let w = cast[ptr Worker](p)
  276. signal(w.taskStarted)
  277. const
  278. MaxThreadPoolSize* = 256 ## Maximum size of the thread pool. 256 threads
  279. ## should be good enough for anybody ;-)
  280. MaxDistinguishedThread* = 32 ## Maximum number of "distinguished" threads.
  281. type
  282. ThreadId* = range[0..MaxDistinguishedThread-1]
  283. var
  284. currentPoolSize: int
  285. maxPoolSize = MaxThreadPoolSize
  286. minPoolSize = 4
  287. gSomeReady : Semaphore
  288. readyWorker: ptr Worker
  289. # A workaround for recursion deadlock issue
  290. # https://github.com/nim-lang/Nim/issues/4597
  291. var
  292. numSlavesLock: Lock
  293. numSlavesRunning {.guard: numSlavesLock}: int
  294. numSlavesWaiting {.guard: numSlavesLock}: int
  295. isSlave {.threadvar.}: bool
  296. numSlavesLock.initLock
  297. gSomeReady.initSemaphore()
  298. proc slave(w: ptr Worker) {.thread.} =
  299. isSlave = true
  300. while true:
  301. if w.shutdown:
  302. w.shutdown = false
  303. atomicDec currentPoolSize
  304. while true:
  305. if w.data != nil:
  306. sleep(threadpoolWaitMs)
  307. else:
  308. # The flowvar finalizer ("finished()") set w.data to nil, so we can
  309. # safely terminate the thread.
  310. #
  311. # TODO: look for scenarios in which the flowvar is never finalized, so
  312. # a shut down thread gets stuck in this loop until the main thread exits.
  313. break
  314. break
  315. when declared(atomicStoreN):
  316. atomicStoreN(addr(w.ready), true, ATOMIC_SEQ_CST)
  317. else:
  318. w.ready = true
  319. readyWorker = w
  320. signal(gSomeReady)
  321. blockUntil(w.taskArrived)
  322. # XXX Somebody needs to look into this (why does this assertion fail
  323. # in Visual Studio?)
  324. when not defined(vcc) and not defined(tcc): assert(not w.ready)
  325. withLock numSlavesLock:
  326. inc numSlavesRunning
  327. w.f(w, w.data)
  328. withLock numSlavesLock:
  329. dec numSlavesRunning
  330. if w.q.len != 0: w.cleanFlowVars
  331. proc distinguishedSlave(w: ptr Worker) {.thread.} =
  332. while true:
  333. when declared(atomicStoreN):
  334. atomicStoreN(addr(w.ready), true, ATOMIC_SEQ_CST)
  335. else:
  336. w.ready = true
  337. signal(w.readyForTask)
  338. blockUntil(w.taskArrived)
  339. assert(not w.ready)
  340. w.f(w, w.data)
  341. if w.q.len != 0: w.cleanFlowVars
  342. var
  343. workers: array[MaxThreadPoolSize, Thread[ptr Worker]]
  344. workersData: array[MaxThreadPoolSize, Worker]
  345. distinguished: array[MaxDistinguishedThread, Thread[ptr Worker]]
  346. distinguishedData: array[MaxDistinguishedThread, Worker]
  347. when defined(nimPinToCpu):
  348. var gCpus: Natural
  349. proc setMinPoolSize*(size: range[1..MaxThreadPoolSize]) =
  350. ## Sets the minimum thread pool size. The default value of this is 4.
  351. minPoolSize = size
  352. proc setMaxPoolSize*(size: range[1..MaxThreadPoolSize]) =
  353. ## Sets the maximum thread pool size. The default value of this
  354. ## is ``MaxThreadPoolSize`` (256).
  355. maxPoolSize = size
  356. if currentPoolSize > maxPoolSize:
  357. for i in maxPoolSize..currentPoolSize-1:
  358. let w = addr(workersData[i])
  359. w.shutdown = true
  360. when defined(nimRecursiveSpawn):
  361. var localThreadId {.threadvar.}: int
  362. proc activateWorkerThread(i: int) {.noinline.} =
  363. workersData[i].taskArrived.initSemaphore()
  364. workersData[i].taskStarted.initSemaphore()
  365. workersData[i].initialized = true
  366. workersData[i].q.empty.initSemaphore()
  367. initLock(workersData[i].q.lock)
  368. createThread(workers[i], slave, addr(workersData[i]))
  369. when defined(nimRecursiveSpawn):
  370. localThreadId = i+1
  371. when defined(nimPinToCpu):
  372. if gCpus > 0: pinToCpu(workers[i], i mod gCpus)
  373. proc activateDistinguishedThread(i: int) {.noinline.} =
  374. distinguishedData[i].taskArrived.initSemaphore()
  375. distinguishedData[i].taskStarted.initSemaphore()
  376. distinguishedData[i].initialized = true
  377. distinguishedData[i].q.empty.initSemaphore()
  378. initLock(distinguishedData[i].q.lock)
  379. distinguishedData[i].readyForTask.initSemaphore()
  380. createThread(distinguished[i], distinguishedSlave, addr(distinguishedData[i]))
  381. proc setup() =
  382. let p = countProcessors()
  383. when defined(nimPinToCpu):
  384. gCpus = p
  385. currentPoolSize = min(p, MaxThreadPoolSize)
  386. readyWorker = addr(workersData[0])
  387. for i in 0..<currentPoolSize: activateWorkerThread(i)
  388. proc preferSpawn*(): bool =
  389. ## Use this proc to determine quickly if a ``spawn`` or a direct call is
  390. ## preferable.
  391. ##
  392. ## If it returns ``true``, a ``spawn`` may make sense. In general
  393. ## it is not necessary to call this directly; use `spawnX template
  394. ## <#spawnX.t>`_ instead.
  395. result = gSomeReady.counter > 0
  396. proc spawn*(call: typed): void {.magic: "Spawn".}
  397. ## Always spawns a new task, so that the ``call`` is never executed on
  398. ## the calling thread.
  399. ##
  400. ## ``call`` has to be proc call ``p(...)`` where ``p`` is gcsafe and has a
  401. ## return type that is either ``void`` or compatible with ``FlowVar[T]``.
  402. proc pinnedSpawn*(id: ThreadId; call: typed): void {.magic: "Spawn".}
  403. ## Always spawns a new task on the worker thread with ``id``, so that
  404. ## the ``call`` is **always** executed on the thread.
  405. ##
  406. ## ``call`` has to be proc call ``p(...)`` where ``p`` is gcsafe and has a
  407. ## return type that is either ``void`` or compatible with ``FlowVar[T]``.
  408. template spawnX*(call): void =
  409. ## Spawns a new task if a CPU core is ready, otherwise executes the
  410. ## call in the calling thread.
  411. ##
  412. ## Usually it is advised to use `spawn proc <#spawn,typed>`_ in order to
  413. ## not block the producer for an unknown amount of time.
  414. ##
  415. ## ``call`` has to be proc call ``p(...)`` where ``p`` is gcsafe and has a
  416. ## return type that is either 'void' or compatible with ``FlowVar[T]``.
  417. (if preferSpawn(): spawn call else: call)
  418. proc parallel*(body: untyped) {.magic: "Parallel".}
  419. ## A parallel section can be used to execute a block in parallel.
  420. ##
  421. ## ``body`` has to be in a DSL that is a particular subset of the language.
  422. ##
  423. ## Please refer to `the manual <manual_experimental.html#parallel-amp-spawn>`_
  424. ## for further information.
  425. var
  426. state: ThreadPoolState
  427. stateLock: Lock
  428. initLock stateLock
  429. proc nimSpawn3(fn: WorkerProc; data: pointer) {.compilerProc.} =
  430. # implementation of 'spawn' that is used by the code generator.
  431. while true:
  432. if selectWorker(readyWorker, fn, data): return
  433. for i in 0..<currentPoolSize:
  434. if selectWorker(addr(workersData[i]), fn, data): return
  435. # determine what to do, but keep in mind this is expensive too:
  436. # state.calls < maxPoolSize: warmup phase
  437. # (state.calls and 127) == 0: periodic check
  438. if state.calls < maxPoolSize or (state.calls and 127) == 0:
  439. # ensure the call to 'advice' is atomic:
  440. if tryAcquire(stateLock):
  441. if currentPoolSize < minPoolSize:
  442. if not workersData[currentPoolSize].initialized:
  443. activateWorkerThread(currentPoolSize)
  444. let w = addr(workersData[currentPoolSize])
  445. atomicInc currentPoolSize
  446. if selectWorker(w, fn, data):
  447. release(stateLock)
  448. return
  449. case advice(state)
  450. of doNothing: discard
  451. of doCreateThread:
  452. if currentPoolSize < maxPoolSize:
  453. if not workersData[currentPoolSize].initialized:
  454. activateWorkerThread(currentPoolSize)
  455. let w = addr(workersData[currentPoolSize])
  456. atomicInc currentPoolSize
  457. if selectWorker(w, fn, data):
  458. release(stateLock)
  459. return
  460. # else we didn't succeed but some other thread, so do nothing.
  461. of doShutdownThread:
  462. if currentPoolSize > minPoolSize:
  463. let w = addr(workersData[currentPoolSize-1])
  464. w.shutdown = true
  465. # we don't free anything here. Too dangerous.
  466. release(stateLock)
  467. # else the acquire failed, but this means some
  468. # other thread succeeded, so we don't need to do anything here.
  469. when defined(nimRecursiveSpawn):
  470. if localThreadId > 0:
  471. # we are a worker thread, so instead of waiting for something which
  472. # might as well never happen (see tparallel_quicksort), we run the task
  473. # on the current thread instead.
  474. var self = addr(workersData[localThreadId-1])
  475. fn(self, data)
  476. blockUntil(self.taskStarted)
  477. return
  478. if isSlave:
  479. # Run under lock until `numSlavesWaiting` increment to avoid a
  480. # race (otherwise two last threads might start waiting together)
  481. withLock numSlavesLock:
  482. if numSlavesRunning <= numSlavesWaiting + 1:
  483. # All the other slaves are waiting
  484. # If we wait now, we-re deadlocked until
  485. # an external spawn happens !
  486. if currentPoolSize < maxPoolSize:
  487. if not workersData[currentPoolSize].initialized:
  488. activateWorkerThread(currentPoolSize)
  489. let w = addr(workersData[currentPoolSize])
  490. atomicInc currentPoolSize
  491. if selectWorker(w, fn, data):
  492. return
  493. else:
  494. # There is no place in the pool. We're deadlocked.
  495. # echo "Deadlock!"
  496. discard
  497. inc numSlavesWaiting
  498. blockUntil(gSomeReady)
  499. if isSlave:
  500. withLock numSlavesLock:
  501. dec numSlavesWaiting
  502. var
  503. distinguishedLock: Lock
  504. initLock distinguishedLock
  505. proc nimSpawn4(fn: WorkerProc; data: pointer; id: ThreadId) {.compilerProc.} =
  506. acquire(distinguishedLock)
  507. if not distinguishedData[id].initialized:
  508. activateDistinguishedThread(id)
  509. release(distinguishedLock)
  510. while true:
  511. if selectWorker(addr(distinguishedData[id]), fn, data): break
  512. blockUntil(distinguishedData[id].readyForTask)
  513. proc sync*() =
  514. ## A simple barrier to wait for all ``spawn``'ed tasks.
  515. ##
  516. ## If you need more elaborate waiting, you have to use an explicit barrier.
  517. while true:
  518. var allReady = true
  519. for i in 0 ..< currentPoolSize:
  520. if not allReady: break
  521. allReady = allReady and workersData[i].ready
  522. if allReady: break
  523. sleep(threadpoolWaitMs)
  524. # We cannot "blockUntil(gSomeReady)" because workers may be shut down between
  525. # the time we establish that some are not "ready" and the time we wait for a
  526. # "signal(gSomeReady)" from inside "slave()" that can never come.
  527. setup()