Skip to content

Commit ce576d0

Browse files
authored
sync: add sync.thread_id() u64 { (#13810)
1 parent 2e963e3 commit ce576d0

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

vlib/sync/thread_default.c.v

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module sync
2+
3+
fn C.pthread_self() usize
4+
5+
pub fn thread_id() u64 {
6+
return u64(C.pthread_self())
7+
}

vlib/sync/thread_test.v

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import sync
2+
3+
fn simple_thread() u64 {
4+
tid := sync.thread_id()
5+
eprintln('simple_thread thread_id: $tid.hex()')
6+
return tid
7+
}
8+
9+
fn test_sync_thread_id() {
10+
mtid := sync.thread_id()
11+
eprintln('main thread_id: $sync.thread_id().hex()')
12+
x := go simple_thread()
13+
y := go simple_thread()
14+
xtid := x.wait()
15+
ytid := y.wait()
16+
eprintln('main thread_id: $sync.thread_id().hex()')
17+
dump(xtid.hex())
18+
dump(ytid.hex())
19+
assert mtid != xtid
20+
assert mtid != ytid
21+
assert xtid != ytid
22+
}

vlib/sync/thread_windows.c.v

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module sync
2+
3+
fn C.GetCurrentThreadId() u32
4+
5+
pub fn thread_id() u64 {
6+
return u64(C.GetCurrentThreadId())
7+
}

0 commit comments

Comments
 (0)