removed nullables

master
emekoi 2018-11-27 16:20:09 +00:00
parent 9385127052
commit 35d93d22db
2 changed files with 29 additions and 38 deletions

View File

@ -62,57 +62,50 @@ pub const Mutex = switch(builtin.os) {
},
builtin.Os.windows => struct {
lock: ?windows.CRITICAL_SECTION,
init_once: windows.PINIT_ONCE,
lock: windows.CRITICAL_SECTION,
init_once: windows.RTL_RUN_ONCE,
pub const Held = struct {
mutex: *Mutex,
pub fn release(self: Held) void {
if (self.mutex.lock) |*lock| {
windows.LeaveCriticalSection(lock);
}
windows.LeaveCriticalSection(&self.mutex.lock);
}
};
pub fn init() Mutex {
return Mutex {
.lock = null,
return Mutex {
.lock = undefined,
.init_once = undefined,
};
}
extern fn initCriticalSection(InitOnce: *windows.PINIT_ONCE, Parameter: ?windows.PVOID, Context: ?windows.PVOID) windows.BOOL {
if (Context) |ctx| {
var mutex = @ptrCast(*?windows.CRITICAL_SECTION, @alignCast(8, ctx));
if (mutex.* == null) {
var lock: windows.CRITICAL_SECTION = undefined;
windows.InitializeCriticalSection(&lock);
mutex.* = lock;
}
return windows.TRUE;
}
return windows.FALSE;
extern fn initCriticalSection(
InitOnce: *windows.RTL_RUN_ONCE,
Parameter: ?windows.PVOID,
Context: ?windows.PVOID
) windows.BOOL {
var lock = @ptrCast(
*windows.CRITICAL_SECTION,
@alignCast(@alignOf(*windows.CRITICAL_SECTION), ctx.?)
);
windows.InitializeCriticalSection(lock);
return windows.TRUE;
}
pub fn deinit(self: *Mutex) void {
if (self.lock) |*lock| {
windows.DeleteCriticalSection(lock);
}
windows.DeleteCriticalSection(&self.lock);
}
pub fn acquire(self: *Mutex) Held {
if (self.lock) |*lock| {
windows.EnterCriticalSection(lock);
} else {
var mutex = @ptrCast(?windows.PVOID, self);
if (windows.InitOnceExecuteOnce(&self.init_once, initCriticalSection, null, mutex) == windows.TRUE) {
windows.EnterCriticalSection(&self.lock.?);
} else {
@panic("unable to initialize Mutex");
}
if (windows.InitOnceExecuteOnce(
&self.init_once,
initCriticalSection,
null, @ptrCast(?windows.PVOID, self)
) == windows.FALSE) {
unreachable;
}
windows.EnterCriticalSection(&self.lock);
return Held { .mutex = self };
}
},

View File

@ -222,9 +222,9 @@ pub const FOREGROUND_RED = 4;
pub const FOREGROUND_INTENSITY = 8;
pub extern "kernel32" stdcallcc fn InitializeCriticalSection(lpCriticalSection: *CRITICAL_SECTION) void;
pub extern "kernel32" stdcallcc fn EnterCriticalSection(lpCriticalSection: *CRITICAL_SECTION) void;
pub extern "kernel32" stdcallcc fn LeaveCriticalSection(lpCriticalSection: *CRITICAL_SECTION) void;
pub extern "kernel32" stdcallcc fn DeleteCriticalSection(lpCriticalSection: *CRITICAL_SECTION) void;
pub extern "kernel32" stdcallcc fn EnterCriticalSection(lpCriticalSection: *CRITICAL_SECTION) void;
pub extern "kernel32" stdcallcc fn LeaveCriticalSection(lpCriticalSection: *CRITICAL_SECTION) void;
pub extern "kernel32" stdcallcc fn DeleteCriticalSection(lpCriticalSection: *CRITICAL_SECTION) void;
pub const LIST_ENTRY = extern struct {
Flink: *LIST_ENTRY,
@ -254,12 +254,10 @@ pub const RTL_CRITICAL_SECTION = extern struct {
pub const CRITICAL_SECTION = RTL_CRITICAL_SECTION;
pub extern "kernel32" stdcallcc fn InitOnceExecuteOnce(InitOnce: *PINIT_ONCE, InitFn: PINIT_ONCE_FN, Context: ?PVOID, Parameter: ?LPVOID) BOOL;
pub extern "kernel32" stdcallcc fn InitOnceExecuteOnce(InitOnce: *RTL_RUN_ONCE, InitFn: PINIT_ONCE_FN, Context: ?PVOID, Parameter: ?LPVOID) BOOL;
pub const PINIT_ONCE_FN = ?extern fn(InitOnce: *PINIT_ONCE, Parameter: ?PVOID, Context: ?PVOID) BOOL;
pub const PINIT_ONCE_FN = ?extern fn(InitOnce: *RTL_RUN_ONCE, Parameter: ?PVOID, Context: ?PVOID) BOOL;
pub const RTL_RUN_ONCE = extern struct {
Ptr: PVOID,
};
pub const PINIT_ONCE = RTL_RUN_ONCE;