Initial Commit
This commit is contained in:
20 files changed
+5193
No files matched your search
@@ -0,0 +1,262 @@
|
||||
#! @file system.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains `system`, the layer of the standard library that talks to the
|
||||
#! operating system, as each system provides it, and what every system shares: a
|
||||
#! directory's entries, how a copy of the program stopped, and stopping the
|
||||
#! program.
|
||||
#!
|
||||
#! The layer of the standard library that talks to the operating system, and the
|
||||
#! only one that knows which system it runs on. `std::io` is written once over
|
||||
#! it, for every system, and is what programs use; this layer is for what
|
||||
#! `std::io` does not cover.
|
||||
#!
|
||||
#! Each system implements the same operations in a section of its own, as
|
||||
#! `linux.sakura` and `windows.sakura` do, with the numbers that differ by
|
||||
#! processor in another, as `linux_x86_64.sakura`. Another system is supported
|
||||
#! by writing those two, with the same names and signatures, and nothing above
|
||||
#! this layer changes, but where the system cannot do what Linux does: as
|
||||
#! Windows cannot `fork`, `std::testing` says with `when` what it does there
|
||||
#! instead.
|
||||
#!
|
||||
#! The operations are thin: they take paths as text, as `path::view` gives it,
|
||||
#! and fail by giving a negative number, the error number Linux would give,
|
||||
#! which another system makes of its own, and which `error_of` turns into an
|
||||
#! `io_error`. Each system provides:
|
||||
#!
|
||||
#! ```
|
||||
#! fd its handle to an open file, with fd::stdin, stdout,
|
||||
#! and stderr, and `path_limit`, the longest path it takes
|
||||
#! open(path, flags, mode) opens a file, as `open_flags` say, creating it with
|
||||
#! the permissions `mode` gives where it is made
|
||||
#! fd::read, write, put, seek, close, stat, chmod
|
||||
#! stat, lstat describes the file at a path, following a link at
|
||||
#! the end of it or not, into a `file_info`
|
||||
#! chmod sets the permissions of the file at a path
|
||||
#! mkdir, rmdir, unlink makes a directory, removes an empty one, removes a file
|
||||
#! rename moves a file to another path, replacing a file there
|
||||
#! getcwd, chdir reads and sets the working directory
|
||||
#! dir_stream, opendir reads the names a directory holds, with their kinds
|
||||
#! error_of what a failure means, as an `io_error`
|
||||
#! fork, wait starts a copy of the program and waits for it,
|
||||
#! which Windows starts from the start, by `respawn`
|
||||
#! pipe, spawn, poll makes a pipe, starts another program, and waits,
|
||||
#! for at most a while, until one of several handles
|
||||
#! can be read
|
||||
#! environment, null_device the variables the program started with, and the
|
||||
#! path of the device that holds nothing
|
||||
#! kill, raise, getpid sends a signal to a process, or to the program
|
||||
#! itself, and gives the program's own number
|
||||
#! ignore, restore, handle has the system throw a signal away, do what it
|
||||
#! does by default, or note it for a handler
|
||||
#! pause waits until a handler has run
|
||||
#! clock, sleep reads the wall clock or the steady one, and waits
|
||||
#! for a length of time
|
||||
#! futex_wait, futex_wake waits while a word holds a value, and wakes those
|
||||
#! waiting on it, which threads, and on Linux processes
|
||||
#! sharing memory, wait for one another with
|
||||
#! cpu_count, thread_id, how many processors the program may run on, the
|
||||
#! yield_now calling thread's number, and letting others run
|
||||
#! ```
|
||||
#!
|
||||
#! Windows also provides `respawn`, which starts a copy of the program from its
|
||||
#! start, and `zone_rule`, the rule of the zone the system is set to, since it
|
||||
#! keeps no files of zones.
|
||||
#!
|
||||
#! What every system shares of signals is here: the `signal` each is, and the
|
||||
#! handlers. A handler never runs in the middle of whatever the program was
|
||||
#! doing, as C's does. The system's own handler only notes that the signal came,
|
||||
#! and the program's runs at its next safe point: where one of the program's own
|
||||
#! functions begins, or a pass of one of its loops, in a program that handles a
|
||||
#! signal somewhere. A wait the signal interrupts runs the handlers too, then
|
||||
#! ends, failing with `io_error::interrupted`. So a handler may do anything a
|
||||
#! function may, and never finds a container, a string, or a writer of std in
|
||||
#! the middle of a change.
|
||||
|
||||
|
||||
module system;
|
||||
|
||||
#! How a copy of the program that `fork` started stopped, or a program `spawn`
|
||||
#! started, as `wait` gives it.
|
||||
pub struct process_status {
|
||||
pub var exited: bool; #! Whether it stopped by exiting, with `code`, rather than by a signal.
|
||||
pub var code: i32; #! The status it exited with.
|
||||
pub var signal: i32; #! The signal that stopped it, when it did not exit.
|
||||
}
|
||||
|
||||
#! What `spawn` starts a program with, all of which it copies before the program
|
||||
#! starts.
|
||||
pub struct spawn_request {
|
||||
#! The path of the program, which is found already.
|
||||
pub var program: strview;
|
||||
#! What the program is given, the first being what it is called.
|
||||
pub var arguments: span<strview>;
|
||||
#! The variables it is given, each as `NAME=value`.
|
||||
pub var environment: span<strview>;
|
||||
#! Where it runs, or empty for where this program runs.
|
||||
pub var directory: strview;
|
||||
#! The handle its standard input is given.
|
||||
pub var input: fd;
|
||||
#! The handle its standard output is given.
|
||||
pub var output: fd;
|
||||
#! The handle its standard error is given.
|
||||
pub var error: fd;
|
||||
}
|
||||
|
||||
#! A name a directory holds, as `dir_stream::next` gives it.
|
||||
pub struct dir_entry {
|
||||
pub var name: strview; #! Only good until the next entry is read.
|
||||
pub var kind: file_kind; #! What the name is itself, not what a link there leads to.
|
||||
}
|
||||
|
||||
#! Stops the program with a status, having run everything asked of it.
|
||||
#!
|
||||
#! Whatever the program has written is written, and nothing it has yet to do is
|
||||
#! done. A status of zero says the program did what it was asked to.
|
||||
#! @param code The status to stop with, which Linux reads as a byte, and
|
||||
#! Windows whole.
|
||||
@intrinsic
|
||||
pub def exit(code: i32): void;
|
||||
|
||||
#! Stops the program at once, reporting where it was.
|
||||
#!
|
||||
#! This is for a program that has found something it cannot go on from. It is
|
||||
#! not graceful: the report names the functions that were running, and the
|
||||
#! status is the one every other fault gives.
|
||||
@intrinsic
|
||||
pub def abort(): void;
|
||||
|
||||
#! Starts a thread of the system running a function given a context, which the
|
||||
#! backend makes: a thread of the virtual machine, or of an executable with
|
||||
#! stacks of its own. `std::thread` starts threads with it.
|
||||
#! @param body The function the thread runs.
|
||||
#! @param context What the function is given.
|
||||
#! @returns The thread's number, or a negative error number. A program
|
||||
#! evaluating a constant starts none.
|
||||
@intrinsic
|
||||
pub def start(body: def(*mut u8): void, context: *mut u8): i64;
|
||||
|
||||
#! A signal the system, or another program, sends a program.
|
||||
#!
|
||||
#! The members are the same on every system, which numbers them in its own way.
|
||||
#! Faults are not among them: reading through a null pointer, dividing by zero,
|
||||
#! and the like stop the program with a report, since a fault is a mistake of
|
||||
#! the program that no handler can put right.
|
||||
pub enum signal: u8 {
|
||||
#! SIGHUP: the terminal went away. By custom, a server reads its settings
|
||||
#! again.
|
||||
hangup,
|
||||
#! SIGINT: Ctrl-C.
|
||||
interrupt,
|
||||
#! SIGQUIT: Ctrl-\.
|
||||
quit,
|
||||
#! SIGKILL: the program stops at once. It cannot be handled or ignored.
|
||||
kill,
|
||||
#! SIGUSR1: whatever the programs sending and receiving it agree on.
|
||||
user1,
|
||||
#! SIGUSR2: the same.
|
||||
user2,
|
||||
#! SIGPIPE: the program wrote to a pipe nothing reads.
|
||||
pipe,
|
||||
#! SIGALRM: a timer ran out.
|
||||
alarm,
|
||||
#! SIGTERM: the program is asked to stop.
|
||||
terminate,
|
||||
#! SIGCHLD: a child stopped, or was paused or resumed.
|
||||
child,
|
||||
#! SIGCONT: the program goes on after being paused.
|
||||
resume,
|
||||
#! SIGSTOP: the program is paused. It cannot be handled or ignored.
|
||||
stop,
|
||||
#! SIGTSTP: Ctrl-Z, asking the program to pause.
|
||||
suspend,
|
||||
#! SIGWINCH: the terminal changed size.
|
||||
resized,
|
||||
}
|
||||
|
||||
#! A function that handles a signal, given the signal it handles, which it owns,
|
||||
#! as a functor holding what it needs: whatever it was given as.
|
||||
pub type handler = function<def(signal): void>;
|
||||
|
||||
# How many members `signal` has.
|
||||
const signal_count: u64 = 14;
|
||||
|
||||
# The handler of each signal, by member, which holds nothing where none was
|
||||
# given, and which signals the program handles, one bit each by the system's
|
||||
# number.
|
||||
var handlers: handler[signal_count];
|
||||
var handled: u64;
|
||||
|
||||
# The signals the program watches for rather than handles, one bit each by the
|
||||
# system's number, and the signals taken from the system that neither `watched`
|
||||
# nor the handlers have yet.
|
||||
var noting: u64;
|
||||
var arrived: u64;
|
||||
|
||||
# Whether a handler is running, so that another runs only once it returns.
|
||||
var dispatching: bool;
|
||||
|
||||
# Has the system note a signal of its number when it comes, rather than doing
|
||||
# what it would, and end the waits the signal interrupts rather than go on with
|
||||
# them. Gives zero, or a negative number.
|
||||
@intrinsic
|
||||
def catch_signal(number: i32): i32;
|
||||
|
||||
# Gives the signals noted since they were last asked for, one bit each by the
|
||||
# system's number, and forgets them in the same step.
|
||||
@intrinsic
|
||||
def take_signals(): u64;
|
||||
|
||||
# Runs the handlers of the signals that have come. Safe points call this, which
|
||||
# std's own code has none of.
|
||||
def dispatch(): void {
|
||||
_ = run_handlers();
|
||||
}
|
||||
|
||||
# Runs the handler of each signal that has come, in the order of the system's
|
||||
# numbers, until none has, giving whether any ran. Within a handler it runs
|
||||
# none, so the signals that come while one runs are handled once it returns. A
|
||||
# signal that came before the program stopped handling it is passed over.
|
||||
# Handlers run in the thread the program began with, as each system's
|
||||
# `is_first_thread` finds it, so a wait another thread's signal ended runs none.
|
||||
def run_handlers(): bool {
|
||||
if dispatching || !is_first_thread() {
|
||||
return false;
|
||||
}
|
||||
|
||||
dispatching = true;
|
||||
var ran := false;
|
||||
while true {
|
||||
arrived = arrived | take_signals();
|
||||
val waiting := arrived & ~noting;
|
||||
arrived = arrived & noting;
|
||||
if waiting == 0 {
|
||||
break;
|
||||
}
|
||||
|
||||
# A handler may stop the program handling a signal that came with its own.
|
||||
for number in 0u..64u {
|
||||
if (((waiting & handled) >> number) & 1) == 0 {
|
||||
continue;
|
||||
}
|
||||
val which := signal_of(number as i64) else { continue; };
|
||||
|
||||
# A handler is taken from its place while it runs, so one handling its own
|
||||
# signal anew replaces it without closing what runs. It goes back where
|
||||
# nothing replaced it, and closes where something did.
|
||||
var by := move(&mut handlers[which as u64]);
|
||||
if !by.holds() {
|
||||
continue;
|
||||
}
|
||||
by(which);
|
||||
ran = true;
|
||||
if !handlers[which as u64].holds() {
|
||||
handlers[which as u64] = move(&mut by);
|
||||
}
|
||||
}
|
||||
}
|
||||
dispatching = false;
|
||||
return ran;
|
||||
}
|
||||
Reference in new issue
Block a user