-
Notifications
You must be signed in to change notification settings - Fork 5.3k
thread: add Windows implementation #5072
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
944dbea
d5b659a
da5e073
779530f
c4c2c4e
f78bdb9
2f95ad6
c6b0bec
2da255b
1138a93
fdfb693
0b13893
428dd06
91c440f
37c1832
cca7795
c7a1233
3d8d124
0d230f2
b0a5ab8
5b5cf22
e696f0d
a901bc0
f49b24f
70024c6
d37414b
d7e09c5
7d0acef
6313b4f
f6cb1ee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,14 @@ | ||
| #pragma once | ||
|
|
||
| // NOLINT(namespace-envoy) | ||
| #if !defined(_MSC_VER) | ||
| #define PACKED_STRUCT(definition, ...) definition, ##__VA_ARGS__ __attribute__((packed)) | ||
|
|
||
| #else | ||
| #ifdef _MSC_VER | ||
| #include <malloc.h> | ||
|
|
||
| #define PACKED_STRUCT(definition, ...) \ | ||
| __pragma(pack(push, 1)) definition, ##__VA_ARGS__; \ | ||
| __pragma(pack(pop)) | ||
|
|
||
| #else | ||
| #define PACKED_STRUCT(definition, ...) definition, ##__VA_ARGS__ __attribute__((packed)) | ||
|
|
||
| #endif |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| #include "common/common/assert.h" | ||
| #include "common/common/thread_impl.h" | ||
|
|
||
| #if defined(__linux__) | ||
| #include <sys/syscall.h> | ||
| #endif | ||
|
|
||
| namespace Envoy { | ||
| namespace Thread { | ||
|
|
||
| namespace { | ||
|
|
||
| int64_t getCurrentThreadId() { | ||
| #ifdef __linux__ | ||
| return static_cast<int64_t>(syscall(SYS_gettid)); | ||
| #elif defined(__APPLE__) | ||
| uint64_t tid; | ||
| pthread_threadid_np(NULL, &tid); | ||
| return tid; | ||
| #else | ||
| #error "Enable and test pthread id retrieval code for you arch in pthread/thread_impl.cc" | ||
| #endif | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| ThreadIdImplPosix::ThreadIdImplPosix(int64_t id) : id_(id) {} | ||
|
|
||
| std::string ThreadIdImplPosix::debugString() const { return std::to_string(id_); } | ||
|
|
||
| bool ThreadIdImplPosix::isCurrentThreadId() const { return id_ == getCurrentThreadId(); } | ||
|
|
||
| ThreadImplPosix::ThreadImplPosix(std::function<void()> thread_routine) | ||
| : thread_routine_(thread_routine) { | ||
| RELEASE_ASSERT(Logger::Registry::initialized(), ""); | ||
| const int rc = pthread_create(&thread_handle_, nullptr, | ||
| [](void* arg) -> void* { | ||
| static_cast<ThreadImplPosix*>(arg)->thread_routine_(); | ||
| return nullptr; | ||
| }, | ||
| this); | ||
| RELEASE_ASSERT(rc == 0, ""); | ||
| } | ||
|
|
||
| void ThreadImplPosix::join() { | ||
| const int rc = pthread_join(thread_handle_, nullptr); | ||
| RELEASE_ASSERT(rc == 0, ""); | ||
| } | ||
|
|
||
| ThreadPtr ThreadFactoryImplPosix::createThread(std::function<void()> thread_routine) { | ||
| return std::make_unique<ThreadImplPosix>(thread_routine); | ||
| } | ||
|
|
||
| ThreadIdPtr ThreadFactoryImplPosix::currentThreadId() { | ||
| return std::make_unique<ThreadIdImplPosix>(getCurrentThreadId()); | ||
| } | ||
|
|
||
| } // namespace Thread | ||
| } // namespace Envoy |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| #pragma once | ||
|
|
||
| #include <pthread.h> | ||
|
|
||
| #include <functional> | ||
|
|
||
| #include "envoy/thread/thread.h" | ||
|
|
||
| namespace Envoy { | ||
| namespace Thread { | ||
|
|
||
| class ThreadIdImplPosix : public ThreadId { | ||
| public: | ||
| ThreadIdImplPosix(int64_t id); | ||
|
|
||
| // Thread::ThreadId | ||
| std::string debugString() const override; | ||
| bool isCurrentThreadId() const override; | ||
|
|
||
| private: | ||
| int64_t id_; | ||
| }; | ||
|
|
||
| /** | ||
| * Wrapper for a pthread thread. We don't use std::thread because it eats exceptions and leads to | ||
| * unusable stack traces. | ||
| */ | ||
| class ThreadImplPosix : public Thread { | ||
| public: | ||
| ThreadImplPosix(std::function<void()> thread_routine); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FWIW I suspect you don't need ThreadImplPosix to be exposed in the header -- it can be private to the ThreadFactoryImplPosix impl and in the cc file. This is not a big deal but I feel like it's nice to minimize what has to go into h files, for compile-speed and for human understanding of interfaces.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree that we don't need ThreadImplPosix exposed in the header, but we do need ThreadImplWin32 exposed (so one of our Windows-specific classes can access data we don't want to put on the interface). It may be more consistent to have both exposed rather than one exposed and one placed in the cc file
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK leave it exposed then, this is not a big deal, and if we do subclass Apple vs Linux in the future we'll need the shared posix impl exposed at least as a class declaration. |
||
|
|
||
| // Thread::Thread | ||
| void join() override; | ||
|
|
||
| private: | ||
| std::function<void()> thread_routine_; | ||
| pthread_t thread_handle_; | ||
| }; | ||
|
|
||
| /** | ||
| * Implementation of ThreadFactory | ||
| */ | ||
| class ThreadFactoryImplPosix : public ThreadFactory { | ||
| public: | ||
| // Thread::ThreadFactory | ||
| ThreadPtr createThread(std::function<void()> thread_routine) override; | ||
sesmith177 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ThreadIdPtr currentThreadId() override; | ||
| }; | ||
|
|
||
| } // namespace Thread | ||
| } // namespace Envoy | ||
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.