ReviewOS

also looking at this

zig-utils/zig-regex

Port to latest zig-master

#2
Closed armagidon-exception wants to merge main into main
53 files +1149 -1017
examples/thread_safety.zigmodified+18-18
Changes to examples/thread_safety.zig
@@ -3,7 +3,7 @@ const Regex = .Regex;
33const SharedRegex = .SharedRegex;
44const RegexCache = .RegexCache;
55
6pub fn main() !void {
6pub fn main(init: std.process.Init) !void {
77 var gpa = std.heap.GeneralPurposeAllocator(.{ .thread_safe = true }){};
88 defer _ = gpa.deinit();
99 const allocator = gpa.allocator();
@@ -17,7 +17,7 @@ pub fn main() !void {
1717 std.debug.print("───────────────────────────────────────────────\n", .{});
1818
1919 // Compile once
20 var regex = try Regex.compile(allocator, "\\d+");
20 var regex = try Regex.compile(allocator, init.io, "\\d+");
2121 defer regex.deinit();
2222
2323 const Worker = struct {
@@ -63,7 +63,7 @@ pub fn main() !void {
6363 std.debug.print("Example 2: SharedRegex with reference counting\n", .{});
6464 std.debug.print("───────────────────────────────────────────────\n", .{});
6565
66 var shared = try SharedRegex.init(allocator, "test");
66 var shared = try SharedRegex.init(allocator, init.io, "test");
6767 defer shared.deinit();
6868
6969 const Worker = struct {
@@ -103,9 +103,9 @@ pub fn main() !void {
103103 std.debug.print("───────────────────────────────────────────────\n", .{});
104104
105105 const Worker = struct {
106 fn run(alloc: std.mem.Allocator, thread_id: usize) void {
106 fn run(alloc: std.mem.Allocator, io: std.Io, thread_id: usize) void {
107107 // Each thread gets its own cache
108 var cache = RegexCache.init(alloc);
108 var cache = RegexCache.init(alloc, io);
109109 defer cache.deinit();
110110
111111 std.debug.print("Thread {d}: Created local cache\n", .{thread_id});
@@ -128,7 +128,7 @@ pub fn main() !void {
128128 var threads: [thread_count]std.Thread = undefined;
129129
130130 for (&threads, 0..) |*thread, i| {
131 thread.* = try std.Thread.spawn(.{}, Worker.run, .{ allocator, i });
131 thread.* = try std.Thread.spawn(.{}, Worker.run, .{ allocator, init.io, i });
132132 }
133133
134134 for (threads) |thread| {
@@ -144,12 +144,12 @@ pub fn main() !void {
144144 std.debug.print("Example 4: Performance with concurrent matching\n", .{});
145145 std.debug.print("───────────────────────────────────────────────\n", .{});
146146
147 var regex = try Regex.compile(allocator, "test");
147 var regex = try Regex.compile(allocator, init.io, "test");
148148 defer regex.deinit();
149149
150150 const Worker = struct {
151 fn run(r: *const Regex) usize {
152 var timer = std.time.Timer.start() catch return 0;
151 fn run(r: *const Regex) i64 {
152 const mark = std.Io.Clock.awake.now(r.io);
153153
154154 var count: usize = 0;
155155 var i: usize = 0;
@@ -159,8 +159,8 @@ pub fn main() !void {
159159 } else |_| {}
160160 }
161161
162 const elapsed_ms = timer.read() / 1_000_000;
163 return elapsed_ms;
162 const elapsed = mark.untilNow(r.io, std.Io.Clock.awake);
163 return elapsed.toMilliseconds();
164164 }
165165 };
166166
@@ -171,15 +171,15 @@ pub fn main() !void {
171171 // Parallel execution
172172 const thread_count = 4;
173173 var threads: [thread_count]std.Thread = undefined;
174 var times: [thread_count]usize = undefined;
174 var times: [thread_count]i64 = undefined;
175175
176176 const ParallelWorker = struct {
177 fn run(r: *const Regex, time_ptr: *usize) void {
177 fn run(r: *const Regex, time_ptr: *i64) void {
178178 time_ptr.* = Worker.run(r);
179179 }
180180 };
181181
182 var parallel_timer = try std.time.Timer.start();
182 var parallel_timer = std.Io.Clock.awake.now(init.io);
183183
184184 for (&threads, 0..) |*thread, i| {
185185 thread.* = try std.Thread.spawn(.{}, ParallelWorker.run, .{ &regex, &times[i] });
@@ -189,7 +189,7 @@ pub fn main() !void {
189189 thread.join();
190190 }
191191
192 const total_parallel = parallel_timer.read() / 1_000_000;
192 const total_parallel = @divExact(parallel_timer.untilNow(init.io, .awake).toNanoseconds(), 1_000_000);
193193
194194 std.debug.print("Parallel ({d} threads): 40,000 matches in {d}ms\n", .{ thread_count, total_parallel });
195195 std.debug.print("Individual thread times: ", .{});
@@ -206,13 +206,13 @@ pub fn main() !void {
206206 std.debug.print("Example 5: Multiple patterns with concurrent access\n", .{});
207207 std.debug.print("───────────────────────────────────────────────\n", .{});
208208
209 var email_regex = try Regex.compile(allocator, "[a-z]+@[a-z]+\\.[a-z]+");
209 var email_regex = try Regex.compile(allocator, init.io, "[a-z]+@[a-z]+\\.[a-z]+");
210210 defer email_regex.deinit();
211211
212 var phone_regex = try Regex.compile(allocator, "\\d{3}-\\d{4}");
212 var phone_regex = try Regex.compile(allocator, init.io, "\\d{3}-\\d{4}");
213213 defer phone_regex.deinit();
214214
215 var url_regex = try Regex.compile(allocator, "https?://");
215 var url_regex = try Regex.compile(allocator, init.io, "https?://");
216216 defer url_regex.deinit();
217217
218218 const Worker = struct {