Skip to content

Commit

Permalink
Implement checks for thread model violations
Browse files Browse the repository at this point in the history
  • Loading branch information
SavchenkoValeriy committed Aug 21, 2022
1 parent 4cf77e1 commit 0873c3e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
5 changes: 5 additions & 0 deletions Source/Swift/Environment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
// EmacsSwiftModule. If not, see <https://www.gnu.org/licenses/>.
//
import EmacsModule
import Foundation

public enum EmacsVersion: Int, Comparable {
case Emacs25 = 25
Expand Down Expand Up @@ -54,9 +55,13 @@ public final class Environment {
guard valid else {
throw EmacsError.lifetimeViolation
}
guard thread == Thread.current else {
throw EmacsError.threadModelViolation
}
return raw.pointee
}
}
let thread = Thread.current

var valid = true
/// Mark this environment as invalid
Expand Down
4 changes: 4 additions & 0 deletions Source/Swift/Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ public enum EmacsError: Error {
///
/// See <doc:Lifetimes> for more details.
case lifetimeViolation
/// Using ``Environment`` in a different thread than issued it.
///
/// See <doc:Lifetimes> for more details.
case threadModelViolation
/// If everything went to hell...
case unknown
}
Expand Down
6 changes: 6 additions & 0 deletions Test/TestModule/Test.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ class TestModule: Module {
try env.defun("swift-env-misuse-lifetime") {
try env.funcall("message", with: "Some message")
}
try env.defun("swift-env-misuse-thread") {
(env: Environment, callback: PersistentEmacsValue) throws in
Task.detached {
try env.funcall(callback)
}
}

if env.version >= .Emacs28 {
let channel = try env.openChannel(name: "test")
Expand Down
7 changes: 5 additions & 2 deletions Test/swift-module-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@
(should (equal x 32))
(funcall done))))

(ert-deftest swift-module:lifetime-checks ()
(ert-deftest-async-tagged swift-module:lifetime-checks (done)
:tags '(emacs-all)
(should-error (swift-env-misuse-lifetime) :type 'swift-error))
(should-error (swift-env-misuse-lifetime) :type 'swift-error)
(swift-env-misuse-thread
(lambda () (funcall done "Shouldn't have called the callback")))
(funcall done))

0 comments on commit 0873c3e

Please # to comment.