-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathkappa.zig
279 lines (229 loc) · 14.1 KB
/
kappa.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
const std = @import("std");
// extern fn time(?*usize) usize;
// extern fn localtime(*const usize) *tm;
// const tm = extern struct {
// tm_sec: c_int, // seconds, range 0 to 59
// tm_min: c_int, // minutes, range 0 to 59
// tm_hour: c_int, // hours, range 0 to 23
// tm_mday: c_int, // day of the month, range 1 to 31
// tm_mon: c_int, // month, range 0 to 11
// tm_year: c_int, // The number of years since 1900
// tm_wday: c_int, // day of the week, range 0 to 6
// tm_yday: c_int, // day in the year, range 0 to 365
// tm_isdst: c_int, // daylight saving time
// tm_gmtoff: c_long,
// tm_zone: [*:0]const u8,
// };
pub fn main() !void {
// const t = time(null);
// std.debug.print("{}\n", .{t});
// const local = localtime(&t);
// std.debug.print("{}\n", .{local});
// Iterm
std.debug.print("kappa: {c}]1337;File=inline=1;width=2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rEk0CQRKJSGQyDgy6Xw2EydXSIRP+exotAkFQqEDQ3S6USCVLH/87+gPwFsCrNqjpEaqAAAAAldEVYdGRhdGU6Y3JlYXRlADIwMTQtMTItMTFUMTA6NDA6NTktMDg6MDD9hrkbAAAAJXRFWHRkYXRlOm1vZGlmeQAyMDE0LTEyLTExVDEwOjQwOjU5LTA4OjAwjNsBpwAAAEZ0RVh0c29mdHdhcmUASW1hZ2VNYWdpY2sgNi42LjYtMiAyMDEwLTEyLTAzIFExNiBodHRwOi8vd3d3LmltYWdlbWFnaWNrLm9yZ0AWPcIAAAAYdEVYdFRodW1iOjpEb2N1bWVudDo6UGFnZXMAMaf/uy8AAAAXdEVYdFRodW1iOjpJbWFnZTo6aGVpZ2h0ADI4U8uiCAAAABZ0RVh0VGh1bWI6OkltYWdlOjpXaWR0aAAyNVcknJsAAAAZdEVYdFRodW1iOjpNaW1ldHlwZQBpbWFnZS9wbmc/slZOAAAAF3RFWHRUaHVtYjo6TVRpbWUAMTMzMTA3NTAxNYzv35cAAAARdEVYdFRodW1iOjpTaXplADg1N0JCvExTcgAAAC10RVh0VGh1bWI6OlVSSQBmaWxlOi8vL3RtcC9taW5pbWFnaWNrMTc3MTctMjMucG5nFe/59AAAAABJRU5ErkJggg==",
// 0x1b,
// });
}
// const std = @import("std");
// test "fuzz" {
// var progress: std.Progress = .{};
// var node = try progress.start("whatever", 0);
// var pool: ThreadPool = undefined;
// try pool.init(std.testing.allocator);
// var n_ran: usize = 0;
// while (true) {
// try testOne(node, &pool, n_ran);
// n_ran += 1;
// }
// }
// fn testOne(parent_node: *std.Progress.Node, pool: *ThreadPool, n_ran: usize) !void {
// const task_name = try std.fmt.allocPrint(std.testing.allocator, "task {d}", .{n_ran});
// defer std.testing.allocator.free(task_name);
// const task_count = std.crypto.random.uintLessThan(usize, 100);
// var node = parent_node.start("task", task_count);
// defer node.end();
// var wg: WaitGroup = undefined;
// try wg.init();
// defer wg.wait();
// var i: usize = 0;
// while (i < task_count) : (i += 1) {
// wg.start();
// try pool.spawn(task, .{ &node, &wg });
// }
// }
// fn task(parent_node: *std.Progress.Node, wg: *WaitGroup) void {
// defer wg.finish();
// parent_node.activate();
// work(parent_node);
// }
// fn work(parent_node: *std.Progress.Node) void {
// const yield_count = std.crypto.random.uintLessThan(usize, 100);
// var node = parent_node.start("work", yield_count);
// node.activate();
// defer node.end();
// var i: usize = 0;
// while (i < yield_count) : (i += 1) {
// std.os.sched_yield() catch return;
// node.completeOne();
// }
// }
// const ThreadPool = struct {
// lock: std.Mutex = .{},
// is_running: bool = true,
// allocator: *std.mem.Allocator,
// workers: []Worker,
// run_queue: RunQueue = .{},
// idle_queue: IdleQueue = .{},
// const IdleQueue = std.SinglyLinkedList(std.ResetEvent);
// const RunQueue = std.SinglyLinkedList(Runnable);
// const Runnable = struct {
// runFn: fn (*Runnable) void,
// };
// const Worker = struct {
// pool: *ThreadPool,
// thread: *std.Thread,
// /// The node is for this worker only and must have an already initialized event
// /// when the thread is spawned.
// idle_node: IdleQueue.Node,
// fn run(worker: *Worker) void {
// while (true) {
// const held = worker.pool.lock.acquire();
// if (worker.pool.run_queue.popFirst()) |run_node| {
// held.release();
// (run_node.data.runFn)(&run_node.data);
// continue;
// }
// if (worker.pool.is_running) {
// worker.idle_node.data.reset();
// worker.pool.idle_queue.prepend(&worker.idle_node);
// held.release();
// worker.idle_node.data.wait();
// continue;
// }
// held.release();
// return;
// }
// }
// };
// pub fn init(self: *ThreadPool, allocator: *std.mem.Allocator) !void {
// self.* = .{
// .allocator = allocator,
// .workers = &[_]Worker{},
// };
// if (std.builtin.single_threaded)
// return;
// const worker_count = std.math.max(1, std.Thread.cpuCount() catch 1);
// self.workers = try allocator.alloc(Worker, worker_count);
// errdefer allocator.free(self.workers);
// var worker_index: usize = 0;
// errdefer self.destroyWorkers(worker_index);
// while (worker_index < worker_count) : (worker_index += 1) {
// const worker = &self.workers[worker_index];
// worker.pool = self;
// // Each worker requires its ResetEvent to be pre-initialized.
// try worker.idle_node.data.init();
// errdefer worker.idle_node.data.deinit();
// worker.thread = try std.Thread.spawn(worker, Worker.run);
// }
// }
// fn destroyWorkers(self: *ThreadPool, spawned: usize) void {
// for (self.workers[0..spawned]) |*worker| {
// worker.thread.wait();
// worker.idle_node.data.deinit();
// }
// }
// pub fn deinit(self: *ThreadPool) void {
// {
// const held = self.lock.acquire();
// defer held.release();
// self.is_running = false;
// while (self.idle_queue.popFirst()) |idle_node|
// idle_node.data.set();
// }
// self.destroyWorkers(self.workers.len);
// self.allocator.free(self.workers);
// }
// pub fn spawn(self: *ThreadPool, comptime func: anytype, args: anytype) !void {
// if (std.builtin.single_threaded) {
// const result = @call(.{}, func, args);
// return;
// }
// const Args = @TypeOf(args);
// const Closure = struct {
// arguments: Args,
// pool: *ThreadPool,
// run_node: RunQueue.Node = .{ .data = .{ .runFn = runFn } },
// fn runFn(runnable: *Runnable) void {
// const run_node = @fieldParentPtr(RunQueue.Node, "data", runnable);
// const closure = @fieldParentPtr(@This(), "run_node", run_node);
// const result = @call(.{}, func, closure.arguments);
// const held = closure.pool.lock.acquire();
// defer held.release();
// closure.pool.allocator.destroy(closure);
// }
// };
// const held = self.lock.acquire();
// defer held.release();
// const closure = try self.allocator.create(Closure);
// closure.* = .{
// .arguments = args,
// .pool = self,
// };
// self.run_queue.prepend(&closure.run_node);
// if (self.idle_queue.popFirst()) |idle_node|
// idle_node.data.set();
// }
// };
// const WaitGroup = struct {
// lock: std.Mutex = .{},
// counter: usize = 0,
// event: std.ResetEvent,
// pub fn init(self: *WaitGroup) !void {
// self.* = .{
// .lock = .{},
// .counter = 0,
// .event = undefined,
// };
// try self.event.init();
// }
// pub fn deinit(self: *WaitGroup) void {
// self.event.deinit();
// self.* = undefined;
// }
// pub fn start(self: *WaitGroup) void {
// const held = self.lock.acquire();
// defer held.release();
// self.counter += 1;
// }
// pub fn finish(self: *WaitGroup) void {
// const held = self.lock.acquire();
// defer held.release();
// self.counter -= 1;
// if (self.counter == 0) {
// self.event.set();
// }
// }
// pub fn wait(self: *WaitGroup) void {
// while (true) {
// const held = self.lock.acquire();
// if (self.counter == 0) {
// held.release();
// return;
// }
// held.release();
// self.event.wait();
// }
// }
// pub fn reset(self: *WaitGroup) void {
// self.event.reset();
// }
// };