Created
June 8, 2018 16:16
-
-
Save Alexander-Miller/43b16e9f8e71bb1b56634ae9a612fdbe to your computer and use it in GitHub Desktop.
emacs future
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "mod-test" | |
version = "0.1.0" | |
[lib] | |
crate-type = ["cdylib"] | |
[dependencies] | |
libc = "0.2" | |
emacs = "0.5.1" | |
git2 = "0.7" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; -*- lexical-binding: t -*- | |
(module-load "........../libmod_test.so") | |
(defun thread-wakeup (m c) | |
(with-mutex m (condition-notify c))) | |
(defun thread-sleep (m c) | |
(with-mutex m (condition-wait c))) | |
(defvar-local future-mutex nil) | |
(defvar-local future-thread nil) | |
(defvar-local future-cond-var nil) | |
(defvar-local future-result nil) | |
(defmacro future-deferred-inc (num) | |
`(-let [buf (get-buffer-create "*X*")] | |
(with-current-buffer buf | |
(setq future-mutex (make-mutex "m")) | |
(setq future-cond-var (make-condition-variable future-mutex "m")) | |
(-let [thread (make-thread | |
(lambda () (setq-local future-result (native-deferred_inc ,num future-mutex future-cond-var))))] | |
(setq-local future-thread thread) | |
(run-with-timer | |
6 nil | |
(lambda () (when (buffer-live-p buf) | |
(with-current-buffer buf | |
(when (thread-alive-p thread) (thread-wakeup future-mutex future-cond-var))) | |
(kill-buffer buf)))))) | |
buf)) | |
(defmacro future-get (b) | |
`(with-current-buffer ,b | |
(thread-wakeup future-mutex future-cond-var) | |
(thread-join future-thread) | |
(-let [ret future-result] | |
(kill-buffer ,b) | |
ret))) | |
;; (setq test (future-deferred-inc 3)) | |
;; (future-get test) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate libc; | |
#[macro_use] | |
extern crate emacs; | |
use std::{time, thread}; | |
use emacs::{Env, Result, Value, CallEnv}; | |
emacs_plugin_is_GPL_compatible!(); | |
emacs_module_init!(init); | |
fn deferred_inc(env: &CallEnv) -> Result<i64> { | |
let x: i64 = env.parse_arg(0)?; | |
let m: Value = env.get_arg(1); | |
let c: Value = env.get_arg(2); | |
let thr = thread::spawn(move|| { | |
std::thread::sleep(time::Duration::from_millis(5000)); | |
x + 1 | |
}); | |
env.call("thread-sleep", &[m, c])?; | |
let res = match thr.join() { | |
Ok(x) => x, | |
Err(_) => -1, | |
}; | |
Ok(res) | |
} | |
fn init(env: &Env) -> Result<Value> { | |
emacs_export_functions! { | |
env, "native-", { | |
"deferred_inc" => (deferred_inc, 3..3, "Return NUMBER plus one after 5 seconds."), | |
} | |
} | |
env.provide("future") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, is it possible to do a call like make-hash-table from the spawned thread?(I tried to do it on my side but I have no RUST knowledge and I was unable to compile it).