summaryrefslogtreecommitdiff
path: root/lib/utils
diff options
context:
space:
mode:
authorLex Neva <lexelby@users.noreply.github.com>2023-02-16 22:58:10 -0500
committerGitHub <noreply@github.com>2023-02-16 22:58:10 -0500
commit7aee0979be4f39cde7a6396cdc0d1d2cc4222d90 (patch)
tree1123050a6ba7428dbcc8b72bd074161bdb3e014f /lib/utils
parent30c73dc39c1ac814d0c63ec102c7f8c2b993fdd0 (diff)
parentf6c2aa89197c82cedfb31465fc4b2acffaeb8f25 (diff)
Merge pull request #2045 from inkstitch/lexelby/interruptible-threads
make simulator threads interruptible
Diffstat (limited to 'lib/utils')
-rw-r--r--lib/utils/threading.py22
1 files changed, 22 insertions, 0 deletions
diff --git a/lib/utils/threading.py b/lib/utils/threading.py
new file mode 100644
index 00000000..f0c22887
--- /dev/null
+++ b/lib/utils/threading.py
@@ -0,0 +1,22 @@
+import threading
+
+from ..exceptions import InkstitchException
+from ..debug import debug
+
+
+class ExitThread(InkstitchException):
+ """This exception is thrown in a thread to cause it to terminate.
+
+ Presumably we should only catch this at the thread's top level.
+ """
+ pass
+
+
+# A default flag used for the main thread. It will never be set.
+_default_stop_flag = threading.Event()
+
+
+def check_stop_flag():
+ if getattr(threading.current_thread(), 'stop', _default_stop_flag).is_set():
+ debug.log("exiting thread")
+ raise ExitThread()