Files
ziran/std/thread_linux.zi

140 lines
6.0 KiB
Ziran

// Background work for native Linux, Android and macOS programs. A thread runs one
// procedure while the thread that started it keeps going, and that thread
// can ask whether the work has finished without waiting, so a frame loop
// keeps drawing while a request is in flight.
//
// A Thread record must stay at one address from ThreadStart until
// ThreadFinished or ThreadWait reports the end. Once it has, everything the
// procedure wrote is visible to the thread that asked, and the record can
// start another procedure.
libc :: #system_library "libc";
ThreadProcedure :: #type (argument: *void) #c_call;
PosixThreadEntry :: #type (argument: *void) -> *void #c_call;
PosixThreadCreate :: (thread: *usize, attributes: *void,
entry: PosixThreadEntry, argument: *void) -> s32 #foreign libc "pthread_create";
PosixThreadJoin :: (thread: usize, result: **void) -> s32 #foreign libc "pthread_join";
// Completion polling must also wait for the notification and native thread
// cleanup to finish, without blocking the polling thread on either of them.
#if OS == .ANDROID {
PosixThreadId :: (thread: usize) -> s32 #foreign libc "pthread_gettid_np";
PosixThreadTryJoin :: (thread: usize, result: **void) -> s32 {
// Bionic retains a joinable thread's record until its sole owner
// joins it. The kernel clears this tid after notifications and TLS
// cleanup have finished. Android has no pthread_tryjoin_np export.
if PosixThreadId(thread) != 0 { return 1 }
return PosixThreadJoin(thread, result)
}
ThreadJoinable :: (thread: *Thread) -> bool { return thread.finished }
} else {
#if #defined(__APPLE__) {
// macOS has no pthread_tryjoin_np. ThreadFinished joins only once
// ThreadRun has returned from the notification, so this waits for
// nothing but the thread's own exit and thread-local destructors.
PosixThreadTryJoin :: (thread: usize, result: **void) -> s32 {
return PosixThreadJoin(thread, result)
}
ThreadJoinable :: (thread: *Thread) -> bool { return thread.returned }
} else {
PosixThreadTryJoin :: (thread: usize, result: **void) -> s32 #foreign libc "pthread_tryjoin_np";
ThreadJoinable :: (thread: *Thread) -> bool { return thread.finished }
}
}
PosixMutexInit :: (mutex: *void, attributes: *void) -> s32 #foreign libc "pthread_mutex_init";
PosixMutexDestroy :: (mutex: *void) -> s32 #foreign libc "pthread_mutex_destroy";
PosixMutexLock :: (mutex: *void) -> s32 #foreign libc "pthread_mutex_lock";
PosixMutexTryLock :: (mutex: *void) -> s32 #foreign libc "pthread_mutex_trylock";
PosixMutexUnlock :: (mutex: *void) -> s32 #foreign libc "pthread_mutex_unlock";
ThreadNotification :: struct { procedure: ThreadProcedure; argument: *void }
Thread :: struct {
handle: usize
// Storage for a pthread_mutex_t, which is at most 40 bytes on the
// supported C libraries; u64 keeps it aligned.
lock: [8]u64
procedure: ThreadProcedure
argument: *void
notification: ThreadNotification
running: bool
finished: bool
// Set after the notification has returned.
returned: bool
}
ThreadLock :: (thread: *Thread) -> *void {
return cast(*void)*thread.lock[0]
}
ThreadRun :: (argument: *void) -> *void {
thread := cast(*Thread)argument
thread.procedure(thread.argument)
unused PosixMutexLock(ThreadLock(thread))
thread.finished = true
unused PosixMutexUnlock(ThreadLock(thread))
// Publish completion before waking a waiter. The owner still joins
// through ThreadFinished/ThreadWait before using or releasing results.
if thread.notification.procedure != null { thread.notification.procedure(thread.notification.argument) }
unused PosixMutexLock(ThreadLock(thread))
thread.returned = true
unused PosixMutexUnlock(ThreadLock(thread))
return null
}
// Starts `procedure(argument)` on a new thread. Returns false when the
// record is still running something or no thread could be started.
// `notification.procedure(notification.argument)` runs once after completion is
// published, so an event loop can wake without polling. It must not join or
// restart this Thread. Both arguments must live until the owner joins it.
#program_export
ThreadStart :: (thread: *Thread, procedure: ThreadProcedure,
argument: *void, notification: ThreadNotification = .{}) -> bool {
if thread == null || thread.running || procedure == null { return false }
if PosixMutexInit(ThreadLock(thread), null) != 0 { return false }
thread.procedure = procedure
thread.argument = argument
thread.notification = notification
thread.finished = false
thread.returned = false
thread.running = true
entry: PosixThreadEntry = ThreadRun
if PosixThreadCreate(*thread.handle, null, entry, cast(*void)thread) != 0 {
thread.running = false
unused PosixMutexDestroy(ThreadLock(thread))
return false
}
return true
}
// Whether the record is running a procedure that has not been reported
// finished yet.
#program_export
ThreadRunning :: (thread: *Thread) -> bool {
return thread != null && thread.running
}
// Whether the procedure started on `thread` has finished. Never waits; the
// first time it reports true the thread is joined and the record is free.
#program_export
ThreadFinished :: (thread: *Thread) -> bool {
if thread == null || !thread.running { return false }
if PosixMutexTryLock(ThreadLock(thread)) != 0 { return false }
finished := ThreadJoinable(thread)
unused PosixMutexUnlock(ThreadLock(thread))
if !finished { return false }
if PosixThreadTryJoin(thread.handle, null) != 0 { return false }
unused PosixMutexDestroy(ThreadLock(thread))
thread.running = false
return true
}
// Waits until the procedure started on `thread` has finished.
#program_export
ThreadWait :: (thread: *Thread) {
if thread == null || !thread.running { return }
unused PosixThreadJoin(thread.handle, null)
unused PosixMutexDestroy(ThreadLock(thread))
thread.running = false
}