Merge pull request #19393 from yunjiaw26/extend_thd_stack_size

Extend thread class to accept setting stack size as a option
pull/19503/head
yunjiaw26 6 years ago committed by GitHub
commit c83c28167e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 11
      src/core/lib/gprpp/thd.h
  2. 26
      src/core/lib/gprpp/thd_posix.cc
  3. 10
      src/core/lib/gprpp/thd_windows.cc

@ -49,7 +49,7 @@ class Thread {
public:
class Options {
public:
Options() : joinable_(true), tracked_(true) {}
Options() : joinable_(true), tracked_(true), stack_size_(0) {}
/// Set whether the thread is joinable or detached.
Options& set_joinable(bool joinable) {
joinable_ = joinable;
@ -64,9 +64,18 @@ class Thread {
}
bool tracked() const { return tracked_; }
/// Sets thread stack size (in bytes). Sets to 0 will use the default stack
/// size which is 64KB for Windows threads and 2MB for Posix(x86) threads.
Options& set_stack_size(size_t bytes) {
stack_size_ = bytes;
return *this;
}
size_t stack_size() const { return stack_size_; }
private:
bool joinable_;
bool tracked_;
size_t stack_size_;
};
/// Default constructor only to allow use in structs that lack constructors
/// Does not produce a validly-constructed thread; must later

@ -31,6 +31,7 @@
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "src/core/lib/gpr/useful.h"
#include "src/core/lib/gprpp/fork.h"
@ -48,6 +49,26 @@ struct thd_arg {
bool tracked;
};
// TODO(yunjiaw): move this to a function-level static, or remove the use of a
// non-constexpr initializer when possible
const size_t page_size = static_cast<size_t>(sysconf(_SC_PAGESIZE));
size_t RoundUpToPageSize(size_t size) {
return (size + page_size - 1) & ~(page_size - 1);
}
// Returns the minimum valid stack size that can be passed to
// pthread_attr_setstacksize.
size_t MinValidStackSize(size_t request_size) {
if (request_size < _SC_THREAD_STACK_MIN) {
request_size = _SC_THREAD_STACK_MIN;
}
// On some systems, pthread_attr_setstacksize() can fail if stacksize is
// not a multiple of the system page size.
return RoundUpToPageSize(request_size);
}
class ThreadInternalsPosix : public internal::ThreadInternalsInterface {
public:
ThreadInternalsPosix(const char* thd_name, void (*thd_body)(void* arg),
@ -79,6 +100,11 @@ class ThreadInternalsPosix : public internal::ThreadInternalsInterface {
0);
}
if (options.stack_size() != 0) {
size_t stack_size = MinValidStackSize(options.stack_size());
GPR_ASSERT(pthread_attr_setstacksize(&attr, stack_size) == 0);
}
*success =
(pthread_create(&pthread_id_, &attr,
[](void* v) -> void* {

@ -75,7 +75,15 @@ class ThreadInternalsWindows
return;
}
}
handle = CreateThread(nullptr, 64 * 1024, thread_body, info_, 0, nullptr);
if (options.stack_size() != 0) {
// Windows will round up the given stack_size value to nearest page.
handle = CreateThread(nullptr, options.stack_size(), thread_body, info_,
0, nullptr);
} else {
handle = CreateThread(nullptr, 64 * 1024, thread_body, info_, 0, nullptr);
}
if (handle == nullptr) {
destroy_thread();
*success = false;

Loading…
Cancel
Save