Landin library reference discuss source

platform/windows/completion.ldn

1--  I/O completion ports, job objects and the console's control events:
2--  kernel32's calls, raw.  hosted/events waits on a port: the overlapped
3--  calls on the handles associated with it, the job its children are
4--  assigned to and its console control handler all end in a packet the
5--  port holds until a wait takes it.
6
7fixed if compiler.os == windows then
8    --- What `get_completions` takes off a port for each packet,
9    --- `OVERLAPPED_ENTRY`: the key the packet's handle was associated with,
10    --- the address of the call's `overlapped` record, the call's status,
11    --- and the bytes it moved.  A packet a job or `post_completion` sent
12    --- carries its own word in `record` and its own count in `moved`.
13    public completion_entry: type = struct
14        key: usize
15        record: usize
16        internal: usize
17        moved: u32
18        reserved: u32
19    end completion_entry
20
21    --- Make a port with `existing` zero and `opened` `invalid_handle`, or
22    --- associate `opened` with the port `existing` under `key`,
23    --- `CreateIoCompletionPort`: the port, or zero with the reason in
24    --- `last_error`.  A handle is associated once, with one port, for as
25    --- long as it is open; asking again fails `error_invalid_parameter`.
26    --- `threads` zero lets as many threads run as there are processors.
27    public extern(c) link(symbol: "CreateIoCompletionPort") completion_port:
28        (opened: handle, existing: handle, key: usize, threads: u32)
29        -> (port: handle)
30
31    --- Take up to `count` packets off `port` into `into`, waiting up to
32    --- `milliseconds`, or `infinite`, for the first,
33    --- `GetQueuedCompletionStatusEx`: nonzero with how many in `taken`, or
34    --- zero with the reason in `last_error`, `WAIT_TIMEOUT` when none came.
35    --- `alertable` zero.
36    public extern(c) link(symbol: "GetQueuedCompletionStatusEx") get_completions:
37        (port: handle, into: ptr mut completion_entry, count: u32,
38         taken: ptr mut u32, milliseconds: u32, alertable: i32)
39        -> (done: i32)
40
41    --- Put a packet of the caller's on `port`, `PostQueuedCompletionStatus`.
42    public extern(c) link(symbol: "PostQueuedCompletionStatus") post_completion:
43        (port: handle, moved: u32, key: usize, record: usize) -> (done: i32)
44
45    --- Make a job without a name, `CreateJobObjectW`: its handle, or zero
46    --- with the reason in `last_error`.
47    public extern(c) link(symbol: "CreateJobObjectW") create_job:
48        (security: usize, name: usize) -> (job: handle)
49
50    --- `SetInformationJobObject`'s class of the basic limits.
51    public job_basic_limits: u32 = 2
52    --- `SetInformationJobObject`'s class of the port the job reports to.
53    public job_completion_port: u32 = 7
54
55    --- `JOBOBJECT_BASIC_LIMIT_INFORMATION`, its padding named.
56    public job_limits: type = struct
57        process_time: i64
58        job_time: i64
59        flags: u32
60        pad_0: u32
61        least_working_set: usize
62        most_working_set: usize
63        process_count: u32
64        pad_1: u32
65        affinity: usize
66        priority: u32
67        scheduling: u32
68    end job_limits
69
70    --- `JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK`: a process the job's
71    --- processes start is not in the job.
72    public job_silent_breakaway: u32 = 0x1000
73
74    --- `JOBOBJECT_ASSOCIATE_COMPLETION_PORT`: the port the job sends its
75    --- messages to, under `key`.
76    public job_port: type = struct
77        key: usize
78        port: handle
79    end job_port
80
81    --- The message a job's packet carries in its `moved`, with the
82    --- process's ID in its `record`, when a process ended,
83    --- `JOB_OBJECT_MSG_EXIT_PROCESS`.
84    public job_exit_process: u32 = 7
85    --- The message when a process ended with an exception's code,
86    --- `JOB_OBJECT_MSG_ABNORMAL_EXIT_PROCESS`.
87    public job_abnormal_exit_process: u32 = 8
88
89    --- Set what the class `class` says of `job` from the `size` bytes at
90    --- `information`, `SetInformationJobObject`.
91    public extern(c) link(symbol: "SetInformationJobObject") set_job:
92        (job: handle, class: u32, information: usize, size: u32)
93        -> (done: i32)
94
95    --- Put `process` in `job`, `AssignProcessToJobObject`.
96    public extern(c) link(symbol: "AssignProcessToJobObject") assign_to_job:
97        (job: handle, process: handle) -> (done: i32)
98
99    --- The console's control event Ctrl+C, `CTRL_C_EVENT`.
100    public control_c: u32 = 0
101    --- Ctrl+Break, `CTRL_BREAK_EVENT`.
102    public control_break: u32 = 1
103    --- The console closed, `CTRL_CLOSE_EVENT`.
104    public control_close: u32 = 2
105    --- The user logging off, `CTRL_LOGOFF_EVENT`.
106    public control_logoff: u32 = 5
107    --- The system shutting down, `CTRL_SHUTDOWN_EVENT`.
108    public control_shutdown: u32 = 6
109
110    --- A routine the system calls on a thread of its own for each control
111    --- event, `PHANDLER_ROUTINE`: nonzero when it handled the event, zero
112    --- to hand it to the next routine, the last of which ends the process.
113    public control_handler: type = extern(c) (event: u32) -> (handled: i32)
114
115    --- Add the routine at `routine`, a `control_handler`, to the process's
116    --- control handlers when `add` is nonzero, or remove it,
117    --- `SetConsoleCtrlHandler`.  With `routine` zero it sets whether the
118    --- process ignores Ctrl+C, which it does when `add` is nonzero: a
119    --- process a parent started in a new process group begins ignoring it,
120    --- and a child inherits the setting.
121    public extern(c) link(symbol: "SetConsoleCtrlHandler") set_control_handler:
122        (routine: usize, add: i32) -> (done: i32)
123
124    --- Send `event` to the processes of the console's process group
125    --- `group`, every process on the console when zero,
126    --- `GenerateConsoleCtrlEvent`.
127    public extern(c) link(symbol: "GenerateConsoleCtrlEvent") send_control_event:
128        (event: u32, group: u32) -> (done: i32)
129
130    --- Leave the process's console, `FreeConsole`.
131    public extern(c) link(symbol: "FreeConsole") free_console: () -> (done: i32)
132    --- Make the process a console of its own, `AllocConsole`.
133    public extern(c) link(symbol: "AllocConsole") allocate_console:
134        () -> (done: i32)
135end if