raz / site / content / advanced / 09-lifecycle.txt

134 lines · 4.2 KiB · raw · history · last change a5145f64

1title: Starting, ending, and signals
2part: Standing alone
3summary: What runs before main, what runs after, and how to wait for a signal.
4---
5With no run time, the life of a program is short enough to say in a few lines. What happens, in order:
6
71. The entry code sets up a stack for signal handlers and installs the handlers for `SIGSEGV` and `SIGFPE`. A program that uses C starts through C instead, so that C's own set-up runs.
82. The bodies of the modules run, each once, imported modules first.
93. The main module's `main` runs.
104. `Rt.exit` runs the procedures registered with `Rt.atExit`, the last registered first, and ends the program with `main`'s status.
11
12## Bodies, then main
13
14The first tour showed bodies running before `main`. They are the place to set a module up, and they can end early with `return;`.
15
16```raz Order run
17module Order;
18import Out;
19
20var steps*: INT;
21
22proc note(what: []const U8);
23begin
24 INC(steps);
25 Out.int(steps); Out.str(". "); Out.str(what); Out.ln;
26end;
27
28begin
29 note("the body runs before main");
30 if steps > 5 then return; end;
31 note("and carries on if it isn't ended early");
32end Order.
33```
34
35That file is a module with a body and no `main`: it can be imported, or its body run as a program. Add the `main` and see both:
36
37```raz Both run
38module Both;
39import Out, Order;
40
41proc main*;
42begin
43 Out.str("main: "); Out.int(Order.steps); Out.str(" steps happened first\n");
44end;
45
46end Both.
47```
48
49## Things to do at the end
50
51`Rt.atExit(p)` registers a procedure with no parameters to run when the program ends by `Rt.exit`: at the end of `main`, by a call to `Rt.exit`, or by a failed check, after its message. It is where a program restores a terminal or removes a lock file. At most 32 are registered, and each runs at most once. Out's buffer is flushed this way, which is why a program that ends in a failure still shows what it printed.
52
53```raz Cleanup run
54module Cleanup;
55import Out, Rt;
56
57proc first;
58begin
59 Out.str("registered first, runs last\n");
60end;
61
62proc second;
63begin
64 Out.str("registered second, runs first\n");
65end;
66
67proc main*;
68begin
69 _ := Rt.atExit(first);
70 _ := Rt.atExit(second);
71 Out.str("main is done\n");
72end;
73
74end Cleanup.
75```
76
77## Two ways to leave in a hurry
78
79`Rt.exit(status)` ends the program after the `atExit` procedures. `Sys.exit(status)` ends it at once, without them. A failed check and `Rt.exit` also skip the `defer` statements still pending in the calls under way. `Sys.exit` is for a child process that must not run what it inherited, and for nothing else.
80
81```raz Hurry
82module Hurry;
83import Out, Rt, Sys;
84
85proc bye;
86begin
87 Out.str("atExit ran\n");
88end;
89
90proc main*;
91begin
92 _ := Rt.atExit(bye);
93 defer Out.str("a deferred statement ran\n");
94 Out.str("leaving\n");
95 Out.flush;
96 Sys.exit(3);
97end;
98
99end Hurry.
100```
101
102```sh
103$ raz Hurry.raz
104$ ./Hurry; echo "status $?"
105```
106
107Neither the deferred statement nor `bye` ran. That's the rule, and the reason to keep cleanup that matters (a terminal, a lock file) in `atExit` and use `Rt.exit` for ending.
108
109## Signals
110
111A program is stopped by a signal it doesn't catch, with a message that names the place for `SIGSEGV` and `SIGFPE`, and without one for the others, and ends with status 2. A program that wants to handle one, say `SIGTERM` for a clean shutdown, doesn't write a handler that runs at an arbitrary moment. It asks for the signals to arrive as data: `Sys.catch` takes a list of signals and gives a descriptor that is ready to read when one comes, and `Sys.caught` says which. So the program waits for signals the way it waits for input.
112
113```raz Terms run
114module Terms;
115import Out, Sys;
116
117proc main*;
118var fd, sig: INT;
119begin
120 fd := Sys.catch([Sys.sigTerm, Sys.sigWinch]);
121 Out.str("asking myself to stop\n");
122 _ := Sys.kill(Sys.pid(), Sys.sigTerm);
123 if Sys.ready(fd, 1000) then
124 sig := Sys.caught(fd);
125 if sig = Sys.sigTerm then Out.str("caught SIGTERM, stopping cleanly\n"); end;
126 end;
127 sig := Sys.caught(fd);
128 Out.int(sig); Out.str(" waiting after that\n"); (* 0: none *)
129end;
130
131end Terms.
132```
133
134`Sys.ready(fd, ms)` waits up to `ms` milliseconds for something to read; with `Sys.readyOf` it waits on several descriptors, so one loop serves a socket, a terminal and a stop signal together. `Sys.ignore(sig, true)` makes a signal do nothing, as a shell ignores `SIGTTOU`.