0xE111 / cat-400

Game framework for nim programming language. Modular and extensible
Mozilla Public License 2.0
88 stars 5 forks source link

Nim 1.4.0 has changes to onThreadDestruction #31

Closed ITR13 closed 11 months ago

ITR13 commented 3 years ago

Using the spawn template on nim 1.4.0+ fails due to the proc for onThreadDestruction not having {.raises: []}.

In my current project I have fixed this locally with:

template spawn*(name: ThreadName, code: untyped) =
  ## Creates a new thread with name `name` and runs `code` inside this thread.
  ## Usage example:
  ##   spawn("thread1"):
  ##     echo "This block is executed inside thread"

  withLock threadsLock:
    if name in threads:
      raise newException(KeyError, "Thread with name '" & name & "' already exists")

    threads[name] = ThreadInfo()
    threads[name].channel.open()
    threads[name].thread.createThread(
      param = name,
      tp = proc(nm: ThreadName) {.thread.} =
        currentThreadName = nm
        onThreadDestruction(proc() {.raises: []} =
          withLock threadsLock:
            try:
              threadsPtr[][nm].channel.close()
              threadsPtr[].del(nm)
            except KeyError:
              quit("Impossible key error somehow happened!")
        )
        code
    )

Not sure if nm causing a key error is actually impossible, but I assume the exception on top of spawn should catch it before it gets to that point?

0xE111 commented 3 years ago

The project will be updated to work with latest nim. I also have a lot of plans to refactor things, and I think I'll just rewrite this threads module from scratch. Maybe. Will leave this opened till fixed. Thanks for the issue.

0xE111 commented 11 months ago

Well, I refactored it :D