sync: add `pub sync.thread_id() u64 {` API

pull/13810/head
Delyan Angelov 2022-03-23 11:27:08 +02:00
parent 8b072aa962
commit 7aaa1b057e
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
5 changed files with 34 additions and 0 deletions

View File

@ -10,6 +10,7 @@ import time
#include <sys/errno.h>
[trusted]
fn C.pthread_self() usize
fn C.pthread_mutex_init(voidptr, voidptr) int
fn C.pthread_mutex_lock(voidptr) int
fn C.pthread_mutex_unlock(voidptr) int

View File

@ -14,6 +14,7 @@ $if !android {
#include <semaphore.h>
[trusted]
fn C.pthread_self() usize
fn C.pthread_mutex_init(voidptr, voidptr) int
fn C.pthread_mutex_lock(voidptr) int
fn C.pthread_mutex_unlock(voidptr) int

View File

@ -8,6 +8,7 @@ import time
#include <synchapi.h>
#include <time.h>
fn C.GetCurrentThreadId() u32
fn C.GetSystemTimeAsFileTime(lpSystemTimeAsFileTime &C._FILETIME)
fn C.InitializeConditionVariable(voidptr)
fn C.WakeConditionVariable(voidptr)

View File

@ -0,0 +1,9 @@
module sync
pub fn thread_id() u64 {
$if windows {
return u64(C.GetCurrentThreadId())
} $else {
return u64(C.pthread_self())
}
}

View File

@ -0,0 +1,22 @@
import sync
fn simple_thread() u64 {
tid := sync.thread_id()
eprintln('simple_thread thread_id: $tid.hex()')
return tid
}
fn test_sync_thread_id() {
mtid := sync.thread_id()
eprintln('main thread_id: $sync.thread_id().hex()')
x := go simple_thread()
y := go simple_thread()
xtid := x.wait()
ytid := y.wait()
eprintln('main thread_id: $sync.thread_id().hex()')
dump(xtid.hex())
dump(ytid.hex())
assert mtid != xtid
assert mtid != ytid
assert xtid != ytid
}