const std = @import("std"); const Atomic = std.atomic.Atomic; const Futex = @import("./futex.zig"); // Credit: this is copypasta from @kprotty. Thank you @kprotty! pub const Mutex = struct { state: Atomic(u32) = Atomic(u32).init(UNLOCKED), const UNLOCKED = 0; const LOCKED = 0b01; const CONTENDED = 0b11; const is_x86 = @import("builtin").target.cpu.arch.isX86(); pub fn tryAcquire(self: *Mutex) bool { return self.acquireFast(true); } pub fn acquire(self: *Mutex) void { if (!self.acquireFast(false)) { self.acquireSlow(); } } inline fn acquireFast(self: *Mutex, comptime strong: bool) bool { // On x86, "lock bts" uses less i-cache & can be faster than "lock cmpxchg" below. if (comptime is_x86) { return self.state.bitSet(@ctz(@as(u32, LOCKED)), .Acquire) == UNLOCKED; } const cas_fn = comptime switch (strong) { true => "compareAndSwap", else => "tryCompareAndSwap", }; return @field(self.state, cas_fn)( UNLOCKED, LOCKED, .Acquire, .Monotonic, ) == null; } noinline fn acquireSlow(self: *Mutex) void { // Spin a little bit on the Mutex state in the hopes that // we can acquire it without having to call Futex.wait(). // Give up spinning if the Mutex is contended. // This helps acquire() latency under micro-contention. // var spin: u8 = 100; while (spin > 0) : (spin -= 1) { std.atomic.spinLoopHint(); switch (self.state.load(.Monotonic)) { UNLOCKED => _ = self.state.tryCompareAndSwap( UNLOCKED, LOCKED, .Acquire, .Monotonic, ) orelse return, LOCKED => continue, CONTENDED => break, else => unreachable, // invalid Mutex state } } // Make sure the state is CONTENDED before sleeping with Futex so release() can wake us up. // Transitioning to CONTENDED may also acquire the mutex in the process. // // If we sleep, we must acquire the Mutex with CONTENDED to ensure that other threads // sleeping on the Futex having seen CONTENDED before are eventually woken up by release(). // This unfortunately ends up in an extra Futex.wake() for the last thread but that's ok. while (true) : (Futex.wait(&self.state, CONTENDED, null) catch unreachable) { // On x86, "xchg" can be faster than "lock cmpxchg" below. if (comptime is_x86) { switch (self.state.swap(CONTENDED, .Acquire)) { UNLOCKED => return, LOCKED, CONTENDED => continue, else => unreachable, // invalid Mutex state } } var state = self.state.load(.Monotonic); while (state != CONTENDED) { state = switch (state) { UNLOCKED => self.state.tryCompareAndSwap(state, CONTENDED, .Acquire, .Monotonic) orelse return, LOCKED => self.state.tryCompareAndSwap(state, CONTENDED, .Monotonic, .Monotonic) orelse break, CONTENDED => unreachable, // checked above else => unreachable, // invalid Mutex state }; } } } pub fn release(self: *Mutex) void { switch (self.state.swap(UNLOCKED, .Release)) { UNLOCKED => unreachable, // released without being acquired LOCKED => {}, CONTENDED => Futex.wake(&self.state, 1), else => unreachable, // invalid Mutex state } } }; pub const Lock = struct { mutex: Mutex, pub fn init() Lock { return Lock{ .mutex = Mutex{} }; } pub inline fn lock(this: *Lock) void { this.mutex.acquire(); } pub inline fn unlock(this: *Lock) void { this.mutex.release(); } }; pub fn spinCycle() void {} > Unnamed repository; edit this file 'description' to name the repository.
aboutsummaryrefslogtreecommitdiff
path: root/packages/integrations/deno/test (unfollow)
AgeCommit message (Expand)AuthorFilesLines
2022-04-26Revert "fix: replace serialize-javascript & random-bytes with custom internal...Gravatar Nate Moore 7-565/+215
2022-04-26[ci] formatGravatar okikio 2-271/+315
2022-04-26fix: replace serialize-javascript & random-bytes with custom internal modulesGravatar Okiki 7-215/+521
2022-04-26[ci] release (#3182)create-astro@0.10.0astro@1.0.0-beta.18@astrojs/vercel@0.1.4@astrojs/tailwind@0.2.1@astrojs/svelte@0.1.2@astrojs/netlify@0.3.3Gravatar github-actions[bot] 54-146/+130
2022-04-26[ci] formatGravatar matthewp 1-1/+1
2022-04-26fix(vercel): `trailingSlash` fix for non-html pages (#3185)Gravatar Juan Martín Seery 2-29/+42
2022-04-26Prevent watcher from running during the build (#3207)Gravatar Matthew Phillips 2-0/+9
2022-04-26Fix lockfile (#3210)Gravatar Juan Martín Seery 1-6/+0
2022-04-26Add missing is:raw in AstroBuiltinAttributes (#3209)Gravatar Erika 2-0/+6
2022-04-26Feat: support `astro add` without npm installing (#3183)Gravatar Ben Holmes 6-30/+49
2022-04-26Add Astro attributes to svg elements (#3205)Gravatar Erika 2-1/+9
2022-04-26[ci] formatGravatar bholmesdev 2-9/+9
2022-04-26Feat: `create astro` add install step (#3190)Gravatar Ben Holmes 7-162/+299
2022-04-26[ci] collect statsGravatar FredKSchott 1-0/+1
2022-04-25fix(markdown): file.url fixes (#3198)Gravatar Juan Martín Seery 11-10/+149
2022-04-25[ci] collect statsGravatar FredKSchott 1-0/+1
2022-04-24add vite to licenseGravatar Fred K. Schott 2-24/+29
2022-04-24feat(markdown): Improved types (#3191)Gravatar Juan Martín Seery 3-6/+47
2022-04-24[ci] collect statsGravatar FredKSchott 1-0/+1
2022-04-23[ci] collect statsGravatar FredKSchott 1-0/+1