summaryrefslogtreecommitdiffstats
path: root/vespalib
diff options
context:
space:
mode:
authorHåvard Pettersen <havardpe@yahooinc.com>2023-02-22 09:18:36 +0000
committerHåvard Pettersen <havardpe@yahooinc.com>2023-02-22 10:43:14 +0000
commited321ca9a3d53acf088cd88d19e6ccf107bd0e76 (patch)
treee65b39fe9a8ff9852c78219b43d275650b28ebaf /vespalib
parent6c4c2f2a432896ee5ae2f6f8320342627840dab8 (diff)
stop using FastOS_ThreadId
Diffstat (limited to 'vespalib')
-rw-r--r--vespalib/src/tests/thread/thread_test.cpp9
-rw-r--r--vespalib/src/vespa/vespalib/util/thread.cpp8
-rw-r--r--vespalib/src/vespa/vespalib/util/thread.h1
3 files changed, 18 insertions, 0 deletions
diff --git a/vespalib/src/tests/thread/thread_test.cpp b/vespalib/src/tests/thread/thread_test.cpp
index 9533fe1c190..cde8a3596bf 100644
--- a/vespalib/src/tests/thread/thread_test.cpp
+++ b/vespalib/src/tests/thread/thread_test.cpp
@@ -2,6 +2,7 @@
#include <vespa/vespalib/testkit/test_kit.h>
#include <vespa/vespalib/util/thread.h>
+#include <iostream>
using namespace vespalib;
@@ -11,6 +12,7 @@ struct Agent : public Runnable {
bool was_run;
Agent() : was_run(false) {}
void run() override {
+ fprintf(stderr, "agent run in thread %zu\n", thread::as_zu(std::this_thread::get_id()));
was_run = true;
}
};
@@ -22,11 +24,18 @@ void my_fun(bool *was_run) {
Runnable::init_fun_t wrap(Runnable::init_fun_t init, bool *init_called) {
return [=](Runnable &target)
{
+ fprintf(stderr, "lambda run in thread %zu\n", thread::as_zu(std::this_thread::get_id()));
*init_called = true;
return init(target);
};
}
+TEST("main thread") {
+ auto my_id = std::this_thread::get_id();
+ std::cerr << "main thread(with <<): " << my_id << "\n";
+ fprintf(stderr, "main thread(with printf): %zu\n", thread::as_zu(my_id));
+}
+
TEST("run vespalib::Runnable with init function") {
Agent agent;
bool init_called = false;
diff --git a/vespalib/src/vespa/vespalib/util/thread.cpp b/vespalib/src/vespa/vespalib/util/thread.cpp
index 1c8fef53ba3..6113924e352 100644
--- a/vespalib/src/vespa/vespalib/util/thread.cpp
+++ b/vespalib/src/vespa/vespalib/util/thread.cpp
@@ -1,6 +1,7 @@
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "thread.h"
+#include <cstring>
namespace vespalib::thread {
@@ -11,4 +12,11 @@ std::thread start(Runnable &runnable, Runnable::init_fun_t init_fun_in) {
});
}
+size_t as_zu(std::thread::id id) {
+ size_t res = 0;
+ static_assert(sizeof(id) <= sizeof(res));
+ std::memcpy(&res, &id, sizeof(id));
+ return res;
+}
+
}
diff --git a/vespalib/src/vespa/vespalib/util/thread.h b/vespalib/src/vespa/vespalib/util/thread.h
index f331f1870c7..2a5693d2d26 100644
--- a/vespalib/src/vespa/vespalib/util/thread.h
+++ b/vespalib/src/vespa/vespalib/util/thread.h
@@ -10,6 +10,7 @@ namespace vespalib {
namespace thread {
[[nodiscard]] std::thread start(Runnable &runnable, Runnable::init_fun_t init_fun);
+size_t as_zu(std::thread::id id);
}
/**