mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-05 15:18:49 +00:00
Strip semicolons in core which were missing
This commit is contained in:
@@ -4,20 +4,20 @@ package dynlib
|
|||||||
import "core:os"
|
import "core:os"
|
||||||
|
|
||||||
load_library :: proc(path: string, global_symbols := false) -> (Library, bool) {
|
load_library :: proc(path: string, global_symbols := false) -> (Library, bool) {
|
||||||
flags := os.RTLD_NOW;
|
flags := os.RTLD_NOW
|
||||||
if global_symbols {
|
if global_symbols {
|
||||||
flags |= os.RTLD_GLOBAL;
|
flags |= os.RTLD_GLOBAL
|
||||||
}
|
}
|
||||||
lib := os.dlopen(path, flags);
|
lib := os.dlopen(path, flags)
|
||||||
return Library(lib), lib != nil;
|
return Library(lib), lib != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
unload_library :: proc(library: Library) {
|
unload_library :: proc(library: Library) {
|
||||||
os.dlclose(rawptr(library));
|
os.dlclose(rawptr(library))
|
||||||
}
|
}
|
||||||
|
|
||||||
symbol_address :: proc(library: Library, symbol: string) -> (ptr: rawptr, found: bool) {
|
symbol_address :: proc(library: Library, symbol: string) -> (ptr: rawptr, found: bool) {
|
||||||
ptr = os.dlsym(rawptr(library), symbol);
|
ptr = os.dlsym(rawptr(library), symbol)
|
||||||
found = ptr != nil;
|
found = ptr != nil
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -788,10 +788,16 @@ destroy_constants :: proc() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
assert_if_nil :: #force_inline proc(integers: ..^Int, loc := #caller_location) {
|
assert_if_nil :: proc{assert_if_nil_int, assert_if_nil_rat}
|
||||||
integers := integers
|
|
||||||
|
|
||||||
for i in &integers {
|
assert_if_nil_int :: #force_inline proc(integers: ..^Int, loc := #caller_location) {
|
||||||
|
for i in integers {
|
||||||
assert(i != nil, "(nil)", loc)
|
assert(i != nil, "(nil)", loc)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
assert_if_nil_rat :: #force_inline proc(rationals: ..^Rat, loc := #caller_location) {
|
||||||
|
for r in rationals {
|
||||||
|
assert(r != nil, "(nil)", loc)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1042,7 +1042,7 @@ internal_is_initialized :: proc { internal_int_is_initialized, }
|
|||||||
internal_int_is_zero :: #force_inline proc(a: ^Int) -> (zero: bool) {
|
internal_int_is_zero :: #force_inline proc(a: ^Int) -> (zero: bool) {
|
||||||
return a.used == 0
|
return a.used == 0
|
||||||
}
|
}
|
||||||
internal_is_zero :: proc { internal_int_is_zero, }
|
internal_is_zero :: proc { internal_int_is_zero, internal_rat_is_zero }
|
||||||
|
|
||||||
/*
|
/*
|
||||||
This procedure will return `true` if the `Int` is positive, `false` if not.
|
This procedure will return `true` if the `Int` is positive, `false` if not.
|
||||||
|
|||||||
+4
-1
@@ -168,7 +168,10 @@ buffer_from_slice :: proc "contextless" (backing: $T/[]$E) -> [dynamic]E {
|
|||||||
data = raw_data(backing),
|
data = raw_data(backing),
|
||||||
len = 0,
|
len = 0,
|
||||||
cap = len(backing),
|
cap = len(backing),
|
||||||
allocator = nil_allocator(),
|
allocator = Allocator{
|
||||||
|
procedure = nil_allocator_proc,
|
||||||
|
data = nil,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -317,7 +317,7 @@ is_literal :: proc(kind: Token_Kind) -> bool {
|
|||||||
}
|
}
|
||||||
is_operator :: proc(kind: Token_Kind) -> bool {
|
is_operator :: proc(kind: Token_Kind) -> bool {
|
||||||
#partial switch kind {
|
#partial switch kind {
|
||||||
case .B_Operator_Begin .. .B_Operator_End:
|
case .B_Operator_Begin ..= .B_Operator_End:
|
||||||
return true
|
return true
|
||||||
case .In, .Not_In:
|
case .In, .Not_In:
|
||||||
return true
|
return true
|
||||||
|
|||||||
+31
-31
@@ -4,67 +4,67 @@ import "core:strings"
|
|||||||
import "core:mem"
|
import "core:mem"
|
||||||
|
|
||||||
read_dir :: proc(fd: Handle, n: int, allocator := context.allocator) -> (fi: []File_Info, err: Errno) {
|
read_dir :: proc(fd: Handle, n: int, allocator := context.allocator) -> (fi: []File_Info, err: Errno) {
|
||||||
dirp: Dir;
|
dirp: Dir
|
||||||
dirp, err = _fdopendir(fd);
|
dirp, err = _fdopendir(fd)
|
||||||
if err != ERROR_NONE {
|
if err != ERROR_NONE {
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
defer _closedir(dirp);
|
defer _closedir(dirp)
|
||||||
|
|
||||||
dirpath: string;
|
dirpath: string
|
||||||
dirpath, err = absolute_path_from_handle(fd);
|
dirpath, err = absolute_path_from_handle(fd)
|
||||||
|
|
||||||
if err != ERROR_NONE {
|
if err != ERROR_NONE {
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
defer delete(dirpath);
|
defer delete(dirpath)
|
||||||
|
|
||||||
n := n;
|
n := n
|
||||||
size := n;
|
size := n
|
||||||
if n <= 0 {
|
if n <= 0 {
|
||||||
n = -1;
|
n = -1
|
||||||
size = 100;
|
size = 100
|
||||||
}
|
}
|
||||||
|
|
||||||
dfi := make([dynamic]File_Info, 0, size, allocator);
|
dfi := make([dynamic]File_Info, 0, size, allocator)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
entry: Dirent;
|
entry: Dirent
|
||||||
end_of_stream: bool;
|
end_of_stream: bool
|
||||||
entry, err, end_of_stream = _readdir(dirp);
|
entry, err, end_of_stream = _readdir(dirp)
|
||||||
if err != ERROR_NONE {
|
if err != ERROR_NONE {
|
||||||
for fi_ in dfi {
|
for fi_ in dfi {
|
||||||
file_info_delete(fi_, allocator);
|
file_info_delete(fi_, allocator)
|
||||||
}
|
}
|
||||||
delete(dfi);
|
delete(dfi)
|
||||||
return;
|
return
|
||||||
} else if end_of_stream {
|
} else if end_of_stream {
|
||||||
break;
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
fi_: File_Info;
|
fi_: File_Info
|
||||||
filename := cast(string)(transmute(cstring)mem.Raw_Cstring{ data = &entry.name[0] });
|
filename := cast(string)(transmute(cstring)mem.Raw_Cstring{ data = &entry.name[0] })
|
||||||
|
|
||||||
if filename == "." || filename == ".." {
|
if filename == "." || filename == ".." {
|
||||||
continue;
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
fullpath := strings.join( []string{ dirpath, filename }, "/", context.temp_allocator);
|
fullpath := strings.join( []string{ dirpath, filename }, "/", context.temp_allocator)
|
||||||
defer delete(fullpath, context.temp_allocator);
|
defer delete(fullpath, context.temp_allocator)
|
||||||
|
|
||||||
fi_, err = stat(fullpath, allocator);
|
fi_, err = stat(fullpath, allocator)
|
||||||
if err != ERROR_NONE {
|
if err != ERROR_NONE {
|
||||||
for fi__ in dfi {
|
for fi__ in dfi {
|
||||||
file_info_delete(fi__, allocator);
|
file_info_delete(fi__, allocator)
|
||||||
}
|
}
|
||||||
delete(dfi);
|
delete(dfi)
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
append(&dfi, fi_);
|
append(&dfi, fi_)
|
||||||
}
|
}
|
||||||
|
|
||||||
return dfi[:], ERROR_NONE;
|
return dfi[:], ERROR_NONE
|
||||||
}
|
}
|
||||||
|
|||||||
+31
-31
@@ -4,67 +4,67 @@ import "core:strings"
|
|||||||
import "core:mem"
|
import "core:mem"
|
||||||
|
|
||||||
read_dir :: proc(fd: Handle, n: int, allocator := context.allocator) -> (fi: []File_Info, err: Errno) {
|
read_dir :: proc(fd: Handle, n: int, allocator := context.allocator) -> (fi: []File_Info, err: Errno) {
|
||||||
dirp: Dir;
|
dirp: Dir
|
||||||
dirp, err = _fdopendir(fd);
|
dirp, err = _fdopendir(fd)
|
||||||
if err != ERROR_NONE {
|
if err != ERROR_NONE {
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
defer _closedir(dirp);
|
defer _closedir(dirp)
|
||||||
|
|
||||||
dirpath: string;
|
dirpath: string
|
||||||
dirpath, err = absolute_path_from_handle(fd);
|
dirpath, err = absolute_path_from_handle(fd)
|
||||||
|
|
||||||
if err != ERROR_NONE {
|
if err != ERROR_NONE {
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
defer delete(dirpath);
|
defer delete(dirpath)
|
||||||
|
|
||||||
n := n;
|
n := n
|
||||||
size := n;
|
size := n
|
||||||
if n <= 0 {
|
if n <= 0 {
|
||||||
n = -1;
|
n = -1
|
||||||
size = 100;
|
size = 100
|
||||||
}
|
}
|
||||||
|
|
||||||
dfi := make([dynamic]File_Info, 0, size, allocator);
|
dfi := make([dynamic]File_Info, 0, size, allocator)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
entry: Dirent;
|
entry: Dirent
|
||||||
end_of_stream: bool;
|
end_of_stream: bool
|
||||||
entry, err, end_of_stream = _readdir(dirp);
|
entry, err, end_of_stream = _readdir(dirp)
|
||||||
if err != ERROR_NONE {
|
if err != ERROR_NONE {
|
||||||
for fi_ in dfi {
|
for fi_ in dfi {
|
||||||
file_info_delete(fi_, allocator);
|
file_info_delete(fi_, allocator)
|
||||||
}
|
}
|
||||||
delete(dfi);
|
delete(dfi)
|
||||||
return;
|
return
|
||||||
} else if end_of_stream {
|
} else if end_of_stream {
|
||||||
break;
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
fi_: File_Info;
|
fi_: File_Info
|
||||||
filename := cast(string)(transmute(cstring)mem.Raw_Cstring{ data = &entry.name[0] });
|
filename := cast(string)(transmute(cstring)mem.Raw_Cstring{ data = &entry.name[0] })
|
||||||
|
|
||||||
if filename == "." || filename == ".." {
|
if filename == "." || filename == ".." {
|
||||||
continue;
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
fullpath := strings.join( []string{ dirpath, filename }, "/", context.temp_allocator);
|
fullpath := strings.join( []string{ dirpath, filename }, "/", context.temp_allocator)
|
||||||
defer delete(fullpath, context.temp_allocator);
|
defer delete(fullpath, context.temp_allocator)
|
||||||
|
|
||||||
fi_, err = stat(fullpath, allocator);
|
fi_, err = stat(fullpath, allocator)
|
||||||
if err != ERROR_NONE {
|
if err != ERROR_NONE {
|
||||||
for fi__ in dfi {
|
for fi__ in dfi {
|
||||||
file_info_delete(fi__, allocator);
|
file_info_delete(fi__, allocator)
|
||||||
}
|
}
|
||||||
delete(dfi);
|
delete(dfi)
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
append(&dfi, fi_);
|
append(&dfi, fi_)
|
||||||
}
|
}
|
||||||
|
|
||||||
return dfi[:], ERROR_NONE;
|
return dfi[:], ERROR_NONE
|
||||||
}
|
}
|
||||||
|
|||||||
+330
-330
@@ -9,183 +9,183 @@ import "core:strings"
|
|||||||
import "core:strconv"
|
import "core:strconv"
|
||||||
import "core:c"
|
import "core:c"
|
||||||
|
|
||||||
Handle :: distinct i32;
|
Handle :: distinct i32
|
||||||
File_Time :: distinct u64;
|
File_Time :: distinct u64
|
||||||
Errno :: distinct int;
|
Errno :: distinct int
|
||||||
|
|
||||||
INVALID_HANDLE :: ~Handle(0);
|
INVALID_HANDLE :: ~Handle(0)
|
||||||
|
|
||||||
ERROR_NONE: Errno : 0;
|
ERROR_NONE: Errno : 0
|
||||||
EPERM: Errno : 1; /* Operation not permitted */
|
EPERM: Errno : 1 /* Operation not permitted */
|
||||||
ENOENT: Errno : 2; /* No such file or directory */
|
ENOENT: Errno : 2 /* No such file or directory */
|
||||||
ESRCH: Errno : 3; /* No such process */
|
ESRCH: Errno : 3 /* No such process */
|
||||||
EINTR: Errno : 4; /* Interrupted system call */
|
EINTR: Errno : 4 /* Interrupted system call */
|
||||||
EIO: Errno : 5; /* Input/output error */
|
EIO: Errno : 5 /* Input/output error */
|
||||||
ENXIO: Errno : 6; /* Device not configured */
|
ENXIO: Errno : 6 /* Device not configured */
|
||||||
E2BIG: Errno : 7; /* Argument list too long */
|
E2BIG: Errno : 7 /* Argument list too long */
|
||||||
ENOEXEC: Errno : 8; /* Exec format error */
|
ENOEXEC: Errno : 8 /* Exec format error */
|
||||||
EBADF: Errno : 9; /* Bad file descriptor */
|
EBADF: Errno : 9 /* Bad file descriptor */
|
||||||
ECHILD: Errno : 10; /* No child processes */
|
ECHILD: Errno : 10 /* No child processes */
|
||||||
EDEADLK: Errno : 11; /* Resource deadlock avoided */
|
EDEADLK: Errno : 11 /* Resource deadlock avoided */
|
||||||
ENOMEM: Errno : 12; /* Cannot allocate memory */
|
ENOMEM: Errno : 12 /* Cannot allocate memory */
|
||||||
EACCES: Errno : 13; /* Permission denied */
|
EACCES: Errno : 13 /* Permission denied */
|
||||||
EFAULT: Errno : 14; /* Bad address */
|
EFAULT: Errno : 14 /* Bad address */
|
||||||
ENOTBLK: Errno : 15; /* Block device required */
|
ENOTBLK: Errno : 15 /* Block device required */
|
||||||
EBUSY: Errno : 16; /* Device / Resource busy */
|
EBUSY: Errno : 16 /* Device / Resource busy */
|
||||||
EEXIST: Errno : 17; /* File exists */
|
EEXIST: Errno : 17 /* File exists */
|
||||||
EXDEV: Errno : 18; /* Cross-device link */
|
EXDEV: Errno : 18 /* Cross-device link */
|
||||||
ENODEV: Errno : 19; /* Operation not supported by device */
|
ENODEV: Errno : 19 /* Operation not supported by device */
|
||||||
ENOTDIR: Errno : 20; /* Not a directory */
|
ENOTDIR: Errno : 20 /* Not a directory */
|
||||||
EISDIR: Errno : 21; /* Is a directory */
|
EISDIR: Errno : 21 /* Is a directory */
|
||||||
EINVAL: Errno : 22; /* Invalid argument */
|
EINVAL: Errno : 22 /* Invalid argument */
|
||||||
ENFILE: Errno : 23; /* Too many open files in system */
|
ENFILE: Errno : 23 /* Too many open files in system */
|
||||||
EMFILE: Errno : 24; /* Too many open files */
|
EMFILE: Errno : 24 /* Too many open files */
|
||||||
ENOTTY: Errno : 25; /* Inappropriate ioctl for device */
|
ENOTTY: Errno : 25 /* Inappropriate ioctl for device */
|
||||||
ETXTBSY: Errno : 26; /* Text file busy */
|
ETXTBSY: Errno : 26 /* Text file busy */
|
||||||
EFBIG: Errno : 27; /* File too large */
|
EFBIG: Errno : 27 /* File too large */
|
||||||
ENOSPC: Errno : 28; /* No space left on device */
|
ENOSPC: Errno : 28 /* No space left on device */
|
||||||
ESPIPE: Errno : 29; /* Illegal seek */
|
ESPIPE: Errno : 29 /* Illegal seek */
|
||||||
EROFS: Errno : 30; /* Read-only file system */
|
EROFS: Errno : 30 /* Read-only file system */
|
||||||
EMLINK: Errno : 31; /* Too many links */
|
EMLINK: Errno : 31 /* Too many links */
|
||||||
EPIPE: Errno : 32; /* Broken pipe */
|
EPIPE: Errno : 32 /* Broken pipe */
|
||||||
|
|
||||||
/* math software */
|
/* math software */
|
||||||
EDOM: Errno : 33; /* Numerical argument out of domain */
|
EDOM: Errno : 33 /* Numerical argument out of domain */
|
||||||
ERANGE: Errno : 34; /* Result too large */
|
ERANGE: Errno : 34 /* Result too large */
|
||||||
|
|
||||||
/* non-blocking and interrupt i/o */
|
/* non-blocking and interrupt i/o */
|
||||||
EAGAIN: Errno : 35; /* Resource temporarily unavailable */
|
EAGAIN: Errno : 35 /* Resource temporarily unavailable */
|
||||||
EWOULDBLOCK: Errno : EAGAIN; /* Operation would block */
|
EWOULDBLOCK: Errno : EAGAIN /* Operation would block */
|
||||||
EINPROGRESS: Errno : 36; /* Operation now in progress */
|
EINPROGRESS: Errno : 36 /* Operation now in progress */
|
||||||
EALREADY: Errno : 37; /* Operation already in progress */
|
EALREADY: Errno : 37 /* Operation already in progress */
|
||||||
|
|
||||||
/* ipc/network software -- argument errors */
|
/* ipc/network software -- argument errors */
|
||||||
ENOTSOCK: Errno : 38; /* Socket operation on non-socket */
|
ENOTSOCK: Errno : 38 /* Socket operation on non-socket */
|
||||||
EDESTADDRREQ: Errno : 39; /* Destination address required */
|
EDESTADDRREQ: Errno : 39 /* Destination address required */
|
||||||
EMSGSIZE: Errno : 40; /* Message too long */
|
EMSGSIZE: Errno : 40 /* Message too long */
|
||||||
EPROTOTYPE: Errno : 41; /* Protocol wrong type for socket */
|
EPROTOTYPE: Errno : 41 /* Protocol wrong type for socket */
|
||||||
ENOPROTOOPT: Errno : 42; /* Protocol not available */
|
ENOPROTOOPT: Errno : 42 /* Protocol not available */
|
||||||
EPROTONOSUPPORT: Errno : 43; /* Protocol not supported */
|
EPROTONOSUPPORT: Errno : 43 /* Protocol not supported */
|
||||||
ESOCKTNOSUPPORT: Errno : 44; /* Socket type not supported */
|
ESOCKTNOSUPPORT: Errno : 44 /* Socket type not supported */
|
||||||
ENOTSUP: Errno : 45; /* Operation not supported */
|
ENOTSUP: Errno : 45 /* Operation not supported */
|
||||||
EPFNOSUPPORT: Errno : 46; /* Protocol family not supported */
|
EPFNOSUPPORT: Errno : 46 /* Protocol family not supported */
|
||||||
EAFNOSUPPORT: Errno : 47; /* Address family not supported by protocol family */
|
EAFNOSUPPORT: Errno : 47 /* Address family not supported by protocol family */
|
||||||
EADDRINUSE: Errno : 48; /* Address already in use */
|
EADDRINUSE: Errno : 48 /* Address already in use */
|
||||||
EADDRNOTAVAIL: Errno : 49; /* Can't assign requested address */
|
EADDRNOTAVAIL: Errno : 49 /* Can't assign requested address */
|
||||||
|
|
||||||
/* ipc/network software -- operational errors */
|
/* ipc/network software -- operational errors */
|
||||||
ENETDOWN: Errno : 50; /* Network is down */
|
ENETDOWN: Errno : 50 /* Network is down */
|
||||||
ENETUNREACH: Errno : 51; /* Network is unreachable */
|
ENETUNREACH: Errno : 51 /* Network is unreachable */
|
||||||
ENETRESET: Errno : 52; /* Network dropped connection on reset */
|
ENETRESET: Errno : 52 /* Network dropped connection on reset */
|
||||||
ECONNABORTED: Errno : 53; /* Software caused connection abort */
|
ECONNABORTED: Errno : 53 /* Software caused connection abort */
|
||||||
ECONNRESET: Errno : 54; /* Connection reset by peer */
|
ECONNRESET: Errno : 54 /* Connection reset by peer */
|
||||||
ENOBUFS: Errno : 55; /* No buffer space available */
|
ENOBUFS: Errno : 55 /* No buffer space available */
|
||||||
EISCONN: Errno : 56; /* Socket is already connected */
|
EISCONN: Errno : 56 /* Socket is already connected */
|
||||||
ENOTCONN: Errno : 57; /* Socket is not connected */
|
ENOTCONN: Errno : 57 /* Socket is not connected */
|
||||||
ESHUTDOWN: Errno : 58; /* Can't send after socket shutdown */
|
ESHUTDOWN: Errno : 58 /* Can't send after socket shutdown */
|
||||||
ETOOMANYREFS: Errno : 59; /* Too many references: can't splice */
|
ETOOMANYREFS: Errno : 59 /* Too many references: can't splice */
|
||||||
ETIMEDOUT: Errno : 60; /* Operation timed out */
|
ETIMEDOUT: Errno : 60 /* Operation timed out */
|
||||||
ECONNREFUSED: Errno : 61; /* Connection refused */
|
ECONNREFUSED: Errno : 61 /* Connection refused */
|
||||||
|
|
||||||
ELOOP: Errno : 62; /* Too many levels of symbolic links */
|
ELOOP: Errno : 62 /* Too many levels of symbolic links */
|
||||||
ENAMETOOLONG: Errno : 63; /* File name too long */
|
ENAMETOOLONG: Errno : 63 /* File name too long */
|
||||||
|
|
||||||
/* should be rearranged */
|
/* should be rearranged */
|
||||||
EHOSTDOWN: Errno : 64; /* Host is down */
|
EHOSTDOWN: Errno : 64 /* Host is down */
|
||||||
EHOSTUNREACH: Errno : 65; /* No route to host */
|
EHOSTUNREACH: Errno : 65 /* No route to host */
|
||||||
ENOTEMPTY: Errno : 66; /* Directory not empty */
|
ENOTEMPTY: Errno : 66 /* Directory not empty */
|
||||||
|
|
||||||
/* quotas & mush */
|
/* quotas & mush */
|
||||||
EPROCLIM: Errno : 67; /* Too many processes */
|
EPROCLIM: Errno : 67 /* Too many processes */
|
||||||
EUSERS: Errno : 68; /* Too many users */
|
EUSERS: Errno : 68 /* Too many users */
|
||||||
EDQUOT: Errno : 69; /* Disc quota exceeded */
|
EDQUOT: Errno : 69 /* Disc quota exceeded */
|
||||||
|
|
||||||
/* Network File System */
|
/* Network File System */
|
||||||
ESTALE: Errno : 70; /* Stale NFS file handle */
|
ESTALE: Errno : 70 /* Stale NFS file handle */
|
||||||
EREMOTE: Errno : 71; /* Too many levels of remote in path */
|
EREMOTE: Errno : 71 /* Too many levels of remote in path */
|
||||||
EBADRPC: Errno : 72; /* RPC struct is bad */
|
EBADRPC: Errno : 72 /* RPC struct is bad */
|
||||||
ERPCMISMATCH: Errno : 73; /* RPC version wrong */
|
ERPCMISMATCH: Errno : 73 /* RPC version wrong */
|
||||||
EPROGUNAVAIL: Errno : 74; /* RPC prog. not avail */
|
EPROGUNAVAIL: Errno : 74 /* RPC prog. not avail */
|
||||||
EPROGMISMATCH: Errno : 75; /* Program version wrong */
|
EPROGMISMATCH: Errno : 75 /* Program version wrong */
|
||||||
EPROCUNAVAIL: Errno : 76; /* Bad procedure for program */
|
EPROCUNAVAIL: Errno : 76 /* Bad procedure for program */
|
||||||
|
|
||||||
ENOLCK: Errno : 77; /* No locks available */
|
ENOLCK: Errno : 77 /* No locks available */
|
||||||
ENOSYS: Errno : 78; /* Function not implemented */
|
ENOSYS: Errno : 78 /* Function not implemented */
|
||||||
|
|
||||||
EFTYPE: Errno : 79; /* Inappropriate file type or format */
|
EFTYPE: Errno : 79 /* Inappropriate file type or format */
|
||||||
EAUTH: Errno : 80; /* Authentication error */
|
EAUTH: Errno : 80 /* Authentication error */
|
||||||
ENEEDAUTH: Errno : 81; /* Need authenticator */
|
ENEEDAUTH: Errno : 81 /* Need authenticator */
|
||||||
|
|
||||||
/* Intelligent device errors */
|
/* Intelligent device errors */
|
||||||
EPWROFF: Errno : 82; /* Device power is off */
|
EPWROFF: Errno : 82 /* Device power is off */
|
||||||
EDEVERR: Errno : 83; /* Device error, e.g. paper out */
|
EDEVERR: Errno : 83 /* Device error, e.g. paper out */
|
||||||
EOVERFLOW: Errno : 84; /* Value too large to be stored in data type */
|
EOVERFLOW: Errno : 84 /* Value too large to be stored in data type */
|
||||||
|
|
||||||
/* Program loading errors */
|
/* Program loading errors */
|
||||||
EBADEXEC: Errno : 85; /* Bad executable */
|
EBADEXEC: Errno : 85 /* Bad executable */
|
||||||
EBADARCH: Errno : 86; /* Bad CPU type in executable */
|
EBADARCH: Errno : 86 /* Bad CPU type in executable */
|
||||||
ESHLIBVERS: Errno : 87; /* Shared library version mismatch */
|
ESHLIBVERS: Errno : 87 /* Shared library version mismatch */
|
||||||
EBADMACHO: Errno : 88; /* Malformed Macho file */
|
EBADMACHO: Errno : 88 /* Malformed Macho file */
|
||||||
|
|
||||||
ECANCELED: Errno : 89; /* Operation canceled */
|
ECANCELED: Errno : 89 /* Operation canceled */
|
||||||
|
|
||||||
EIDRM: Errno : 90; /* Identifier removed */
|
EIDRM: Errno : 90 /* Identifier removed */
|
||||||
ENOMSG: Errno : 91; /* No message of desired type */
|
ENOMSG: Errno : 91 /* No message of desired type */
|
||||||
EILSEQ: Errno : 92; /* Illegal byte sequence */
|
EILSEQ: Errno : 92 /* Illegal byte sequence */
|
||||||
ENOATTR: Errno : 93; /* Attribute not found */
|
ENOATTR: Errno : 93 /* Attribute not found */
|
||||||
|
|
||||||
EBADMSG: Errno : 94; /* Bad message */
|
EBADMSG: Errno : 94 /* Bad message */
|
||||||
EMULTIHOP: Errno : 95; /* Reserved */
|
EMULTIHOP: Errno : 95 /* Reserved */
|
||||||
ENODATA: Errno : 96; /* No message available on STREAM */
|
ENODATA: Errno : 96 /* No message available on STREAM */
|
||||||
ENOLINK: Errno : 97; /* Reserved */
|
ENOLINK: Errno : 97 /* Reserved */
|
||||||
ENOSR: Errno : 98; /* No STREAM resources */
|
ENOSR: Errno : 98 /* No STREAM resources */
|
||||||
ENOSTR: Errno : 99; /* Not a STREAM */
|
ENOSTR: Errno : 99 /* Not a STREAM */
|
||||||
EPROTO: Errno : 100; /* Protocol error */
|
EPROTO: Errno : 100 /* Protocol error */
|
||||||
ETIME: Errno : 101; /* STREAM ioctl timeout */
|
ETIME: Errno : 101 /* STREAM ioctl timeout */
|
||||||
|
|
||||||
ENOPOLICY: Errno : 103; /* No such policy registered */
|
ENOPOLICY: Errno : 103 /* No such policy registered */
|
||||||
|
|
||||||
ENOTRECOVERABLE: Errno : 104; /* State not recoverable */
|
ENOTRECOVERABLE: Errno : 104 /* State not recoverable */
|
||||||
EOWNERDEAD: Errno : 105; /* Previous owner died */
|
EOWNERDEAD: Errno : 105 /* Previous owner died */
|
||||||
|
|
||||||
EQFULL: Errno : 106; /* Interface output queue is full */
|
EQFULL: Errno : 106 /* Interface output queue is full */
|
||||||
ELAST: Errno : 106; /* Must be equal largest errno */
|
ELAST: Errno : 106 /* Must be equal largest errno */
|
||||||
|
|
||||||
O_RDONLY :: 0x0000;
|
O_RDONLY :: 0x0000
|
||||||
O_WRONLY :: 0x0001;
|
O_WRONLY :: 0x0001
|
||||||
O_RDWR :: 0x0002;
|
O_RDWR :: 0x0002
|
||||||
O_CREATE :: 0x0200;
|
O_CREATE :: 0x0200
|
||||||
O_EXCL :: 0x0800;
|
O_EXCL :: 0x0800
|
||||||
O_NOCTTY :: 0;
|
O_NOCTTY :: 0
|
||||||
O_TRUNC :: 0x0400;
|
O_TRUNC :: 0x0400
|
||||||
O_NONBLOCK :: 0x0004;
|
O_NONBLOCK :: 0x0004
|
||||||
O_APPEND :: 0x0008;
|
O_APPEND :: 0x0008
|
||||||
O_SYNC :: 0x0080;
|
O_SYNC :: 0x0080
|
||||||
O_ASYNC :: 0x0040;
|
O_ASYNC :: 0x0040
|
||||||
O_CLOEXEC :: 0x1000000;
|
O_CLOEXEC :: 0x1000000
|
||||||
|
|
||||||
SEEK_SET :: 0;
|
SEEK_SET :: 0
|
||||||
SEEK_CUR :: 1;
|
SEEK_CUR :: 1
|
||||||
SEEK_END :: 2;
|
SEEK_END :: 2
|
||||||
SEEK_DATA :: 3;
|
SEEK_DATA :: 3
|
||||||
SEEK_HOLE :: 4;
|
SEEK_HOLE :: 4
|
||||||
SEEK_MAX :: SEEK_HOLE;
|
SEEK_MAX :: SEEK_HOLE
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// NOTE(zangent): These are OS specific!
|
// NOTE(zangent): These are OS specific!
|
||||||
// Do not mix these up!
|
// Do not mix these up!
|
||||||
RTLD_LAZY :: 0x1;
|
RTLD_LAZY :: 0x1
|
||||||
RTLD_NOW :: 0x2;
|
RTLD_NOW :: 0x2
|
||||||
RTLD_LOCAL :: 0x4;
|
RTLD_LOCAL :: 0x4
|
||||||
RTLD_GLOBAL :: 0x8;
|
RTLD_GLOBAL :: 0x8
|
||||||
RTLD_NODELETE :: 0x80;
|
RTLD_NODELETE :: 0x80
|
||||||
RTLD_NOLOAD :: 0x10;
|
RTLD_NOLOAD :: 0x10
|
||||||
RTLD_FIRST :: 0x100;
|
RTLD_FIRST :: 0x100
|
||||||
|
|
||||||
|
|
||||||
// "Argv" arguments converted to Odin strings
|
// "Argv" arguments converted to Odin strings
|
||||||
args := _alloc_command_line_arguments();
|
args := _alloc_command_line_arguments()
|
||||||
|
|
||||||
Unix_File_Time :: struct {
|
Unix_File_Time :: struct {
|
||||||
seconds: i64,
|
seconds: i64,
|
||||||
@@ -214,7 +214,7 @@ OS_Stat :: struct {
|
|||||||
_spare: i32, // RESERVED
|
_spare: i32, // RESERVED
|
||||||
_reserve1,
|
_reserve1,
|
||||||
_reserve2: i64, // RESERVED
|
_reserve2: i64, // RESERVED
|
||||||
};
|
}
|
||||||
|
|
||||||
// NOTE(laleksic, 2021-01-21): Comment and rename these to match OS_Stat above
|
// NOTE(laleksic, 2021-01-21): Comment and rename these to match OS_Stat above
|
||||||
Dirent :: struct {
|
Dirent :: struct {
|
||||||
@@ -223,159 +223,159 @@ Dirent :: struct {
|
|||||||
reclen: u16,
|
reclen: u16,
|
||||||
type: u8,
|
type: u8,
|
||||||
name: [256]byte,
|
name: [256]byte,
|
||||||
};
|
}
|
||||||
|
|
||||||
Dir :: distinct rawptr; // DIR*
|
Dir :: distinct rawptr // DIR*
|
||||||
|
|
||||||
// File type
|
// File type
|
||||||
S_IFMT :: 0o170000; // Type of file mask
|
S_IFMT :: 0o170000 // Type of file mask
|
||||||
S_IFIFO :: 0o010000; // Named pipe (fifo)
|
S_IFIFO :: 0o010000 // Named pipe (fifo)
|
||||||
S_IFCHR :: 0o020000; // Character special
|
S_IFCHR :: 0o020000 // Character special
|
||||||
S_IFDIR :: 0o040000; // Directory
|
S_IFDIR :: 0o040000 // Directory
|
||||||
S_IFBLK :: 0o060000; // Block special
|
S_IFBLK :: 0o060000 // Block special
|
||||||
S_IFREG :: 0o100000; // Regular
|
S_IFREG :: 0o100000 // Regular
|
||||||
S_IFLNK :: 0o120000; // Symbolic link
|
S_IFLNK :: 0o120000 // Symbolic link
|
||||||
S_IFSOCK :: 0o140000; // Socket
|
S_IFSOCK :: 0o140000 // Socket
|
||||||
|
|
||||||
// File mode
|
// File mode
|
||||||
// Read, write, execute/search by owner
|
// Read, write, execute/search by owner
|
||||||
S_IRWXU :: 0o0700; // RWX mask for owner
|
S_IRWXU :: 0o0700 // RWX mask for owner
|
||||||
S_IRUSR :: 0o0400; // R for owner
|
S_IRUSR :: 0o0400 // R for owner
|
||||||
S_IWUSR :: 0o0200; // W for owner
|
S_IWUSR :: 0o0200 // W for owner
|
||||||
S_IXUSR :: 0o0100; // X for owner
|
S_IXUSR :: 0o0100 // X for owner
|
||||||
|
|
||||||
// Read, write, execute/search by group
|
// Read, write, execute/search by group
|
||||||
S_IRWXG :: 0o0070; // RWX mask for group
|
S_IRWXG :: 0o0070 // RWX mask for group
|
||||||
S_IRGRP :: 0o0040; // R for group
|
S_IRGRP :: 0o0040 // R for group
|
||||||
S_IWGRP :: 0o0020; // W for group
|
S_IWGRP :: 0o0020 // W for group
|
||||||
S_IXGRP :: 0o0010; // X for group
|
S_IXGRP :: 0o0010 // X for group
|
||||||
|
|
||||||
// Read, write, execute/search by others
|
// Read, write, execute/search by others
|
||||||
S_IRWXO :: 0o0007; // RWX mask for other
|
S_IRWXO :: 0o0007 // RWX mask for other
|
||||||
S_IROTH :: 0o0004; // R for other
|
S_IROTH :: 0o0004 // R for other
|
||||||
S_IWOTH :: 0o0002; // W for other
|
S_IWOTH :: 0o0002 // W for other
|
||||||
S_IXOTH :: 0o0001; // X for other
|
S_IXOTH :: 0o0001 // X for other
|
||||||
|
|
||||||
S_ISUID :: 0o4000; // Set user id on execution
|
S_ISUID :: 0o4000 // Set user id on execution
|
||||||
S_ISGID :: 0o2000; // Set group id on execution
|
S_ISGID :: 0o2000 // Set group id on execution
|
||||||
S_ISVTX :: 0o1000; // Directory restrcted delete
|
S_ISVTX :: 0o1000 // Directory restrcted delete
|
||||||
|
|
||||||
S_ISLNK :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFLNK; }
|
S_ISLNK :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFLNK }
|
||||||
S_ISREG :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFREG; }
|
S_ISREG :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFREG }
|
||||||
S_ISDIR :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFDIR; }
|
S_ISDIR :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFDIR }
|
||||||
S_ISCHR :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFCHR; }
|
S_ISCHR :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFCHR }
|
||||||
S_ISBLK :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFBLK; }
|
S_ISBLK :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFBLK }
|
||||||
S_ISFIFO :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFIFO; }
|
S_ISFIFO :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFIFO }
|
||||||
S_ISSOCK :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFSOCK; }
|
S_ISSOCK :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFSOCK }
|
||||||
|
|
||||||
R_OK :: 4; // Test for read permission
|
R_OK :: 4 // Test for read permission
|
||||||
W_OK :: 2; // Test for write permission
|
W_OK :: 2 // Test for write permission
|
||||||
X_OK :: 1; // Test for execute permission
|
X_OK :: 1 // Test for execute permission
|
||||||
F_OK :: 0; // Test for file existance
|
F_OK :: 0 // Test for file existance
|
||||||
|
|
||||||
foreign libc {
|
foreign libc {
|
||||||
@(link_name="__error") __error :: proc() -> ^int ---;
|
@(link_name="__error") __error :: proc() -> ^int ---
|
||||||
|
|
||||||
@(link_name="open") _unix_open :: proc(path: cstring, flags: i32, mode: u16) -> Handle ---;
|
@(link_name="open") _unix_open :: proc(path: cstring, flags: i32, mode: u16) -> Handle ---
|
||||||
@(link_name="close") _unix_close :: proc(handle: Handle) ---;
|
@(link_name="close") _unix_close :: proc(handle: Handle) ---
|
||||||
@(link_name="read") _unix_read :: proc(handle: Handle, buffer: rawptr, count: int) -> int ---;
|
@(link_name="read") _unix_read :: proc(handle: Handle, buffer: rawptr, count: int) -> int ---
|
||||||
@(link_name="write") _unix_write :: proc(handle: Handle, buffer: rawptr, count: int) -> int ---;
|
@(link_name="write") _unix_write :: proc(handle: Handle, buffer: rawptr, count: int) -> int ---
|
||||||
@(link_name="lseek") _unix_lseek :: proc(fs: Handle, offset: int, whence: int) -> int ---;
|
@(link_name="lseek") _unix_lseek :: proc(fs: Handle, offset: int, whence: int) -> int ---
|
||||||
@(link_name="gettid") _unix_gettid :: proc() -> u64 ---;
|
@(link_name="gettid") _unix_gettid :: proc() -> u64 ---
|
||||||
@(link_name="getpagesize") _unix_getpagesize :: proc() -> i32 ---;
|
@(link_name="getpagesize") _unix_getpagesize :: proc() -> i32 ---
|
||||||
@(link_name="stat64") _unix_stat :: proc(path: cstring, stat: ^OS_Stat) -> c.int ---;
|
@(link_name="stat64") _unix_stat :: proc(path: cstring, stat: ^OS_Stat) -> c.int ---
|
||||||
@(link_name="lstat") _unix_lstat :: proc(path: cstring, stat: ^OS_Stat) -> c.int ---;
|
@(link_name="lstat") _unix_lstat :: proc(path: cstring, stat: ^OS_Stat) -> c.int ---
|
||||||
@(link_name="fstat") _unix_fstat :: proc(fd: Handle, stat: ^OS_Stat) -> c.int ---;
|
@(link_name="fstat") _unix_fstat :: proc(fd: Handle, stat: ^OS_Stat) -> c.int ---
|
||||||
@(link_name="readlink") _unix_readlink :: proc(path: cstring, buf: ^byte, bufsiz: c.size_t) -> c.ssize_t ---;
|
@(link_name="readlink") _unix_readlink :: proc(path: cstring, buf: ^byte, bufsiz: c.size_t) -> c.ssize_t ---
|
||||||
@(link_name="access") _unix_access :: proc(path: cstring, mask: int) -> int ---;
|
@(link_name="access") _unix_access :: proc(path: cstring, mask: int) -> int ---
|
||||||
@(link_name="fdopendir") _unix_fdopendir :: proc(fd: Handle) -> Dir ---;
|
@(link_name="fdopendir") _unix_fdopendir :: proc(fd: Handle) -> Dir ---
|
||||||
@(link_name="closedir") _unix_closedir :: proc(dirp: Dir) -> c.int ---;
|
@(link_name="closedir") _unix_closedir :: proc(dirp: Dir) -> c.int ---
|
||||||
@(link_name="rewinddir") _unix_rewinddir :: proc(dirp: Dir) ---;
|
@(link_name="rewinddir") _unix_rewinddir :: proc(dirp: Dir) ---
|
||||||
@(link_name="readdir_r") _unix_readdir_r :: proc(dirp: Dir, entry: ^Dirent, result: ^^Dirent) -> c.int ---;
|
@(link_name="readdir_r") _unix_readdir_r :: proc(dirp: Dir, entry: ^Dirent, result: ^^Dirent) -> c.int ---
|
||||||
|
|
||||||
@(link_name="malloc") _unix_malloc :: proc(size: int) -> rawptr ---;
|
@(link_name="malloc") _unix_malloc :: proc(size: int) -> rawptr ---
|
||||||
@(link_name="calloc") _unix_calloc :: proc(num, size: int) -> rawptr ---;
|
@(link_name="calloc") _unix_calloc :: proc(num, size: int) -> rawptr ---
|
||||||
@(link_name="free") _unix_free :: proc(ptr: rawptr) ---;
|
@(link_name="free") _unix_free :: proc(ptr: rawptr) ---
|
||||||
@(link_name="realloc") _unix_realloc :: proc(ptr: rawptr, size: int) -> rawptr ---;
|
@(link_name="realloc") _unix_realloc :: proc(ptr: rawptr, size: int) -> rawptr ---
|
||||||
@(link_name="getenv") _unix_getenv :: proc(cstring) -> cstring ---;
|
@(link_name="getenv") _unix_getenv :: proc(cstring) -> cstring ---
|
||||||
@(link_name="getcwd") _unix_getcwd :: proc(buf: cstring, len: c.size_t) -> cstring ---;
|
@(link_name="getcwd") _unix_getcwd :: proc(buf: cstring, len: c.size_t) -> cstring ---
|
||||||
@(link_name="chdir") _unix_chdir :: proc(buf: cstring) -> c.int ---;
|
@(link_name="chdir") _unix_chdir :: proc(buf: cstring) -> c.int ---
|
||||||
@(link_name="realpath") _unix_realpath :: proc(path: cstring, resolved_path: rawptr) -> rawptr ---;
|
@(link_name="realpath") _unix_realpath :: proc(path: cstring, resolved_path: rawptr) -> rawptr ---
|
||||||
|
|
||||||
@(link_name="exit") _unix_exit :: proc(status: int) -> ! ---;
|
@(link_name="exit") _unix_exit :: proc(status: int) -> ! ---
|
||||||
}
|
}
|
||||||
|
|
||||||
foreign dl {
|
foreign dl {
|
||||||
@(link_name="dlopen") _unix_dlopen :: proc(filename: cstring, flags: int) -> rawptr ---;
|
@(link_name="dlopen") _unix_dlopen :: proc(filename: cstring, flags: int) -> rawptr ---
|
||||||
@(link_name="dlsym") _unix_dlsym :: proc(handle: rawptr, symbol: cstring) -> rawptr ---;
|
@(link_name="dlsym") _unix_dlsym :: proc(handle: rawptr, symbol: cstring) -> rawptr ---
|
||||||
@(link_name="dlclose") _unix_dlclose :: proc(handle: rawptr) -> int ---;
|
@(link_name="dlclose") _unix_dlclose :: proc(handle: rawptr) -> int ---
|
||||||
@(link_name="dlerror") _unix_dlerror :: proc() -> cstring ---;
|
@(link_name="dlerror") _unix_dlerror :: proc() -> cstring ---
|
||||||
}
|
}
|
||||||
|
|
||||||
get_last_error :: proc() -> int {
|
get_last_error :: proc() -> int {
|
||||||
return __error()^;
|
return __error()^
|
||||||
}
|
}
|
||||||
|
|
||||||
open :: proc(path: string, flags: int = O_RDONLY, mode: int = 0) -> (Handle, Errno) {
|
open :: proc(path: string, flags: int = O_RDONLY, mode: int = 0) -> (Handle, Errno) {
|
||||||
cstr := strings.clone_to_cstring(path);
|
cstr := strings.clone_to_cstring(path)
|
||||||
handle := _unix_open(cstr, i32(flags), u16(mode));
|
handle := _unix_open(cstr, i32(flags), u16(mode))
|
||||||
delete(cstr);
|
delete(cstr)
|
||||||
if handle == -1 {
|
if handle == -1 {
|
||||||
return INVALID_HANDLE, 1;
|
return INVALID_HANDLE, 1
|
||||||
}
|
}
|
||||||
return handle, 0;
|
return handle, 0
|
||||||
}
|
}
|
||||||
|
|
||||||
close :: proc(fd: Handle) {
|
close :: proc(fd: Handle) {
|
||||||
_unix_close(fd);
|
_unix_close(fd)
|
||||||
}
|
}
|
||||||
|
|
||||||
write :: proc(fd: Handle, data: []u8) -> (int, Errno) {
|
write :: proc(fd: Handle, data: []u8) -> (int, Errno) {
|
||||||
assert(fd != -1);
|
assert(fd != -1)
|
||||||
|
|
||||||
if len(data) == 0 {
|
if len(data) == 0 {
|
||||||
return 0, 0;
|
return 0, 0
|
||||||
}
|
}
|
||||||
bytes_written := _unix_write(fd, raw_data(data), len(data));
|
bytes_written := _unix_write(fd, raw_data(data), len(data))
|
||||||
if bytes_written == -1 {
|
if bytes_written == -1 {
|
||||||
return 0, 1;
|
return 0, 1
|
||||||
}
|
}
|
||||||
return bytes_written, 0;
|
return bytes_written, 0
|
||||||
}
|
}
|
||||||
|
|
||||||
read :: proc(fd: Handle, data: []u8) -> (int, Errno) {
|
read :: proc(fd: Handle, data: []u8) -> (int, Errno) {
|
||||||
assert(fd != -1);
|
assert(fd != -1)
|
||||||
|
|
||||||
bytes_read := _unix_read(fd, raw_data(data), len(data));
|
bytes_read := _unix_read(fd, raw_data(data), len(data))
|
||||||
if bytes_read == -1 {
|
if bytes_read == -1 {
|
||||||
return 0, 1;
|
return 0, 1
|
||||||
}
|
}
|
||||||
return bytes_read, 0;
|
return bytes_read, 0
|
||||||
}
|
}
|
||||||
|
|
||||||
seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Errno) {
|
seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Errno) {
|
||||||
assert(fd != -1);
|
assert(fd != -1)
|
||||||
|
|
||||||
final_offset := i64(_unix_lseek(fd, int(offset), whence));
|
final_offset := i64(_unix_lseek(fd, int(offset), whence))
|
||||||
if final_offset == -1 {
|
if final_offset == -1 {
|
||||||
return 0, 1;
|
return 0, 1
|
||||||
}
|
}
|
||||||
return final_offset, 0;
|
return final_offset, 0
|
||||||
}
|
}
|
||||||
|
|
||||||
file_size :: proc(fd: Handle) -> (i64, Errno) {
|
file_size :: proc(fd: Handle) -> (i64, Errno) {
|
||||||
prev, _ := seek(fd, 0, SEEK_CUR);
|
prev, _ := seek(fd, 0, SEEK_CUR)
|
||||||
size, err := seek(fd, 0, SEEK_END);
|
size, err := seek(fd, 0, SEEK_END)
|
||||||
seek(fd, prev, SEEK_SET);
|
seek(fd, prev, SEEK_SET)
|
||||||
return i64(size), err;
|
return i64(size), err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// NOTE(bill): Uses startup to initialize it
|
// NOTE(bill): Uses startup to initialize it
|
||||||
stdin: Handle = 0; // get_std_handle(win32.STD_INPUT_HANDLE);
|
stdin: Handle = 0 // get_std_handle(win32.STD_INPUT_HANDLE);
|
||||||
stdout: Handle = 1; // get_std_handle(win32.STD_OUTPUT_HANDLE);
|
stdout: Handle = 1 // get_std_handle(win32.STD_OUTPUT_HANDLE);
|
||||||
stderr: Handle = 2; // get_std_handle(win32.STD_ERROR_HANDLE);
|
stderr: Handle = 2 // get_std_handle(win32.STD_ERROR_HANDLE);
|
||||||
|
|
||||||
/* TODO(zangent): Implement these!
|
/* TODO(zangent): Implement these!
|
||||||
last_write_time :: proc(fd: Handle) -> File_Time {}
|
last_write_time :: proc(fd: Handle) -> File_Time {}
|
||||||
@@ -383,239 +383,239 @@ last_write_time_by_name :: proc(name: string) -> File_Time {}
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
is_path_separator :: proc(r: rune) -> bool {
|
is_path_separator :: proc(r: rune) -> bool {
|
||||||
return r == '/';
|
return r == '/'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@private
|
@private
|
||||||
_stat :: proc(path: string) -> (OS_Stat, Errno) {
|
_stat :: proc(path: string) -> (OS_Stat, Errno) {
|
||||||
cstr := strings.clone_to_cstring(path, context.temp_allocator);
|
cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
||||||
|
|
||||||
s: OS_Stat;
|
s: OS_Stat
|
||||||
result := _unix_stat(cstr, &s);
|
result := _unix_stat(cstr, &s)
|
||||||
if result == -1 {
|
if result == -1 {
|
||||||
return s, Errno(get_last_error());
|
return s, Errno(get_last_error())
|
||||||
}
|
}
|
||||||
return s, ERROR_NONE;
|
return s, ERROR_NONE
|
||||||
}
|
}
|
||||||
|
|
||||||
@private
|
@private
|
||||||
_lstat :: proc(path: string) -> (OS_Stat, Errno) {
|
_lstat :: proc(path: string) -> (OS_Stat, Errno) {
|
||||||
cstr := strings.clone_to_cstring(path, context.temp_allocator);
|
cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
||||||
|
|
||||||
s: OS_Stat;
|
s: OS_Stat
|
||||||
result := _unix_lstat(cstr, &s);
|
result := _unix_lstat(cstr, &s)
|
||||||
if result == -1 {
|
if result == -1 {
|
||||||
return s, Errno(get_last_error());
|
return s, Errno(get_last_error())
|
||||||
}
|
}
|
||||||
return s, ERROR_NONE;
|
return s, ERROR_NONE
|
||||||
}
|
}
|
||||||
|
|
||||||
@private
|
@private
|
||||||
_fstat :: proc(fd: Handle) -> (OS_Stat, Errno) {
|
_fstat :: proc(fd: Handle) -> (OS_Stat, Errno) {
|
||||||
s: OS_Stat;
|
s: OS_Stat
|
||||||
result := _unix_fstat(fd, &s);
|
result := _unix_fstat(fd, &s)
|
||||||
if result == -1 {
|
if result == -1 {
|
||||||
return s, Errno(get_last_error());
|
return s, Errno(get_last_error())
|
||||||
}
|
}
|
||||||
return s, ERROR_NONE;
|
return s, ERROR_NONE
|
||||||
}
|
}
|
||||||
|
|
||||||
@private
|
@private
|
||||||
_fdopendir :: proc(fd: Handle) -> (Dir, Errno) {
|
_fdopendir :: proc(fd: Handle) -> (Dir, Errno) {
|
||||||
dirp := _unix_fdopendir(fd);
|
dirp := _unix_fdopendir(fd)
|
||||||
if dirp == cast(Dir)nil {
|
if dirp == cast(Dir)nil {
|
||||||
return nil, Errno(get_last_error());
|
return nil, Errno(get_last_error())
|
||||||
}
|
}
|
||||||
return dirp, ERROR_NONE;
|
return dirp, ERROR_NONE
|
||||||
}
|
}
|
||||||
|
|
||||||
@private
|
@private
|
||||||
_closedir :: proc(dirp: Dir) -> Errno {
|
_closedir :: proc(dirp: Dir) -> Errno {
|
||||||
rc := _unix_closedir(dirp);
|
rc := _unix_closedir(dirp)
|
||||||
if rc != 0 {
|
if rc != 0 {
|
||||||
return Errno(get_last_error());
|
return Errno(get_last_error())
|
||||||
}
|
}
|
||||||
return ERROR_NONE;
|
return ERROR_NONE
|
||||||
}
|
}
|
||||||
|
|
||||||
@private
|
@private
|
||||||
_rewinddir :: proc(dirp: Dir) {
|
_rewinddir :: proc(dirp: Dir) {
|
||||||
_unix_rewinddir(dirp);
|
_unix_rewinddir(dirp)
|
||||||
}
|
}
|
||||||
|
|
||||||
@private
|
@private
|
||||||
_readdir :: proc(dirp: Dir) -> (entry: Dirent, err: Errno, end_of_stream: bool) {
|
_readdir :: proc(dirp: Dir) -> (entry: Dirent, err: Errno, end_of_stream: bool) {
|
||||||
result: ^Dirent;
|
result: ^Dirent
|
||||||
rc := _unix_readdir_r(dirp, &entry, &result);
|
rc := _unix_readdir_r(dirp, &entry, &result)
|
||||||
|
|
||||||
if rc != 0 {
|
if rc != 0 {
|
||||||
err = Errno(get_last_error());
|
err = Errno(get_last_error())
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
err = ERROR_NONE;
|
err = ERROR_NONE
|
||||||
|
|
||||||
if result == nil {
|
if result == nil {
|
||||||
end_of_stream = true;
|
end_of_stream = true
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
end_of_stream = false;
|
end_of_stream = false
|
||||||
|
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@private
|
@private
|
||||||
_readlink :: proc(path: string) -> (string, Errno) {
|
_readlink :: proc(path: string) -> (string, Errno) {
|
||||||
path_cstr := strings.clone_to_cstring(path, context.temp_allocator);
|
path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
||||||
|
|
||||||
bufsz : uint = 256;
|
bufsz : uint = 256
|
||||||
buf := make([]byte, bufsz);
|
buf := make([]byte, bufsz)
|
||||||
for {
|
for {
|
||||||
rc := _unix_readlink(path_cstr, &(buf[0]), bufsz);
|
rc := _unix_readlink(path_cstr, &(buf[0]), bufsz)
|
||||||
if rc == -1 {
|
if rc == -1 {
|
||||||
delete(buf);
|
delete(buf)
|
||||||
return "", Errno(get_last_error());
|
return "", Errno(get_last_error())
|
||||||
} else if rc == int(bufsz) {
|
} else if rc == int(bufsz) {
|
||||||
// NOTE(laleksic, 2021-01-21): Any cleaner way to resize the slice?
|
// NOTE(laleksic, 2021-01-21): Any cleaner way to resize the slice?
|
||||||
bufsz *= 2;
|
bufsz *= 2
|
||||||
delete(buf);
|
delete(buf)
|
||||||
buf = make([]byte, bufsz);
|
buf = make([]byte, bufsz)
|
||||||
} else {
|
} else {
|
||||||
return strings.string_from_ptr(&buf[0], rc), ERROR_NONE;
|
return strings.string_from_ptr(&buf[0], rc), ERROR_NONE
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
absolute_path_from_handle :: proc(fd: Handle) -> (string, Errno) {
|
absolute_path_from_handle :: proc(fd: Handle) -> (string, Errno) {
|
||||||
buf : [256]byte;
|
buf : [256]byte
|
||||||
fd_str := strconv.itoa( buf[:], cast(int)fd );
|
fd_str := strconv.itoa( buf[:], cast(int)fd )
|
||||||
|
|
||||||
procfs_path := strings.concatenate( []string{ "/proc/self/fd/", fd_str } );
|
procfs_path := strings.concatenate( []string{ "/proc/self/fd/", fd_str } )
|
||||||
defer delete(procfs_path);
|
defer delete(procfs_path)
|
||||||
|
|
||||||
return _readlink(procfs_path);
|
return _readlink(procfs_path)
|
||||||
}
|
}
|
||||||
|
|
||||||
absolute_path_from_relative :: proc(rel: string) -> (path: string, err: Errno) {
|
absolute_path_from_relative :: proc(rel: string) -> (path: string, err: Errno) {
|
||||||
rel := rel;
|
rel := rel
|
||||||
if rel == "" {
|
if rel == "" {
|
||||||
rel = ".";
|
rel = "."
|
||||||
}
|
}
|
||||||
|
|
||||||
rel_cstr := strings.clone_to_cstring(rel, context.temp_allocator);
|
rel_cstr := strings.clone_to_cstring(rel, context.temp_allocator)
|
||||||
|
|
||||||
path_ptr := _unix_realpath(rel_cstr, nil);
|
path_ptr := _unix_realpath(rel_cstr, nil)
|
||||||
if path_ptr == nil {
|
if path_ptr == nil {
|
||||||
return "", Errno(get_last_error());
|
return "", Errno(get_last_error())
|
||||||
}
|
}
|
||||||
defer _unix_free(path_ptr);
|
defer _unix_free(path_ptr)
|
||||||
|
|
||||||
path_cstr := transmute(cstring)path_ptr;
|
path_cstr := transmute(cstring)path_ptr
|
||||||
path = strings.clone( string(path_cstr) );
|
path = strings.clone( string(path_cstr) )
|
||||||
|
|
||||||
return path, ERROR_NONE;
|
return path, ERROR_NONE
|
||||||
}
|
}
|
||||||
|
|
||||||
access :: proc(path: string, mask: int) -> bool {
|
access :: proc(path: string, mask: int) -> bool {
|
||||||
cstr := strings.clone_to_cstring(path, context.temp_allocator);
|
cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
||||||
return _unix_access(cstr, mask) == 0;
|
return _unix_access(cstr, mask) == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
heap_alloc :: proc(size: int) -> rawptr {
|
heap_alloc :: proc(size: int) -> rawptr {
|
||||||
assert(size > 0);
|
assert(size > 0)
|
||||||
return _unix_calloc(1, size);
|
return _unix_calloc(1, size)
|
||||||
}
|
}
|
||||||
heap_resize :: proc(ptr: rawptr, new_size: int) -> rawptr {
|
heap_resize :: proc(ptr: rawptr, new_size: int) -> rawptr {
|
||||||
return _unix_realloc(ptr, new_size);
|
return _unix_realloc(ptr, new_size)
|
||||||
}
|
}
|
||||||
heap_free :: proc(ptr: rawptr) {
|
heap_free :: proc(ptr: rawptr) {
|
||||||
_unix_free(ptr);
|
_unix_free(ptr)
|
||||||
}
|
}
|
||||||
|
|
||||||
getenv :: proc(name: string) -> (string, bool) {
|
getenv :: proc(name: string) -> (string, bool) {
|
||||||
path_str := strings.clone_to_cstring(name, context.temp_allocator);
|
path_str := strings.clone_to_cstring(name, context.temp_allocator)
|
||||||
cstr := _unix_getenv(path_str);
|
cstr := _unix_getenv(path_str)
|
||||||
if cstr == nil {
|
if cstr == nil {
|
||||||
return "", false;
|
return "", false
|
||||||
}
|
}
|
||||||
return string(cstr), true;
|
return string(cstr), true
|
||||||
}
|
}
|
||||||
|
|
||||||
get_current_directory :: proc() -> string {
|
get_current_directory :: proc() -> string {
|
||||||
page_size := get_page_size(); // NOTE(tetra): See note in os_linux.odin/get_current_directory.
|
page_size := get_page_size() // NOTE(tetra): See note in os_linux.odin/get_current_directory.
|
||||||
buf := make([dynamic]u8, page_size);
|
buf := make([dynamic]u8, page_size)
|
||||||
for {
|
for {
|
||||||
cwd := _unix_getcwd(cstring(raw_data(buf)), c.size_t(len(buf)));
|
cwd := _unix_getcwd(cstring(raw_data(buf)), c.size_t(len(buf)))
|
||||||
if cwd != nil {
|
if cwd != nil {
|
||||||
return string(cwd);
|
return string(cwd)
|
||||||
}
|
}
|
||||||
if Errno(get_last_error()) != ERANGE {
|
if Errno(get_last_error()) != ERANGE {
|
||||||
return "";
|
return ""
|
||||||
}
|
}
|
||||||
resize(&buf, len(buf)+page_size);
|
resize(&buf, len(buf)+page_size)
|
||||||
}
|
}
|
||||||
unreachable();
|
unreachable()
|
||||||
}
|
}
|
||||||
|
|
||||||
set_current_directory :: proc(path: string) -> (err: Errno) {
|
set_current_directory :: proc(path: string) -> (err: Errno) {
|
||||||
cstr := strings.clone_to_cstring(path, context.temp_allocator);
|
cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
||||||
res := _unix_chdir(cstr);
|
res := _unix_chdir(cstr)
|
||||||
if res == -1 {
|
if res == -1 {
|
||||||
return Errno(get_last_error());
|
return Errno(get_last_error())
|
||||||
}
|
}
|
||||||
return ERROR_NONE;
|
return ERROR_NONE
|
||||||
}
|
}
|
||||||
|
|
||||||
exit :: proc "contextless" (code: int) -> ! {
|
exit :: proc "contextless" (code: int) -> ! {
|
||||||
_unix_exit(code);
|
_unix_exit(code)
|
||||||
}
|
}
|
||||||
|
|
||||||
current_thread_id :: proc "contextless" () -> int {
|
current_thread_id :: proc "contextless" () -> int {
|
||||||
tid: u64;
|
tid: u64
|
||||||
// NOTE(Oskar): available from OSX 10.6 and iOS 3.2.
|
// NOTE(Oskar): available from OSX 10.6 and iOS 3.2.
|
||||||
// For older versions there is `syscall(SYS_thread_selfid)`, but not really
|
// For older versions there is `syscall(SYS_thread_selfid)`, but not really
|
||||||
// the same thing apparently.
|
// the same thing apparently.
|
||||||
foreign pthread { pthread_threadid_np :: proc "c" (rawptr, ^u64) -> c.int ---; }
|
foreign pthread { pthread_threadid_np :: proc "c" (rawptr, ^u64) -> c.int --- }
|
||||||
pthread_threadid_np(nil, &tid);
|
pthread_threadid_np(nil, &tid)
|
||||||
return int(tid);
|
return int(tid)
|
||||||
}
|
}
|
||||||
|
|
||||||
dlopen :: proc(filename: string, flags: int) -> rawptr {
|
dlopen :: proc(filename: string, flags: int) -> rawptr {
|
||||||
cstr := strings.clone_to_cstring(filename, context.temp_allocator);
|
cstr := strings.clone_to_cstring(filename, context.temp_allocator)
|
||||||
handle := _unix_dlopen(cstr, flags);
|
handle := _unix_dlopen(cstr, flags)
|
||||||
return handle;
|
return handle
|
||||||
}
|
}
|
||||||
dlsym :: proc(handle: rawptr, symbol: string) -> rawptr {
|
dlsym :: proc(handle: rawptr, symbol: string) -> rawptr {
|
||||||
assert(handle != nil);
|
assert(handle != nil)
|
||||||
cstr := strings.clone_to_cstring(symbol, context.temp_allocator);
|
cstr := strings.clone_to_cstring(symbol, context.temp_allocator)
|
||||||
proc_handle := _unix_dlsym(handle, cstr);
|
proc_handle := _unix_dlsym(handle, cstr)
|
||||||
return proc_handle;
|
return proc_handle
|
||||||
}
|
}
|
||||||
dlclose :: proc(handle: rawptr) -> bool {
|
dlclose :: proc(handle: rawptr) -> bool {
|
||||||
assert(handle != nil);
|
assert(handle != nil)
|
||||||
return _unix_dlclose(handle) == 0;
|
return _unix_dlclose(handle) == 0
|
||||||
}
|
}
|
||||||
dlerror :: proc() -> string {
|
dlerror :: proc() -> string {
|
||||||
return string(_unix_dlerror());
|
return string(_unix_dlerror())
|
||||||
}
|
}
|
||||||
|
|
||||||
get_page_size :: proc() -> int {
|
get_page_size :: proc() -> int {
|
||||||
// NOTE(tetra): The page size never changes, so why do anything complicated
|
// NOTE(tetra): The page size never changes, so why do anything complicated
|
||||||
// if we don't have to.
|
// if we don't have to.
|
||||||
@static page_size := -1;
|
@static page_size := -1
|
||||||
if page_size != -1 {
|
if page_size != -1 {
|
||||||
return page_size;
|
return page_size
|
||||||
}
|
}
|
||||||
|
|
||||||
page_size = int(_unix_getpagesize());
|
page_size = int(_unix_getpagesize())
|
||||||
return page_size;
|
return page_size
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
_alloc_command_line_arguments :: proc() -> []string {
|
_alloc_command_line_arguments :: proc() -> []string {
|
||||||
res := make([]string, len(runtime.args__));
|
res := make([]string, len(runtime.args__))
|
||||||
for arg, i in runtime.args__ {
|
for arg, i in runtime.args__ {
|
||||||
res[i] = string(arg);
|
res[i] = string(arg)
|
||||||
}
|
}
|
||||||
return res;
|
return res
|
||||||
}
|
}
|
||||||
|
|||||||
+358
-358
@@ -9,176 +9,176 @@ import "core:c"
|
|||||||
import "core:strconv"
|
import "core:strconv"
|
||||||
import "core:intrinsics"
|
import "core:intrinsics"
|
||||||
|
|
||||||
Handle :: distinct i32;
|
Handle :: distinct i32
|
||||||
File_Time :: distinct u64;
|
File_Time :: distinct u64
|
||||||
Errno :: distinct i32;
|
Errno :: distinct i32
|
||||||
|
|
||||||
INVALID_HANDLE :: ~Handle(0);
|
INVALID_HANDLE :: ~Handle(0)
|
||||||
|
|
||||||
ERROR_NONE: Errno : 0;
|
ERROR_NONE: Errno : 0
|
||||||
EPERM: Errno : 1;
|
EPERM: Errno : 1
|
||||||
ENOENT: Errno : 2;
|
ENOENT: Errno : 2
|
||||||
ESRCH: Errno : 3;
|
ESRCH: Errno : 3
|
||||||
EINTR: Errno : 4;
|
EINTR: Errno : 4
|
||||||
EIO: Errno : 5;
|
EIO: Errno : 5
|
||||||
ENXIO: Errno : 6;
|
ENXIO: Errno : 6
|
||||||
EBADF: Errno : 9;
|
EBADF: Errno : 9
|
||||||
EAGAIN: Errno : 11;
|
EAGAIN: Errno : 11
|
||||||
ENOMEM: Errno : 12;
|
ENOMEM: Errno : 12
|
||||||
EACCES: Errno : 13;
|
EACCES: Errno : 13
|
||||||
EFAULT: Errno : 14;
|
EFAULT: Errno : 14
|
||||||
EEXIST: Errno : 17;
|
EEXIST: Errno : 17
|
||||||
ENODEV: Errno : 19;
|
ENODEV: Errno : 19
|
||||||
ENOTDIR: Errno : 20;
|
ENOTDIR: Errno : 20
|
||||||
EISDIR: Errno : 21;
|
EISDIR: Errno : 21
|
||||||
EINVAL: Errno : 22;
|
EINVAL: Errno : 22
|
||||||
ENFILE: Errno : 23;
|
ENFILE: Errno : 23
|
||||||
EMFILE: Errno : 24;
|
EMFILE: Errno : 24
|
||||||
ETXTBSY: Errno : 26;
|
ETXTBSY: Errno : 26
|
||||||
EFBIG: Errno : 27;
|
EFBIG: Errno : 27
|
||||||
ENOSPC: Errno : 28;
|
ENOSPC: Errno : 28
|
||||||
ESPIPE: Errno : 29;
|
ESPIPE: Errno : 29
|
||||||
EROFS: Errno : 30;
|
EROFS: Errno : 30
|
||||||
EPIPE: Errno : 32;
|
EPIPE: Errno : 32
|
||||||
|
|
||||||
ERANGE: Errno : 34; /* Result too large */
|
ERANGE: Errno : 34 /* Result too large */
|
||||||
EDEADLK: Errno : 35; /* Resource deadlock would occur */
|
EDEADLK: Errno : 35 /* Resource deadlock would occur */
|
||||||
ENAMETOOLONG: Errno : 36; /* File name too long */
|
ENAMETOOLONG: Errno : 36 /* File name too long */
|
||||||
ENOLCK: Errno : 37; /* No record locks available */
|
ENOLCK: Errno : 37 /* No record locks available */
|
||||||
|
|
||||||
ENOSYS: Errno : 38; /* Invalid system call number */
|
ENOSYS: Errno : 38 /* Invalid system call number */
|
||||||
|
|
||||||
ENOTEMPTY: Errno : 39; /* Directory not empty */
|
ENOTEMPTY: Errno : 39 /* Directory not empty */
|
||||||
ELOOP: Errno : 40; /* Too many symbolic links encountered */
|
ELOOP: Errno : 40 /* Too many symbolic links encountered */
|
||||||
EWOULDBLOCK: Errno : EAGAIN; /* Operation would block */
|
EWOULDBLOCK: Errno : EAGAIN /* Operation would block */
|
||||||
ENOMSG: Errno : 42; /* No message of desired type */
|
ENOMSG: Errno : 42 /* No message of desired type */
|
||||||
EIDRM: Errno : 43; /* Identifier removed */
|
EIDRM: Errno : 43 /* Identifier removed */
|
||||||
ECHRNG: Errno : 44; /* Channel number out of range */
|
ECHRNG: Errno : 44 /* Channel number out of range */
|
||||||
EL2NSYNC: Errno : 45; /* Level 2 not synchronized */
|
EL2NSYNC: Errno : 45 /* Level 2 not synchronized */
|
||||||
EL3HLT: Errno : 46; /* Level 3 halted */
|
EL3HLT: Errno : 46 /* Level 3 halted */
|
||||||
EL3RST: Errno : 47; /* Level 3 reset */
|
EL3RST: Errno : 47 /* Level 3 reset */
|
||||||
ELNRNG: Errno : 48; /* Link number out of range */
|
ELNRNG: Errno : 48 /* Link number out of range */
|
||||||
EUNATCH: Errno : 49; /* Protocol driver not attached */
|
EUNATCH: Errno : 49 /* Protocol driver not attached */
|
||||||
ENOCSI: Errno : 50; /* No CSI structure available */
|
ENOCSI: Errno : 50 /* No CSI structure available */
|
||||||
EL2HLT: Errno : 51; /* Level 2 halted */
|
EL2HLT: Errno : 51 /* Level 2 halted */
|
||||||
EBADE: Errno : 52; /* Invalid exchange */
|
EBADE: Errno : 52 /* Invalid exchange */
|
||||||
EBADR: Errno : 53; /* Invalid request descriptor */
|
EBADR: Errno : 53 /* Invalid request descriptor */
|
||||||
EXFULL: Errno : 54; /* Exchange full */
|
EXFULL: Errno : 54 /* Exchange full */
|
||||||
ENOANO: Errno : 55; /* No anode */
|
ENOANO: Errno : 55 /* No anode */
|
||||||
EBADRQC: Errno : 56; /* Invalid request code */
|
EBADRQC: Errno : 56 /* Invalid request code */
|
||||||
EBADSLT: Errno : 57; /* Invalid slot */
|
EBADSLT: Errno : 57 /* Invalid slot */
|
||||||
EDEADLOCK: Errno : EDEADLK;
|
EDEADLOCK: Errno : EDEADLK
|
||||||
EBFONT: Errno : 59; /* Bad font file format */
|
EBFONT: Errno : 59 /* Bad font file format */
|
||||||
ENOSTR: Errno : 60; /* Device not a stream */
|
ENOSTR: Errno : 60 /* Device not a stream */
|
||||||
ENODATA: Errno : 61; /* No data available */
|
ENODATA: Errno : 61 /* No data available */
|
||||||
ETIME: Errno : 62; /* Timer expired */
|
ETIME: Errno : 62 /* Timer expired */
|
||||||
ENOSR: Errno : 63; /* Out of streams resources */
|
ENOSR: Errno : 63 /* Out of streams resources */
|
||||||
ENONET: Errno : 64; /* Machine is not on the network */
|
ENONET: Errno : 64 /* Machine is not on the network */
|
||||||
ENOPKG: Errno : 65; /* Package not installed */
|
ENOPKG: Errno : 65 /* Package not installed */
|
||||||
EREMOTE: Errno : 66; /* Object is remote */
|
EREMOTE: Errno : 66 /* Object is remote */
|
||||||
ENOLINK: Errno : 67; /* Link has been severed */
|
ENOLINK: Errno : 67 /* Link has been severed */
|
||||||
EADV: Errno : 68; /* Advertise error */
|
EADV: Errno : 68 /* Advertise error */
|
||||||
ESRMNT: Errno : 69; /* Srmount error */
|
ESRMNT: Errno : 69 /* Srmount error */
|
||||||
ECOMM: Errno : 70; /* Communication error on send */
|
ECOMM: Errno : 70 /* Communication error on send */
|
||||||
EPROTO: Errno : 71; /* Protocol error */
|
EPROTO: Errno : 71 /* Protocol error */
|
||||||
EMULTIHOP: Errno : 72; /* Multihop attempted */
|
EMULTIHOP: Errno : 72 /* Multihop attempted */
|
||||||
EDOTDOT: Errno : 73; /* RFS specific error */
|
EDOTDOT: Errno : 73 /* RFS specific error */
|
||||||
EBADMSG: Errno : 74; /* Not a data message */
|
EBADMSG: Errno : 74 /* Not a data message */
|
||||||
EOVERFLOW: Errno : 75; /* Value too large for defined data type */
|
EOVERFLOW: Errno : 75 /* Value too large for defined data type */
|
||||||
ENOTUNIQ: Errno : 76; /* Name not unique on network */
|
ENOTUNIQ: Errno : 76 /* Name not unique on network */
|
||||||
EBADFD: Errno : 77; /* File descriptor in bad state */
|
EBADFD: Errno : 77 /* File descriptor in bad state */
|
||||||
EREMCHG: Errno : 78; /* Remote address changed */
|
EREMCHG: Errno : 78 /* Remote address changed */
|
||||||
ELIBACC: Errno : 79; /* Can not access a needed shared library */
|
ELIBACC: Errno : 79 /* Can not access a needed shared library */
|
||||||
ELIBBAD: Errno : 80; /* Accessing a corrupted shared library */
|
ELIBBAD: Errno : 80 /* Accessing a corrupted shared library */
|
||||||
ELIBSCN: Errno : 81; /* .lib section in a.out corrupted */
|
ELIBSCN: Errno : 81 /* .lib section in a.out corrupted */
|
||||||
ELIBMAX: Errno : 82; /* Attempting to link in too many shared libraries */
|
ELIBMAX: Errno : 82 /* Attempting to link in too many shared libraries */
|
||||||
ELIBEXEC: Errno : 83; /* Cannot exec a shared library directly */
|
ELIBEXEC: Errno : 83 /* Cannot exec a shared library directly */
|
||||||
EILSEQ: Errno : 84; /* Illegal byte sequence */
|
EILSEQ: Errno : 84 /* Illegal byte sequence */
|
||||||
ERESTART: Errno : 85; /* Interrupted system call should be restarted */
|
ERESTART: Errno : 85 /* Interrupted system call should be restarted */
|
||||||
ESTRPIPE: Errno : 86; /* Streams pipe error */
|
ESTRPIPE: Errno : 86 /* Streams pipe error */
|
||||||
EUSERS: Errno : 87; /* Too many users */
|
EUSERS: Errno : 87 /* Too many users */
|
||||||
ENOTSOCK: Errno : 88; /* Socket operation on non-socket */
|
ENOTSOCK: Errno : 88 /* Socket operation on non-socket */
|
||||||
EDESTADDRREQ: Errno : 89; /* Destination address required */
|
EDESTADDRREQ: Errno : 89 /* Destination address required */
|
||||||
EMSGSIZE: Errno : 90; /* Message too long */
|
EMSGSIZE: Errno : 90 /* Message too long */
|
||||||
EPROTOTYPE: Errno : 91; /* Protocol wrong type for socket */
|
EPROTOTYPE: Errno : 91 /* Protocol wrong type for socket */
|
||||||
ENOPROTOOPT: Errno : 92; /* Protocol not available */
|
ENOPROTOOPT: Errno : 92 /* Protocol not available */
|
||||||
EPROTONOSUPPORT:Errno : 93; /* Protocol not supported */
|
EPROTONOSUPPORT:Errno : 93 /* Protocol not supported */
|
||||||
ESOCKTNOSUPPORT:Errno : 94; /* Socket type not supported */
|
ESOCKTNOSUPPORT:Errno : 94 /* Socket type not supported */
|
||||||
EOPNOTSUPP: Errno : 95; /* Operation not supported on transport endpoint */
|
EOPNOTSUPP: Errno : 95 /* Operation not supported on transport endpoint */
|
||||||
EPFNOSUPPORT: Errno : 96; /* Protocol family not supported */
|
EPFNOSUPPORT: Errno : 96 /* Protocol family not supported */
|
||||||
EAFNOSUPPORT: Errno : 97; /* Address family not supported by protocol */
|
EAFNOSUPPORT: Errno : 97 /* Address family not supported by protocol */
|
||||||
EADDRINUSE: Errno : 98; /* Address already in use */
|
EADDRINUSE: Errno : 98 /* Address already in use */
|
||||||
EADDRNOTAVAIL: Errno : 99; /* Cannot assign requested address */
|
EADDRNOTAVAIL: Errno : 99 /* Cannot assign requested address */
|
||||||
ENETDOWN: Errno : 100; /* Network is down */
|
ENETDOWN: Errno : 100 /* Network is down */
|
||||||
ENETUNREACH: Errno : 101; /* Network is unreachable */
|
ENETUNREACH: Errno : 101 /* Network is unreachable */
|
||||||
ENETRESET: Errno : 102; /* Network dropped connection because of reset */
|
ENETRESET: Errno : 102 /* Network dropped connection because of reset */
|
||||||
ECONNABORTED: Errno : 103; /* Software caused connection abort */
|
ECONNABORTED: Errno : 103 /* Software caused connection abort */
|
||||||
ECONNRESET: Errno : 104; /* Connection reset by peer */
|
ECONNRESET: Errno : 104 /* Connection reset by peer */
|
||||||
ENOBUFS: Errno : 105; /* No buffer space available */
|
ENOBUFS: Errno : 105 /* No buffer space available */
|
||||||
EISCONN: Errno : 106; /* Transport endpoint is already connected */
|
EISCONN: Errno : 106 /* Transport endpoint is already connected */
|
||||||
ENOTCONN: Errno : 107; /* Transport endpoint is not connected */
|
ENOTCONN: Errno : 107 /* Transport endpoint is not connected */
|
||||||
ESHUTDOWN: Errno : 108; /* Cannot send after transport endpoint shutdown */
|
ESHUTDOWN: Errno : 108 /* Cannot send after transport endpoint shutdown */
|
||||||
ETOOMANYREFS: Errno : 109; /* Too many references: cannot splice */
|
ETOOMANYREFS: Errno : 109 /* Too many references: cannot splice */
|
||||||
ETIMEDOUT: Errno : 110; /* Connection timed out */
|
ETIMEDOUT: Errno : 110 /* Connection timed out */
|
||||||
ECONNREFUSED: Errno : 111; /* Connection refused */
|
ECONNREFUSED: Errno : 111 /* Connection refused */
|
||||||
EHOSTDOWN: Errno : 112; /* Host is down */
|
EHOSTDOWN: Errno : 112 /* Host is down */
|
||||||
EHOSTUNREACH: Errno : 113; /* No route to host */
|
EHOSTUNREACH: Errno : 113 /* No route to host */
|
||||||
EALREADY: Errno : 114; /* Operation already in progress */
|
EALREADY: Errno : 114 /* Operation already in progress */
|
||||||
EINPROGRESS: Errno : 115; /* Operation now in progress */
|
EINPROGRESS: Errno : 115 /* Operation now in progress */
|
||||||
ESTALE: Errno : 116; /* Stale file handle */
|
ESTALE: Errno : 116 /* Stale file handle */
|
||||||
EUCLEAN: Errno : 117; /* Structure needs cleaning */
|
EUCLEAN: Errno : 117 /* Structure needs cleaning */
|
||||||
ENOTNAM: Errno : 118; /* Not a XENIX named type file */
|
ENOTNAM: Errno : 118 /* Not a XENIX named type file */
|
||||||
ENAVAIL: Errno : 119; /* No XENIX semaphores available */
|
ENAVAIL: Errno : 119 /* No XENIX semaphores available */
|
||||||
EISNAM: Errno : 120; /* Is a named type file */
|
EISNAM: Errno : 120 /* Is a named type file */
|
||||||
EREMOTEIO: Errno : 121; /* Remote I/O error */
|
EREMOTEIO: Errno : 121 /* Remote I/O error */
|
||||||
EDQUOT: Errno : 122; /* Quota exceeded */
|
EDQUOT: Errno : 122 /* Quota exceeded */
|
||||||
|
|
||||||
ENOMEDIUM: Errno : 123; /* No medium found */
|
ENOMEDIUM: Errno : 123 /* No medium found */
|
||||||
EMEDIUMTYPE: Errno : 124; /* Wrong medium type */
|
EMEDIUMTYPE: Errno : 124 /* Wrong medium type */
|
||||||
ECANCELED: Errno : 125; /* Operation Canceled */
|
ECANCELED: Errno : 125 /* Operation Canceled */
|
||||||
ENOKEY: Errno : 126; /* Required key not available */
|
ENOKEY: Errno : 126 /* Required key not available */
|
||||||
EKEYEXPIRED: Errno : 127; /* Key has expired */
|
EKEYEXPIRED: Errno : 127 /* Key has expired */
|
||||||
EKEYREVOKED: Errno : 128; /* Key has been revoked */
|
EKEYREVOKED: Errno : 128 /* Key has been revoked */
|
||||||
EKEYREJECTED: Errno : 129; /* Key was rejected by service */
|
EKEYREJECTED: Errno : 129 /* Key was rejected by service */
|
||||||
|
|
||||||
/* for robust mutexes */
|
/* for robust mutexes */
|
||||||
EOWNERDEAD: Errno : 130; /* Owner died */
|
EOWNERDEAD: Errno : 130 /* Owner died */
|
||||||
ENOTRECOVERABLE: Errno : 131; /* State not recoverable */
|
ENOTRECOVERABLE: Errno : 131 /* State not recoverable */
|
||||||
|
|
||||||
ERFKILL: Errno : 132; /* Operation not possible due to RF-kill */
|
ERFKILL: Errno : 132 /* Operation not possible due to RF-kill */
|
||||||
|
|
||||||
EHWPOISON: Errno : 133; /* Memory page has hardware error */
|
EHWPOISON: Errno : 133 /* Memory page has hardware error */
|
||||||
|
|
||||||
O_RDONLY :: 0x00000;
|
O_RDONLY :: 0x00000
|
||||||
O_WRONLY :: 0x00001;
|
O_WRONLY :: 0x00001
|
||||||
O_RDWR :: 0x00002;
|
O_RDWR :: 0x00002
|
||||||
O_CREATE :: 0x00040;
|
O_CREATE :: 0x00040
|
||||||
O_EXCL :: 0x00080;
|
O_EXCL :: 0x00080
|
||||||
O_NOCTTY :: 0x00100;
|
O_NOCTTY :: 0x00100
|
||||||
O_TRUNC :: 0x00200;
|
O_TRUNC :: 0x00200
|
||||||
O_NONBLOCK :: 0x00800;
|
O_NONBLOCK :: 0x00800
|
||||||
O_APPEND :: 0x00400;
|
O_APPEND :: 0x00400
|
||||||
O_SYNC :: 0x01000;
|
O_SYNC :: 0x01000
|
||||||
O_ASYNC :: 0x02000;
|
O_ASYNC :: 0x02000
|
||||||
O_CLOEXEC :: 0x80000;
|
O_CLOEXEC :: 0x80000
|
||||||
|
|
||||||
|
|
||||||
SEEK_SET :: 0;
|
SEEK_SET :: 0
|
||||||
SEEK_CUR :: 1;
|
SEEK_CUR :: 1
|
||||||
SEEK_END :: 2;
|
SEEK_END :: 2
|
||||||
SEEK_DATA :: 3;
|
SEEK_DATA :: 3
|
||||||
SEEK_HOLE :: 4;
|
SEEK_HOLE :: 4
|
||||||
SEEK_MAX :: SEEK_HOLE;
|
SEEK_MAX :: SEEK_HOLE
|
||||||
|
|
||||||
// NOTE(zangent): These are OS specific!
|
// NOTE(zangent): These are OS specific!
|
||||||
// Do not mix these up!
|
// Do not mix these up!
|
||||||
RTLD_LAZY :: 0x001;
|
RTLD_LAZY :: 0x001
|
||||||
RTLD_NOW :: 0x002;
|
RTLD_NOW :: 0x002
|
||||||
RTLD_BINDING_MASK :: 0x3;
|
RTLD_BINDING_MASK :: 0x3
|
||||||
RTLD_GLOBAL :: 0x100;
|
RTLD_GLOBAL :: 0x100
|
||||||
|
|
||||||
// "Argv" arguments converted to Odin strings
|
// "Argv" arguments converted to Odin strings
|
||||||
args := _alloc_command_line_arguments();
|
args := _alloc_command_line_arguments()
|
||||||
|
|
||||||
Unix_File_Time :: struct {
|
Unix_File_Time :: struct {
|
||||||
seconds: i64,
|
seconds: i64,
|
||||||
@@ -205,7 +205,7 @@ OS_Stat :: struct {
|
|||||||
_reserve1,
|
_reserve1,
|
||||||
_reserve2,
|
_reserve2,
|
||||||
_reserve3: i64,
|
_reserve3: i64,
|
||||||
};
|
}
|
||||||
|
|
||||||
// NOTE(laleksic, 2021-01-21): Comment and rename these to match OS_Stat above
|
// NOTE(laleksic, 2021-01-21): Comment and rename these to match OS_Stat above
|
||||||
Dirent :: struct {
|
Dirent :: struct {
|
||||||
@@ -214,427 +214,427 @@ Dirent :: struct {
|
|||||||
reclen: u16,
|
reclen: u16,
|
||||||
type: u8,
|
type: u8,
|
||||||
name: [256]byte,
|
name: [256]byte,
|
||||||
};
|
}
|
||||||
|
|
||||||
Dir :: distinct rawptr; // DIR*
|
Dir :: distinct rawptr // DIR*
|
||||||
|
|
||||||
// File type
|
// File type
|
||||||
S_IFMT :: 0o170000; // Type of file mask
|
S_IFMT :: 0o170000 // Type of file mask
|
||||||
S_IFIFO :: 0o010000; // Named pipe (fifo)
|
S_IFIFO :: 0o010000 // Named pipe (fifo)
|
||||||
S_IFCHR :: 0o020000; // Character special
|
S_IFCHR :: 0o020000 // Character special
|
||||||
S_IFDIR :: 0o040000; // Directory
|
S_IFDIR :: 0o040000 // Directory
|
||||||
S_IFBLK :: 0o060000; // Block special
|
S_IFBLK :: 0o060000 // Block special
|
||||||
S_IFREG :: 0o100000; // Regular
|
S_IFREG :: 0o100000 // Regular
|
||||||
S_IFLNK :: 0o120000; // Symbolic link
|
S_IFLNK :: 0o120000 // Symbolic link
|
||||||
S_IFSOCK :: 0o140000; // Socket
|
S_IFSOCK :: 0o140000 // Socket
|
||||||
|
|
||||||
// File mode
|
// File mode
|
||||||
// Read, write, execute/search by owner
|
// Read, write, execute/search by owner
|
||||||
S_IRWXU :: 0o0700; // RWX mask for owner
|
S_IRWXU :: 0o0700 // RWX mask for owner
|
||||||
S_IRUSR :: 0o0400; // R for owner
|
S_IRUSR :: 0o0400 // R for owner
|
||||||
S_IWUSR :: 0o0200; // W for owner
|
S_IWUSR :: 0o0200 // W for owner
|
||||||
S_IXUSR :: 0o0100; // X for owner
|
S_IXUSR :: 0o0100 // X for owner
|
||||||
|
|
||||||
// Read, write, execute/search by group
|
// Read, write, execute/search by group
|
||||||
S_IRWXG :: 0o0070; // RWX mask for group
|
S_IRWXG :: 0o0070 // RWX mask for group
|
||||||
S_IRGRP :: 0o0040; // R for group
|
S_IRGRP :: 0o0040 // R for group
|
||||||
S_IWGRP :: 0o0020; // W for group
|
S_IWGRP :: 0o0020 // W for group
|
||||||
S_IXGRP :: 0o0010; // X for group
|
S_IXGRP :: 0o0010 // X for group
|
||||||
|
|
||||||
// Read, write, execute/search by others
|
// Read, write, execute/search by others
|
||||||
S_IRWXO :: 0o0007; // RWX mask for other
|
S_IRWXO :: 0o0007 // RWX mask for other
|
||||||
S_IROTH :: 0o0004; // R for other
|
S_IROTH :: 0o0004 // R for other
|
||||||
S_IWOTH :: 0o0002; // W for other
|
S_IWOTH :: 0o0002 // W for other
|
||||||
S_IXOTH :: 0o0001; // X for other
|
S_IXOTH :: 0o0001 // X for other
|
||||||
|
|
||||||
S_ISUID :: 0o4000; // Set user id on execution
|
S_ISUID :: 0o4000 // Set user id on execution
|
||||||
S_ISGID :: 0o2000; // Set group id on execution
|
S_ISGID :: 0o2000 // Set group id on execution
|
||||||
S_ISVTX :: 0o1000; // Directory restrcted delete
|
S_ISVTX :: 0o1000 // Directory restrcted delete
|
||||||
|
|
||||||
|
|
||||||
S_ISLNK :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFLNK; }
|
S_ISLNK :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFLNK }
|
||||||
S_ISREG :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFREG; }
|
S_ISREG :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFREG }
|
||||||
S_ISDIR :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFDIR; }
|
S_ISDIR :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFDIR }
|
||||||
S_ISCHR :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFCHR; }
|
S_ISCHR :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFCHR }
|
||||||
S_ISBLK :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFBLK; }
|
S_ISBLK :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFBLK }
|
||||||
S_ISFIFO :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFIFO; }
|
S_ISFIFO :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFIFO }
|
||||||
S_ISSOCK :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFSOCK; }
|
S_ISSOCK :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFSOCK }
|
||||||
|
|
||||||
F_OK :: 0; // Test for file existance
|
F_OK :: 0 // Test for file existance
|
||||||
X_OK :: 1; // Test for execute permission
|
X_OK :: 1 // Test for execute permission
|
||||||
W_OK :: 2; // Test for write permission
|
W_OK :: 2 // Test for write permission
|
||||||
R_OK :: 4; // Test for read permission
|
R_OK :: 4 // Test for read permission
|
||||||
|
|
||||||
SYS_GETTID :: 186;
|
SYS_GETTID :: 186
|
||||||
|
|
||||||
foreign libc {
|
foreign libc {
|
||||||
@(link_name="__errno_location") __errno_location :: proc() -> ^int ---;
|
@(link_name="__errno_location") __errno_location :: proc() -> ^int ---
|
||||||
|
|
||||||
@(link_name="open") _unix_open :: proc(path: cstring, flags: c.int, mode: c.int) -> Handle ---;
|
@(link_name="open") _unix_open :: proc(path: cstring, flags: c.int, mode: c.int) -> Handle ---
|
||||||
@(link_name="close") _unix_close :: proc(fd: Handle) -> c.int ---;
|
@(link_name="close") _unix_close :: proc(fd: Handle) -> c.int ---
|
||||||
@(link_name="read") _unix_read :: proc(fd: Handle, buf: rawptr, size: c.size_t) -> c.ssize_t ---;
|
@(link_name="read") _unix_read :: proc(fd: Handle, buf: rawptr, size: c.size_t) -> c.ssize_t ---
|
||||||
@(link_name="write") _unix_write :: proc(fd: Handle, buf: rawptr, size: c.size_t) -> c.ssize_t ---;
|
@(link_name="write") _unix_write :: proc(fd: Handle, buf: rawptr, size: c.size_t) -> c.ssize_t ---
|
||||||
@(link_name="lseek64") _unix_seek :: proc(fd: Handle, offset: i64, whence: c.int) -> i64 ---;
|
@(link_name="lseek64") _unix_seek :: proc(fd: Handle, offset: i64, whence: c.int) -> i64 ---
|
||||||
@(link_name="gettid") _unix_gettid :: proc() -> u64 ---;
|
@(link_name="gettid") _unix_gettid :: proc() -> u64 ---
|
||||||
@(link_name="getpagesize") _unix_getpagesize :: proc() -> c.int ---;
|
@(link_name="getpagesize") _unix_getpagesize :: proc() -> c.int ---
|
||||||
@(link_name="stat64") _unix_stat :: proc(path: cstring, stat: ^OS_Stat) -> c.int ---;
|
@(link_name="stat64") _unix_stat :: proc(path: cstring, stat: ^OS_Stat) -> c.int ---
|
||||||
@(link_name="lstat") _unix_lstat :: proc(path: cstring, stat: ^OS_Stat) -> c.int ---;
|
@(link_name="lstat") _unix_lstat :: proc(path: cstring, stat: ^OS_Stat) -> c.int ---
|
||||||
@(link_name="fstat") _unix_fstat :: proc(fd: Handle, stat: ^OS_Stat) -> c.int ---;
|
@(link_name="fstat") _unix_fstat :: proc(fd: Handle, stat: ^OS_Stat) -> c.int ---
|
||||||
@(link_name="fdopendir") _unix_fdopendir :: proc(fd: Handle) -> Dir ---;
|
@(link_name="fdopendir") _unix_fdopendir :: proc(fd: Handle) -> Dir ---
|
||||||
@(link_name="closedir") _unix_closedir :: proc(dirp: Dir) -> c.int ---;
|
@(link_name="closedir") _unix_closedir :: proc(dirp: Dir) -> c.int ---
|
||||||
@(link_name="rewinddir") _unix_rewinddir :: proc(dirp: Dir) ---;
|
@(link_name="rewinddir") _unix_rewinddir :: proc(dirp: Dir) ---
|
||||||
@(link_name="readdir_r") _unix_readdir_r :: proc(dirp: Dir, entry: ^Dirent, result: ^^Dirent) -> c.int ---;
|
@(link_name="readdir_r") _unix_readdir_r :: proc(dirp: Dir, entry: ^Dirent, result: ^^Dirent) -> c.int ---
|
||||||
@(link_name="readlink") _unix_readlink :: proc(path: cstring, buf: ^byte, bufsiz: c.size_t) -> c.ssize_t ---;
|
@(link_name="readlink") _unix_readlink :: proc(path: cstring, buf: ^byte, bufsiz: c.size_t) -> c.ssize_t ---
|
||||||
@(link_name="access") _unix_access :: proc(path: cstring, mask: c.int) -> c.int ---;
|
@(link_name="access") _unix_access :: proc(path: cstring, mask: c.int) -> c.int ---
|
||||||
|
|
||||||
@(link_name="malloc") _unix_malloc :: proc(size: c.size_t) -> rawptr ---;
|
@(link_name="malloc") _unix_malloc :: proc(size: c.size_t) -> rawptr ---
|
||||||
@(link_name="calloc") _unix_calloc :: proc(num, size: c.size_t) -> rawptr ---;
|
@(link_name="calloc") _unix_calloc :: proc(num, size: c.size_t) -> rawptr ---
|
||||||
@(link_name="free") _unix_free :: proc(ptr: rawptr) ---;
|
@(link_name="free") _unix_free :: proc(ptr: rawptr) ---
|
||||||
@(link_name="realloc") _unix_realloc :: proc(ptr: rawptr, size: c.size_t) -> rawptr ---;
|
@(link_name="realloc") _unix_realloc :: proc(ptr: rawptr, size: c.size_t) -> rawptr ---
|
||||||
@(link_name="getenv") _unix_getenv :: proc(cstring) -> cstring ---;
|
@(link_name="getenv") _unix_getenv :: proc(cstring) -> cstring ---
|
||||||
@(link_name="getcwd") _unix_getcwd :: proc(buf: cstring, len: c.size_t) -> cstring ---;
|
@(link_name="getcwd") _unix_getcwd :: proc(buf: cstring, len: c.size_t) -> cstring ---
|
||||||
@(link_name="chdir") _unix_chdir :: proc(buf: cstring) -> c.int ---;
|
@(link_name="chdir") _unix_chdir :: proc(buf: cstring) -> c.int ---
|
||||||
@(link_name="realpath") _unix_realpath :: proc(path: cstring, resolved_path: rawptr) -> rawptr ---;
|
@(link_name="realpath") _unix_realpath :: proc(path: cstring, resolved_path: rawptr) -> rawptr ---
|
||||||
|
|
||||||
@(link_name="exit") _unix_exit :: proc(status: c.int) -> ! ---;
|
@(link_name="exit") _unix_exit :: proc(status: c.int) -> ! ---
|
||||||
}
|
}
|
||||||
foreign dl {
|
foreign dl {
|
||||||
@(link_name="dlopen") _unix_dlopen :: proc(filename: cstring, flags: c.int) -> rawptr ---;
|
@(link_name="dlopen") _unix_dlopen :: proc(filename: cstring, flags: c.int) -> rawptr ---
|
||||||
@(link_name="dlsym") _unix_dlsym :: proc(handle: rawptr, symbol: cstring) -> rawptr ---;
|
@(link_name="dlsym") _unix_dlsym :: proc(handle: rawptr, symbol: cstring) -> rawptr ---
|
||||||
@(link_name="dlclose") _unix_dlclose :: proc(handle: rawptr) -> c.int ---;
|
@(link_name="dlclose") _unix_dlclose :: proc(handle: rawptr) -> c.int ---
|
||||||
@(link_name="dlerror") _unix_dlerror :: proc() -> cstring ---;
|
@(link_name="dlerror") _unix_dlerror :: proc() -> cstring ---
|
||||||
}
|
}
|
||||||
|
|
||||||
is_path_separator :: proc(r: rune) -> bool {
|
is_path_separator :: proc(r: rune) -> bool {
|
||||||
return r == '/';
|
return r == '/'
|
||||||
}
|
}
|
||||||
|
|
||||||
get_last_error :: proc() -> int {
|
get_last_error :: proc() -> int {
|
||||||
return __errno_location()^;
|
return __errno_location()^
|
||||||
}
|
}
|
||||||
|
|
||||||
open :: proc(path: string, flags: int = O_RDONLY, mode: int = 0) -> (Handle, Errno) {
|
open :: proc(path: string, flags: int = O_RDONLY, mode: int = 0) -> (Handle, Errno) {
|
||||||
cstr := strings.clone_to_cstring(path);
|
cstr := strings.clone_to_cstring(path)
|
||||||
handle := _unix_open(cstr, c.int(flags), c.int(mode));
|
handle := _unix_open(cstr, c.int(flags), c.int(mode))
|
||||||
delete(cstr);
|
delete(cstr)
|
||||||
if handle == -1 {
|
if handle == -1 {
|
||||||
return INVALID_HANDLE, Errno(get_last_error());
|
return INVALID_HANDLE, Errno(get_last_error())
|
||||||
}
|
}
|
||||||
return handle, ERROR_NONE;
|
return handle, ERROR_NONE
|
||||||
}
|
}
|
||||||
|
|
||||||
close :: proc(fd: Handle) -> Errno {
|
close :: proc(fd: Handle) -> Errno {
|
||||||
result := _unix_close(fd);
|
result := _unix_close(fd)
|
||||||
if result == -1 {
|
if result == -1 {
|
||||||
return Errno(get_last_error());
|
return Errno(get_last_error())
|
||||||
}
|
}
|
||||||
return ERROR_NONE;
|
return ERROR_NONE
|
||||||
}
|
}
|
||||||
|
|
||||||
read :: proc(fd: Handle, data: []byte) -> (int, Errno) {
|
read :: proc(fd: Handle, data: []byte) -> (int, Errno) {
|
||||||
bytes_read := _unix_read(fd, &data[0], c.size_t(len(data)));
|
bytes_read := _unix_read(fd, &data[0], c.size_t(len(data)))
|
||||||
if bytes_read == -1 {
|
if bytes_read == -1 {
|
||||||
return -1, Errno(get_last_error());
|
return -1, Errno(get_last_error())
|
||||||
}
|
}
|
||||||
return int(bytes_read), ERROR_NONE;
|
return int(bytes_read), ERROR_NONE
|
||||||
}
|
}
|
||||||
|
|
||||||
write :: proc(fd: Handle, data: []byte) -> (int, Errno) {
|
write :: proc(fd: Handle, data: []byte) -> (int, Errno) {
|
||||||
if len(data) == 0 {
|
if len(data) == 0 {
|
||||||
return 0, ERROR_NONE;
|
return 0, ERROR_NONE
|
||||||
}
|
}
|
||||||
bytes_written := _unix_write(fd, &data[0], c.size_t(len(data)));
|
bytes_written := _unix_write(fd, &data[0], c.size_t(len(data)))
|
||||||
if bytes_written == -1 {
|
if bytes_written == -1 {
|
||||||
return -1, Errno(get_last_error());
|
return -1, Errno(get_last_error())
|
||||||
}
|
}
|
||||||
return int(bytes_written), ERROR_NONE;
|
return int(bytes_written), ERROR_NONE
|
||||||
}
|
}
|
||||||
|
|
||||||
seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Errno) {
|
seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Errno) {
|
||||||
res := _unix_seek(fd, offset, c.int(whence));
|
res := _unix_seek(fd, offset, c.int(whence))
|
||||||
if res == -1 {
|
if res == -1 {
|
||||||
return -1, Errno(get_last_error());
|
return -1, Errno(get_last_error())
|
||||||
}
|
}
|
||||||
return res, ERROR_NONE;
|
return res, ERROR_NONE
|
||||||
}
|
}
|
||||||
|
|
||||||
file_size :: proc(fd: Handle) -> (i64, Errno) {
|
file_size :: proc(fd: Handle) -> (i64, Errno) {
|
||||||
s, err := _fstat(fd);
|
s, err := _fstat(fd)
|
||||||
if err != ERROR_NONE {
|
if err != ERROR_NONE {
|
||||||
return 0, err;
|
return 0, err
|
||||||
}
|
}
|
||||||
return max(s.size, 0), ERROR_NONE;
|
return max(s.size, 0), ERROR_NONE
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// NOTE(bill): Uses startup to initialize it
|
// NOTE(bill): Uses startup to initialize it
|
||||||
|
|
||||||
stdin: Handle = 0;
|
stdin: Handle = 0
|
||||||
stdout: Handle = 1;
|
stdout: Handle = 1
|
||||||
stderr: Handle = 2;
|
stderr: Handle = 2
|
||||||
|
|
||||||
/* TODO(zangent): Implement these!
|
/* TODO(zangent): Implement these!
|
||||||
last_write_time :: proc(fd: Handle) -> File_Time {}
|
last_write_time :: proc(fd: Handle) -> File_Time {}
|
||||||
last_write_time_by_name :: proc(name: string) -> File_Time {}
|
last_write_time_by_name :: proc(name: string) -> File_Time {}
|
||||||
*/
|
*/
|
||||||
last_write_time :: proc(fd: Handle) -> (File_Time, Errno) {
|
last_write_time :: proc(fd: Handle) -> (File_Time, Errno) {
|
||||||
s, err := _fstat(fd);
|
s, err := _fstat(fd)
|
||||||
if err != ERROR_NONE {
|
if err != ERROR_NONE {
|
||||||
return 0, err;
|
return 0, err
|
||||||
}
|
}
|
||||||
modified := s.modified.seconds * 1_000_000_000 + s.modified.nanoseconds;
|
modified := s.modified.seconds * 1_000_000_000 + s.modified.nanoseconds
|
||||||
return File_Time(modified), ERROR_NONE;
|
return File_Time(modified), ERROR_NONE
|
||||||
}
|
}
|
||||||
|
|
||||||
last_write_time_by_name :: proc(name: string) -> (File_Time, Errno) {
|
last_write_time_by_name :: proc(name: string) -> (File_Time, Errno) {
|
||||||
s, err := _stat(name);
|
s, err := _stat(name)
|
||||||
if err != ERROR_NONE {
|
if err != ERROR_NONE {
|
||||||
return 0, err;
|
return 0, err
|
||||||
}
|
}
|
||||||
modified := s.modified.seconds * 1_000_000_000 + s.modified.nanoseconds;
|
modified := s.modified.seconds * 1_000_000_000 + s.modified.nanoseconds
|
||||||
return File_Time(modified), ERROR_NONE;
|
return File_Time(modified), ERROR_NONE
|
||||||
}
|
}
|
||||||
|
|
||||||
@private
|
@private
|
||||||
_stat :: proc(path: string) -> (OS_Stat, Errno) {
|
_stat :: proc(path: string) -> (OS_Stat, Errno) {
|
||||||
cstr := strings.clone_to_cstring(path);
|
cstr := strings.clone_to_cstring(path)
|
||||||
defer delete(cstr);
|
defer delete(cstr)
|
||||||
|
|
||||||
s: OS_Stat;
|
s: OS_Stat
|
||||||
result := _unix_stat(cstr, &s);
|
result := _unix_stat(cstr, &s)
|
||||||
if result == -1 {
|
if result == -1 {
|
||||||
return s, Errno(get_last_error());
|
return s, Errno(get_last_error())
|
||||||
}
|
}
|
||||||
return s, ERROR_NONE;
|
return s, ERROR_NONE
|
||||||
}
|
}
|
||||||
|
|
||||||
@private
|
@private
|
||||||
_lstat :: proc(path: string) -> (OS_Stat, Errno) {
|
_lstat :: proc(path: string) -> (OS_Stat, Errno) {
|
||||||
cstr := strings.clone_to_cstring(path);
|
cstr := strings.clone_to_cstring(path)
|
||||||
defer delete(cstr);
|
defer delete(cstr)
|
||||||
|
|
||||||
s: OS_Stat;
|
s: OS_Stat
|
||||||
result := _unix_lstat(cstr, &s);
|
result := _unix_lstat(cstr, &s)
|
||||||
if result == -1 {
|
if result == -1 {
|
||||||
return s, Errno(get_last_error());
|
return s, Errno(get_last_error())
|
||||||
}
|
}
|
||||||
return s, ERROR_NONE;
|
return s, ERROR_NONE
|
||||||
}
|
}
|
||||||
|
|
||||||
@private
|
@private
|
||||||
_fstat :: proc(fd: Handle) -> (OS_Stat, Errno) {
|
_fstat :: proc(fd: Handle) -> (OS_Stat, Errno) {
|
||||||
s: OS_Stat;
|
s: OS_Stat
|
||||||
result := _unix_fstat(fd, &s);
|
result := _unix_fstat(fd, &s)
|
||||||
if result == -1 {
|
if result == -1 {
|
||||||
return s, Errno(get_last_error());
|
return s, Errno(get_last_error())
|
||||||
}
|
}
|
||||||
return s, ERROR_NONE;
|
return s, ERROR_NONE
|
||||||
}
|
}
|
||||||
|
|
||||||
@private
|
@private
|
||||||
_fdopendir :: proc(fd: Handle) -> (Dir, Errno) {
|
_fdopendir :: proc(fd: Handle) -> (Dir, Errno) {
|
||||||
dirp := _unix_fdopendir(fd);
|
dirp := _unix_fdopendir(fd)
|
||||||
if dirp == cast(Dir)nil {
|
if dirp == cast(Dir)nil {
|
||||||
return nil, Errno(get_last_error());
|
return nil, Errno(get_last_error())
|
||||||
}
|
}
|
||||||
return dirp, ERROR_NONE;
|
return dirp, ERROR_NONE
|
||||||
}
|
}
|
||||||
|
|
||||||
@private
|
@private
|
||||||
_closedir :: proc(dirp: Dir) -> Errno {
|
_closedir :: proc(dirp: Dir) -> Errno {
|
||||||
rc := _unix_closedir(dirp);
|
rc := _unix_closedir(dirp)
|
||||||
if rc != 0 {
|
if rc != 0 {
|
||||||
return Errno(get_last_error());
|
return Errno(get_last_error())
|
||||||
}
|
}
|
||||||
return ERROR_NONE;
|
return ERROR_NONE
|
||||||
}
|
}
|
||||||
|
|
||||||
@private
|
@private
|
||||||
_rewinddir :: proc(dirp: Dir) {
|
_rewinddir :: proc(dirp: Dir) {
|
||||||
_unix_rewinddir(dirp);
|
_unix_rewinddir(dirp)
|
||||||
}
|
}
|
||||||
|
|
||||||
@private
|
@private
|
||||||
_readdir :: proc(dirp: Dir) -> (entry: Dirent, err: Errno, end_of_stream: bool) {
|
_readdir :: proc(dirp: Dir) -> (entry: Dirent, err: Errno, end_of_stream: bool) {
|
||||||
result: ^Dirent;
|
result: ^Dirent
|
||||||
rc := _unix_readdir_r(dirp, &entry, &result);
|
rc := _unix_readdir_r(dirp, &entry, &result)
|
||||||
|
|
||||||
if rc != 0 {
|
if rc != 0 {
|
||||||
err = Errno(get_last_error());
|
err = Errno(get_last_error())
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
err = ERROR_NONE;
|
err = ERROR_NONE
|
||||||
|
|
||||||
if result == nil {
|
if result == nil {
|
||||||
end_of_stream = true;
|
end_of_stream = true
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
end_of_stream = false;
|
end_of_stream = false
|
||||||
|
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@private
|
@private
|
||||||
_readlink :: proc(path: string) -> (string, Errno) {
|
_readlink :: proc(path: string) -> (string, Errno) {
|
||||||
path_cstr := strings.clone_to_cstring(path);
|
path_cstr := strings.clone_to_cstring(path)
|
||||||
defer delete(path_cstr);
|
defer delete(path_cstr)
|
||||||
|
|
||||||
bufsz : uint = 256;
|
bufsz : uint = 256
|
||||||
buf := make([]byte, bufsz);
|
buf := make([]byte, bufsz)
|
||||||
for {
|
for {
|
||||||
rc := _unix_readlink(path_cstr, &(buf[0]), bufsz);
|
rc := _unix_readlink(path_cstr, &(buf[0]), bufsz)
|
||||||
if rc == -1 {
|
if rc == -1 {
|
||||||
delete(buf);
|
delete(buf)
|
||||||
return "", Errno(get_last_error());
|
return "", Errno(get_last_error())
|
||||||
} else if rc == int(bufsz) {
|
} else if rc == int(bufsz) {
|
||||||
// NOTE(laleksic, 2021-01-21): Any cleaner way to resize the slice?
|
// NOTE(laleksic, 2021-01-21): Any cleaner way to resize the slice?
|
||||||
bufsz *= 2;
|
bufsz *= 2
|
||||||
delete(buf);
|
delete(buf)
|
||||||
buf = make([]byte, bufsz);
|
buf = make([]byte, bufsz)
|
||||||
} else {
|
} else {
|
||||||
return strings.string_from_ptr(&buf[0], rc), ERROR_NONE;
|
return strings.string_from_ptr(&buf[0], rc), ERROR_NONE
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
absolute_path_from_handle :: proc(fd: Handle) -> (string, Errno) {
|
absolute_path_from_handle :: proc(fd: Handle) -> (string, Errno) {
|
||||||
buf : [256]byte;
|
buf : [256]byte
|
||||||
fd_str := strconv.itoa( buf[:], cast(int)fd );
|
fd_str := strconv.itoa( buf[:], cast(int)fd )
|
||||||
|
|
||||||
procfs_path := strings.concatenate( []string{ "/proc/self/fd/", fd_str } );
|
procfs_path := strings.concatenate( []string{ "/proc/self/fd/", fd_str } )
|
||||||
defer delete(procfs_path);
|
defer delete(procfs_path)
|
||||||
|
|
||||||
return _readlink(procfs_path);
|
return _readlink(procfs_path)
|
||||||
}
|
}
|
||||||
|
|
||||||
absolute_path_from_relative :: proc(rel: string) -> (path: string, err: Errno) {
|
absolute_path_from_relative :: proc(rel: string) -> (path: string, err: Errno) {
|
||||||
rel := rel;
|
rel := rel
|
||||||
if rel == "" {
|
if rel == "" {
|
||||||
rel = ".";
|
rel = "."
|
||||||
}
|
}
|
||||||
|
|
||||||
rel_cstr := strings.clone_to_cstring(rel);
|
rel_cstr := strings.clone_to_cstring(rel)
|
||||||
defer delete(rel_cstr);
|
defer delete(rel_cstr)
|
||||||
|
|
||||||
path_ptr := _unix_realpath(rel_cstr, nil);
|
path_ptr := _unix_realpath(rel_cstr, nil)
|
||||||
if path_ptr == nil {
|
if path_ptr == nil {
|
||||||
return "", Errno(get_last_error());
|
return "", Errno(get_last_error())
|
||||||
}
|
}
|
||||||
defer _unix_free(path_ptr);
|
defer _unix_free(path_ptr)
|
||||||
|
|
||||||
path_cstr := transmute(cstring)path_ptr;
|
path_cstr := transmute(cstring)path_ptr
|
||||||
path = strings.clone( string(path_cstr) );
|
path = strings.clone( string(path_cstr) )
|
||||||
|
|
||||||
return path, ERROR_NONE;
|
return path, ERROR_NONE
|
||||||
}
|
}
|
||||||
|
|
||||||
access :: proc(path: string, mask: int) -> (bool, Errno) {
|
access :: proc(path: string, mask: int) -> (bool, Errno) {
|
||||||
cstr := strings.clone_to_cstring(path);
|
cstr := strings.clone_to_cstring(path)
|
||||||
defer delete(cstr);
|
defer delete(cstr)
|
||||||
result := _unix_access(cstr, c.int(mask));
|
result := _unix_access(cstr, c.int(mask))
|
||||||
if result == -1 {
|
if result == -1 {
|
||||||
return false, Errno(get_last_error());
|
return false, Errno(get_last_error())
|
||||||
}
|
}
|
||||||
return true, ERROR_NONE;
|
return true, ERROR_NONE
|
||||||
}
|
}
|
||||||
|
|
||||||
heap_alloc :: proc(size: int) -> rawptr {
|
heap_alloc :: proc(size: int) -> rawptr {
|
||||||
assert(size >= 0);
|
assert(size >= 0)
|
||||||
return _unix_calloc(1, c.size_t(size));
|
return _unix_calloc(1, c.size_t(size))
|
||||||
}
|
}
|
||||||
|
|
||||||
heap_resize :: proc(ptr: rawptr, new_size: int) -> rawptr {
|
heap_resize :: proc(ptr: rawptr, new_size: int) -> rawptr {
|
||||||
return _unix_realloc(ptr, c.size_t(new_size));
|
return _unix_realloc(ptr, c.size_t(new_size))
|
||||||
}
|
}
|
||||||
|
|
||||||
heap_free :: proc(ptr: rawptr) {
|
heap_free :: proc(ptr: rawptr) {
|
||||||
_unix_free(ptr);
|
_unix_free(ptr)
|
||||||
}
|
}
|
||||||
|
|
||||||
getenv :: proc(name: string) -> (string, bool) {
|
getenv :: proc(name: string) -> (string, bool) {
|
||||||
path_str := strings.clone_to_cstring(name);
|
path_str := strings.clone_to_cstring(name)
|
||||||
defer delete(path_str);
|
defer delete(path_str)
|
||||||
cstr := _unix_getenv(path_str);
|
cstr := _unix_getenv(path_str)
|
||||||
if cstr == nil {
|
if cstr == nil {
|
||||||
return "", false;
|
return "", false
|
||||||
}
|
}
|
||||||
return string(cstr), true;
|
return string(cstr), true
|
||||||
}
|
}
|
||||||
|
|
||||||
get_current_directory :: proc() -> string {
|
get_current_directory :: proc() -> string {
|
||||||
// NOTE(tetra): I would use PATH_MAX here, but I was not able to find
|
// NOTE(tetra): I would use PATH_MAX here, but I was not able to find
|
||||||
// an authoritative value for it across all systems.
|
// an authoritative value for it across all systems.
|
||||||
// The largest value I could find was 4096, so might as well use the page size.
|
// The largest value I could find was 4096, so might as well use the page size.
|
||||||
page_size := get_page_size();
|
page_size := get_page_size()
|
||||||
buf := make([dynamic]u8, page_size);
|
buf := make([dynamic]u8, page_size)
|
||||||
for {
|
for {
|
||||||
#no_bounds_check cwd := _unix_getcwd(cstring(&buf[0]), c.size_t(len(buf)));
|
#no_bounds_check cwd := _unix_getcwd(cstring(&buf[0]), c.size_t(len(buf)))
|
||||||
if cwd != nil {
|
if cwd != nil {
|
||||||
return string(cwd);
|
return string(cwd)
|
||||||
}
|
}
|
||||||
if Errno(get_last_error()) != ERANGE {
|
if Errno(get_last_error()) != ERANGE {
|
||||||
return "";
|
return ""
|
||||||
}
|
}
|
||||||
resize(&buf, len(buf)+page_size);
|
resize(&buf, len(buf)+page_size)
|
||||||
}
|
}
|
||||||
unreachable();
|
unreachable()
|
||||||
}
|
}
|
||||||
|
|
||||||
set_current_directory :: proc(path: string) -> (err: Errno) {
|
set_current_directory :: proc(path: string) -> (err: Errno) {
|
||||||
cstr := strings.clone_to_cstring(path, context.temp_allocator);
|
cstr := strings.clone_to_cstring(path, context.temp_allocator)
|
||||||
res := _unix_chdir(cstr);
|
res := _unix_chdir(cstr)
|
||||||
if res == -1 {
|
if res == -1 {
|
||||||
return Errno(get_last_error());
|
return Errno(get_last_error())
|
||||||
}
|
}
|
||||||
return ERROR_NONE;
|
return ERROR_NONE
|
||||||
}
|
}
|
||||||
|
|
||||||
exit :: proc "contextless" (code: int) -> ! {
|
exit :: proc "contextless" (code: int) -> ! {
|
||||||
_unix_exit(c.int(code));
|
_unix_exit(c.int(code))
|
||||||
}
|
}
|
||||||
|
|
||||||
current_thread_id :: proc "contextless" () -> int {
|
current_thread_id :: proc "contextless" () -> int {
|
||||||
return cast(int)intrinsics.syscall(SYS_GETTID);
|
return cast(int)intrinsics.syscall(SYS_GETTID)
|
||||||
}
|
}
|
||||||
|
|
||||||
dlopen :: proc(filename: string, flags: int) -> rawptr {
|
dlopen :: proc(filename: string, flags: int) -> rawptr {
|
||||||
cstr := strings.clone_to_cstring(filename);
|
cstr := strings.clone_to_cstring(filename)
|
||||||
defer delete(cstr);
|
defer delete(cstr)
|
||||||
handle := _unix_dlopen(cstr, c.int(flags));
|
handle := _unix_dlopen(cstr, c.int(flags))
|
||||||
return handle;
|
return handle
|
||||||
}
|
}
|
||||||
dlsym :: proc(handle: rawptr, symbol: string) -> rawptr {
|
dlsym :: proc(handle: rawptr, symbol: string) -> rawptr {
|
||||||
assert(handle != nil);
|
assert(handle != nil)
|
||||||
cstr := strings.clone_to_cstring(symbol);
|
cstr := strings.clone_to_cstring(symbol)
|
||||||
defer delete(cstr);
|
defer delete(cstr)
|
||||||
proc_handle := _unix_dlsym(handle, cstr);
|
proc_handle := _unix_dlsym(handle, cstr)
|
||||||
return proc_handle;
|
return proc_handle
|
||||||
}
|
}
|
||||||
dlclose :: proc(handle: rawptr) -> bool {
|
dlclose :: proc(handle: rawptr) -> bool {
|
||||||
assert(handle != nil);
|
assert(handle != nil)
|
||||||
return _unix_dlclose(handle) == 0;
|
return _unix_dlclose(handle) == 0
|
||||||
}
|
}
|
||||||
dlerror :: proc() -> string {
|
dlerror :: proc() -> string {
|
||||||
return string(_unix_dlerror());
|
return string(_unix_dlerror())
|
||||||
}
|
}
|
||||||
|
|
||||||
get_page_size :: proc() -> int {
|
get_page_size :: proc() -> int {
|
||||||
// NOTE(tetra): The page size never changes, so why do anything complicated
|
// NOTE(tetra): The page size never changes, so why do anything complicated
|
||||||
// if we don't have to.
|
// if we don't have to.
|
||||||
@static page_size := -1;
|
@static page_size := -1
|
||||||
if page_size != -1 {
|
if page_size != -1 {
|
||||||
return page_size;
|
return page_size
|
||||||
}
|
}
|
||||||
|
|
||||||
page_size = int(_unix_getpagesize());
|
page_size = int(_unix_getpagesize())
|
||||||
return page_size;
|
return page_size
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
_alloc_command_line_arguments :: proc() -> []string {
|
_alloc_command_line_arguments :: proc() -> []string {
|
||||||
res := make([]string, len(runtime.args__));
|
res := make([]string, len(runtime.args__))
|
||||||
for arg, i in runtime.args__ {
|
for arg, i in runtime.args__ {
|
||||||
res[i] = string(arg);
|
res[i] = string(arg)
|
||||||
}
|
}
|
||||||
return res;
|
return res
|
||||||
}
|
}
|
||||||
|
|||||||
+43
-43
@@ -54,102 +54,102 @@ File_Info :: struct {
|
|||||||
_make_time_from_unix_file_time :: proc(uft: Unix_File_Time) -> time.Time {
|
_make_time_from_unix_file_time :: proc(uft: Unix_File_Time) -> time.Time {
|
||||||
return time.Time{
|
return time.Time{
|
||||||
_nsec = uft.nanoseconds + uft.seconds * 1_000_000_000,
|
_nsec = uft.nanoseconds + uft.seconds * 1_000_000_000,
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@private
|
@private
|
||||||
_fill_file_info_from_stat :: proc(fi: ^File_Info, s: OS_Stat) {
|
_fill_file_info_from_stat :: proc(fi: ^File_Info, s: OS_Stat) {
|
||||||
fi.size = s.size;
|
fi.size = s.size
|
||||||
fi.mode = cast(File_Mode)s.mode;
|
fi.mode = cast(File_Mode)s.mode
|
||||||
fi.is_dir = S_ISDIR(u32(s.mode));
|
fi.is_dir = S_ISDIR(u32(s.mode))
|
||||||
|
|
||||||
// NOTE(laleksic, 2021-01-21): Not really creation time, but closest we can get (maybe better to leave it 0?)
|
// NOTE(laleksic, 2021-01-21): Not really creation time, but closest we can get (maybe better to leave it 0?)
|
||||||
fi.creation_time = _make_time_from_unix_file_time(s.status_change);
|
fi.creation_time = _make_time_from_unix_file_time(s.status_change)
|
||||||
|
|
||||||
fi.modification_time = _make_time_from_unix_file_time(s.modified);
|
fi.modification_time = _make_time_from_unix_file_time(s.modified)
|
||||||
fi.access_time = _make_time_from_unix_file_time(s.last_access);
|
fi.access_time = _make_time_from_unix_file_time(s.last_access)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@private
|
@private
|
||||||
path_base :: proc(path: string) -> string {
|
path_base :: proc(path: string) -> string {
|
||||||
is_separator :: proc(c: byte) -> bool {
|
is_separator :: proc(c: byte) -> bool {
|
||||||
return c == '/';
|
return c == '/'
|
||||||
}
|
}
|
||||||
|
|
||||||
if path == "" {
|
if path == "" {
|
||||||
return ".";
|
return "."
|
||||||
}
|
}
|
||||||
|
|
||||||
path := path;
|
path := path
|
||||||
for len(path) > 0 && is_separator(path[len(path)-1]) {
|
for len(path) > 0 && is_separator(path[len(path)-1]) {
|
||||||
path = path[:len(path)-1];
|
path = path[:len(path)-1]
|
||||||
}
|
}
|
||||||
|
|
||||||
i := len(path)-1;
|
i := len(path)-1
|
||||||
for i >= 0 && !is_separator(path[i]) {
|
for i >= 0 && !is_separator(path[i]) {
|
||||||
i -= 1;
|
i -= 1
|
||||||
}
|
}
|
||||||
if i >= 0 {
|
if i >= 0 {
|
||||||
path = path[i+1:];
|
path = path[i+1:]
|
||||||
}
|
}
|
||||||
if path == "" {
|
if path == "" {
|
||||||
return "/";
|
return "/"
|
||||||
}
|
}
|
||||||
return path;
|
return path
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
lstat :: proc(name: string, allocator := context.allocator) -> (fi: File_Info, err: Errno) {
|
lstat :: proc(name: string, allocator := context.allocator) -> (fi: File_Info, err: Errno) {
|
||||||
|
|
||||||
context.allocator = allocator;
|
context.allocator = allocator
|
||||||
|
|
||||||
s: OS_Stat;
|
s: OS_Stat
|
||||||
s, err = _lstat(name);
|
s, err = _lstat(name)
|
||||||
if err != ERROR_NONE {
|
if err != ERROR_NONE {
|
||||||
return fi, err;
|
return fi, err
|
||||||
}
|
}
|
||||||
_fill_file_info_from_stat(&fi, s);
|
_fill_file_info_from_stat(&fi, s)
|
||||||
fi.fullpath, err = absolute_path_from_relative(name);
|
fi.fullpath, err = absolute_path_from_relative(name)
|
||||||
if err != ERROR_NONE {
|
if err != ERROR_NONE {
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
fi.name = path_base(fi.fullpath);
|
fi.name = path_base(fi.fullpath)
|
||||||
return fi, ERROR_NONE;
|
return fi, ERROR_NONE
|
||||||
}
|
}
|
||||||
|
|
||||||
stat :: proc(name: string, allocator := context.allocator) -> (fi: File_Info, err: Errno) {
|
stat :: proc(name: string, allocator := context.allocator) -> (fi: File_Info, err: Errno) {
|
||||||
|
|
||||||
context.allocator = allocator;
|
context.allocator = allocator
|
||||||
|
|
||||||
s: OS_Stat;
|
s: OS_Stat
|
||||||
s, err = _stat(name);
|
s, err = _stat(name)
|
||||||
if err != ERROR_NONE {
|
if err != ERROR_NONE {
|
||||||
return fi, err;
|
return fi, err
|
||||||
}
|
}
|
||||||
_fill_file_info_from_stat(&fi, s);
|
_fill_file_info_from_stat(&fi, s)
|
||||||
fi.fullpath, err = absolute_path_from_relative(name);
|
fi.fullpath, err = absolute_path_from_relative(name)
|
||||||
if err != ERROR_NONE {
|
if err != ERROR_NONE {
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
fi.name = path_base(fi.fullpath);
|
fi.name = path_base(fi.fullpath)
|
||||||
return fi, ERROR_NONE;
|
return fi, ERROR_NONE
|
||||||
}
|
}
|
||||||
|
|
||||||
fstat :: proc(fd: Handle, allocator := context.allocator) -> (fi: File_Info, err: Errno) {
|
fstat :: proc(fd: Handle, allocator := context.allocator) -> (fi: File_Info, err: Errno) {
|
||||||
|
|
||||||
context.allocator = allocator;
|
context.allocator = allocator
|
||||||
|
|
||||||
s: OS_Stat;
|
s: OS_Stat
|
||||||
s, err = _fstat(fd);
|
s, err = _fstat(fd)
|
||||||
if err != ERROR_NONE {
|
if err != ERROR_NONE {
|
||||||
return fi, err;
|
return fi, err
|
||||||
}
|
}
|
||||||
_fill_file_info_from_stat(&fi, s);
|
_fill_file_info_from_stat(&fi, s)
|
||||||
fi.fullpath, err = absolute_path_from_handle(fd);
|
fi.fullpath, err = absolute_path_from_handle(fd)
|
||||||
if err != ERROR_NONE {
|
if err != ERROR_NONE {
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
fi.name = path_base(fi.fullpath);
|
fi.name = path_base(fi.fullpath)
|
||||||
return fi, ERROR_NONE;
|
return fi, ERROR_NONE
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,43 +9,43 @@ when ODIN_OS == "darwin" {
|
|||||||
|
|
||||||
import "core:strings"
|
import "core:strings"
|
||||||
|
|
||||||
SEPARATOR :: '/';
|
SEPARATOR :: '/'
|
||||||
SEPARATOR_STRING :: `/`;
|
SEPARATOR_STRING :: `/`
|
||||||
LIST_SEPARATOR :: ':';
|
LIST_SEPARATOR :: ':'
|
||||||
|
|
||||||
is_reserved_name :: proc(path: string) -> bool {
|
is_reserved_name :: proc(path: string) -> bool {
|
||||||
return false;
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
is_abs :: proc(path: string) -> bool {
|
is_abs :: proc(path: string) -> bool {
|
||||||
return strings.has_prefix(path, "/");
|
return strings.has_prefix(path, "/")
|
||||||
}
|
}
|
||||||
|
|
||||||
abs :: proc(path: string, allocator := context.allocator) -> (string, bool) {
|
abs :: proc(path: string, allocator := context.allocator) -> (string, bool) {
|
||||||
rel := path;
|
rel := path
|
||||||
if rel == "" {
|
if rel == "" {
|
||||||
rel = ".";
|
rel = "."
|
||||||
}
|
}
|
||||||
rel_cstr := strings.clone_to_cstring(rel, context.temp_allocator);
|
rel_cstr := strings.clone_to_cstring(rel, context.temp_allocator)
|
||||||
path_ptr := realpath(rel_cstr, nil);
|
path_ptr := realpath(rel_cstr, nil)
|
||||||
if path_ptr == nil {
|
if path_ptr == nil {
|
||||||
return "", __error()^ == 0;
|
return "", __error()^ == 0
|
||||||
}
|
}
|
||||||
defer _unix_free(path_ptr);
|
defer _unix_free(path_ptr)
|
||||||
|
|
||||||
path_cstr := cstring(path_ptr);
|
path_cstr := cstring(path_ptr)
|
||||||
path_str := strings.clone(string(path_cstr), allocator);
|
path_str := strings.clone(string(path_cstr), allocator)
|
||||||
return path_str, true;
|
return path_str, true
|
||||||
}
|
}
|
||||||
|
|
||||||
join :: proc(elems: ..string, allocator := context.allocator) -> string {
|
join :: proc(elems: ..string, allocator := context.allocator) -> string {
|
||||||
for e, i in elems {
|
for e, i in elems {
|
||||||
if e != "" {
|
if e != "" {
|
||||||
p := strings.join(elems[i:], SEPARATOR_STRING, context.temp_allocator);
|
p := strings.join(elems[i:], SEPARATOR_STRING, context.temp_allocator)
|
||||||
return clean(p, allocator);
|
return clean(p, allocator)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return "";
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
@(private)
|
@(private)
|
||||||
|
|||||||
@@ -8,22 +8,22 @@ when ODIN_DEFAULT_TO_NIL_ALLOCATOR {
|
|||||||
default_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
default_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
||||||
size, alignment: int,
|
size, alignment: int,
|
||||||
old_memory: rawptr, old_size: int, loc := #caller_location) -> ([]byte, Allocator_Error) {
|
old_memory: rawptr, old_size: int, loc := #caller_location) -> ([]byte, Allocator_Error) {
|
||||||
return nil, .None;
|
return nil, .None
|
||||||
}
|
}
|
||||||
|
|
||||||
default_allocator :: proc() -> Allocator {
|
default_allocator :: proc() -> Allocator {
|
||||||
return Allocator{
|
return Allocator{
|
||||||
procedure = default_allocator_proc,
|
procedure = default_allocator_proc,
|
||||||
data = nil,
|
data = nil,
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// TODO(bill): reimplement these procedures in the os_specific stuff
|
// TODO(bill): reimplement these procedures in the os_specific stuff
|
||||||
import "core:os"
|
import "core:os"
|
||||||
|
|
||||||
default_allocator_proc :: os.heap_allocator_proc;
|
default_allocator_proc :: os.heap_allocator_proc
|
||||||
|
|
||||||
default_allocator :: proc() -> Allocator {
|
default_allocator :: proc() -> Allocator {
|
||||||
return os.heap_allocator();
|
return os.heap_allocator()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,86 +4,86 @@ import "core:intrinsics"
|
|||||||
|
|
||||||
@(link_name="__umodti3")
|
@(link_name="__umodti3")
|
||||||
umodti3 :: proc "c" (a, b: u128) -> u128 {
|
umodti3 :: proc "c" (a, b: u128) -> u128 {
|
||||||
r: u128 = ---;
|
r: u128 = ---
|
||||||
_ = udivmod128(a, b, &r);
|
_ = udivmod128(a, b, &r)
|
||||||
return r;
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@(link_name="__udivmodti4")
|
@(link_name="__udivmodti4")
|
||||||
udivmodti4 :: proc "c" (a, b: u128, rem: ^u128) -> u128 {
|
udivmodti4 :: proc "c" (a, b: u128, rem: ^u128) -> u128 {
|
||||||
return udivmod128(a, b, rem);
|
return udivmod128(a, b, rem)
|
||||||
}
|
}
|
||||||
|
|
||||||
@(link_name="__udivti3")
|
@(link_name="__udivti3")
|
||||||
udivti3 :: proc "c" (a, b: u128) -> u128 {
|
udivti3 :: proc "c" (a, b: u128) -> u128 {
|
||||||
return udivmodti4(a, b, nil);
|
return udivmodti4(a, b, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@(link_name="__modti3")
|
@(link_name="__modti3")
|
||||||
modti3 :: proc "c" (a, b: i128) -> i128 {
|
modti3 :: proc "c" (a, b: i128) -> i128 {
|
||||||
s_a := a >> (128 - 1);
|
s_a := a >> (128 - 1)
|
||||||
s_b := b >> (128 - 1);
|
s_b := b >> (128 - 1)
|
||||||
an := (a ~ s_a) - s_a;
|
an := (a ~ s_a) - s_a
|
||||||
bn := (b ~ s_b) - s_b;
|
bn := (b ~ s_b) - s_b
|
||||||
|
|
||||||
r: u128 = ---;
|
r: u128 = ---
|
||||||
_ = udivmod128(transmute(u128)an, transmute(u128)bn, &r);
|
_ = udivmod128(transmute(u128)an, transmute(u128)bn, &r)
|
||||||
return (transmute(i128)r ~ s_a) - s_a;
|
return (transmute(i128)r ~ s_a) - s_a
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@(link_name="__divmodti4")
|
@(link_name="__divmodti4")
|
||||||
divmodti4 :: proc "c" (a, b: i128, rem: ^i128) -> i128 {
|
divmodti4 :: proc "c" (a, b: i128, rem: ^i128) -> i128 {
|
||||||
u := udivmod128(transmute(u128)a, transmute(u128)b, cast(^u128)rem);
|
u := udivmod128(transmute(u128)a, transmute(u128)b, cast(^u128)rem)
|
||||||
return transmute(i128)u;
|
return transmute(i128)u
|
||||||
}
|
}
|
||||||
|
|
||||||
@(link_name="__divti3")
|
@(link_name="__divti3")
|
||||||
divti3 :: proc "c" (a, b: i128) -> i128 {
|
divti3 :: proc "c" (a, b: i128) -> i128 {
|
||||||
u := udivmodti4(transmute(u128)a, transmute(u128)b, nil);
|
u := udivmodti4(transmute(u128)a, transmute(u128)b, nil)
|
||||||
return transmute(i128)u;
|
return transmute(i128)u
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@(link_name="__fixdfti")
|
@(link_name="__fixdfti")
|
||||||
fixdfti :: proc(a: u64) -> i128 {
|
fixdfti :: proc(a: u64) -> i128 {
|
||||||
significandBits :: 52;
|
significandBits :: 52
|
||||||
typeWidth :: (size_of(u64)*8);
|
typeWidth :: (size_of(u64)*8)
|
||||||
exponentBits :: (typeWidth - significandBits - 1);
|
exponentBits :: (typeWidth - significandBits - 1)
|
||||||
maxExponent :: ((1 << exponentBits) - 1);
|
maxExponent :: ((1 << exponentBits) - 1)
|
||||||
exponentBias :: (maxExponent >> 1);
|
exponentBias :: (maxExponent >> 1)
|
||||||
|
|
||||||
implicitBit :: (u64(1) << significandBits);
|
implicitBit :: (u64(1) << significandBits)
|
||||||
significandMask :: (implicitBit - 1);
|
significandMask :: (implicitBit - 1)
|
||||||
signBit :: (u64(1) << (significandBits + exponentBits));
|
signBit :: (u64(1) << (significandBits + exponentBits))
|
||||||
absMask :: (signBit - 1);
|
absMask :: (signBit - 1)
|
||||||
exponentMask :: (absMask ~ significandMask);
|
exponentMask :: (absMask ~ significandMask)
|
||||||
|
|
||||||
// Break a into sign, exponent, significand
|
// Break a into sign, exponent, significand
|
||||||
aRep := a;
|
aRep := a
|
||||||
aAbs := aRep & absMask;
|
aAbs := aRep & absMask
|
||||||
sign := i128(-1 if aRep & signBit != 0 else 1);
|
sign := i128(-1 if aRep & signBit != 0 else 1)
|
||||||
exponent := u64((aAbs >> significandBits) - exponentBias);
|
exponent := u64((aAbs >> significandBits) - exponentBias)
|
||||||
significand := u64((aAbs & significandMask) | implicitBit);
|
significand := u64((aAbs & significandMask) | implicitBit)
|
||||||
|
|
||||||
// If exponent is negative, the result is zero.
|
// If exponent is negative, the result is zero.
|
||||||
if exponent < 0 {
|
if exponent < 0 {
|
||||||
return 0;
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the value is too large for the integer type, saturate.
|
// If the value is too large for the integer type, saturate.
|
||||||
if exponent >= size_of(i128) * 8 {
|
if exponent >= size_of(i128) * 8 {
|
||||||
return max(i128) if sign == 1 else min(i128);
|
return max(i128) if sign == 1 else min(i128)
|
||||||
}
|
}
|
||||||
|
|
||||||
// If 0 <= exponent < significandBits, right shift to get the result.
|
// If 0 <= exponent < significandBits, right shift to get the result.
|
||||||
// Otherwise, shift left.
|
// Otherwise, shift left.
|
||||||
if exponent < significandBits {
|
if exponent < significandBits {
|
||||||
return sign * i128(significand >> (significandBits - exponent));
|
return sign * i128(significand >> (significandBits - exponent))
|
||||||
} else {
|
} else {
|
||||||
return sign * (i128(significand) << (exponent - significandBits));
|
return sign * (i128(significand) << (exponent - significandBits))
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import "core:os"
|
|||||||
// TODO(bill): reimplement `os.write` so that it does not rely on package os
|
// TODO(bill): reimplement `os.write` so that it does not rely on package os
|
||||||
// NOTE: Use os_specific_linux.odin, os_specific_darwin.odin, etc
|
// NOTE: Use os_specific_linux.odin, os_specific_darwin.odin, etc
|
||||||
_os_write :: proc "contextless" (data: []byte) -> (int, _OS_Errno) {
|
_os_write :: proc "contextless" (data: []byte) -> (int, _OS_Errno) {
|
||||||
context = default_context();
|
context = default_context()
|
||||||
n, err := os.write(os.stderr, data);
|
n, err := os.write(os.stderr, data)
|
||||||
return int(n), _OS_Errno(err);
|
return int(n), _OS_Errno(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,15 +4,15 @@ package runtime
|
|||||||
@(link_name="memset")
|
@(link_name="memset")
|
||||||
memset :: proc "c" (ptr: rawptr, val: i32, len: int) -> rawptr {
|
memset :: proc "c" (ptr: rawptr, val: i32, len: int) -> rawptr {
|
||||||
if ptr == nil || len == 0 {
|
if ptr == nil || len == 0 {
|
||||||
return ptr;
|
return ptr
|
||||||
}
|
}
|
||||||
b := byte(val);
|
b := byte(val)
|
||||||
|
|
||||||
p_start := uintptr(ptr);
|
p_start := uintptr(ptr)
|
||||||
p_end := p_start + uintptr(max(len, 0));
|
p_end := p_start + uintptr(max(len, 0))
|
||||||
for p := p_start; p < p_end; p += 1 {
|
for p := p_start; p < p_end; p += 1 {
|
||||||
(^byte)(p)^ = b;
|
(^byte)(p)^ = b
|
||||||
}
|
}
|
||||||
|
|
||||||
return ptr;
|
return ptr
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,13 +9,13 @@ import "core:intrinsics"
|
|||||||
foreign import pthread "System.framework"
|
foreign import pthread "System.framework"
|
||||||
|
|
||||||
_current_thread_id :: proc "contextless" () -> int {
|
_current_thread_id :: proc "contextless" () -> int {
|
||||||
tid: u64;
|
tid: u64
|
||||||
// NOTE(Oskar): available from OSX 10.6 and iOS 3.2.
|
// NOTE(Oskar): available from OSX 10.6 and iOS 3.2.
|
||||||
// For older versions there is `syscall(SYS_thread_selfid)`, but not really
|
// For older versions there is `syscall(SYS_thread_selfid)`, but not really
|
||||||
// the same thing apparently.
|
// the same thing apparently.
|
||||||
foreign pthread { pthread_threadid_np :: proc "c" (rawptr, ^u64) -> c.int ---; }
|
foreign pthread { pthread_threadid_np :: proc "c" (rawptr, ^u64) -> c.int --- }
|
||||||
pthread_threadid_np(nil, &tid);
|
pthread_threadid_np(nil, &tid)
|
||||||
return int(tid);
|
return int(tid)
|
||||||
}
|
}
|
||||||
|
|
||||||
foreign {
|
foreign {
|
||||||
@@ -26,38 +26,38 @@ foreign {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_atomic_try_wait_slow :: proc(ptr: ^u32, val: u32) {
|
_atomic_try_wait_slow :: proc(ptr: ^u32, val: u32) {
|
||||||
history: uint = 10;
|
history: uint = 10
|
||||||
for {
|
for {
|
||||||
// Exponential wait
|
// Exponential wait
|
||||||
_darwin_usleep(history >> 2);
|
_darwin_usleep(history >> 2)
|
||||||
history += history >> 2;
|
history += history >> 2
|
||||||
if history > (1 << 10) {
|
if history > (1 << 10) {
|
||||||
history = 1 << 10;
|
history = 1 << 10
|
||||||
}
|
}
|
||||||
|
|
||||||
if atomic_load(ptr) != val {
|
if atomic_load(ptr) != val {
|
||||||
break;
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_atomic_wait :: proc(ptr: ^u32, val: u32) {
|
_atomic_wait :: proc(ptr: ^u32, val: u32) {
|
||||||
if intrinsics.expect(atomic_load(ptr) != val, true) {
|
if intrinsics.expect(atomic_load(ptr) != val, true) {
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
for i in 0..<16 {
|
for i in 0..<16 {
|
||||||
if atomic_load(ptr) != val {
|
if atomic_load(ptr) != val {
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
if i < 12 {
|
if i < 12 {
|
||||||
intrinsics.cpu_relax();
|
intrinsics.cpu_relax()
|
||||||
} else {
|
} else {
|
||||||
_darwin_sched_yield();
|
_darwin_sched_yield()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for val == atomic_load(ptr) {
|
for val == atomic_load(ptr) {
|
||||||
_atomic_try_wait_slow(ptr, val);
|
_atomic_try_wait_slow(ptr, val)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -73,7 +73,7 @@ _mutex_unlock :: proc(m: ^Mutex) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_mutex_try_lock :: proc(m: ^Mutex) -> bool {
|
_mutex_try_lock :: proc(m: ^Mutex) -> bool {
|
||||||
return false;
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
_RW_Mutex :: struct {
|
_RW_Mutex :: struct {
|
||||||
@@ -86,7 +86,7 @@ _rw_mutex_unlock :: proc(rw: ^RW_Mutex) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_rw_mutex_try_lock :: proc(rw: ^RW_Mutex) -> bool {
|
_rw_mutex_try_lock :: proc(rw: ^RW_Mutex) -> bool {
|
||||||
return false;
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
_rw_mutex_shared_lock :: proc(rw: ^RW_Mutex) {
|
_rw_mutex_shared_lock :: proc(rw: ^RW_Mutex) {
|
||||||
@@ -96,7 +96,7 @@ _rw_mutex_shared_unlock :: proc(rw: ^RW_Mutex) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_rw_mutex_try_shared_lock :: proc(rw: ^RW_Mutex) -> bool {
|
_rw_mutex_try_shared_lock :: proc(rw: ^RW_Mutex) -> bool {
|
||||||
return false;
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -110,7 +110,7 @@ _recursive_mutex_unlock :: proc(m: ^Recursive_Mutex) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_recursive_mutex_try_lock :: proc(m: ^Recursive_Mutex) -> bool {
|
_recursive_mutex_try_lock :: proc(m: ^Recursive_Mutex) -> bool {
|
||||||
return false;
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -123,7 +123,7 @@ _cond_wait :: proc(c: ^Cond, m: ^Mutex) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_cond_wait_with_timeout :: proc(c: ^Cond, m: ^Mutex, timeout: time.Duration) -> bool {
|
_cond_wait_with_timeout :: proc(c: ^Cond, m: ^Mutex, timeout: time.Duration) -> bool {
|
||||||
return false;
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
_cond_signal :: proc(c: ^Cond) {
|
_cond_signal :: proc(c: ^Cond) {
|
||||||
|
|||||||
@@ -10,6 +10,6 @@ _current_thread_id :: proc "contextless" () -> int {
|
|||||||
syscall :: proc(number: i32, #c_vararg args: ..any) -> i32 ---
|
syscall :: proc(number: i32, #c_vararg args: ..any) -> i32 ---
|
||||||
}
|
}
|
||||||
|
|
||||||
SYS_GETTID :: 186;
|
SYS_GETTID :: 186
|
||||||
return int(syscall(SYS_GETTID));
|
return int(syscall(SYS_GETTID))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,30 +18,30 @@ _Mutex :: struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_mutex_lock :: proc(m: ^Mutex) {
|
_mutex_lock :: proc(m: ^Mutex) {
|
||||||
err := unix.pthread_mutex_lock(&m.impl.pthread_mutex);
|
err := unix.pthread_mutex_lock(&m.impl.pthread_mutex)
|
||||||
assert(err == 0);
|
assert(err == 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
_mutex_unlock :: proc(m: ^Mutex) {
|
_mutex_unlock :: proc(m: ^Mutex) {
|
||||||
err := unix.pthread_mutex_unlock(&m.impl.pthread_mutex);
|
err := unix.pthread_mutex_unlock(&m.impl.pthread_mutex)
|
||||||
assert(err == 0);
|
assert(err == 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
_mutex_try_lock :: proc(m: ^Mutex) -> bool {
|
_mutex_try_lock :: proc(m: ^Mutex) -> bool {
|
||||||
err := unix.pthread_mutex_trylock(&m.impl.pthread_mutex);
|
err := unix.pthread_mutex_trylock(&m.impl.pthread_mutex)
|
||||||
return err == 0;
|
return err == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
RW_Mutex_State :: distinct uint;
|
RW_Mutex_State :: distinct uint
|
||||||
RW_Mutex_State_Half_Width :: size_of(RW_Mutex_State)*8/2;
|
RW_Mutex_State_Half_Width :: size_of(RW_Mutex_State)*8/2
|
||||||
RW_Mutex_State_Is_Writing :: RW_Mutex_State(1);
|
RW_Mutex_State_Is_Writing :: RW_Mutex_State(1)
|
||||||
RW_Mutex_State_Writer :: RW_Mutex_State(1)<<1;
|
RW_Mutex_State_Writer :: RW_Mutex_State(1)<<1
|
||||||
RW_Mutex_State_Reader :: RW_Mutex_State(1)<<RW_Mutex_State_Half_Width;
|
RW_Mutex_State_Reader :: RW_Mutex_State(1)<<RW_Mutex_State_Half_Width
|
||||||
|
|
||||||
RW_Mutex_State_Writer_Mask :: RW_Mutex_State(1<<(RW_Mutex_State_Half_Width-1) - 1) << 1;
|
RW_Mutex_State_Writer_Mask :: RW_Mutex_State(1<<(RW_Mutex_State_Half_Width-1) - 1) << 1
|
||||||
RW_Mutex_State_Reader_Mask :: RW_Mutex_State(1<<(RW_Mutex_State_Half_Width-1) - 1) << RW_Mutex_State_Half_Width;
|
RW_Mutex_State_Reader_Mask :: RW_Mutex_State(1<<(RW_Mutex_State_Half_Width-1) - 1) << RW_Mutex_State_Half_Width
|
||||||
|
|
||||||
|
|
||||||
_RW_Mutex :: struct {
|
_RW_Mutex :: struct {
|
||||||
@@ -53,72 +53,72 @@ _RW_Mutex :: struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_rw_mutex_lock :: proc(rw: ^RW_Mutex) {
|
_rw_mutex_lock :: proc(rw: ^RW_Mutex) {
|
||||||
_ = atomic_add(&rw.impl.state, RW_Mutex_State_Writer);
|
_ = atomic_add(&rw.impl.state, RW_Mutex_State_Writer)
|
||||||
mutex_lock(&rw.impl.mutex);
|
mutex_lock(&rw.impl.mutex)
|
||||||
|
|
||||||
state := atomic_or(&rw.impl.state, RW_Mutex_State_Writer);
|
state := atomic_or(&rw.impl.state, RW_Mutex_State_Writer)
|
||||||
if state & RW_Mutex_State_Reader_Mask != 0 {
|
if state & RW_Mutex_State_Reader_Mask != 0 {
|
||||||
sema_wait(&rw.impl.sema);
|
sema_wait(&rw.impl.sema)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_rw_mutex_unlock :: proc(rw: ^RW_Mutex) {
|
_rw_mutex_unlock :: proc(rw: ^RW_Mutex) {
|
||||||
_ = atomic_and(&rw.impl.state, ~RW_Mutex_State_Is_Writing);
|
_ = atomic_and(&rw.impl.state, ~RW_Mutex_State_Is_Writing)
|
||||||
mutex_unlock(&rw.impl.mutex);
|
mutex_unlock(&rw.impl.mutex)
|
||||||
}
|
}
|
||||||
|
|
||||||
_rw_mutex_try_lock :: proc(rw: ^RW_Mutex) -> bool {
|
_rw_mutex_try_lock :: proc(rw: ^RW_Mutex) -> bool {
|
||||||
if mutex_try_lock(&rw.impl.mutex) {
|
if mutex_try_lock(&rw.impl.mutex) {
|
||||||
state := atomic_load(&rw.impl.state);
|
state := atomic_load(&rw.impl.state)
|
||||||
if state & RW_Mutex_State_Reader_Mask == 0 {
|
if state & RW_Mutex_State_Reader_Mask == 0 {
|
||||||
_ = atomic_or(&rw.impl.state, RW_Mutex_State_Is_Writing);
|
_ = atomic_or(&rw.impl.state, RW_Mutex_State_Is_Writing)
|
||||||
return true;
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
mutex_unlock(&rw.impl.mutex);
|
mutex_unlock(&rw.impl.mutex)
|
||||||
}
|
}
|
||||||
return false;
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
_rw_mutex_shared_lock :: proc(rw: ^RW_Mutex) {
|
_rw_mutex_shared_lock :: proc(rw: ^RW_Mutex) {
|
||||||
state := atomic_load(&rw.impl.state);
|
state := atomic_load(&rw.impl.state)
|
||||||
for state & (RW_Mutex_State_Is_Writing|RW_Mutex_State_Writer_Mask) == 0 {
|
for state & (RW_Mutex_State_Is_Writing|RW_Mutex_State_Writer_Mask) == 0 {
|
||||||
ok: bool;
|
ok: bool
|
||||||
state, ok = atomic_compare_exchange_weak(&rw.impl.state, state, state + RW_Mutex_State_Reader);
|
state, ok = atomic_compare_exchange_weak(&rw.impl.state, state, state + RW_Mutex_State_Reader)
|
||||||
if ok {
|
if ok {
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mutex_lock(&rw.impl.mutex);
|
mutex_lock(&rw.impl.mutex)
|
||||||
_ = atomic_add(&rw.impl.state, RW_Mutex_State_Reader);
|
_ = atomic_add(&rw.impl.state, RW_Mutex_State_Reader)
|
||||||
mutex_unlock(&rw.impl.mutex);
|
mutex_unlock(&rw.impl.mutex)
|
||||||
}
|
}
|
||||||
|
|
||||||
_rw_mutex_shared_unlock :: proc(rw: ^RW_Mutex) {
|
_rw_mutex_shared_unlock :: proc(rw: ^RW_Mutex) {
|
||||||
state := atomic_sub(&rw.impl.state, RW_Mutex_State_Reader);
|
state := atomic_sub(&rw.impl.state, RW_Mutex_State_Reader)
|
||||||
|
|
||||||
if (state & RW_Mutex_State_Reader_Mask == RW_Mutex_State_Reader) &&
|
if (state & RW_Mutex_State_Reader_Mask == RW_Mutex_State_Reader) &&
|
||||||
(state & RW_Mutex_State_Is_Writing != 0) {
|
(state & RW_Mutex_State_Is_Writing != 0) {
|
||||||
sema_post(&rw.impl.sema);
|
sema_post(&rw.impl.sema)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_rw_mutex_try_shared_lock :: proc(rw: ^RW_Mutex) -> bool {
|
_rw_mutex_try_shared_lock :: proc(rw: ^RW_Mutex) -> bool {
|
||||||
state := atomic_load(&rw.impl.state);
|
state := atomic_load(&rw.impl.state)
|
||||||
if state & (RW_Mutex_State_Is_Writing|RW_Mutex_State_Writer_Mask) == 0 {
|
if state & (RW_Mutex_State_Is_Writing|RW_Mutex_State_Writer_Mask) == 0 {
|
||||||
_, ok := atomic_compare_exchange_strong(&rw.impl.state, state, state + RW_Mutex_State_Reader);
|
_, ok := atomic_compare_exchange_strong(&rw.impl.state, state, state + RW_Mutex_State_Reader)
|
||||||
if ok {
|
if ok {
|
||||||
return true;
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if mutex_try_lock(&rw.impl.mutex) {
|
if mutex_try_lock(&rw.impl.mutex) {
|
||||||
_ = atomic_add(&rw.impl.state, RW_Mutex_State_Reader);
|
_ = atomic_add(&rw.impl.state, RW_Mutex_State_Reader)
|
||||||
mutex_unlock(&rw.impl.mutex);
|
mutex_unlock(&rw.impl.mutex)
|
||||||
return true;
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -129,42 +129,42 @@ _Recursive_Mutex :: struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_recursive_mutex_lock :: proc(m: ^Recursive_Mutex) {
|
_recursive_mutex_lock :: proc(m: ^Recursive_Mutex) {
|
||||||
tid := _current_thread_id();
|
tid := _current_thread_id()
|
||||||
if tid != m.impl.owner {
|
if tid != m.impl.owner {
|
||||||
mutex_lock(&m.impl.mutex);
|
mutex_lock(&m.impl.mutex)
|
||||||
}
|
}
|
||||||
// inside the lock
|
// inside the lock
|
||||||
m.impl.owner = tid;
|
m.impl.owner = tid
|
||||||
m.impl.recursion += 1;
|
m.impl.recursion += 1
|
||||||
}
|
}
|
||||||
|
|
||||||
_recursive_mutex_unlock :: proc(m: ^Recursive_Mutex) {
|
_recursive_mutex_unlock :: proc(m: ^Recursive_Mutex) {
|
||||||
tid := _current_thread_id();
|
tid := _current_thread_id()
|
||||||
assert(tid == m.impl.owner);
|
assert(tid == m.impl.owner)
|
||||||
m.impl.recursion -= 1;
|
m.impl.recursion -= 1
|
||||||
recursion := m.impl.recursion;
|
recursion := m.impl.recursion
|
||||||
if recursion == 0 {
|
if recursion == 0 {
|
||||||
m.impl.owner = 0;
|
m.impl.owner = 0
|
||||||
}
|
}
|
||||||
if recursion == 0 {
|
if recursion == 0 {
|
||||||
mutex_unlock(&m.impl.mutex);
|
mutex_unlock(&m.impl.mutex)
|
||||||
}
|
}
|
||||||
// outside the lock
|
// outside the lock
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_recursive_mutex_try_lock :: proc(m: ^Recursive_Mutex) -> bool {
|
_recursive_mutex_try_lock :: proc(m: ^Recursive_Mutex) -> bool {
|
||||||
tid := _current_thread_id();
|
tid := _current_thread_id()
|
||||||
if m.impl.owner == tid {
|
if m.impl.owner == tid {
|
||||||
return mutex_try_lock(&m.impl.mutex);
|
return mutex_try_lock(&m.impl.mutex)
|
||||||
}
|
}
|
||||||
if !mutex_try_lock(&m.impl.mutex) {
|
if !mutex_try_lock(&m.impl.mutex) {
|
||||||
return false;
|
return false
|
||||||
}
|
}
|
||||||
// inside the lock
|
// inside the lock
|
||||||
m.impl.owner = tid;
|
m.impl.owner = tid
|
||||||
m.impl.recursion += 1;
|
m.impl.recursion += 1
|
||||||
return true;
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -173,29 +173,29 @@ _Cond :: struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_cond_wait :: proc(c: ^Cond, m: ^Mutex) {
|
_cond_wait :: proc(c: ^Cond, m: ^Mutex) {
|
||||||
err := unix.pthread_cond_wait(&c.impl.pthread_cond, &m.impl.pthread_mutex);
|
err := unix.pthread_cond_wait(&c.impl.pthread_cond, &m.impl.pthread_mutex)
|
||||||
assert(err == 0);
|
assert(err == 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
_cond_wait_with_timeout :: proc(c: ^Cond, m: ^Mutex, timeout: time.Duration) -> bool {
|
_cond_wait_with_timeout :: proc(c: ^Cond, m: ^Mutex, timeout: time.Duration) -> bool {
|
||||||
ns := time.duration_nanoseconds(timeout);
|
ns := time.duration_nanoseconds(timeout)
|
||||||
timeout_timespec := &time.TimeSpec{
|
timeout_timespec := &time.TimeSpec{
|
||||||
tv_sec = ns / 1e9,
|
tv_sec = ns / 1e9,
|
||||||
tv_nsec = ns % 1e9,
|
tv_nsec = ns % 1e9,
|
||||||
};
|
}
|
||||||
err := unix.pthread_cond_timedwait(&c.impl.pthread_cond, &m.impl.pthread_mutex, timeout_timespec);
|
err := unix.pthread_cond_timedwait(&c.impl.pthread_cond, &m.impl.pthread_mutex, timeout_timespec)
|
||||||
// TODO(bill):
|
// TODO(bill):
|
||||||
return err == 0;
|
return err == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
_cond_signal :: proc(c: ^Cond) {
|
_cond_signal :: proc(c: ^Cond) {
|
||||||
err := unix.pthread_cond_signal(&c.impl.pthread_cond);
|
err := unix.pthread_cond_signal(&c.impl.pthread_cond)
|
||||||
assert(err == 0);
|
assert(err == 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
_cond_broadcast :: proc(c: ^Cond) {
|
_cond_broadcast :: proc(c: ^Cond) {
|
||||||
err := unix.pthread_cond_broadcast(&c.impl.pthread_cond);
|
err := unix.pthread_cond_broadcast(&c.impl.pthread_cond)
|
||||||
assert(err == 0);
|
assert(err == 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
_Sema :: struct {
|
_Sema :: struct {
|
||||||
@@ -205,25 +205,25 @@ _Sema :: struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_sema_wait :: proc(s: ^Sema) {
|
_sema_wait :: proc(s: ^Sema) {
|
||||||
mutex_lock(&s.impl.mutex);
|
mutex_lock(&s.impl.mutex)
|
||||||
defer mutex_unlock(&s.impl.mutex);
|
defer mutex_unlock(&s.impl.mutex)
|
||||||
|
|
||||||
for s.impl.count == 0 {
|
for s.impl.count == 0 {
|
||||||
cond_wait(&s.impl.cond, &s.impl.mutex);
|
cond_wait(&s.impl.cond, &s.impl.mutex)
|
||||||
}
|
}
|
||||||
|
|
||||||
s.impl.count -= 1;
|
s.impl.count -= 1
|
||||||
if s.impl.count > 0 {
|
if s.impl.count > 0 {
|
||||||
cond_signal(&s.impl.cond);
|
cond_signal(&s.impl.cond)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_sema_post :: proc(s: ^Sema, count := 1) {
|
_sema_post :: proc(s: ^Sema, count := 1) {
|
||||||
mutex_lock(&s.impl.mutex);
|
mutex_lock(&s.impl.mutex)
|
||||||
defer mutex_unlock(&s.impl.mutex);
|
defer mutex_unlock(&s.impl.mutex)
|
||||||
|
|
||||||
s.impl.count += count;
|
s.impl.count += count
|
||||||
cond_signal(&s.impl.cond);
|
cond_signal(&s.impl.cond)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+15
-15
@@ -7,13 +7,13 @@ import "core:c"
|
|||||||
foreign import pthread "System.framework"
|
foreign import pthread "System.framework"
|
||||||
|
|
||||||
current_thread_id :: proc "contextless" () -> int {
|
current_thread_id :: proc "contextless" () -> int {
|
||||||
tid: u64;
|
tid: u64
|
||||||
// NOTE(Oskar): available from OSX 10.6 and iOS 3.2.
|
// NOTE(Oskar): available from OSX 10.6 and iOS 3.2.
|
||||||
// For older versions there is `syscall(SYS_thread_selfid)`, but not really
|
// For older versions there is `syscall(SYS_thread_selfid)`, but not really
|
||||||
// the same thing apparently.
|
// the same thing apparently.
|
||||||
foreign pthread { pthread_threadid_np :: proc "c" (rawptr, ^u64) -> c.int ---; }
|
foreign pthread { pthread_threadid_np :: proc "c" (rawptr, ^u64) -> c.int --- }
|
||||||
pthread_threadid_np(nil, &tid);
|
pthread_threadid_np(nil, &tid)
|
||||||
return int(tid);
|
return int(tid)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -28,27 +28,27 @@ Semaphore :: struct #align 16 {
|
|||||||
// See core/sys/unix/pthread_linux.odin/pthread_t.
|
// See core/sys/unix/pthread_linux.odin/pthread_t.
|
||||||
|
|
||||||
semaphore_init :: proc(s: ^Semaphore, initial_count := 0) {
|
semaphore_init :: proc(s: ^Semaphore, initial_count := 0) {
|
||||||
ct := darwin.mach_task_self();
|
ct := darwin.mach_task_self()
|
||||||
res := darwin.semaphore_create(ct, &s.handle, 0, c.int(initial_count));
|
res := darwin.semaphore_create(ct, &s.handle, 0, c.int(initial_count))
|
||||||
assert(res == 0);
|
assert(res == 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
semaphore_destroy :: proc(s: ^Semaphore) {
|
semaphore_destroy :: proc(s: ^Semaphore) {
|
||||||
ct := darwin.mach_task_self();
|
ct := darwin.mach_task_self()
|
||||||
res := darwin.semaphore_destroy(ct, s.handle);
|
res := darwin.semaphore_destroy(ct, s.handle)
|
||||||
assert(res == 0);
|
assert(res == 0)
|
||||||
s.handle = {};
|
s.handle = {}
|
||||||
}
|
}
|
||||||
|
|
||||||
semaphore_post :: proc(s: ^Semaphore, count := 1) {
|
semaphore_post :: proc(s: ^Semaphore, count := 1) {
|
||||||
// NOTE: SPEED: If there's one syscall to do this, we should use it instead of the loop.
|
// NOTE: SPEED: If there's one syscall to do this, we should use it instead of the loop.
|
||||||
for in 0..<count {
|
for in 0..<count {
|
||||||
res := darwin.semaphore_signal(s.handle);
|
res := darwin.semaphore_signal(s.handle)
|
||||||
assert(res == 0);
|
assert(res == 0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
semaphore_wait_for :: proc(s: ^Semaphore) {
|
semaphore_wait_for :: proc(s: ^Semaphore) {
|
||||||
res := darwin.semaphore_wait(s.handle);
|
res := darwin.semaphore_wait(s.handle)
|
||||||
assert(res == 0);
|
assert(res == 0)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,8 +4,8 @@ import "core:sys/unix"
|
|||||||
import "core:intrinsics"
|
import "core:intrinsics"
|
||||||
|
|
||||||
current_thread_id :: proc "contextless" () -> int {
|
current_thread_id :: proc "contextless" () -> int {
|
||||||
SYS_GETTID :: 186;
|
SYS_GETTID :: 186
|
||||||
return int(intrinsics.syscall(SYS_GETTID));
|
return int(intrinsics.syscall(SYS_GETTID))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -18,21 +18,21 @@ Semaphore :: struct #align 16 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
semaphore_init :: proc(s: ^Semaphore, initial_count := 0) {
|
semaphore_init :: proc(s: ^Semaphore, initial_count := 0) {
|
||||||
assert(unix.sem_init(&s.handle, 0, u32(initial_count)) == 0);
|
assert(unix.sem_init(&s.handle, 0, u32(initial_count)) == 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
semaphore_destroy :: proc(s: ^Semaphore) {
|
semaphore_destroy :: proc(s: ^Semaphore) {
|
||||||
assert(unix.sem_destroy(&s.handle) == 0);
|
assert(unix.sem_destroy(&s.handle) == 0)
|
||||||
s.handle = {};
|
s.handle = {}
|
||||||
}
|
}
|
||||||
|
|
||||||
semaphore_post :: proc(s: ^Semaphore, count := 1) {
|
semaphore_post :: proc(s: ^Semaphore, count := 1) {
|
||||||
// NOTE: SPEED: If there's one syscall to do this, we should use it instead of the loop.
|
// NOTE: SPEED: If there's one syscall to do this, we should use it instead of the loop.
|
||||||
for in 0..<count {
|
for in 0..<count {
|
||||||
assert(unix.sem_post(&s.handle) == 0);
|
assert(unix.sem_post(&s.handle) == 0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
semaphore_wait_for :: proc(s: ^Semaphore) {
|
semaphore_wait_for :: proc(s: ^Semaphore) {
|
||||||
assert(unix.sem_wait(&s.handle) == 0);
|
assert(unix.sem_wait(&s.handle) == 0)
|
||||||
}
|
}
|
||||||
|
|||||||
+72
-72
@@ -12,30 +12,30 @@ Mutex :: struct {
|
|||||||
|
|
||||||
mutex_init :: proc(m: ^Mutex) {
|
mutex_init :: proc(m: ^Mutex) {
|
||||||
// NOTE(tetra, 2019-11-01): POSIX OOM if we cannot init the attrs or the mutex.
|
// NOTE(tetra, 2019-11-01): POSIX OOM if we cannot init the attrs or the mutex.
|
||||||
attrs: unix.pthread_mutexattr_t;
|
attrs: unix.pthread_mutexattr_t
|
||||||
assert(unix.pthread_mutexattr_init(&attrs) == 0);
|
assert(unix.pthread_mutexattr_init(&attrs) == 0)
|
||||||
defer unix.pthread_mutexattr_destroy(&attrs); // ignores destruction error
|
defer unix.pthread_mutexattr_destroy(&attrs) // ignores destruction error
|
||||||
unix.pthread_mutexattr_settype(&attrs, unix.PTHREAD_MUTEX_RECURSIVE);
|
unix.pthread_mutexattr_settype(&attrs, unix.PTHREAD_MUTEX_RECURSIVE)
|
||||||
|
|
||||||
assert(unix.pthread_mutex_init(&m.handle, &attrs) == 0);
|
assert(unix.pthread_mutex_init(&m.handle, &attrs) == 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
mutex_destroy :: proc(m: ^Mutex) {
|
mutex_destroy :: proc(m: ^Mutex) {
|
||||||
assert(unix.pthread_mutex_destroy(&m.handle) == 0);
|
assert(unix.pthread_mutex_destroy(&m.handle) == 0)
|
||||||
m.handle = {};
|
m.handle = {}
|
||||||
}
|
}
|
||||||
|
|
||||||
mutex_lock :: proc(m: ^Mutex) {
|
mutex_lock :: proc(m: ^Mutex) {
|
||||||
assert(unix.pthread_mutex_lock(&m.handle) == 0);
|
assert(unix.pthread_mutex_lock(&m.handle) == 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns false if someone else holds the lock.
|
// Returns false if someone else holds the lock.
|
||||||
mutex_try_lock :: proc(m: ^Mutex) -> bool {
|
mutex_try_lock :: proc(m: ^Mutex) -> bool {
|
||||||
return unix.pthread_mutex_trylock(&m.handle) == 0;
|
return unix.pthread_mutex_trylock(&m.handle) == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
mutex_unlock :: proc(m: ^Mutex) {
|
mutex_unlock :: proc(m: ^Mutex) {
|
||||||
assert(unix.pthread_mutex_unlock(&m.handle) == 0);
|
assert(unix.pthread_mutex_unlock(&m.handle) == 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -46,29 +46,29 @@ Blocking_Mutex :: struct {
|
|||||||
|
|
||||||
blocking_mutex_init :: proc(m: ^Blocking_Mutex) {
|
blocking_mutex_init :: proc(m: ^Blocking_Mutex) {
|
||||||
// NOTE(tetra, 2019-11-01): POSIX OOM if we cannot init the attrs or the mutex.
|
// NOTE(tetra, 2019-11-01): POSIX OOM if we cannot init the attrs or the mutex.
|
||||||
attrs: unix.pthread_mutexattr_t;
|
attrs: unix.pthread_mutexattr_t
|
||||||
assert(unix.pthread_mutexattr_init(&attrs) == 0);
|
assert(unix.pthread_mutexattr_init(&attrs) == 0)
|
||||||
defer unix.pthread_mutexattr_destroy(&attrs); // ignores destruction error
|
defer unix.pthread_mutexattr_destroy(&attrs) // ignores destruction error
|
||||||
|
|
||||||
assert(unix.pthread_mutex_init(&m.handle, &attrs) == 0);
|
assert(unix.pthread_mutex_init(&m.handle, &attrs) == 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
blocking_mutex_destroy :: proc(m: ^Blocking_Mutex) {
|
blocking_mutex_destroy :: proc(m: ^Blocking_Mutex) {
|
||||||
assert(unix.pthread_mutex_destroy(&m.handle) == 0);
|
assert(unix.pthread_mutex_destroy(&m.handle) == 0)
|
||||||
m.handle = {};
|
m.handle = {}
|
||||||
}
|
}
|
||||||
|
|
||||||
blocking_mutex_lock :: proc(m: ^Blocking_Mutex) {
|
blocking_mutex_lock :: proc(m: ^Blocking_Mutex) {
|
||||||
assert(unix.pthread_mutex_lock(&m.handle) == 0);
|
assert(unix.pthread_mutex_lock(&m.handle) == 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns false if someone else holds the lock.
|
// Returns false if someone else holds the lock.
|
||||||
blocking_mutex_try_lock :: proc(m: ^Blocking_Mutex) -> bool {
|
blocking_mutex_try_lock :: proc(m: ^Blocking_Mutex) -> bool {
|
||||||
return unix.pthread_mutex_trylock(&m.handle) == 0;
|
return unix.pthread_mutex_trylock(&m.handle) == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
blocking_mutex_unlock :: proc(m: ^Blocking_Mutex) {
|
blocking_mutex_unlock :: proc(m: ^Blocking_Mutex) {
|
||||||
assert(unix.pthread_mutex_unlock(&m.handle) == 0);
|
assert(unix.pthread_mutex_unlock(&m.handle) == 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -90,42 +90,42 @@ Condition :: struct {
|
|||||||
|
|
||||||
condition_init :: proc(c: ^Condition, mutex: Condition_Mutex_Ptr) -> bool {
|
condition_init :: proc(c: ^Condition, mutex: Condition_Mutex_Ptr) -> bool {
|
||||||
// NOTE(tetra, 2019-11-01): POSIX OOM if we cannot init the attrs or the condition.
|
// NOTE(tetra, 2019-11-01): POSIX OOM if we cannot init the attrs or the condition.
|
||||||
attrs: unix.pthread_condattr_t;
|
attrs: unix.pthread_condattr_t
|
||||||
if unix.pthread_condattr_init(&attrs) != 0 {
|
if unix.pthread_condattr_init(&attrs) != 0 {
|
||||||
return false;
|
return false
|
||||||
}
|
}
|
||||||
defer unix.pthread_condattr_destroy(&attrs); // ignores destruction error
|
defer unix.pthread_condattr_destroy(&attrs) // ignores destruction error
|
||||||
|
|
||||||
c.flag = false;
|
c.flag = false
|
||||||
c.mutex = mutex;
|
c.mutex = mutex
|
||||||
return unix.pthread_cond_init(&c.handle, &attrs) == 0;
|
return unix.pthread_cond_init(&c.handle, &attrs) == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
condition_destroy :: proc(c: ^Condition) {
|
condition_destroy :: proc(c: ^Condition) {
|
||||||
assert(unix.pthread_cond_destroy(&c.handle) == 0);
|
assert(unix.pthread_cond_destroy(&c.handle) == 0)
|
||||||
c.handle = {};
|
c.handle = {}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Awaken exactly one thread who is waiting on the condition
|
// Awaken exactly one thread who is waiting on the condition
|
||||||
condition_signal :: proc(c: ^Condition) -> bool {
|
condition_signal :: proc(c: ^Condition) -> bool {
|
||||||
switch m in c.mutex {
|
switch m in c.mutex {
|
||||||
case ^Mutex:
|
case ^Mutex:
|
||||||
mutex_lock(m);
|
mutex_lock(m)
|
||||||
defer mutex_unlock(m);
|
defer mutex_unlock(m)
|
||||||
atomic_swap(&c.flag, true, .Sequentially_Consistent);
|
atomic_swap(&c.flag, true, .Sequentially_Consistent)
|
||||||
return unix.pthread_cond_signal(&c.handle) == 0;
|
return unix.pthread_cond_signal(&c.handle) == 0
|
||||||
case ^Blocking_Mutex:
|
case ^Blocking_Mutex:
|
||||||
blocking_mutex_lock(m);
|
blocking_mutex_lock(m)
|
||||||
defer blocking_mutex_unlock(m);
|
defer blocking_mutex_unlock(m)
|
||||||
atomic_swap(&c.flag, true, .Sequentially_Consistent);
|
atomic_swap(&c.flag, true, .Sequentially_Consistent)
|
||||||
return unix.pthread_cond_signal(&c.handle) == 0;
|
return unix.pthread_cond_signal(&c.handle) == 0
|
||||||
}
|
}
|
||||||
return false;
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Awaken all threads who are waiting on the condition
|
// Awaken all threads who are waiting on the condition
|
||||||
condition_broadcast :: proc(c: ^Condition) -> bool {
|
condition_broadcast :: proc(c: ^Condition) -> bool {
|
||||||
return unix.pthread_cond_broadcast(&c.handle) == 0;
|
return unix.pthread_cond_broadcast(&c.handle) == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait for the condition to be signalled.
|
// Wait for the condition to be signalled.
|
||||||
@@ -134,48 +134,48 @@ condition_broadcast :: proc(c: ^Condition) -> bool {
|
|||||||
condition_wait_for :: proc(c: ^Condition) -> bool {
|
condition_wait_for :: proc(c: ^Condition) -> bool {
|
||||||
switch m in c.mutex {
|
switch m in c.mutex {
|
||||||
case ^Mutex:
|
case ^Mutex:
|
||||||
mutex_lock(m);
|
mutex_lock(m)
|
||||||
defer mutex_unlock(m);
|
defer mutex_unlock(m)
|
||||||
// NOTE(tetra): If a thread comes by and steals the flag immediately after the signal occurs,
|
// NOTE(tetra): If a thread comes by and steals the flag immediately after the signal occurs,
|
||||||
// the thread that gets signalled and wakes up, discovers that the flag was taken and goes
|
// the thread that gets signalled and wakes up, discovers that the flag was taken and goes
|
||||||
// back to sleep.
|
// back to sleep.
|
||||||
// Though this overall behavior is the most sane, there may be a better way to do this that means that
|
// Though this overall behavior is the most sane, there may be a better way to do this that means that
|
||||||
// the first thread to wait, gets the flag first.
|
// the first thread to wait, gets the flag first.
|
||||||
if atomic_swap(&c.flag, false, .Sequentially_Consistent) {
|
if atomic_swap(&c.flag, false, .Sequentially_Consistent) {
|
||||||
return true;
|
return true
|
||||||
}
|
}
|
||||||
for {
|
for {
|
||||||
if unix.pthread_cond_wait(&c.handle, &m.handle) != 0 {
|
if unix.pthread_cond_wait(&c.handle, &m.handle) != 0 {
|
||||||
return false;
|
return false
|
||||||
}
|
}
|
||||||
if atomic_swap(&c.flag, false, .Sequentially_Consistent) {
|
if atomic_swap(&c.flag, false, .Sequentially_Consistent) {
|
||||||
return true;
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false
|
||||||
|
|
||||||
case ^Blocking_Mutex:
|
case ^Blocking_Mutex:
|
||||||
blocking_mutex_lock(m);
|
blocking_mutex_lock(m)
|
||||||
defer blocking_mutex_unlock(m);
|
defer blocking_mutex_unlock(m)
|
||||||
// NOTE(tetra): If a thread comes by and steals the flag immediately after the signal occurs,
|
// NOTE(tetra): If a thread comes by and steals the flag immediately after the signal occurs,
|
||||||
// the thread that gets signalled and wakes up, discovers that the flag was taken and goes
|
// the thread that gets signalled and wakes up, discovers that the flag was taken and goes
|
||||||
// back to sleep.
|
// back to sleep.
|
||||||
// Though this overall behavior is the most sane, there may be a better way to do this that means that
|
// Though this overall behavior is the most sane, there may be a better way to do this that means that
|
||||||
// the first thread to wait, gets the flag first.
|
// the first thread to wait, gets the flag first.
|
||||||
if atomic_swap(&c.flag, false, .Sequentially_Consistent) {
|
if atomic_swap(&c.flag, false, .Sequentially_Consistent) {
|
||||||
return true;
|
return true
|
||||||
}
|
}
|
||||||
for {
|
for {
|
||||||
if unix.pthread_cond_wait(&c.handle, &m.handle) != 0 {
|
if unix.pthread_cond_wait(&c.handle, &m.handle) != 0 {
|
||||||
return false;
|
return false
|
||||||
}
|
}
|
||||||
if atomic_swap(&c.flag, false, .Sequentially_Consistent) {
|
if atomic_swap(&c.flag, false, .Sequentially_Consistent) {
|
||||||
return true;
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false
|
||||||
}
|
}
|
||||||
return false;
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait for the condition to be signalled.
|
// Wait for the condition to be signalled.
|
||||||
@@ -184,65 +184,65 @@ condition_wait_for :: proc(c: ^Condition) -> bool {
|
|||||||
condition_wait_for_timeout :: proc(c: ^Condition, duration: time.Duration) -> bool {
|
condition_wait_for_timeout :: proc(c: ^Condition, duration: time.Duration) -> bool {
|
||||||
switch m in c.mutex {
|
switch m in c.mutex {
|
||||||
case ^Mutex:
|
case ^Mutex:
|
||||||
mutex_lock(m);
|
mutex_lock(m)
|
||||||
defer mutex_unlock(m);
|
defer mutex_unlock(m)
|
||||||
// NOTE(tetra): If a thread comes by and steals the flag immediately after the signal occurs,
|
// NOTE(tetra): If a thread comes by and steals the flag immediately after the signal occurs,
|
||||||
// the thread that gets signalled and wakes up, discovers that the flag was taken and goes
|
// the thread that gets signalled and wakes up, discovers that the flag was taken and goes
|
||||||
// back to sleep.
|
// back to sleep.
|
||||||
// Though this overall behavior is the most sane, there may be a better way to do this that means that
|
// Though this overall behavior is the most sane, there may be a better way to do this that means that
|
||||||
// the first thread to wait, gets the flag first.
|
// the first thread to wait, gets the flag first.
|
||||||
if atomic_swap(&c.flag, false, .Sequentially_Consistent) {
|
if atomic_swap(&c.flag, false, .Sequentially_Consistent) {
|
||||||
return true;
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
ns := time.duration_nanoseconds(duration);
|
ns := time.duration_nanoseconds(duration)
|
||||||
timeout: time.TimeSpec;
|
timeout: time.TimeSpec
|
||||||
timeout.tv_sec = ns / 1e9;
|
timeout.tv_sec = ns / 1e9
|
||||||
timeout.tv_nsec = ns % 1e9;
|
timeout.tv_nsec = ns % 1e9
|
||||||
|
|
||||||
for {
|
for {
|
||||||
if unix.pthread_cond_timedwait(&c.handle, &m.handle, &timeout) != 0 {
|
if unix.pthread_cond_timedwait(&c.handle, &m.handle, &timeout) != 0 {
|
||||||
return false;
|
return false
|
||||||
}
|
}
|
||||||
if atomic_swap(&c.flag, false, .Sequentially_Consistent) {
|
if atomic_swap(&c.flag, false, .Sequentially_Consistent) {
|
||||||
return true;
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false
|
||||||
|
|
||||||
case ^Blocking_Mutex:
|
case ^Blocking_Mutex:
|
||||||
blocking_mutex_lock(m);
|
blocking_mutex_lock(m)
|
||||||
defer blocking_mutex_unlock(m);
|
defer blocking_mutex_unlock(m)
|
||||||
// NOTE(tetra): If a thread comes by and steals the flag immediately after the signal occurs,
|
// NOTE(tetra): If a thread comes by and steals the flag immediately after the signal occurs,
|
||||||
// the thread that gets signalled and wakes up, discovers that the flag was taken and goes
|
// the thread that gets signalled and wakes up, discovers that the flag was taken and goes
|
||||||
// back to sleep.
|
// back to sleep.
|
||||||
// Though this overall behavior is the most sane, there may be a better way to do this that means that
|
// Though this overall behavior is the most sane, there may be a better way to do this that means that
|
||||||
// the first thread to wait, gets the flag first.
|
// the first thread to wait, gets the flag first.
|
||||||
if atomic_swap(&c.flag, false, .Sequentially_Consistent) {
|
if atomic_swap(&c.flag, false, .Sequentially_Consistent) {
|
||||||
return true;
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
ns := time.duration_nanoseconds(duration);
|
ns := time.duration_nanoseconds(duration)
|
||||||
|
|
||||||
timeout: time.TimeSpec;
|
timeout: time.TimeSpec
|
||||||
timeout.tv_sec = ns / 1e9;
|
timeout.tv_sec = ns / 1e9
|
||||||
timeout.tv_nsec = ns % 1e9;
|
timeout.tv_nsec = ns % 1e9
|
||||||
|
|
||||||
for {
|
for {
|
||||||
if unix.pthread_cond_timedwait(&c.handle, &m.handle, &timeout) != 0 {
|
if unix.pthread_cond_timedwait(&c.handle, &m.handle, &timeout) != 0 {
|
||||||
return false;
|
return false
|
||||||
}
|
}
|
||||||
if atomic_swap(&c.flag, false, .Sequentially_Consistent) {
|
if atomic_swap(&c.flag, false, .Sequentially_Consistent) {
|
||||||
return true;
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false
|
||||||
}
|
}
|
||||||
return false;
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
thread_yield :: proc() {
|
thread_yield :: proc() {
|
||||||
unix.sched_yield();
|
unix.sched_yield()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package darwin;
|
package darwin
|
||||||
|
|
||||||
foreign import pthread "System.framework"
|
foreign import pthread "System.framework"
|
||||||
|
|
||||||
@@ -8,22 +8,22 @@ import "core:c"
|
|||||||
// However all other sync primitives are aligned for robustness.
|
// However all other sync primitives are aligned for robustness.
|
||||||
// I cannot currently align these though.
|
// I cannot currently align these though.
|
||||||
// See core/sys/unix/pthread_linux.odin/pthread_t.
|
// See core/sys/unix/pthread_linux.odin/pthread_t.
|
||||||
task_t :: distinct u64;
|
task_t :: distinct u64
|
||||||
semaphore_t :: distinct u64;
|
semaphore_t :: distinct u64
|
||||||
|
|
||||||
kern_return_t :: distinct u64;
|
kern_return_t :: distinct u64
|
||||||
thread_act_t :: distinct u64;
|
thread_act_t :: distinct u64
|
||||||
|
|
||||||
@(default_calling_convention="c")
|
@(default_calling_convention="c")
|
||||||
foreign pthread {
|
foreign pthread {
|
||||||
mach_task_self :: proc() -> task_t ---;
|
mach_task_self :: proc() -> task_t ---
|
||||||
|
|
||||||
semaphore_create :: proc(task: task_t, semaphore: ^semaphore_t, policy, value: c.int) -> kern_return_t ---;
|
semaphore_create :: proc(task: task_t, semaphore: ^semaphore_t, policy, value: c.int) -> kern_return_t ---
|
||||||
semaphore_destroy :: proc(task: task_t, semaphore: semaphore_t) -> kern_return_t ---;
|
semaphore_destroy :: proc(task: task_t, semaphore: semaphore_t) -> kern_return_t ---
|
||||||
|
|
||||||
semaphore_signal :: proc(semaphore: semaphore_t) -> kern_return_t ---;
|
semaphore_signal :: proc(semaphore: semaphore_t) -> kern_return_t ---
|
||||||
semaphore_signal_all :: proc(semaphore: semaphore_t) -> kern_return_t ---;
|
semaphore_signal_all :: proc(semaphore: semaphore_t) -> kern_return_t ---
|
||||||
semaphore_signal_thread :: proc(semaphore: semaphore_t, thread: thread_act_t) -> kern_return_t ---;
|
semaphore_signal_thread :: proc(semaphore: semaphore_t, thread: thread_act_t) -> kern_return_t ---
|
||||||
|
|
||||||
semaphore_wait :: proc(semaphore: semaphore_t) -> kern_return_t ---;
|
semaphore_wait :: proc(semaphore: semaphore_t) -> kern_return_t ---
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,82 +1,82 @@
|
|||||||
package unix;
|
package unix
|
||||||
|
|
||||||
import "core:c"
|
import "core:c"
|
||||||
|
|
||||||
// NOTE(tetra): No 32-bit Macs.
|
// NOTE(tetra): No 32-bit Macs.
|
||||||
// Source: _pthread_types.h on my Mac.
|
// Source: _pthread_types.h on my Mac.
|
||||||
PTHREAD_SIZE :: 8176;
|
PTHREAD_SIZE :: 8176
|
||||||
PTHREAD_ATTR_SIZE :: 56;
|
PTHREAD_ATTR_SIZE :: 56
|
||||||
PTHREAD_MUTEXATTR_SIZE :: 8;
|
PTHREAD_MUTEXATTR_SIZE :: 8
|
||||||
PTHREAD_MUTEX_SIZE :: 56;
|
PTHREAD_MUTEX_SIZE :: 56
|
||||||
PTHREAD_CONDATTR_SIZE :: 8;
|
PTHREAD_CONDATTR_SIZE :: 8
|
||||||
PTHREAD_COND_SIZE :: 40;
|
PTHREAD_COND_SIZE :: 40
|
||||||
PTHREAD_ONCE_SIZE :: 8;
|
PTHREAD_ONCE_SIZE :: 8
|
||||||
PTHREAD_RWLOCK_SIZE :: 192;
|
PTHREAD_RWLOCK_SIZE :: 192
|
||||||
PTHREAD_RWLOCKATTR_SIZE :: 16;
|
PTHREAD_RWLOCKATTR_SIZE :: 16
|
||||||
|
|
||||||
pthread_t :: distinct u64;
|
pthread_t :: distinct u64
|
||||||
|
|
||||||
pthread_attr_t :: struct #align 16 {
|
pthread_attr_t :: struct #align 16 {
|
||||||
sig: c.long,
|
sig: c.long,
|
||||||
_: [PTHREAD_ATTR_SIZE] c.char,
|
_: [PTHREAD_ATTR_SIZE] c.char,
|
||||||
};
|
}
|
||||||
|
|
||||||
pthread_cond_t :: struct #align 16 {
|
pthread_cond_t :: struct #align 16 {
|
||||||
sig: c.long,
|
sig: c.long,
|
||||||
_: [PTHREAD_COND_SIZE] c.char,
|
_: [PTHREAD_COND_SIZE] c.char,
|
||||||
};
|
}
|
||||||
|
|
||||||
pthread_condattr_t :: struct #align 16 {
|
pthread_condattr_t :: struct #align 16 {
|
||||||
sig: c.long,
|
sig: c.long,
|
||||||
_: [PTHREAD_CONDATTR_SIZE] c.char,
|
_: [PTHREAD_CONDATTR_SIZE] c.char,
|
||||||
};
|
}
|
||||||
|
|
||||||
pthread_mutex_t :: struct #align 16 {
|
pthread_mutex_t :: struct #align 16 {
|
||||||
sig: c.long,
|
sig: c.long,
|
||||||
_: [PTHREAD_MUTEX_SIZE] c.char,
|
_: [PTHREAD_MUTEX_SIZE] c.char,
|
||||||
};
|
}
|
||||||
|
|
||||||
pthread_mutexattr_t :: struct #align 16 {
|
pthread_mutexattr_t :: struct #align 16 {
|
||||||
sig: c.long,
|
sig: c.long,
|
||||||
_: [PTHREAD_MUTEXATTR_SIZE] c.char,
|
_: [PTHREAD_MUTEXATTR_SIZE] c.char,
|
||||||
};
|
}
|
||||||
|
|
||||||
pthread_once_t :: struct #align 16 {
|
pthread_once_t :: struct #align 16 {
|
||||||
sig: c.long,
|
sig: c.long,
|
||||||
_: [PTHREAD_ONCE_SIZE] c.char,
|
_: [PTHREAD_ONCE_SIZE] c.char,
|
||||||
};
|
}
|
||||||
|
|
||||||
pthread_rwlock_t :: struct #align 16 {
|
pthread_rwlock_t :: struct #align 16 {
|
||||||
sig: c.long,
|
sig: c.long,
|
||||||
_: [PTHREAD_RWLOCK_SIZE] c.char,
|
_: [PTHREAD_RWLOCK_SIZE] c.char,
|
||||||
};
|
}
|
||||||
|
|
||||||
pthread_rwlockattr_t :: struct #align 16 {
|
pthread_rwlockattr_t :: struct #align 16 {
|
||||||
sig: c.long,
|
sig: c.long,
|
||||||
_: [PTHREAD_RWLOCKATTR_SIZE] c.char,
|
_: [PTHREAD_RWLOCKATTR_SIZE] c.char,
|
||||||
};
|
}
|
||||||
|
|
||||||
SCHED_OTHER :: 1; // Avoid if you are writing portable software.
|
SCHED_OTHER :: 1 // Avoid if you are writing portable software.
|
||||||
SCHED_FIFO :: 4;
|
SCHED_FIFO :: 4
|
||||||
SCHED_RR :: 2; // Round robin.
|
SCHED_RR :: 2 // Round robin.
|
||||||
|
|
||||||
SCHED_PARAM_SIZE :: 4;
|
SCHED_PARAM_SIZE :: 4
|
||||||
|
|
||||||
sched_param :: struct {
|
sched_param :: struct {
|
||||||
sched_priority: c.int,
|
sched_priority: c.int,
|
||||||
_: [SCHED_PARAM_SIZE] c.char,
|
_: [SCHED_PARAM_SIZE] c.char,
|
||||||
};
|
}
|
||||||
|
|
||||||
// Source: https://github.com/apple/darwin-libpthread/blob/03c4628c8940cca6fd6a82957f683af804f62e7f/pthread/pthread.h#L138
|
// Source: https://github.com/apple/darwin-libpthread/blob/03c4628c8940cca6fd6a82957f683af804f62e7f/pthread/pthread.h#L138
|
||||||
PTHREAD_CREATE_JOINABLE :: 1;
|
PTHREAD_CREATE_JOINABLE :: 1
|
||||||
PTHREAD_CREATE_DETACHED :: 2;
|
PTHREAD_CREATE_DETACHED :: 2
|
||||||
PTHREAD_INHERIT_SCHED :: 1;
|
PTHREAD_INHERIT_SCHED :: 1
|
||||||
PTHREAD_EXPLICIT_SCHED :: 2;
|
PTHREAD_EXPLICIT_SCHED :: 2
|
||||||
PTHREAD_PROCESS_SHARED :: 1;
|
PTHREAD_PROCESS_SHARED :: 1
|
||||||
PTHREAD_PROCESS_PRIVATE :: 2;
|
PTHREAD_PROCESS_PRIVATE :: 2
|
||||||
|
|
||||||
|
|
||||||
PTHREAD_MUTEX_NORMAL :: 0;
|
PTHREAD_MUTEX_NORMAL :: 0
|
||||||
PTHREAD_MUTEX_RECURSIVE :: 1;
|
PTHREAD_MUTEX_RECURSIVE :: 1
|
||||||
PTHREAD_MUTEX_ERRORCHECK :: 2;
|
PTHREAD_MUTEX_ERRORCHECK :: 2
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package unix;
|
package unix
|
||||||
|
|
||||||
import "core:c"
|
import "core:c"
|
||||||
|
|
||||||
@@ -6,78 +6,78 @@ import "core:c"
|
|||||||
// I cannot currently do this.
|
// I cannot currently do this.
|
||||||
// And at the time of writing there is a bug with putting it
|
// And at the time of writing there is a bug with putting it
|
||||||
// as the only field in a struct.
|
// as the only field in a struct.
|
||||||
pthread_t :: distinct u64;
|
pthread_t :: distinct u64
|
||||||
// pthread_t :: struct #align 16 { x: u64 };
|
// pthread_t :: struct #align 16 { x: u64 };
|
||||||
|
|
||||||
// NOTE(tetra): Got all the size constants from pthreadtypes-arch.h on my
|
// NOTE(tetra): Got all the size constants from pthreadtypes-arch.h on my
|
||||||
// Linux machine.
|
// Linux machine.
|
||||||
|
|
||||||
PTHREAD_COND_T_SIZE :: 48;
|
PTHREAD_COND_T_SIZE :: 48
|
||||||
|
|
||||||
PTHREAD_MUTEXATTR_T_SIZE :: 4;
|
PTHREAD_MUTEXATTR_T_SIZE :: 4
|
||||||
PTHREAD_CONDATTR_T_SIZE :: 4;
|
PTHREAD_CONDATTR_T_SIZE :: 4
|
||||||
PTHREAD_RWLOCKATTR_T_SIZE :: 8;
|
PTHREAD_RWLOCKATTR_T_SIZE :: 8
|
||||||
PTHREAD_BARRIERATTR_T_SIZE :: 4;
|
PTHREAD_BARRIERATTR_T_SIZE :: 4
|
||||||
|
|
||||||
// WARNING: The sizes of these things are different yet again
|
// WARNING: The sizes of these things are different yet again
|
||||||
// on non-X86!
|
// on non-X86!
|
||||||
when size_of(int) == 8 {
|
when size_of(int) == 8 {
|
||||||
PTHREAD_ATTR_T_SIZE :: 56;
|
PTHREAD_ATTR_T_SIZE :: 56
|
||||||
PTHREAD_MUTEX_T_SIZE :: 40;
|
PTHREAD_MUTEX_T_SIZE :: 40
|
||||||
PTHREAD_RWLOCK_T_SIZE :: 56;
|
PTHREAD_RWLOCK_T_SIZE :: 56
|
||||||
PTHREAD_BARRIER_T_SIZE :: 32;
|
PTHREAD_BARRIER_T_SIZE :: 32
|
||||||
} else when size_of(int) == 4 {
|
} else when size_of(int) == 4 {
|
||||||
PTHREAD_ATTR_T_SIZE :: 32;
|
PTHREAD_ATTR_T_SIZE :: 32
|
||||||
PTHREAD_MUTEX_T_SIZE :: 32;
|
PTHREAD_MUTEX_T_SIZE :: 32
|
||||||
PTHREAD_RWLOCK_T_SIZE :: 44;
|
PTHREAD_RWLOCK_T_SIZE :: 44
|
||||||
PTHREAD_BARRIER_T_SIZE :: 20;
|
PTHREAD_BARRIER_T_SIZE :: 20
|
||||||
}
|
}
|
||||||
|
|
||||||
pthread_cond_t :: struct #align 16 {
|
pthread_cond_t :: struct #align 16 {
|
||||||
_: [PTHREAD_COND_T_SIZE] c.char,
|
_: [PTHREAD_COND_T_SIZE] c.char,
|
||||||
};
|
}
|
||||||
pthread_mutex_t :: struct #align 16 {
|
pthread_mutex_t :: struct #align 16 {
|
||||||
_: [PTHREAD_MUTEX_T_SIZE] c.char,
|
_: [PTHREAD_MUTEX_T_SIZE] c.char,
|
||||||
};
|
}
|
||||||
pthread_rwlock_t :: struct #align 16 {
|
pthread_rwlock_t :: struct #align 16 {
|
||||||
_: [PTHREAD_RWLOCK_T_SIZE] c.char,
|
_: [PTHREAD_RWLOCK_T_SIZE] c.char,
|
||||||
};
|
}
|
||||||
pthread_barrier_t :: struct #align 16 {
|
pthread_barrier_t :: struct #align 16 {
|
||||||
_: [PTHREAD_BARRIER_T_SIZE] c.char,
|
_: [PTHREAD_BARRIER_T_SIZE] c.char,
|
||||||
};
|
}
|
||||||
|
|
||||||
pthread_attr_t :: struct #align 16 {
|
pthread_attr_t :: struct #align 16 {
|
||||||
_: [PTHREAD_ATTR_T_SIZE] c.char,
|
_: [PTHREAD_ATTR_T_SIZE] c.char,
|
||||||
};
|
}
|
||||||
pthread_condattr_t :: struct #align 16 {
|
pthread_condattr_t :: struct #align 16 {
|
||||||
_: [PTHREAD_CONDATTR_T_SIZE] c.char,
|
_: [PTHREAD_CONDATTR_T_SIZE] c.char,
|
||||||
};
|
}
|
||||||
pthread_mutexattr_t :: struct #align 16 {
|
pthread_mutexattr_t :: struct #align 16 {
|
||||||
_: [PTHREAD_MUTEXATTR_T_SIZE] c.char,
|
_: [PTHREAD_MUTEXATTR_T_SIZE] c.char,
|
||||||
};
|
}
|
||||||
pthread_rwlockattr_t :: struct #align 16 {
|
pthread_rwlockattr_t :: struct #align 16 {
|
||||||
_: [PTHREAD_RWLOCKATTR_T_SIZE] c.char,
|
_: [PTHREAD_RWLOCKATTR_T_SIZE] c.char,
|
||||||
};
|
}
|
||||||
pthread_barrierattr_t :: struct #align 16 {
|
pthread_barrierattr_t :: struct #align 16 {
|
||||||
_: [PTHREAD_BARRIERATTR_T_SIZE] c.char,
|
_: [PTHREAD_BARRIERATTR_T_SIZE] c.char,
|
||||||
};
|
}
|
||||||
|
|
||||||
PTHREAD_MUTEX_NORMAL :: 0;
|
PTHREAD_MUTEX_NORMAL :: 0
|
||||||
PTHREAD_MUTEX_RECURSIVE :: 1;
|
PTHREAD_MUTEX_RECURSIVE :: 1
|
||||||
PTHREAD_MUTEX_ERRORCHECK :: 2;
|
PTHREAD_MUTEX_ERRORCHECK :: 2
|
||||||
|
|
||||||
|
|
||||||
// TODO(tetra, 2019-11-01): Maybe make `enum c.int`s for these?
|
// TODO(tetra, 2019-11-01): Maybe make `enum c.int`s for these?
|
||||||
PTHREAD_CREATE_JOINABLE :: 0;
|
PTHREAD_CREATE_JOINABLE :: 0
|
||||||
PTHREAD_CREATE_DETACHED :: 1;
|
PTHREAD_CREATE_DETACHED :: 1
|
||||||
PTHREAD_INHERIT_SCHED :: 0;
|
PTHREAD_INHERIT_SCHED :: 0
|
||||||
PTHREAD_EXPLICIT_SCHED :: 1;
|
PTHREAD_EXPLICIT_SCHED :: 1
|
||||||
PTHREAD_PROCESS_PRIVATE :: 0;
|
PTHREAD_PROCESS_PRIVATE :: 0
|
||||||
PTHREAD_PROCESS_SHARED :: 1;
|
PTHREAD_PROCESS_SHARED :: 1
|
||||||
|
|
||||||
SCHED_OTHER :: 0;
|
SCHED_OTHER :: 0
|
||||||
SCHED_FIFO :: 1;
|
SCHED_FIFO :: 1
|
||||||
SCHED_RR :: 2; // Round robin.
|
SCHED_RR :: 2 // Round robin.
|
||||||
|
|
||||||
sched_param :: struct {
|
sched_param :: struct {
|
||||||
sched_priority: c.int,
|
sched_priority: c.int,
|
||||||
@@ -88,9 +88,9 @@ sem_t :: struct #align 16 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
when size_of(int) == 8 {
|
when size_of(int) == 8 {
|
||||||
SEM_T_SIZE :: 32;
|
SEM_T_SIZE :: 32
|
||||||
} else when size_of(int) == 4 {
|
} else when size_of(int) == 4 {
|
||||||
SEM_T_SIZE :: 16;
|
SEM_T_SIZE :: 16
|
||||||
}
|
}
|
||||||
|
|
||||||
foreign import "system:pthread"
|
foreign import "system:pthread"
|
||||||
@@ -99,16 +99,16 @@ foreign import "system:pthread"
|
|||||||
foreign pthread {
|
foreign pthread {
|
||||||
// create named semaphore.
|
// create named semaphore.
|
||||||
// used in process-shared semaphores.
|
// used in process-shared semaphores.
|
||||||
sem_open :: proc(name: cstring, flags: c.int) -> ^sem_t ---;
|
sem_open :: proc(name: cstring, flags: c.int) -> ^sem_t ---
|
||||||
|
|
||||||
sem_init :: proc(sem: ^sem_t, pshared: c.int, initial_value: c.uint) -> c.int ---;
|
sem_init :: proc(sem: ^sem_t, pshared: c.int, initial_value: c.uint) -> c.int ---
|
||||||
sem_destroy :: proc(sem: ^sem_t) -> c.int ---;
|
sem_destroy :: proc(sem: ^sem_t) -> c.int ---
|
||||||
sem_post :: proc(sem: ^sem_t) -> c.int ---;
|
sem_post :: proc(sem: ^sem_t) -> c.int ---
|
||||||
sem_wait :: proc(sem: ^sem_t) -> c.int ---;
|
sem_wait :: proc(sem: ^sem_t) -> c.int ---
|
||||||
sem_trywait :: proc(sem: ^sem_t) -> c.int ---;
|
sem_trywait :: proc(sem: ^sem_t) -> c.int ---
|
||||||
// sem_timedwait :: proc(sem: ^sem_t, timeout: time.TimeSpec) -> c.int ---;
|
// sem_timedwait :: proc(sem: ^sem_t, timeout: time.TimeSpec) -> c.int ---;
|
||||||
|
|
||||||
// NOTE: unclear whether pthread_yield is well-supported on Linux systems,
|
// NOTE: unclear whether pthread_yield is well-supported on Linux systems,
|
||||||
// see https://linux.die.net/man/3/pthread_yield
|
// see https://linux.die.net/man/3/pthread_yield
|
||||||
pthread_yield :: proc() -> c.int ---;
|
pthread_yield :: proc() -> c.int ---
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package unix;
|
package unix
|
||||||
|
|
||||||
foreign import "system:pthread"
|
foreign import "system:pthread"
|
||||||
|
|
||||||
@@ -11,34 +11,34 @@ import "core:time"
|
|||||||
|
|
||||||
@(default_calling_convention="c")
|
@(default_calling_convention="c")
|
||||||
foreign pthread {
|
foreign pthread {
|
||||||
pthread_create :: proc(t: ^pthread_t, attrs: ^pthread_attr_t, routine: proc(data: rawptr) -> rawptr, arg: rawptr) -> c.int ---;
|
pthread_create :: proc(t: ^pthread_t, attrs: ^pthread_attr_t, routine: proc(data: rawptr) -> rawptr, arg: rawptr) -> c.int ---
|
||||||
|
|
||||||
// retval is a pointer to a location to put the return value of the thread proc.
|
// retval is a pointer to a location to put the return value of the thread proc.
|
||||||
pthread_join :: proc(t: pthread_t, retval: ^rawptr) -> c.int ---;
|
pthread_join :: proc(t: pthread_t, retval: ^rawptr) -> c.int ---
|
||||||
|
|
||||||
pthread_self :: proc() -> pthread_t ---;
|
pthread_self :: proc() -> pthread_t ---
|
||||||
|
|
||||||
pthread_equal :: proc(a, b: pthread_t) -> b32 ---;
|
pthread_equal :: proc(a, b: pthread_t) -> b32 ---
|
||||||
|
|
||||||
sched_get_priority_min :: proc(policy: c.int) -> c.int ---;
|
sched_get_priority_min :: proc(policy: c.int) -> c.int ---
|
||||||
sched_get_priority_max :: proc(policy: c.int) -> c.int ---;
|
sched_get_priority_max :: proc(policy: c.int) -> c.int ---
|
||||||
|
|
||||||
// NOTE: POSIX says this can fail with OOM.
|
// NOTE: POSIX says this can fail with OOM.
|
||||||
pthread_attr_init :: proc(attrs: ^pthread_attr_t) -> c.int ---;
|
pthread_attr_init :: proc(attrs: ^pthread_attr_t) -> c.int ---
|
||||||
|
|
||||||
pthread_attr_destroy :: proc(attrs: ^pthread_attr_t) -> c.int ---;
|
pthread_attr_destroy :: proc(attrs: ^pthread_attr_t) -> c.int ---
|
||||||
|
|
||||||
pthread_attr_getschedparam :: proc(attrs: ^pthread_attr_t, param: ^sched_param) -> c.int ---;
|
pthread_attr_getschedparam :: proc(attrs: ^pthread_attr_t, param: ^sched_param) -> c.int ---
|
||||||
pthread_attr_setschedparam :: proc(attrs: ^pthread_attr_t, param: ^sched_param) -> c.int ---;
|
pthread_attr_setschedparam :: proc(attrs: ^pthread_attr_t, param: ^sched_param) -> c.int ---
|
||||||
|
|
||||||
pthread_attr_getschedpolicy :: proc(t: ^pthread_attr_t, policy: ^c.int) -> c.int ---;
|
pthread_attr_getschedpolicy :: proc(t: ^pthread_attr_t, policy: ^c.int) -> c.int ---
|
||||||
pthread_attr_setschedpolicy :: proc(t: ^pthread_attr_t, policy: c.int) -> c.int ---;
|
pthread_attr_setschedpolicy :: proc(t: ^pthread_attr_t, policy: c.int) -> c.int ---
|
||||||
|
|
||||||
// states: PTHREAD_CREATE_DETACHED, PTHREAD_CREATE_JOINABLE
|
// states: PTHREAD_CREATE_DETACHED, PTHREAD_CREATE_JOINABLE
|
||||||
pthread_attr_setdetachstate :: proc(attrs: ^pthread_attr_t, detach_state: c.int) -> c.int ---;
|
pthread_attr_setdetachstate :: proc(attrs: ^pthread_attr_t, detach_state: c.int) -> c.int ---
|
||||||
|
|
||||||
// scheds: PTHREAD_INHERIT_SCHED, PTHREAD_EXPLICIT_SCHED
|
// scheds: PTHREAD_INHERIT_SCHED, PTHREAD_EXPLICIT_SCHED
|
||||||
pthread_attr_setinheritsched :: proc(attrs: ^pthread_attr_t, sched: c.int) -> c.int ---;
|
pthread_attr_setinheritsched :: proc(attrs: ^pthread_attr_t, sched: c.int) -> c.int ---
|
||||||
|
|
||||||
// NOTE(tetra, 2019-11-06): WARNING: Different systems have different alignment requirements.
|
// NOTE(tetra, 2019-11-06): WARNING: Different systems have different alignment requirements.
|
||||||
// For maximum usefulness, use the OS's page size.
|
// For maximum usefulness, use the OS's page size.
|
||||||
@@ -49,63 +49,63 @@ foreign pthread {
|
|||||||
// guard pages. If you are using this procedure, YOU must set them up manually.
|
// guard pages. If you are using this procedure, YOU must set them up manually.
|
||||||
// If you forget to do this, you WILL get stack corruption bugs if you do not EXTREMELY
|
// If you forget to do this, you WILL get stack corruption bugs if you do not EXTREMELY
|
||||||
// know what you are doing!
|
// know what you are doing!
|
||||||
pthread_attr_setstack :: proc(attrs: ^pthread_attr_t, stack_ptr: rawptr, stack_size: u64) -> c.int ---;
|
pthread_attr_setstack :: proc(attrs: ^pthread_attr_t, stack_ptr: rawptr, stack_size: u64) -> c.int ---
|
||||||
pthread_attr_getstack :: proc(attrs: ^pthread_attr_t, stack_ptr: ^rawptr, stack_size: ^u64) -> c.int ---;
|
pthread_attr_getstack :: proc(attrs: ^pthread_attr_t, stack_ptr: ^rawptr, stack_size: ^u64) -> c.int ---
|
||||||
|
|
||||||
sched_yield :: proc() -> c.int ---;
|
sched_yield :: proc() -> c.int ---
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@(default_calling_convention="c")
|
@(default_calling_convention="c")
|
||||||
foreign pthread {
|
foreign pthread {
|
||||||
// NOTE: POSIX says this can fail with OOM.
|
// NOTE: POSIX says this can fail with OOM.
|
||||||
pthread_cond_init :: proc(cond: ^pthread_cond_t, attrs: ^pthread_condattr_t) -> c.int ---;
|
pthread_cond_init :: proc(cond: ^pthread_cond_t, attrs: ^pthread_condattr_t) -> c.int ---
|
||||||
|
|
||||||
pthread_cond_destroy :: proc(cond: ^pthread_cond_t) -> c.int ---;
|
pthread_cond_destroy :: proc(cond: ^pthread_cond_t) -> c.int ---
|
||||||
|
|
||||||
pthread_cond_signal :: proc(cond: ^pthread_cond_t) -> c.int ---;
|
pthread_cond_signal :: proc(cond: ^pthread_cond_t) -> c.int ---
|
||||||
|
|
||||||
// same as signal, but wakes up _all_ threads that are waiting
|
// same as signal, but wakes up _all_ threads that are waiting
|
||||||
pthread_cond_broadcast :: proc(cond: ^pthread_cond_t) -> c.int ---;
|
pthread_cond_broadcast :: proc(cond: ^pthread_cond_t) -> c.int ---
|
||||||
|
|
||||||
|
|
||||||
// assumes the mutex is pre-locked
|
// assumes the mutex is pre-locked
|
||||||
pthread_cond_wait :: proc(cond: ^pthread_cond_t, mutex: ^pthread_mutex_t) -> c.int ---;
|
pthread_cond_wait :: proc(cond: ^pthread_cond_t, mutex: ^pthread_mutex_t) -> c.int ---
|
||||||
pthread_cond_timedwait :: proc(cond: ^pthread_cond_t, mutex: ^pthread_mutex_t, timeout: ^time.TimeSpec) -> c.int ---;
|
pthread_cond_timedwait :: proc(cond: ^pthread_cond_t, mutex: ^pthread_mutex_t, timeout: ^time.TimeSpec) -> c.int ---
|
||||||
|
|
||||||
pthread_condattr_init :: proc(attrs: ^pthread_condattr_t) -> c.int ---;
|
pthread_condattr_init :: proc(attrs: ^pthread_condattr_t) -> c.int ---
|
||||||
pthread_condattr_destroy :: proc(attrs: ^pthread_condattr_t) -> c.int ---;
|
pthread_condattr_destroy :: proc(attrs: ^pthread_condattr_t) -> c.int ---
|
||||||
|
|
||||||
// p-shared = "process-shared" - i.e: is this condition shared among multiple processes?
|
// p-shared = "process-shared" - i.e: is this condition shared among multiple processes?
|
||||||
// values: PTHREAD_PROCESS_PRIVATE, PTHREAD_PROCESS_SHARED
|
// values: PTHREAD_PROCESS_PRIVATE, PTHREAD_PROCESS_SHARED
|
||||||
pthread_condattr_setpshared :: proc(attrs: ^pthread_condattr_t, value: c.int) -> c.int ---;
|
pthread_condattr_setpshared :: proc(attrs: ^pthread_condattr_t, value: c.int) -> c.int ---
|
||||||
pthread_condattr_getpshared :: proc(attrs: ^pthread_condattr_t, result: ^c.int) -> c.int ---;
|
pthread_condattr_getpshared :: proc(attrs: ^pthread_condattr_t, result: ^c.int) -> c.int ---
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@(default_calling_convention="c")
|
@(default_calling_convention="c")
|
||||||
foreign pthread {
|
foreign pthread {
|
||||||
// NOTE: POSIX says this can fail with OOM.
|
// NOTE: POSIX says this can fail with OOM.
|
||||||
pthread_mutex_init :: proc(mutex: ^pthread_mutex_t, attrs: ^pthread_mutexattr_t) -> c.int ---;
|
pthread_mutex_init :: proc(mutex: ^pthread_mutex_t, attrs: ^pthread_mutexattr_t) -> c.int ---
|
||||||
|
|
||||||
pthread_mutex_destroy :: proc(mutex: ^pthread_mutex_t) -> c.int ---;
|
pthread_mutex_destroy :: proc(mutex: ^pthread_mutex_t) -> c.int ---
|
||||||
|
|
||||||
pthread_mutex_trylock :: proc(mutex: ^pthread_mutex_t) -> c.int ---;
|
pthread_mutex_trylock :: proc(mutex: ^pthread_mutex_t) -> c.int ---
|
||||||
|
|
||||||
pthread_mutex_lock :: proc(mutex: ^pthread_mutex_t) -> c.int ---;
|
pthread_mutex_lock :: proc(mutex: ^pthread_mutex_t) -> c.int ---
|
||||||
|
|
||||||
pthread_mutex_timedlock :: proc(mutex: ^pthread_mutex_t, timeout: ^time.TimeSpec) -> c.int ---;
|
pthread_mutex_timedlock :: proc(mutex: ^pthread_mutex_t, timeout: ^time.TimeSpec) -> c.int ---
|
||||||
|
|
||||||
pthread_mutex_unlock :: proc(mutex: ^pthread_mutex_t) -> c.int ---;
|
pthread_mutex_unlock :: proc(mutex: ^pthread_mutex_t) -> c.int ---
|
||||||
|
|
||||||
|
|
||||||
pthread_mutexattr_init :: proc(attrs: ^pthread_mutexattr_t) -> c.int ---;
|
pthread_mutexattr_init :: proc(attrs: ^pthread_mutexattr_t) -> c.int ---
|
||||||
pthread_mutexattr_destroy :: proc(attrs: ^pthread_mutexattr_t) -> c.int ---;
|
pthread_mutexattr_destroy :: proc(attrs: ^pthread_mutexattr_t) -> c.int ---
|
||||||
pthread_mutexattr_settype :: proc(attrs: ^pthread_mutexattr_t, type: c.int) -> c.int ---;
|
pthread_mutexattr_settype :: proc(attrs: ^pthread_mutexattr_t, type: c.int) -> c.int ---
|
||||||
|
|
||||||
// p-shared = "process-shared" - i.e: is this mutex shared among multiple processes?
|
// p-shared = "process-shared" - i.e: is this mutex shared among multiple processes?
|
||||||
// values: PTHREAD_PROCESS_PRIVATE, PTHREAD_PROCESS_SHARED
|
// values: PTHREAD_PROCESS_PRIVATE, PTHREAD_PROCESS_SHARED
|
||||||
pthread_mutexattr_setpshared :: proc(attrs: ^pthread_mutexattr_t, value: c.int) -> c.int ---;
|
pthread_mutexattr_setpshared :: proc(attrs: ^pthread_mutexattr_t, value: c.int) -> c.int ---
|
||||||
pthread_mutexattr_getpshared :: proc(attrs: ^pthread_mutexattr_t, result: ^c.int) -> c.int ---;
|
pthread_mutexattr_getpshared :: proc(attrs: ^pthread_mutexattr_t, result: ^c.int) -> c.int ---
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,87 +38,87 @@ Thread_Os_Specific :: struct #align 16 {
|
|||||||
//
|
//
|
||||||
_create :: proc(procedure: Thread_Proc, priority := Thread_Priority.Normal) -> ^Thread {
|
_create :: proc(procedure: Thread_Proc, priority := Thread_Priority.Normal) -> ^Thread {
|
||||||
__linux_thread_entry_proc :: proc "c" (t: rawptr) -> rawptr {
|
__linux_thread_entry_proc :: proc "c" (t: rawptr) -> rawptr {
|
||||||
context = runtime.default_context();
|
context = runtime.default_context()
|
||||||
t := (^Thread)(t);
|
t := (^Thread)(t)
|
||||||
sync.condition_wait_for(&t.start_gate);
|
sync.condition_wait_for(&t.start_gate)
|
||||||
sync.condition_destroy(&t.start_gate);
|
sync.condition_destroy(&t.start_gate)
|
||||||
sync.mutex_destroy(&t.start_mutex);
|
sync.mutex_destroy(&t.start_mutex)
|
||||||
t.start_gate = {};
|
t.start_gate = {}
|
||||||
t.start_mutex = {};
|
t.start_mutex = {}
|
||||||
|
|
||||||
context = t.init_context.? or_else runtime.default_context();
|
context = t.init_context.? or_else runtime.default_context()
|
||||||
|
|
||||||
t.procedure(t);
|
t.procedure(t)
|
||||||
|
|
||||||
if t.init_context == nil {
|
if t.init_context == nil {
|
||||||
if context.temp_allocator.data == &runtime.global_default_temp_allocator_data {
|
if context.temp_allocator.data == &runtime.global_default_temp_allocator_data {
|
||||||
runtime.default_temp_allocator_destroy(auto_cast context.temp_allocator.data);
|
runtime.default_temp_allocator_destroy(auto_cast context.temp_allocator.data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
intrinsics.atomic_store(&t.done, true);
|
intrinsics.atomic_store(&t.done, true)
|
||||||
return nil;
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
attrs: unix.pthread_attr_t;
|
attrs: unix.pthread_attr_t
|
||||||
if unix.pthread_attr_init(&attrs) != 0 {
|
if unix.pthread_attr_init(&attrs) != 0 {
|
||||||
return nil; // NOTE(tetra, 2019-11-01): POSIX OOM.
|
return nil // NOTE(tetra, 2019-11-01): POSIX OOM.
|
||||||
}
|
}
|
||||||
defer unix.pthread_attr_destroy(&attrs);
|
defer unix.pthread_attr_destroy(&attrs)
|
||||||
|
|
||||||
// NOTE(tetra, 2019-11-01): These only fail if their argument is invalid.
|
// NOTE(tetra, 2019-11-01): These only fail if their argument is invalid.
|
||||||
assert(unix.pthread_attr_setdetachstate(&attrs, unix.PTHREAD_CREATE_JOINABLE) == 0);
|
assert(unix.pthread_attr_setdetachstate(&attrs, unix.PTHREAD_CREATE_JOINABLE) == 0)
|
||||||
assert(unix.pthread_attr_setinheritsched(&attrs, unix.PTHREAD_EXPLICIT_SCHED) == 0);
|
assert(unix.pthread_attr_setinheritsched(&attrs, unix.PTHREAD_EXPLICIT_SCHED) == 0)
|
||||||
|
|
||||||
thread := new(Thread);
|
thread := new(Thread)
|
||||||
if thread == nil {
|
if thread == nil {
|
||||||
return nil;
|
return nil
|
||||||
}
|
}
|
||||||
thread.creation_allocator = context.allocator;
|
thread.creation_allocator = context.allocator
|
||||||
|
|
||||||
// Set thread priority.
|
// Set thread priority.
|
||||||
policy: i32;
|
policy: i32
|
||||||
res := unix.pthread_attr_getschedpolicy(&attrs, &policy);
|
res := unix.pthread_attr_getschedpolicy(&attrs, &policy)
|
||||||
assert(res == 0);
|
assert(res == 0)
|
||||||
params: unix.sched_param;
|
params: unix.sched_param
|
||||||
res = unix.pthread_attr_getschedparam(&attrs, ¶ms);
|
res = unix.pthread_attr_getschedparam(&attrs, ¶ms)
|
||||||
assert(res == 0);
|
assert(res == 0)
|
||||||
low := unix.sched_get_priority_min(policy);
|
low := unix.sched_get_priority_min(policy)
|
||||||
high := unix.sched_get_priority_max(policy);
|
high := unix.sched_get_priority_max(policy)
|
||||||
switch priority {
|
switch priority {
|
||||||
case .Normal: // Okay
|
case .Normal: // Okay
|
||||||
case .Low: params.sched_priority = low + 1;
|
case .Low: params.sched_priority = low + 1
|
||||||
case .High: params.sched_priority = high;
|
case .High: params.sched_priority = high
|
||||||
}
|
}
|
||||||
res = unix.pthread_attr_setschedparam(&attrs, ¶ms);
|
res = unix.pthread_attr_setschedparam(&attrs, ¶ms)
|
||||||
assert(res == 0);
|
assert(res == 0)
|
||||||
|
|
||||||
if unix.pthread_create(&thread.unix_thread, &attrs, __linux_thread_entry_proc, thread) != 0 {
|
if unix.pthread_create(&thread.unix_thread, &attrs, __linux_thread_entry_proc, thread) != 0 {
|
||||||
free(thread, thread.creation_allocator);
|
free(thread, thread.creation_allocator)
|
||||||
return nil;
|
return nil
|
||||||
}
|
}
|
||||||
thread.procedure = procedure;
|
thread.procedure = procedure
|
||||||
|
|
||||||
sync.mutex_init(&thread.start_mutex);
|
sync.mutex_init(&thread.start_mutex)
|
||||||
sync.condition_init(&thread.start_gate, &thread.start_mutex);
|
sync.condition_init(&thread.start_gate, &thread.start_mutex)
|
||||||
|
|
||||||
return thread;
|
return thread
|
||||||
}
|
}
|
||||||
|
|
||||||
_start :: proc(t: ^Thread) {
|
_start :: proc(t: ^Thread) {
|
||||||
if intrinsics.atomic_xchg(&t.started, true) {
|
if intrinsics.atomic_xchg(&t.started, true) {
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
sync.condition_signal(&t.start_gate);
|
sync.condition_signal(&t.start_gate)
|
||||||
}
|
}
|
||||||
|
|
||||||
_is_done :: proc(t: ^Thread) -> bool {
|
_is_done :: proc(t: ^Thread) -> bool {
|
||||||
return intrinsics.atomic_load(&t.done);
|
return intrinsics.atomic_load(&t.done)
|
||||||
}
|
}
|
||||||
|
|
||||||
_join :: proc(t: ^Thread) {
|
_join :: proc(t: ^Thread) {
|
||||||
if unix.pthread_equal(unix.pthread_self(), t.unix_thread) {
|
if unix.pthread_equal(unix.pthread_self(), t.unix_thread) {
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
// if unix.pthread_self().x == t.unix_thread.x do return;
|
// if unix.pthread_self().x == t.unix_thread.x do return;
|
||||||
|
|
||||||
@@ -131,9 +131,9 @@ _join :: proc(t: ^Thread) {
|
|||||||
if intrinsics.atomic_xchg(&t.already_joined, true) {
|
if intrinsics.atomic_xchg(&t.already_joined, true) {
|
||||||
for {
|
for {
|
||||||
if intrinsics.atomic_load(&t.done) {
|
if intrinsics.atomic_load(&t.done) {
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
intrinsics.cpu_relax();
|
intrinsics.cpu_relax()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -143,29 +143,29 @@ _join :: proc(t: ^Thread) {
|
|||||||
// that you may join a different thread from the one you called join on,
|
// that you may join a different thread from the one you called join on,
|
||||||
// if the thread handle is reused.
|
// if the thread handle is reused.
|
||||||
if intrinsics.atomic_load(&t.done) {
|
if intrinsics.atomic_load(&t.done) {
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ret_val: rawptr;
|
ret_val: rawptr
|
||||||
_ = unix.pthread_join(t.unix_thread, &ret_val);
|
_ = unix.pthread_join(t.unix_thread, &ret_val)
|
||||||
if !intrinsics.atomic_load(&t.done) {
|
if !intrinsics.atomic_load(&t.done) {
|
||||||
panic("thread not done after join");
|
panic("thread not done after join")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_join_multiple :: proc(threads: ..^Thread) {
|
_join_multiple :: proc(threads: ..^Thread) {
|
||||||
for t in threads {
|
for t in threads {
|
||||||
_join(t);
|
_join(t)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
_destroy :: proc(t: ^Thread) {
|
_destroy :: proc(t: ^Thread) {
|
||||||
_join(t);
|
_join(t)
|
||||||
sync.condition_destroy(&t.start_gate);
|
sync.condition_destroy(&t.start_gate)
|
||||||
sync.mutex_destroy(&t.start_mutex);
|
sync.mutex_destroy(&t.start_mutex)
|
||||||
t.unix_thread = {};
|
t.unix_thread = {}
|
||||||
free(t, t.creation_allocator);
|
free(t, t.creation_allocator)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -174,5 +174,5 @@ _terminate :: proc(t: ^Thread, exit_code: int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_yield :: proc() {
|
_yield :: proc() {
|
||||||
unix.sched_yield();
|
unix.sched_yield()
|
||||||
}
|
}
|
||||||
|
|||||||
+41
-41
@@ -1,7 +1,7 @@
|
|||||||
//+build linux, darwin, freebsd
|
//+build linux, darwin, freebsd
|
||||||
package time
|
package time
|
||||||
|
|
||||||
IS_SUPPORTED :: true; // NOTE: Times on Darwin are UTC.
|
IS_SUPPORTED :: true // NOTE: Times on Darwin are UTC.
|
||||||
|
|
||||||
when ODIN_OS == "darwin" {
|
when ODIN_OS == "darwin" {
|
||||||
foreign import libc "System.framework"
|
foreign import libc "System.framework"
|
||||||
@@ -12,81 +12,81 @@ when ODIN_OS == "darwin" {
|
|||||||
|
|
||||||
@(default_calling_convention="c")
|
@(default_calling_convention="c")
|
||||||
foreign libc {
|
foreign libc {
|
||||||
@(link_name="clock_gettime") _unix_clock_gettime :: proc(clock_id: u64, timespec: ^TimeSpec) -> i32 ---;
|
@(link_name="clock_gettime") _unix_clock_gettime :: proc(clock_id: u64, timespec: ^TimeSpec) -> i32 ---
|
||||||
@(link_name="sleep") _unix_sleep :: proc(seconds: u32) -> i32 ---;
|
@(link_name="sleep") _unix_sleep :: proc(seconds: u32) -> i32 ---
|
||||||
@(link_name="nanosleep") _unix_nanosleep :: proc(requested: ^TimeSpec, remaining: ^TimeSpec) -> i32 ---;
|
@(link_name="nanosleep") _unix_nanosleep :: proc(requested: ^TimeSpec, remaining: ^TimeSpec) -> i32 ---
|
||||||
}
|
}
|
||||||
|
|
||||||
TimeSpec :: struct {
|
TimeSpec :: struct {
|
||||||
tv_sec : i64, /* seconds */
|
tv_sec : i64, /* seconds */
|
||||||
tv_nsec : i64, /* nanoseconds */
|
tv_nsec : i64, /* nanoseconds */
|
||||||
};
|
}
|
||||||
|
|
||||||
CLOCK_REALTIME :: 0; // NOTE(tetra): May jump in time, when user changes the system time.
|
CLOCK_REALTIME :: 0 // NOTE(tetra): May jump in time, when user changes the system time.
|
||||||
CLOCK_MONOTONIC :: 1; // NOTE(tetra): May stand still while system is asleep.
|
CLOCK_MONOTONIC :: 1 // NOTE(tetra): May stand still while system is asleep.
|
||||||
CLOCK_PROCESS_CPUTIME_ID :: 2;
|
CLOCK_PROCESS_CPUTIME_ID :: 2
|
||||||
CLOCK_THREAD_CPUTIME_ID :: 3;
|
CLOCK_THREAD_CPUTIME_ID :: 3
|
||||||
CLOCK_MONOTONIC_RAW :: 4; // NOTE(tetra): "RAW" means: Not adjusted by NTP.
|
CLOCK_MONOTONIC_RAW :: 4 // NOTE(tetra): "RAW" means: Not adjusted by NTP.
|
||||||
CLOCK_REALTIME_COARSE :: 5; // NOTE(tetra): "COARSE" clocks are apparently much faster, but not "fine-grained."
|
CLOCK_REALTIME_COARSE :: 5 // NOTE(tetra): "COARSE" clocks are apparently much faster, but not "fine-grained."
|
||||||
CLOCK_MONOTONIC_COARSE :: 6;
|
CLOCK_MONOTONIC_COARSE :: 6
|
||||||
CLOCK_BOOTTIME :: 7; // NOTE(tetra): Same as MONOTONIC, except also including time system was asleep.
|
CLOCK_BOOTTIME :: 7 // NOTE(tetra): Same as MONOTONIC, except also including time system was asleep.
|
||||||
CLOCK_REALTIME_ALARM :: 8;
|
CLOCK_REALTIME_ALARM :: 8
|
||||||
CLOCK_BOOTTIME_ALARM :: 9;
|
CLOCK_BOOTTIME_ALARM :: 9
|
||||||
|
|
||||||
// TODO(tetra, 2019-11-05): The original implementation of this package for Darwin used this constants.
|
// TODO(tetra, 2019-11-05): The original implementation of this package for Darwin used this constants.
|
||||||
// I do not know if Darwin programmers are used to the existance of these constants or not, so
|
// I do not know if Darwin programmers are used to the existance of these constants or not, so
|
||||||
// I'm leaving aliases to them for now.
|
// I'm leaving aliases to them for now.
|
||||||
CLOCK_SYSTEM :: CLOCK_REALTIME;
|
CLOCK_SYSTEM :: CLOCK_REALTIME
|
||||||
CLOCK_CALENDAR :: CLOCK_MONOTONIC;
|
CLOCK_CALENDAR :: CLOCK_MONOTONIC
|
||||||
|
|
||||||
|
|
||||||
clock_gettime :: proc(clock_id: u64) -> TimeSpec {
|
clock_gettime :: proc(clock_id: u64) -> TimeSpec {
|
||||||
ts : TimeSpec; // NOTE(tetra): Do we need to initialize this?
|
ts : TimeSpec // NOTE(tetra): Do we need to initialize this?
|
||||||
_unix_clock_gettime(clock_id, &ts);
|
_unix_clock_gettime(clock_id, &ts)
|
||||||
return ts;
|
return ts
|
||||||
}
|
}
|
||||||
|
|
||||||
now :: proc() -> Time {
|
now :: proc() -> Time {
|
||||||
time_spec_now := clock_gettime(CLOCK_REALTIME);
|
time_spec_now := clock_gettime(CLOCK_REALTIME)
|
||||||
ns := time_spec_now.tv_sec * 1e9 + time_spec_now.tv_nsec;
|
ns := time_spec_now.tv_sec * 1e9 + time_spec_now.tv_nsec
|
||||||
return Time{_nsec=ns};
|
return Time{_nsec=ns}
|
||||||
}
|
}
|
||||||
|
|
||||||
boot_time :: proc() -> Time {
|
boot_time :: proc() -> Time {
|
||||||
ts_now := clock_gettime(CLOCK_REALTIME);
|
ts_now := clock_gettime(CLOCK_REALTIME)
|
||||||
ts_boottime := clock_gettime(CLOCK_BOOTTIME);
|
ts_boottime := clock_gettime(CLOCK_BOOTTIME)
|
||||||
|
|
||||||
ns := (ts_now.tv_sec - ts_boottime.tv_sec) * 1e9 + ts_now.tv_nsec - ts_boottime.tv_nsec;
|
ns := (ts_now.tv_sec - ts_boottime.tv_sec) * 1e9 + ts_now.tv_nsec - ts_boottime.tv_nsec
|
||||||
return Time{_nsec=ns};
|
return Time{_nsec=ns}
|
||||||
}
|
}
|
||||||
|
|
||||||
seconds_since_boot :: proc() -> f64 {
|
seconds_since_boot :: proc() -> f64 {
|
||||||
ts_boottime := clock_gettime(CLOCK_BOOTTIME);
|
ts_boottime := clock_gettime(CLOCK_BOOTTIME)
|
||||||
return f64(ts_boottime.tv_sec) + f64(ts_boottime.tv_nsec) / 1e9;
|
return f64(ts_boottime.tv_sec) + f64(ts_boottime.tv_nsec) / 1e9
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
sleep :: proc(d: Duration) {
|
sleep :: proc(d: Duration) {
|
||||||
ds := duration_seconds(d);
|
ds := duration_seconds(d)
|
||||||
seconds := u32(ds);
|
seconds := u32(ds)
|
||||||
nanoseconds := i64((ds - f64(seconds)) * 1e9);
|
nanoseconds := i64((ds - f64(seconds)) * 1e9)
|
||||||
|
|
||||||
if seconds > 0 { _unix_sleep(seconds); }
|
if seconds > 0 { _unix_sleep(seconds) }
|
||||||
if nanoseconds > 0 { nanosleep(nanoseconds); }
|
if nanoseconds > 0 { nanosleep(nanoseconds) }
|
||||||
}
|
}
|
||||||
|
|
||||||
nanosleep :: proc(nanoseconds: i64) -> int {
|
nanosleep :: proc(nanoseconds: i64) -> int {
|
||||||
// NOTE(tetra): Should we remove this assert? We are measuring nanoseconds after all...
|
// NOTE(tetra): Should we remove this assert? We are measuring nanoseconds after all...
|
||||||
assert(nanoseconds <= 999999999);
|
assert(nanoseconds <= 999999999)
|
||||||
|
|
||||||
requested := TimeSpec{tv_nsec = nanoseconds};
|
requested := TimeSpec{tv_nsec = nanoseconds}
|
||||||
remaining: TimeSpec; // NOTE(tetra): Do we need to initialize this?
|
remaining: TimeSpec // NOTE(tetra): Do we need to initialize this?
|
||||||
return int(_unix_nanosleep(&requested, &remaining));
|
return int(_unix_nanosleep(&requested, &remaining))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
_tick_now :: proc() -> Tick {
|
_tick_now :: proc() -> Tick {
|
||||||
t := clock_gettime(CLOCK_MONOTONIC_RAW);
|
t := clock_gettime(CLOCK_MONOTONIC_RAW)
|
||||||
_nsec := t.tv_sec*1e9 + t.tv_nsec;
|
_nsec := t.tv_sec*1e9 + t.tv_nsec
|
||||||
return Tick{_nsec = _nsec};
|
return Tick{_nsec = _nsec}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user