| 1 | (* Rt.exit runs what Rt.atExit registered, the last first, each once, |
| 2 | then ends the program with main's status. *) |
| 3 | module AtExit; |
| 4 | import Out, Rt; |
| 5 | |
| 6 | proc first; |
| 7 | begin |
| 8 | Out.str("first\n"); |
| 9 | end; |
| 10 | |
| 11 | proc second; |
| 12 | begin |
| 13 | Out.str("second, which exits again\n"); |
| 14 | Rt.exit(5); (* the rest still run, once each; the status is now 5 *) |
| 15 | end; |
| 16 | |
| 17 | proc third; |
| 18 | begin |
| 19 | Out.str("third\n"); |
| 20 | end; |
| 21 | |
| 22 | proc nothing; |
| 23 | begin |
| 24 | end; |
| 25 | |
| 26 | proc main*: INT; |
| 27 | var i, full: I64; |
| 28 | begin |
| 29 | _ := Rt.atExit(first); |
| 30 | _ := Rt.atExit(second); |
| 31 | _ := Rt.atExit(third); |
| 32 | full := 0; |
| 33 | for i := 1 to 40 do |
| 34 | if Rt.atExit(nothing) = Rt.Full then INC(full); end; |
| 35 | end; |
| 36 | Out.int(full); Out.str(" refused\n"); |
| 37 | Out.str("main ends\n"); |
| 38 | return 3; |
| 39 | end; |
| 40 | |
| 41 | end AtExit. |