This repository has been archived by the owner on Sep 27, 2024. It is now read-only.
forked from cekongnetcv/pike
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignal_posix.zig
173 lines (143 loc) · 5.23 KB
/
signal_posix.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const std = @import("std");
const pike = @import("pike.zig");
const posix = @import("os/posix.zig");
const PackedWaker = @import("waker.zig").PackedWaker;
const os = std.os;
const system = os.system;
const mem = std.mem;
const meta = std.meta;
const builtin = @import("builtin");
pub const SignalType = packed struct {
terminate: bool = false,
interrupt: bool = false,
quit: bool = false,
hup: bool = false,
fn toSet(self: SignalType) os.sigset_t {
const sigaddset = if (comptime builtin.target.isDarwin()) system.sigaddset else os.linux.sigaddset;
var set = mem.zeroes(os.sigset_t);
if (self.terminate) sigaddset(&set, os.SIG.TERM);
if (self.interrupt) sigaddset(&set, os.SIG.INT);
if (self.quit) sigaddset(&set, os.SIG.QUIT);
if (self.hup) sigaddset(&set, os.SIG.HUP);
return set;
}
};
pub const Signal = struct {
const EMPTY_SIGACTION = os.Sigaction{
.handler = .{ .handler = null },
.mask = mem.zeroes(os.sigset_t),
.flags = 0,
};
const MaskInt = meta.Int(.unsigned, @bitSizeOf(SignalType));
const Self = @This();
var lock: std.Thread.Mutex = .{};
var mask: SignalType = .{};
var waker: PackedWaker(pike.Task, SignalType) = .{};
current: SignalType,
previous: [@bitSizeOf(SignalType)]os.Sigaction,
fn handler(signal: c_int) callconv(.C) void {
const current_held = lock.lock();
_=current_held;
const current_mask = mask;
lock.unlock();
//current_held.release();
switch (signal) {
os.SIG.TERM => {
if (!current_mask.terminate) return;
const held = lock.lock();
_=held;
const next_node = waker.wake(.{ .terminate = true });
lock.unlock();
//held.release();
if (next_node) |node| pike.dispatch(&node.data, .{});
},
os.SIG.INT => {
if (!current_mask.interrupt) return;
const held = lock.lock();
_=held;
const next_node = waker.wake(.{ .interrupt = true });
lock.unlock();
//held.release();
if (next_node) |node| pike.dispatch(&node.data, .{});
},
os.SIG.QUIT => {
if (!current_mask.quit) return;
const held = lock.lock();
_=held;
const next_node = waker.wake(.{ .quit = true });
lock.unlock();
//held.release();
if (next_node) |node| pike.dispatch(&node.data, .{});
},
os.SIG.HUP => {
if (!current_mask.hup) return;
const held = lock.lock();
_=held;
const next_node = waker.wake(.{ .hup = true });
lock.unlock();
//held.release();
if (next_node) |node| pike.dispatch(&node.data, .{});
},
else => {},
}
}
pub fn init(current: SignalType) !Self {
const held = lock.lock();
_=held;
defer lock.unlock();
const new_mask = @bitCast(SignalType, @bitCast(MaskInt, current) | @bitCast(MaskInt, mask));
const sigaction = os.Sigaction{
.handler = .{ .handler = handler },
.mask = new_mask.toSet(),
.flags = 0,
};
var previous = [_]os.Sigaction{EMPTY_SIGACTION} ** @bitSizeOf(SignalType);
os.sigaction(os.SIG.TERM, &sigaction, &previous[std.meta.fieldIndex(SignalType, "terminate").?]);
os.sigaction(os.SIG.INT, &sigaction, &previous[std.meta.fieldIndex(SignalType, "interrupt").?]);
os.sigaction(os.SIG.QUIT, &sigaction, &previous[std.meta.fieldIndex(SignalType, "quit").?]);
os.sigaction(os.SIG.HUP, &sigaction, &previous[std.meta.fieldIndex(SignalType, "hup").?]);
mask = new_mask;
return Self{
.current = current,
.previous = previous,
};
}
pub fn deinit(self: *Self) void {
for (self.previous) |sigaction, i| {
os.sigaction(
switch (i) {
0 => os.SIG.TERM,
1 => os.SIG.INT,
2 => os.SIG.QUIT,
3 => os.SIG.HUP,
else => unreachable,
},
&sigaction,
null,
);
}
}
pub fn wait(self: *Self) callconv(.Async) !void {
const held = lock.lock();
_=held;
if (waker.wait(self.current)) {
lock.unlock();
//held.release();
} else {
suspend {
var node = @TypeOf(waker).FrameNode{ .data = pike.Task.init(@frame()) };
@TypeOf(waker).FrameList.append(&waker.heads, self.current, &node);
lock.unlock();
//held.release();
}
const next_held = lock.lock();
_=next_held;
const next_node = waker.next(self.current);
lock.unlock();
//next_held.release();
if (next_node) |node| {
pike.dispatch(&node.data, .{});
}
}
}
};