From ce576d01c495df4b4c61f3e75c41e775b62018fa Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Wed, 23 Mar 2022 18:19:14 +0200 Subject: [PATCH] sync: add `sync.thread_id() u64 {` (#13810) --- vlib/sync/thread_default.c.v | 7 +++++++ vlib/sync/thread_test.v | 22 ++++++++++++++++++++++ vlib/sync/thread_windows.c.v | 7 +++++++ 3 files changed, 36 insertions(+) create mode 100644 vlib/sync/thread_default.c.v create mode 100644 vlib/sync/thread_test.v create mode 100644 vlib/sync/thread_windows.c.v diff --git a/vlib/sync/thread_default.c.v b/vlib/sync/thread_default.c.v new file mode 100644 index 000000000..74d6d517c --- /dev/null +++ b/vlib/sync/thread_default.c.v @@ -0,0 +1,7 @@ +module sync + +fn C.pthread_self() usize + +pub fn thread_id() u64 { + return u64(C.pthread_self()) +} diff --git a/vlib/sync/thread_test.v b/vlib/sync/thread_test.v new file mode 100644 index 000000000..797d7e9ea --- /dev/null +++ b/vlib/sync/thread_test.v @@ -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 +} diff --git a/vlib/sync/thread_windows.c.v b/vlib/sync/thread_windows.c.v new file mode 100644 index 000000000..33241d5a0 --- /dev/null +++ b/vlib/sync/thread_windows.c.v @@ -0,0 +1,7 @@ +module sync + +fn C.GetCurrentThreadId() u32 + +pub fn thread_id() u64 { + return u64(C.GetCurrentThreadId()) +} -- 2.30.2