Skip to content

Commit

Permalink
Add planck.core/sleep
Browse files Browse the repository at this point in the history
Fixes #558
  • Loading branch information
mfikes committed Nov 29, 2017
1 parent 843ccfd commit d9d055c
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file. This change
- Site docs for `--fn-invoke-direct` ([#547](https://github.com/mfikes/planck/issues/547))
- Implement http put, patch, delete, head ([#548](https://github.com/mfikes/planck/issues/548))
- Make `dir` work on aliases ([#552](https://github.com/mfikes/planck/issues/552))
- Add `planck.core/sleep` ([#558](https://github.com/mfikes/planck/issues/558))

### Changed
- Update build to use Lein 2.8.1
Expand Down
2 changes: 2 additions & 0 deletions planck-c/engine.c
Expand Up @@ -492,6 +492,8 @@ void *do_engine_init(void *data) {
register_global_function(ctx, "PLANCK_SOCKET_WRITE", function_socket_write);
register_global_function(ctx, "PLANCK_SOCKET_CLOSE", function_socket_close);

register_global_function(ctx, "PLANCK_SLEEP", function_sleep);

display_launch_timing("register fns");

// Monkey patch cljs.core/system-time to use Planck's high-res timer
Expand Down
24 changes: 24 additions & 0 deletions planck-c/functions.c
Expand Up @@ -11,6 +11,7 @@
#include <limits.h>
#include <pthread.h>
#include <errno.h>
#include <time.h>

#include <JavaScriptCore/JavaScript.h>

Expand Down Expand Up @@ -1296,3 +1297,26 @@ JSValueRef function_socket_close(JSContextRef ctx, JSObjectRef function, JSObjec
}
return JSValueMakeNull(ctx);
}

JSValueRef function_sleep(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject,
size_t argc, const JSValueRef args[], JSValueRef *exception) {
if (argc == 2
&& JSValueGetType(ctx, args[0]) == kJSTypeNumber
&& JSValueGetType(ctx, args[1]) == kJSTypeNumber) {

int millis = (int) JSValueToNumber(ctx, args[0], NULL);
int nanos = (int) JSValueToNumber(ctx, args[1], NULL);

struct timespec t;
t.tv_sec = millis / 1000;
t.tv_nsec = 1000 * 1000 * (millis % 1000) + nanos;

if (t.tv_sec != 0 || t.tv_nsec != 0) {
int err = nanosleep(&t, NULL);
if (err) {
engine_perror("sleep");
}
}
}
return JSValueMakeNull(ctx);
}
3 changes: 3 additions & 0 deletions planck-c/functions.h
Expand Up @@ -135,3 +135,6 @@ JSValueRef function_socket_write(JSContextRef ctx, JSObjectRef function, JSObjec

JSValueRef function_socket_close(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject,
size_t argc, const JSValueRef args[], JSValueRef *exception);

JSValueRef function_sleep(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject,
size_t argc, const JSValueRef args[], JSValueRef *exception);
12 changes: 12 additions & 0 deletions planck-cljs/src/planck/core.cljs
Expand Up @@ -343,6 +343,18 @@
:args (s/cat :state map?)
:ret map?)

(defn sleep
"Causes execution to block for the specified number of milliseconds plus the
optionally specified number of nanoseconds.
millis should not be negative and nanos should be in the range 0–999999"
([millis] (sleep millis 0))
([millis nanos] (js/PLANCK_SLEEP millis nanos)))

(s/fdef sleep
:args (s/alt :unary (s/cat :millis #(and (integer? %) (not (neg? %))))
:binary (s/cat :millis #(and (integer? %) (not (neg? %))) :nanos #(and (integer? %) (<= 0 % 999999)))))

;; Ensure planck.io and planck.http are loaded so that their
;; facilities are available
(repl/side-load-ns 'planck.http)
Expand Down
10 changes: 10 additions & 0 deletions planck-cljs/test/planck/core_test.cljs
Expand Up @@ -113,3 +113,13 @@
(is (= "abc" (planck.core/-read-line buffered-reader)))
(is (= "def" (planck.core/-read-line buffered-reader)))
(is (nil? (planck.core/-read-line buffered-reader)))))

(deftest sleep-test
(let [before (system-time)
_ (planck.core/sleep 10)
after (system-time)]
(is (> (- after before) 8)))
(let [before (system-time)
_ (planck.core/sleep 0 100000)
after (system-time)]
(is (> (- after before) 0.08))))
12 changes: 12 additions & 0 deletions site/src/planck-core.md
Expand Up @@ -26,6 +26,7 @@ _Vars_
[read-line](#read-line)<br/>
[read-password](#read-password)<br/>
[resolve](#resolve)<br/>
[sleep](#sleep)<br/>
[slurp](#slurp)<br/>
[spit](#spit)<br/>
[with-open](#with-open)<br/>
Expand Down Expand Up @@ -198,6 +199,17 @@ Spec<br/>
_args_: `(cat :sym symbol?)`<br/>
_ret_: `(nilable var?)`<br/>

### <a name="sleep"></a>sleep
`([f & opts])`

Causes execution to block for the specified number of milliseconds plus the
optionally specified number of nanoseconds.

`millis` should not be negative and `nanos` should be in the range 0–999999

Spec<br/>
_args_: `(alt :unary (cat :millis #(and (integer? %) (not (neg? %)))) :binary (cat :millis #(and (integer? %) (not (neg? %))) :nanos #(and (integer? %) (<= 0 % 999999))))`<br/>

### <a name="slurp"></a>slurp
`([f & opts])`

Expand Down

0 comments on commit d9d055c

Please sign in to comment.