Skip to content

Commit 9515b73

Browse files
committed
Replace 'timeout.trp' with 'ThreadUtil' module
This also adds the start of a 'Time' module (which is what I initially thought would be the place to put the new timeout function.
1 parent 866b6d1 commit 9515b73

File tree

5 files changed

+35
-29
lines changed

5 files changed

+35
-29
lines changed

examples/fromuserguide/basic_updateableserver.trp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import timeout
1+
import ThreadUtil
2+
23
let fun v_one n =
34
receive [ hn ("REQUEST", senderid) =>
45
let val _ = send (senderid, n)
@@ -26,7 +27,8 @@ let fun v_one n =
2627
val _ = send (service, ("UPDATE", v_two))
2728
val _ = send (service, ("COMPUTE", self(), fn x => x * x, 42))
2829
val _ = receive [ hn x => print x]
29-
in exitAfterTimeout
30-
authority 1000 0 "force terminating the server example after 1s"
30+
in ThreadUtil.spawnTimeout (fn () => print "force terminating the server example after 1s";
31+
exit (authority, 0))
32+
1000
3133
end
3234

lib/Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ build:
55
# Standard Library
66
$(COMPILER) ./Number.trp -l
77
$(COMPILER) ./List.trp -l
8+
$(COMPILER) ./ThreadUtil.trp -l
89
$(COMPILER) ./ListPair.trp -l
910
$(COMPILER) ./DeclassifyUtil.trp -l
1011
$(COMPILER) ./String.trp -l
@@ -13,8 +14,6 @@ build:
1314
$(COMPILER) ./StencilVector.trp -l
1415
$(COMPILER) ./HashMap.trp -l
1516
$(COMPILER) ./HashSet.trp -l
16-
# Old stuff, here be dragons...
17-
$(COMPILER) ./timeout.trp -l
1817

1918
clean:
2019
rm -rf out

lib/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ reviewed rigorously rather than depend on the monitor.
1515
- `Number` : Operations for numbers, i.e. integer and floats.
1616
- `StencilVector` : Memory-efficient implementation of small (sparse) arrays.
1717
- `String` : Operations for strings
18+
- `ThreadUtil` : Additional functions for thread management.
1819
- `Unit` : Unit testing.
1920

2021
## How to add a new file
@@ -34,6 +35,11 @@ target of the *makefile*.
3435
- Each function that is exported has to be documented (`(** <text> *)`). In the long run, we will
3536
auto-generate documentation for the Standard Library.
3637

38+
### Other notes
39+
40+
- The `ThreadUtil` module was initially named `Thread`. But, this suggests incorrectly, that
41+
threading is implemented here rather than being a language primitive.
42+
3743
## TODO
3844

3945
The [modules](#modules) mentioned above already follow the [design principles](#design-principles).

lib/ThreadUtil.trp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
let (** Run function `f` after `duration` milliseconds. The function `f` is given `()` as its
2+
* argument. *)
3+
fun spawnTimeout f duration =
4+
spawn (fn () => sleep duration; f ())
5+
6+
(** Run function `f` each `duration` milliseconds. The function `f` is given the current
7+
* iteration count as argument. *)
8+
fun spawnInterval f duration =
9+
(* TODO: If `f` takes a considerable time, this will drift! *)
10+
let fun f' i = sleep duration; f i; f' (i+1)
11+
in spawn (fn () => f' 0) end
12+
13+
(* TODO: Cancel `spawnTimeout` and `spawnInterval`. But, this requires a non-blocking `receive`
14+
* operation. *)
15+
16+
(*--- Module ---*)
17+
val ThreadUtil = {
18+
spawnTimeout,
19+
spawnInterval
20+
}
21+
22+
in [ ("ThreadUtil", ThreadUtil) ]
23+
end

lib/timeout.trp

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)