Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added 'activeProducer' feature #32

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,22 +14,26 @@
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)
deinitLock(m.L)

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 @@
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
Loading