Skip to content

Commit

Permalink
added 'activeProducer' feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Araq committed Apr 17, 2024
1 parent 3ca95cf commit 05f8515
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
10 changes: 5 additions & 5 deletions malebolgia.nimble
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Package

version = "1.3.1"
author = "Araq"
description = "Malebolgia creates new spawns. Experiments with thread pools and related APIs."
license = "MIT"
srcDir = "src"
version = "1.3.2"
author = "Araq"
description = "Malebolgia creates new spawns. Experiments with thread pools and related APIs."
license = "MIT"
srcDir = "src"


# Dependencies
Expand Down
15 changes: 11 additions & 4 deletions src/malebolgia.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type
runningTasks: int
stopToken: Atomic[bool]
shouldEndAt: Time
usesTimeout: bool
usesTimeout, activeProducer: bool

proc `=destroy`(m: var Master) {.inline.} =

Check warning on line 19 in src/malebolgia.nim

View workflow job for this annotation

GitHub Actions / linux-amd64-nim-devel (master)

A custom '=destroy' hook which takes a 'var T' parameter is deprecated; it should take a 'T' parameter [Deprecated]

Check warning on line 19 in src/malebolgia.nim

View workflow job for this annotation

GitHub Actions / macos-amd64-nim-devel (master)

A custom '=destroy' hook which takes a 'var T' parameter is deprecated; it should take a 'T' parameter [Deprecated]

Check warning on line 19 in src/malebolgia.nim

View workflow job for this annotation

GitHub Actions / windows-amd64-nim-devel (master)

A custom '=destroy' hook which takes a 'var T' parameter is deprecated; it should take a 'T' parameter [Deprecated]

Check warning on line 19 in src/malebolgia.nim

View workflow job for this annotation

GitHub Actions / windows-i386-nim-devel (master)

A custom '=destroy' hook which takes a 'var T' parameter is deprecated; it should take a 'T' parameter [Deprecated]

Check warning on line 19 in src/malebolgia.nim

View workflow job for this annotation

GitHub Actions / linux-amd64-nim-devel (master)

A custom '=destroy' hook which takes a 'var T' parameter is deprecated; it should take a 'T' parameter [Deprecated]

Check warning on line 19 in src/malebolgia.nim

View workflow job for this annotation

GitHub Actions / macos-amd64-nim-devel (master)

A custom '=destroy' hook which takes a 'var T' parameter is deprecated; it should take a 'T' parameter [Deprecated]

Check warning on line 19 in src/malebolgia.nim

View workflow job for this annotation

GitHub Actions / windows-amd64-nim-devel (master)

A custom '=destroy' hook which takes a 'var T' parameter is deprecated; it should take a 'T' parameter [Deprecated]

Check warning on line 19 in src/malebolgia.nim

View workflow job for this annotation

GitHub Actions / windows-i386-nim-devel (master)

A custom '=destroy' hook which takes a 'var T' parameter is deprecated; it should take a 'T' parameter [Deprecated]
deinitCond(m.c)
Expand All @@ -23,13 +23,17 @@ proc `=destroy`(m: var Master) {.inline.} =
proc `=copy`(dest: var Master; src: Master) {.error.}
proc `=sink`(dest: var Master; src: Master) {.error.}

proc createMaster*(timeout = default(Duration)): Master =
proc createMaster*(timeout = default(Duration); activeProducer = false): Master =
## Set `activeProducer` to true to prevent the master thread from
## running a task directly.
## But beware! This can introduce deadlocks for recursive loads!
result = default(Master)
initCond(result.c)
initLock(result.L)
if timeout != default(Duration):
result.usesTimeout = true
result.shouldEndAt = getTime() + timeout
result.activeProducer = activeProducer

proc cancel*(m: var Master) =
## Try to stop all running tasks immediately.
Expand Down Expand Up @@ -163,17 +167,20 @@ proc panicStop*() =
deinitCond(chan.spaceAvailable)
deinitLock(chan.L)

proc shouldSend(master: var Master): bool {.inline.} =
master.activeProducer or busyThreads.load(moRelaxed) < ThreadPoolSize-1

template spawnImplRes[T](master: var Master; fn: typed; res: T) =
if stillHaveTime(master):
if busyThreads.load(moRelaxed) < ThreadPoolSize-1:
if shouldSend(master):
taskCreated master
send PoolTask(m: addr(master), t: toTask(fn), result: addr res)
else:
res = fn

template spawnImplNoRes(master: var Master; fn: typed) =
if stillHaveTime(master):
if busyThreads.load(moRelaxed) < ThreadPoolSize-1:
if shouldSend(master):
taskCreated master
send PoolTask(m: addr(master), t: toTask(fn), result: nil)
else:
Expand Down

0 comments on commit 05f8515

Please sign in to comment.