sync: add `pub sync.thread_id() u64 {` API
parent
8b072aa962
commit
7aaa1b057e
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,9 @@
|
|||
module sync
|
||||
|
||||
pub fn thread_id() u64 {
|
||||
$if windows {
|
||||
return u64(C.GetCurrentThreadId())
|
||||
} $else {
|
||||
return u64(C.pthread_self())
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
||||
}
|
||||
Loading…
Reference in New Issue