Allow compilation with threads off.

This commit is contained in:
Alberto Torres 2024-09-13 02:15:32 +02:00
parent c77187fa9f
commit 5b588fddf1
4 changed files with 108 additions and 95 deletions

View file

@ -75,6 +75,7 @@ proc newLoadableResource*[T: LoadableResource](
result.str = proc(): string = ""
result.use_threads = use_threads
when compileOption("threads"):
# main -> thread channels
var to_start: Channel[LoadableResource]
# main <- thread channels
@ -83,7 +84,7 @@ var to_return: Channel[(LoadableResource, bool, string, SliceMem[byte])]
proc start*[T: LoadableResource](self: T) =
self.status = Started
if self.use_threads:
to_start.send self
when compileOption("threads"): to_start.send self
else:
self.start_func(self)
@ -97,6 +98,7 @@ proc onload*[T: LoadableResource](self: T, ok: bool, err: string, data = SliceMe
# self.result[] = (ok, err, data)
if self.onload_func != nil:
if self.use_threads:
when compileOption("threads"):
to_return.send((self.LoadableResource, ok, err, data))
else:
self.onload_func(ok, err, data)
@ -124,6 +126,7 @@ proc cancel*[T: LoadableResource](self: T) =
# results.add res.result[]
# res.result = nil
when compileOption("threads"):
var worker: Thread[void]
proc workerThreadProc() {.thread.} =
while true:
@ -246,7 +249,10 @@ proc loadUri*(
var start_func: proc(self: LoadableResource)
var self: Fetch
var uri = uri
when compileOption("threads"):
var use_threads = use_threads
else:
var use_threads = false
when not defined(onlyLocalFiles):
when defined(emscripten):
const is_remote = true

View file

@ -367,6 +367,7 @@ proc loadOptimized*(tex: Texture, slices: seq[SliceMem[byte]],
, flip = flip, min_channels = min_channels)
when compileOption("threads"):
# main -> thread channels
type DecodeChanMsg = tuple[
tex: Texture, slices: seq[SliceMem[byte]],
@ -392,7 +393,7 @@ proc loadOptimizedThreaded*(tex: Texture, slices: seq[SliceMem[byte]],
callback_compressed: CallbackCompressed = nil,
flip = true, min_channels = 0) =
when false:
when not compileOption("threads"):
loadOptimized(tex, slices, callback_uncompressed, callback_compressed, flip, min_channels)
else:
decode_chan.send((tex: tex, slices: slices,
@ -400,6 +401,7 @@ proc loadOptimizedThreaded*(tex: Texture, slices: seq[SliceMem[byte]],
callback_compressed: callback_compressed,
flip: flip, min_channels: min_channels))
when compileOption("threads"):
var workers = newSeq[Thread[void]](myouEngineNumTextureThreads)
proc workerThreadProc() {.thread.} =
# TODO: handle errors

View file

@ -77,6 +77,7 @@ import ./screen
import ./platform/platform
import ./loaders/blend
import ./util
when compileOption("threads"):
from loadable import updateLoadableWorkerThreads
from ./gpu_formats/texture_optimize import updateTextureWorkerThreads
import arr_ref
@ -167,6 +168,8 @@ proc myou_main_loop*(self: MyouEngine) =
##
## You usually don't need to call this. Use `run <#run,MyouEngine>`_
## instead.
self.renderer.unbind_all_ubos()
when compileOption("threads"):
updateTextureWorkerThreads()
updateLoadableWorkerThreads()
# TODO: make a table object that can be iterated while changing, e.g. with a

View file

@ -46,6 +46,7 @@ import ../screen
import ../util
import ../graphics/render
import ../input
when compileOption("threads"):
from loadable import terminateLoadableWorkerThreads
from ../gpu_formats/texture_optimize import terminateTextureWorkerThreads
@ -234,6 +235,7 @@ proc start_platform_main_loop*(engine: MyouEngine, main_loop: proc(self: MyouEng
for i in 0 ..< window.screen.frame_interval:
window.swapBuffers()
glfw.pollEvents()
when compileOption("threads"):
terminateLoadableWorkerThreads()
terminateTextureWorkerThreads()
glfw.terminate()