Skip to content

Commit

Permalink
chromium/base: Port thread support to OS/2.
Browse files Browse the repository at this point in the history
Needed for #3.
  • Loading branch information
dmik committed Mar 25, 2020
1 parent b831168 commit 5e1dbc5
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
3 changes: 2 additions & 1 deletion chromium/base/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,7 @@ jumbo_component("base") {
"threading/platform_thread_android.cc",
"threading/platform_thread_linux.cc",
"threading/platform_thread_mac.mm",
"threading/platform_thread_os2.cc",
"threading/platform_thread_win.cc",
"threading/platform_thread_win.h",
"threading/post_task_and_reply_impl.cc",
Expand Down Expand Up @@ -1870,7 +1871,7 @@ jumbo_component("base") {
]
}

if (is_posix && !is_mac && !is_ios && !is_nacl) {
if (is_posix && !is_mac && !is_ios && !is_nacl && !is_os2) {
sources += [
"posix/can_lower_nice_to.cc",
"posix/can_lower_nice_to.h",
Expand Down
2 changes: 2 additions & 0 deletions chromium/base/threading/platform_thread_internal_posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ int ThreadPriorityToNiceValue(ThreadPriority priority);
// specific implementation of kThreadPriorityToNiceValueMap.
BASE_EXPORT ThreadPriority NiceValueToThreadPriority(int nice_value);

#if !defined(OS_OS2)
// If non-nullopt, this return value will be used as the platform-specific
// result of CanIncreaseThreadPriority().
Optional<bool> CanIncreaseCurrentThreadPriorityForPlatform(
Expand All @@ -46,6 +47,7 @@ bool SetCurrentThreadPriorityForPlatform(ThreadPriority priority);
// If non-null, this return value will be used as the platform-specific result
// of CanIncreaseThreadPriority().
Optional<ThreadPriority> GetCurrentThreadPriorityForPlatform();
#endif

#if defined(OS_LINUX)
// Current thread id is cached in thread local storage for performance reasons.
Expand Down
38 changes: 38 additions & 0 deletions chromium/base/threading/platform_thread_os2.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/threading/platform_thread.h"

#include "base/threading/platform_thread_internal_posix.h"
#include "base/threading/thread_id_name_manager.h"

namespace base {

namespace internal {

// kLIBC maps nice values to OS/2 priorities in __libc_back_priorityOS2FromUnix,
// consult it for details.
const ThreadPriorityToNiceValuePair kThreadPriorityToNiceValueMap[4] = {
{ThreadPriority::BACKGROUND, 20}, // PRTYC_IDLETIME + 0
{ThreadPriority::NORMAL, 0}, // PRTYC_REGULAR + 0
{ThreadPriority::DISPLAY, -10}, // PRTYC_FOREGROUNDSERVER + 0
{ThreadPriority::REALTIME_AUDIO, -18}, // PRTYC_TIMECRITICAL + 0
};

} // namespace internal

void InitThreading() {}

void TerminateOnThread() {}

size_t GetDefaultThreadStackSize(const pthread_attr_t& attributes) {
return 0;
}

// static
void PlatformThread::SetName(const std::string& name) {
ThreadIdNameManager::GetInstance()->SetName(name);
}

} // namespace base
17 changes: 17 additions & 0 deletions chromium/base/threading/platform_thread_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

#include <errno.h>
#include <pthread.h>
#if !defined(OS_OS2)
#include <sched.h>
#endif
#include <stddef.h>
#include <stdint.h>
#include <sys/time.h>
Expand Down Expand Up @@ -216,7 +218,12 @@ PlatformThreadHandle PlatformThread::CurrentHandle() {

// static
void PlatformThread::YieldCurrentThread() {
#if defined(OS_OS2)
// Under OS/2 this will end up in DosSleep(0) which does yielding.
pthread_yield();
#else
sched_yield();
#endif
}

// static
Expand Down Expand Up @@ -290,6 +297,8 @@ void PlatformThread::Detach(PlatformThreadHandle thread_handle) {
bool PlatformThread::CanIncreaseThreadPriority(ThreadPriority priority) {
#if defined(OS_NACL)
return false;
#elif defined(OS_OS2)
return true;
#else
auto platform_specific_ability =
internal::CanIncreaseCurrentThreadPriorityForPlatform(priority);
Expand All @@ -306,8 +315,10 @@ void PlatformThread::SetCurrentThreadPriorityImpl(ThreadPriority priority) {
#if defined(OS_NACL)
NOTIMPLEMENTED();
#else
#if !defined(OS_OS2)
if (internal::SetCurrentThreadPriorityForPlatform(priority))
return;
#endif

// setpriority(2) should change the whole thread group's (i.e. process)
// priority. However, as stated in the bugs section of
Expand All @@ -329,11 +340,13 @@ ThreadPriority PlatformThread::GetCurrentThreadPriority() {
NOTIMPLEMENTED();
return ThreadPriority::NORMAL;
#else
#if !defined(OS_OS2)
// Mirrors SetCurrentThreadPriority()'s implementation.
auto platform_specific_priority =
internal::GetCurrentThreadPriorityForPlatform();
if (platform_specific_priority)
return platform_specific_priority.value();
#endif

// Need to clear errno before calling getpriority():
// http://man7.org/linux/man-pages/man2/getpriority.2.html
Expand All @@ -353,9 +366,13 @@ ThreadPriority PlatformThread::GetCurrentThreadPriority() {

// static
size_t PlatformThread::GetDefaultThreadStackSize() {
#if !defined(OS_OS2)
pthread_attr_t attributes;
pthread_attr_init(&attributes);
return base::GetDefaultThreadStackSize(attributes);
#else
return 0;
#endif
}

} // namespace base

0 comments on commit 5e1dbc5

Please sign in to comment.