Skip to content
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

Cherry pick PR #3205: Add pthread attributes support. #3215

Merged
merged 1 commit into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions starboard/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ since the version previous to it.

## Version 16

### Added support for pthread create attributes.
The standard pthread APIs `pthread_attr_init`, `pthread_attr_destroy`,
`pthread_attr_getdetachstate`, `pthread_attr_getstacksize`,
`pthread_attr_setdetachstate`, `pthread_attr_setstacksize` were added.

### Deprecated the `SbThreadSetName` and `SbThreadGetName` APIs.
Replaced the `SbThreadSetName`/`SbThreadGetName` with the POSIX
`pthread_setname_np`/`pthread_getname_np`.
Expand Down
13 changes: 13 additions & 0 deletions starboard/elf_loader/exported_symbols.cc
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,19 @@ ExportedSymbols::ExportedSymbols() {
map_["gmtime_r"] = reinterpret_cast<const void*>(&__abi_wrap_gmtime_r);
map_["lseek"] = reinterpret_cast<const void*>(&__abi_wrap_lseek);
map_["mmap"] = reinterpret_cast<const void*>(&__abi_wrap_mmap);

map_["pthread_attr_init"] =
reinterpret_cast<const void*>(&__abi_wrap_pthread_attr_init);
map_["pthread_attr_destroy"] =
reinterpret_cast<const void*>(&__abi_wrap_pthread_attr_destroy);
map_["pthread_attr_getdetachstate"] =
reinterpret_cast<const void*>(&__abi_wrap_pthread_attr_getdetachstate);
map_["pthread_attr_getstacksize"] =
reinterpret_cast<const void*>(&__abi_wrap_pthread_attr_getstacksize);
map_["pthread_attr_setdetachstate"] =
reinterpret_cast<const void*>(&__abi_wrap_pthread_attr_setdetachstate);
map_["pthread_attr_setstacksize"] =
reinterpret_cast<const void*>(&__abi_wrap_pthread_attr_setstacksize);
map_["pthread_cond_broadcast"] =
reinterpret_cast<const void*>(&__abi_wrap_pthread_cond_broadcast);
map_["pthread_cond_destroy"] =
Expand Down
1 change: 1 addition & 0 deletions starboard/nplb/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ target(gtest_target_type, "nplb") {
"posix_compliance/posix_string_compare_no_case_test.cc",
"posix_compliance/posix_string_format_test.cc",
"posix_compliance/posix_string_format_wide_test.cc",
"posix_compliance/posix_thread_attr_test.cc",
"posix_compliance/posix_thread_create_test.cc",
"posix_compliance/posix_thread_detach_test.cc",
"posix_compliance/posix_thread_get_current_test.cc",
Expand Down
61 changes: 61 additions & 0 deletions starboard/nplb/posix_compliance/posix_thread_attr_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2024 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <pthread.h>

#include "testing/gtest/include/gtest/gtest.h"
namespace starboard {
namespace nplb {
namespace {

constexpr int kStackSize = 4 * 1024 * 1024;

TEST(PosixThreadAttrTest, InitAttr) {
pthread_attr_t attr;
EXPECT_EQ(pthread_attr_init(&attr), 0);
EXPECT_EQ(pthread_attr_destroy(&attr), 0);
}

TEST(PosixThreadAttrTest, DetachAttr) {
pthread_attr_t attr;
EXPECT_EQ(pthread_attr_init(&attr), 0);
int detach_state = PTHREAD_CREATE_JOINABLE;

EXPECT_EQ(pthread_attr_getdetachstate(&attr, &detach_state), 0);
EXPECT_EQ(detach_state, PTHREAD_CREATE_JOINABLE);

EXPECT_EQ(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED), 0);

EXPECT_EQ(pthread_attr_getdetachstate(&attr, &detach_state), 0);
EXPECT_EQ(detach_state, PTHREAD_CREATE_DETACHED);

EXPECT_EQ(pthread_attr_destroy(&attr), 0);
}

TEST(PosixThreadAttrTest, StackSizeAttr) {
pthread_attr_t attr;
EXPECT_EQ(pthread_attr_init(&attr), 0);
size_t stack_size = 0;

EXPECT_EQ(pthread_attr_setstacksize(&attr, kStackSize), 0);

EXPECT_EQ(pthread_attr_getstacksize(&attr, &stack_size), 0);
EXPECT_EQ(stack_size, kStackSize);

EXPECT_EQ(pthread_attr_destroy(&attr), 0);
}

} // namespace
} // namespace nplb
} // namespace starboard
Original file line number Diff line number Diff line change
Expand Up @@ -197,5 +197,45 @@ int __abi_wrap_pthread_getname_np(pthread_t thread, char* name, size_t len);
int pthread_getname_np(pthread_t thread, char* name, size_t len) {
return __abi_wrap_pthread_getname_np(thread, name, len);
}

int __abi_wrap_pthread_attr_destroy(pthread_attr_t* attr);

int pthread_attr_destroy(pthread_attr_t* attr) {
return __abi_wrap_pthread_attr_destroy(attr);
}

int __abi_wrap_pthread_attr_init(pthread_attr_t* attr);

int pthread_attr_init(pthread_attr_t* attr) {
return __abi_wrap_pthread_attr_init(attr);
}

int __abi_wrap_pthread_attr_getstacksize(const pthread_attr_t* attr,
size_t* stack_size);

int pthread_attr_getstacksize(const pthread_attr_t* attr, size_t* stack_size) {
return __abi_wrap_pthread_attr_getstacksize(attr, stack_size);
}

int __abi_wrap_pthread_attr_setstacksize(pthread_attr_t* attr,
size_t stack_size);

int pthread_attr_setstacksize(pthread_attr_t* attr, size_t stack_size) {
return __abi_wrap_pthread_attr_setstacksize(attr, stack_size);
}

int __abi_wrap_pthread_attr_getdetachstate(const pthread_attr_t* att,
int* detachs_state);

int pthread_attr_getdetachstate(const pthread_attr_t* attr, int* detach_state) {
return __abi_wrap_pthread_attr_getdetachstate(attr, detach_state);
}

int __abi_wrap_pthread_attr_setdetachstate(pthread_attr_t* attr,
int detach_state);

int pthread_attr_setdetachstate(pthread_attr_t* attr, int detach_state) {
return __abi_wrap_pthread_attr_setdetachstate(attr, detach_state);
}
}
#endif // SB_API_VERSION >= 16
Original file line number Diff line number Diff line change
Expand Up @@ -380,3 +380,37 @@ int __abi_wrap_pthread_getname_np(musl_pthread_t thread,
size_t len) {
return pthread_getname_np(reinterpret_cast<pthread_t>(thread), name, len);
}

int __abi_wrap_pthread_attr_init(musl_pthread_attr_t* attr) {
return pthread_attr_init(PTHREAD_INTERNAL_ATTR(attr));
}

int __abi_wrap_pthread_attr_destroy(musl_pthread_attr_t* attr) {
return pthread_attr_destroy(PTHREAD_INTERNAL_ATTR(attr));
}

int __abi_wrap_pthread_attr_getstacksize(const musl_pthread_attr_t* attr,
size_t* stack_size) {
return pthread_attr_getstacksize(CONST_PTHREAD_INTERNAL_ATTR(attr),
stack_size);
}

int __abi_wrap_pthread_attr_setstacksize(musl_pthread_attr_t* attr,
size_t stack_size) {
return pthread_attr_setstacksize(PTHREAD_INTERNAL_ATTR(attr), stack_size);
}

int __abi_wrap_pthread_attr_getdetachstate(const musl_pthread_attr_t* attr,
int* detach_state) {
return pthread_attr_getdetachstate(CONST_PTHREAD_INTERNAL_ATTR(attr),
detach_state);
}

int __abi_wrap_pthread_attr_setdetachstate(musl_pthread_attr_t* attr,
int detach_state) {
int d = PTHREAD_CREATE_JOINABLE;
if (detach_state == MUSL_PTHREAD_CREATE_DETACHED) {
d = PTHREAD_CREATE_DETACHED;
}
return pthread_attr_setdetachstate(PTHREAD_INTERNAL_ATTR(attr), d);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
extern "C" {
#endif

#define MUSL_PTHREAD_CREATE_JOINABLE 0
#define MUSL_PTHREAD_CREATE_DETACHED 1

// Max size of the native mutex type.
#define MUSL_MUTEX_MAX_SIZE 80

Expand Down Expand Up @@ -118,6 +121,21 @@ SB_EXPORT int __abi_wrap_pthread_getname_np(musl_pthread_t thread,
char* name,
size_t len);

SB_EXPORT int __abi_wrap_pthread_attr_init(musl_pthread_attr_t* attr);
SB_EXPORT int __abi_wrap_pthread_attr_destroy(musl_pthread_attr_t* attr);

SB_EXPORT int __abi_wrap_pthread_attr_getstacksize(
const musl_pthread_attr_t* attr,
size_t* stack_size);
SB_EXPORT int __abi_wrap_pthread_attr_setstacksize(musl_pthread_attr_t* attr,
size_t stack_size);

SB_EXPORT int __abi_wrap_pthread_attr_getdetachstate(
const musl_pthread_attr_t* attr,
int* detach_state);
SB_EXPORT int __abi_wrap_pthread_attr_setdetachstate(musl_pthread_attr_t* attr,
int detach_state);

#ifdef __cplusplus
} // extern "C"
#endif
Expand Down
13 changes: 13 additions & 0 deletions starboard/shared/win32/posix_emu/include/pthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
#define PTHREAD_COND_INITIALIZER _INITIALIZER
#define PTHREAD_ONCE_INIT _INITIALIZER

#define PTHREAD_CREATE_JOINABLE 0
#define PTHREAD_CREATE_DETACHED 1

#ifdef __cplusplus
extern "C" {
#endif
Expand Down Expand Up @@ -94,6 +97,16 @@ int pthread_equal(pthread_t t1, pthread_t t2);

int pthread_setname_np(pthread_t thread, const char* name);
int pthread_getname_np(pthread_t thread, char* name, size_t len);

int pthread_attr_init(pthread_attr_t* attr);
int pthread_attr_destroy(pthread_attr_t* attr);

int pthread_attr_getstacksize(const pthread_attr_t* attr, size_t* stack_size);
int pthread_attr_setstacksize(pthread_attr_t* attr, size_t stack_size);

int pthread_attr_getdetachstate(const pthread_attr_t* attr, int* detach_state);
int pthread_attr_setdetachstate(pthread_attr_t* attr, int detach_state);

#ifdef __cplusplus
}
#endif
Expand Down
50 changes: 48 additions & 2 deletions starboard/shared/win32/posix_emu/pthread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ void ResetWinError();
int RunThreadLocalDestructors(ThreadSubsystemSingleton* singleton);
int CountTlsObjectsRemaining(ThreadSubsystemSingleton* singleton);

typedef struct pthread_attr_impl_t {
size_t stack_size;
int detach_state;
} pthread_attr_impl_t;

extern "C" {

int pthread_mutex_destroy(pthread_mutex_t* mutex) {
Expand Down Expand Up @@ -225,13 +230,20 @@ int pthread_create(pthread_t* thread,

info->entry_point_ = start_routine;
info->user_context_ = arg;

info->thread_private_.wait_for_join_ = true;
if (reinterpret_cast<pthread_attr_impl_t*>(*attr)->detach_state ==
PTHREAD_CREATE_DETACHED) {
info->thread_private_.wait_for_join_ = false;
}

// Create the thread suspended, and then resume once ThreadCreateInfo::handle_
// has been set, so that it's always valid in the ThreadCreateInfo
// destructor.
uintptr_t handle =
_beginthreadex(NULL, 0, ThreadTrampoline, info, CREATE_SUSPENDED, NULL);
unsigned int stack_size =
reinterpret_cast<pthread_attr_impl_t*>(*attr)->stack_size;
uintptr_t handle = _beginthreadex(NULL, stack_size, ThreadTrampoline, info,
CREATE_SUSPENDED, NULL);
SB_DCHECK(handle);
info->thread_private_.handle_ = reinterpret_cast<HANDLE>(handle);
ResetWinError();
Expand Down Expand Up @@ -351,4 +363,38 @@ int pthread_getname_np(pthread_t thread, char* name, size_t len) {
starboard::strlcpy(name, thread_private->name_.c_str(), len);
return 0;
}

int pthread_attr_init(pthread_attr_t* attr) {
*attr = calloc(sizeof(pthread_attr_impl_t), 1);
if (*attr) {
return 0;
}
return -1;
}

int pthread_attr_destroy(pthread_attr_t* attr) {
free(*attr);
return 0;
}

int pthread_attr_getstacksize(const pthread_attr_t* attr, size_t* stack_size) {
*stack_size = reinterpret_cast<pthread_attr_impl_t*>(*attr)->stack_size;
return 0;
}

int pthread_attr_setstacksize(pthread_attr_t* attr, size_t stack_size) {
reinterpret_cast<pthread_attr_impl_t*>(*attr)->stack_size = stack_size;
return 0;
}

int pthread_attr_getdetachstate(const pthread_attr_t* attr, int* detach_state) {
*detach_state = reinterpret_cast<pthread_attr_impl_t*>(*attr)->detach_state;
return 0;
}

int pthread_attr_setdetachstate(pthread_attr_t* attr, int detach_state) {
reinterpret_cast<pthread_attr_impl_t*>(*attr)->detach_state = detach_state;
return 0;
}

} // extern "C"
6 changes: 6 additions & 0 deletions starboard/tools/api_leak_detector/api_leak_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@
'read',
'sched_yield',
'stat',
'pthread_attr_init',
'pthread_attr_destroy',
'pthread_attr_getdetachstate',
'pthread_attr_getstacksize',
'pthread_attr_setdetachstate',
'pthread_attr_setstacksize',
'pthread_cond_broadcast',
'pthread_cond_destroy',
'pthread_cond_init',
Expand Down
Loading
Loading