summaryrefslogtreecommitdiff
path: root/src/mongo/platform
diff options
context:
space:
mode:
authorBen Caimano <ben.caimano@10gen.com>2020-10-26 15:33:45 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2020-11-18 19:46:00 +0000
commit1983f34ee27edb027954292cb140347b0f646f0a (patch)
tree157917a039b436f99e73599dde4d5b49c9f658b0 /src/mongo/platform
parentb04ef6fca8c37a58e345844b9a044a43aba6ea0f (diff)
downloadmongo-1983f34ee27edb027954292cb140347b0f646f0a.tar.gz
SERVER-52822 Introduced ThreadContext class
Diffstat (limited to 'src/mongo/platform')
-rw-r--r--src/mongo/platform/process_id.cpp31
-rw-r--r--src/mongo/platform/process_id.h5
2 files changed, 36 insertions, 0 deletions
diff --git a/src/mongo/platform/process_id.cpp b/src/mongo/platform/process_id.cpp
index 48bc0354316..cff6af7e2db 100644
--- a/src/mongo/platform/process_id.cpp
+++ b/src/mongo/platform/process_id.cpp
@@ -31,11 +31,21 @@
#include "mongo/platform/process_id.h"
+#ifndef _WIN32
+#include <pthread.h>
+#endif
+
+#if defined(__linux__)
+#include <sys/syscall.h>
+#include <sys/types.h>
+#endif
+
#include <iostream>
#include <limits>
#include <sstream>
#include "mongo/base/static_assert.h"
+#include "mongo/util/assert_util.h"
namespace mongo {
@@ -51,12 +61,33 @@ inline NativeProcessId getCurrentNativeProcessId() {
return getpid();
}
#endif
+
+#ifdef _WIN32
+inline NativeProcessId getCurrentNativeThreadId() {
+ return GetCurrentThreadId();
+}
+#elif __APPLE__
+inline NativeProcessId getCurrentNativeThreadId() {
+ // macOS deprecated syscall in 10.12.
+ uint64_t tid;
+ invariant(::pthread_threadid_np(NULL, &tid) == 0);
+ return tid;
+}
+#else
+inline NativeProcessId getCurrentNativeThreadId() {
+ return ::syscall(SYS_gettid);
+}
+#endif
} // namespace
ProcessId ProcessId::getCurrent() {
return fromNative(getCurrentNativeProcessId());
}
+ProcessId ProcessId::getCurrentThreadId() {
+ return fromNative(getCurrentNativeThreadId());
+}
+
int64_t ProcessId::asInt64() const {
typedef std::numeric_limits<NativeProcessId> limits;
if (limits::is_signed)
diff --git a/src/mongo/platform/process_id.h b/src/mongo/platform/process_id.h
index 2e9d1f721f7..2e2e6396de9 100644
--- a/src/mongo/platform/process_id.h
+++ b/src/mongo/platform/process_id.h
@@ -56,6 +56,11 @@ public:
static ProcessId getCurrent();
/**
+ * Gets the thread id for the currently executing process.
+ */
+ static ProcessId getCurrentThreadId();
+
+ /**
* Constructs a ProcessId from a NativeProcessId.
*/
static inline ProcessId fromNative(NativeProcessId npid) {