Skip to content

Commit

Permalink
Added some thread library stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
benzap committed Nov 25, 2019
1 parent 7dc4d8d commit 07e7baf
Show file tree
Hide file tree
Showing 13 changed files with 230 additions and 7 deletions.
22 changes: 22 additions & 0 deletions changelog.org
Expand Up @@ -54,6 +54,28 @@

#+END_SRC

- Added threading and promise libraries

#+BEGIN_SRC clojure

local f = thread.future(function()
println(str("Hello from " thread.id() "!"))
print("Waiting")
for i in range(5) do
thread.sleep(500)
print(".")
end
println("Done")
end)

println("Started!")
deref(f)
println("Finished!")

#+END_SRC



* 0.8.0 (First Release)
- Module System is now working. Module paths on windows are supplied
within the Environment Variable ~EDEN_MODULE_PATH~ as semi-colon
Expand Down
38 changes: 38 additions & 0 deletions examples/eden/async.eden
@@ -0,0 +1,38 @@
;; Async Example

local channel-in = async.chan()
local channel-out = async.chan()

thread.future(function(cin cout)
while true do
local value = async.take!!(cin)
if value then
println("Putting Value: " value)
async.put!!(cout value)
else
println("Ending Thread...")
async.close!(cout)
return nil
end
end
end channel-in channel-out)

thread.future(function(cin)

async.put!!(cin "Hello")
async.put!!(cin "World")
async.close!(cin)

end channel-in)

local loop? = true
while loop? do
local value = async.take!!(channel-out)
if value then
println("Took Value: " value)
else
loop? = false
end
end

println("Finished!")
3 changes: 3 additions & 0 deletions examples/eden/basic_http_server.eden
Expand Up @@ -10,3 +10,6 @@ local server = http.server.run-server(app {:port 8000})

println("Started server on port 8000")

while true do
thread.sleep(1000)
end
4 changes: 3 additions & 1 deletion examples/eden/http_server.eden
Expand Up @@ -72,4 +72,6 @@ println("Started Web Server")
println("Access at http://localhost:8000/index.html")


while true do end
while true do
thread.sleep(1000)
end
29 changes: 29 additions & 0 deletions examples/eden/test.eden
@@ -0,0 +1,29 @@
;; Testing


c = async.chan()
fc = thread.future(function(c)
async.put!(c 42)
end c)

p = promise.new()
f = thread.future(function(p)
println("hello " thread.id() " world!")
promise.deliver(p 42)
return 42
end p)

d = thread.delay(function()
println("I Processed This")
return 10
end)

println("Started!")
println("Future Value : " deref(f))
println("Delay Value : " deref(d))
println("Delay Value : " deref(d))
println("Promise Value : " deref(p))
println("Finished!")

println("Future With Channels")
println(async.<!!(c))
14 changes: 14 additions & 0 deletions examples/eden/threading.eden
@@ -0,0 +1,14 @@

local f = thread.future(function()
println(str("Hello from " thread.id() "!"))
print("Waiting")
for i in range(5) do
thread.sleep(500)
print(".")
end
println("Done")
end)

println("Started!")
deref(f)
println("Finished!")
1 change: 1 addition & 0 deletions project.clj
Expand Up @@ -6,6 +6,7 @@
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.9.0"]
[org.clojure/clojurescript "1.10.520"]
[org.clojure/core.async "0.5.527"]
[org.clojure/tools.reader "1.3.2"]
[org.clojure/tools.cli "0.4.1"]

Expand Down
8 changes: 4 additions & 4 deletions readme.org
Expand Up @@ -237,23 +237,23 @@ collections.

#+BEGIN_SRC clojure

[eden "0.8.0"]
[eden "0.9.0"]

#+END_SRC

*Clojure CLI/deps.edn*

#+BEGIN_SRC clojure

eden {:mvn/version "0.8.0"}
eden {:mvn/version "0.9.0"}

#+END_SRC

*Gradle*

#+BEGIN_SRC groovy

compile 'eden:eden:0.8.0'
compile 'eden:eden:0.9.0'

#+END_SRC

Expand All @@ -264,7 +264,7 @@ collections.
<dependency>
<groupId>eden</groupId>
<artifactId>eden</artifactId>
<version>0.8.0</version>
<version>0.9.0</version>
</dependency>

#+END_SRC
Expand Down
10 changes: 8 additions & 2 deletions src/eden/stdlib.cljc
Expand Up @@ -14,7 +14,10 @@
[eden.stdlib.io :refer [import-stdlib-io]]
[eden.stdlib.transit :refer [import-stdlib-transit]]
[eden.stdlib.shell :refer [import-stdlib-shell]]
[eden.stdlib.markdown :refer [import-stdlib-markdown]]))
[eden.stdlib.markdown :refer [import-stdlib-markdown]]
[eden.stdlib.thread :refer [import-stdlib-thread]]
[eden.stdlib.promise :refer [import-stdlib-promise]]))
;;[eden.stdlib.async :refer [import-stdlib-async]]))
;;[eden.stdlib.database :refer [import-stdlib-database]]))


Expand All @@ -34,5 +37,8 @@
import-stdlib-io
import-stdlib-transit
import-stdlib-shell
import-stdlib-markdown))
import-stdlib-markdown
import-stdlib-thread
import-stdlib-promise))
;;import-stdlib-async))
;;import-stdlib-database))
57 changes: 57 additions & 0 deletions src/eden/stdlib/async.cljc
@@ -0,0 +1,57 @@
(ns eden.stdlib.async
(:require
[eden.def :refer [set-var!]]
[clojure.core.async :as async]))


(def async-ns
{:<!! async/<!!
:>!! async/>!!
:admix async/admix
:alts!! async/alts!!
:buffer async/buffer
:chan async/chan
:close! async/close!
:dropping-buffer async/dropping-buffer
:into async/into
:map async/map
:merge async/merge
:mix async/mix
:mult async/mult
:offer! async/offer!
:onto-chan async/onto-chan
:pipe async/pipe
:pipeline async/pipeline
:pipeline-blocking async/pipeline-blocking
:pipeline-async async/pipeline-async
:poll! async/poll!
:promise-chan async/promise-chan
:pub async/pub
:put! async/put!
:put!! async/>!!
:reduce async/reduce
:sliding-buffer async/sliding-buffer
:solo-mode async/solo-mode
:split async/split
:sub async/sub
:take async/take
:take! async/take!
:take!! async/<!!
:tap async/tap
:timeout async/timeout
:to-chan async/to-chan
:toggle async/toggle
:transduce async/transduce
:unblocking-buffer? async/unblocking-buffer?
:unmix async/unmix
:unmix-all async/unmix-all
:unsub async/unsub
:unsub-all async/unsub-all
:untap async/untap
:untap-all async/untap-all})


(defn import-stdlib-async
[eden]
(-> eden
(set-var! 'async async-ns)))
7 changes: 7 additions & 0 deletions src/eden/stdlib/core.cljc
Expand Up @@ -11,6 +11,12 @@
;;(uuid (str (uuid)))


(defn throw-fn
([msg data] (throw (ex-info msg data)))
([msg] (throw-fn msg {}))
([] (throw-fn "No Error Message" {})))


(defn import-stdlib-core [eden]
(-> eden
(set-var! 'apply apply)
Expand Down Expand Up @@ -230,6 +236,7 @@
(set-var! 'take-last take-last)
(set-var! 'take-nth take-nth)
(set-var! 'take-while take-while)
(set-var! 'throw throw-fn)
(set-var! 'true? true?)
(set-var! 'type type)
(set-var! 'unreduced unreduced)
Expand Down
15 changes: 15 additions & 0 deletions src/eden/stdlib/promise.cljc
@@ -0,0 +1,15 @@
(ns eden.stdlib.promise
(:require
[eden.def :refer [set-var!]]))


(def promise-ns
{:new promise
:deliver deliver
:realized? realized?})


(defn import-stdlib-promise
[eden]
(-> eden
(set-var! 'promise promise-ns)))
29 changes: 29 additions & 0 deletions src/eden/stdlib/thread.cljc
@@ -0,0 +1,29 @@
(ns eden.stdlib.thread
(:require
[eden.def :refer [set-var!]]))


(defn future-wrap [f & args]
(future (apply f args)))


(defn delay-wrap [f & args]
(delay (apply f args)))


(def thread
{:future future-wrap
:delay delay-wrap
:delay? delay?
:realized? realized?
:id #(.getId (Thread/currentThread))
:name #(.getName (Thread/currentThread))
:state #(.getState (Thread/currentThread))
:current #(Thread/currentThread)
:sleep #(Thread/sleep %)})


(defn import-stdlib-thread
[eden]
(-> eden
(set-var! 'thread thread)))

0 comments on commit 07e7baf

Please sign in to comment.