2013-10-29 01:54:42 +04:00
|
|
|
// Copyright (c) 2012 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.
|
|
|
|
|
|
|
|
// WARNING: You should *NOT* be using this class directly. PlatformThread is
|
|
|
|
// the low-level platform-specific abstraction to the OS's threading interface.
|
|
|
|
// You should instead be using a message-loop driven Thread, see thread.h.
|
|
|
|
|
|
|
|
#ifndef BASE_THREADING_PLATFORM_THREAD_H_
|
|
|
|
#define BASE_THREADING_PLATFORM_THREAD_H_
|
|
|
|
|
2016-09-06 10:57:21 +03:00
|
|
|
#include <stddef.h>
|
|
|
|
|
2013-10-29 01:54:42 +04:00
|
|
|
#include "base/base_export.h"
|
2016-09-06 10:57:21 +03:00
|
|
|
#include "base/macros.h"
|
2013-10-29 01:54:42 +04:00
|
|
|
#include "base/time/time.h"
|
|
|
|
#include "build/build_config.h"
|
|
|
|
|
|
|
|
#if defined(OS_WIN)
|
2019-06-12 13:10:48 +03:00
|
|
|
#include "base/win/windows_types.h"
|
|
|
|
#elif defined(OS_FUCHSIA)
|
|
|
|
#include <zircon/types.h>
|
2017-10-26 17:10:41 +03:00
|
|
|
#elif defined(OS_MACOSX)
|
|
|
|
#include <mach/mach_types.h>
|
2013-10-29 01:54:42 +04:00
|
|
|
#elif defined(OS_POSIX)
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace base {
|
|
|
|
|
2014-11-18 16:48:21 +03:00
|
|
|
// Used for logging. Always an integer value.
|
2013-10-29 01:54:42 +04:00
|
|
|
#if defined(OS_WIN)
|
|
|
|
typedef DWORD PlatformThreadId;
|
2019-06-12 13:10:48 +03:00
|
|
|
#elif defined(OS_FUCHSIA)
|
|
|
|
typedef zx_handle_t PlatformThreadId;
|
2017-10-26 17:10:41 +03:00
|
|
|
#elif defined(OS_MACOSX)
|
|
|
|
typedef mach_port_t PlatformThreadId;
|
2013-10-29 01:54:42 +04:00
|
|
|
#elif defined(OS_POSIX)
|
|
|
|
typedef pid_t PlatformThreadId;
|
|
|
|
#endif
|
|
|
|
|
2014-11-18 16:48:21 +03:00
|
|
|
// Used for thread checking and debugging.
|
|
|
|
// Meant to be as fast as possible.
|
|
|
|
// These are produced by PlatformThread::CurrentRef(), and used to later
|
|
|
|
// check if we are on the same thread or not by using ==. These are safe
|
|
|
|
// to copy between threads, but can't be copied to another process as they
|
|
|
|
// have no meaning there. Also, the internal identifier can be re-used
|
|
|
|
// after a thread dies, so a PlatformThreadRef cannot be reliably used
|
|
|
|
// to distinguish a new thread from an old, dead thread.
|
|
|
|
class PlatformThreadRef {
|
|
|
|
public:
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
typedef DWORD RefType;
|
2019-06-12 13:10:48 +03:00
|
|
|
#else // OS_POSIX
|
2014-11-18 16:48:21 +03:00
|
|
|
typedef pthread_t RefType;
|
|
|
|
#endif
|
2019-06-12 13:10:48 +03:00
|
|
|
constexpr PlatformThreadRef() : id_(0) {}
|
2014-11-18 16:48:21 +03:00
|
|
|
|
2019-06-12 13:10:48 +03:00
|
|
|
explicit constexpr PlatformThreadRef(RefType id) : id_(id) {}
|
2014-11-18 16:48:21 +03:00
|
|
|
|
|
|
|
bool operator==(PlatformThreadRef other) const {
|
|
|
|
return id_ == other.id_;
|
|
|
|
}
|
|
|
|
|
2017-10-26 17:10:41 +03:00
|
|
|
bool operator!=(PlatformThreadRef other) const { return id_ != other.id_; }
|
|
|
|
|
2014-11-18 16:48:21 +03:00
|
|
|
bool is_null() const {
|
|
|
|
return id_ == 0;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
RefType id_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Used to operate on threads.
|
2013-10-29 01:54:42 +04:00
|
|
|
class PlatformThreadHandle {
|
|
|
|
public:
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
typedef void* Handle;
|
2019-06-12 13:10:48 +03:00
|
|
|
#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
|
2013-10-29 01:54:42 +04:00
|
|
|
typedef pthread_t Handle;
|
|
|
|
#endif
|
|
|
|
|
2019-06-12 13:10:48 +03:00
|
|
|
constexpr PlatformThreadHandle() : handle_(0) {}
|
2013-10-29 01:54:42 +04:00
|
|
|
|
2019-06-12 13:10:48 +03:00
|
|
|
explicit constexpr PlatformThreadHandle(Handle handle) : handle_(handle) {}
|
2013-10-29 01:54:42 +04:00
|
|
|
|
2014-11-18 16:48:21 +03:00
|
|
|
bool is_equal(const PlatformThreadHandle& other) const {
|
2013-10-29 01:54:42 +04:00
|
|
|
return handle_ == other.handle_;
|
|
|
|
}
|
|
|
|
|
2014-11-18 16:48:21 +03:00
|
|
|
bool is_null() const {
|
2013-10-29 01:54:42 +04:00
|
|
|
return !handle_;
|
|
|
|
}
|
|
|
|
|
2014-11-18 16:48:21 +03:00
|
|
|
Handle platform_handle() const {
|
2013-10-29 01:54:42 +04:00
|
|
|
return handle_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Handle handle_;
|
|
|
|
};
|
|
|
|
|
|
|
|
const PlatformThreadId kInvalidThreadId(0);
|
|
|
|
|
2016-09-06 10:57:21 +03:00
|
|
|
// Valid values for priority of Thread::Options and SimpleThread::Options, and
|
|
|
|
// SetCurrentThreadPriority(), listed in increasing order of importance.
|
2017-03-29 16:23:17 +03:00
|
|
|
enum class ThreadPriority : int {
|
2013-10-29 01:54:42 +04:00
|
|
|
// Suitable for threads that shouldn't disrupt high priority work.
|
2016-09-06 10:57:21 +03:00
|
|
|
BACKGROUND,
|
|
|
|
// Default priority level.
|
|
|
|
NORMAL,
|
|
|
|
// Suitable for threads which generate data for the display (at ~60Hz).
|
|
|
|
DISPLAY,
|
|
|
|
// Suitable for low-latency, glitch-resistant audio.
|
|
|
|
REALTIME_AUDIO,
|
2013-10-29 01:54:42 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
// A namespace for low-level thread functions.
|
|
|
|
class BASE_EXPORT PlatformThread {
|
|
|
|
public:
|
|
|
|
// Implement this interface to run code on a background thread. Your
|
|
|
|
// ThreadMain method will be called on the newly created thread.
|
|
|
|
class BASE_EXPORT Delegate {
|
|
|
|
public:
|
|
|
|
virtual void ThreadMain() = 0;
|
|
|
|
|
|
|
|
protected:
|
2019-06-12 13:10:48 +03:00
|
|
|
virtual ~Delegate() = default;
|
2013-10-29 01:54:42 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
// Gets the current thread id, which may be useful for logging purposes.
|
|
|
|
static PlatformThreadId CurrentId();
|
|
|
|
|
2014-11-18 16:48:21 +03:00
|
|
|
// Gets the current thread reference, which can be used to check if
|
|
|
|
// we're on the right thread quickly.
|
|
|
|
static PlatformThreadRef CurrentRef();
|
|
|
|
|
2016-09-06 10:57:21 +03:00
|
|
|
// Get the handle representing the current thread. On Windows, this is a
|
|
|
|
// pseudo handle constant which will always represent the thread using it and
|
|
|
|
// hence should not be shared with other threads nor be used to differentiate
|
|
|
|
// the current thread from another.
|
2013-10-29 01:54:42 +04:00
|
|
|
static PlatformThreadHandle CurrentHandle();
|
|
|
|
|
|
|
|
// Yield the current thread so another thread can be scheduled.
|
|
|
|
static void YieldCurrentThread();
|
|
|
|
|
2020-07-08 15:54:33 +03:00
|
|
|
// Sleeps for the specified duration (real-time; ignores time overrides).
|
|
|
|
// Note: The sleep duration may be in base::Time or base::TimeTicks, depending
|
|
|
|
// on platform. If you're looking to use this in unit tests testing delayed
|
|
|
|
// tasks, this will be unreliable - instead, use
|
|
|
|
// base::test::TaskEnvironment with MOCK_TIME mode.
|
2013-10-29 01:54:42 +04:00
|
|
|
static void Sleep(base::TimeDelta duration);
|
|
|
|
|
2017-03-29 16:23:17 +03:00
|
|
|
// Sets the thread name visible to debuggers/tools. This will try to
|
|
|
|
// initialize the context for current thread unless it's a WorkerThread.
|
2016-09-06 10:57:21 +03:00
|
|
|
static void SetName(const std::string& name);
|
2013-10-29 01:54:42 +04:00
|
|
|
|
|
|
|
// Gets the thread name, if previously set by SetName.
|
|
|
|
static const char* GetName();
|
|
|
|
|
|
|
|
// Creates a new thread. The |stack_size| parameter can be 0 to indicate
|
|
|
|
// that the default stack size should be used. Upon success,
|
|
|
|
// |*thread_handle| will be assigned a handle to the newly created thread,
|
|
|
|
// and |delegate|'s ThreadMain method will be executed on the newly created
|
|
|
|
// thread.
|
|
|
|
// NOTE: When you are done with the thread handle, you must call Join to
|
|
|
|
// release system resources associated with the thread. You must ensure that
|
|
|
|
// the Delegate object outlives the thread.
|
2016-09-06 10:57:21 +03:00
|
|
|
static bool Create(size_t stack_size,
|
|
|
|
Delegate* delegate,
|
|
|
|
PlatformThreadHandle* thread_handle) {
|
|
|
|
return CreateWithPriority(stack_size, delegate, thread_handle,
|
|
|
|
ThreadPriority::NORMAL);
|
|
|
|
}
|
2013-10-29 01:54:42 +04:00
|
|
|
|
|
|
|
// CreateWithPriority() does the same thing as Create() except the priority of
|
2016-09-06 10:57:21 +03:00
|
|
|
// the thread is set based on |priority|.
|
2013-10-29 01:54:42 +04:00
|
|
|
static bool CreateWithPriority(size_t stack_size, Delegate* delegate,
|
|
|
|
PlatformThreadHandle* thread_handle,
|
|
|
|
ThreadPriority priority);
|
|
|
|
|
|
|
|
// CreateNonJoinable() does the same thing as Create() except the thread
|
|
|
|
// cannot be Join()'d. Therefore, it also does not output a
|
|
|
|
// PlatformThreadHandle.
|
|
|
|
static bool CreateNonJoinable(size_t stack_size, Delegate* delegate);
|
|
|
|
|
2017-03-29 16:23:17 +03:00
|
|
|
// CreateNonJoinableWithPriority() does the same thing as CreateNonJoinable()
|
|
|
|
// except the priority of the thread is set based on |priority|.
|
|
|
|
static bool CreateNonJoinableWithPriority(size_t stack_size,
|
|
|
|
Delegate* delegate,
|
|
|
|
ThreadPriority priority);
|
|
|
|
|
2013-10-29 01:54:42 +04:00
|
|
|
// Joins with a thread created via the Create function. This function blocks
|
|
|
|
// the caller until the designated thread exits. This will invalidate
|
|
|
|
// |thread_handle|.
|
|
|
|
static void Join(PlatformThreadHandle thread_handle);
|
|
|
|
|
2017-03-29 16:23:17 +03:00
|
|
|
// Detaches and releases the thread handle. The thread is no longer joinable
|
|
|
|
// and |thread_handle| is invalidated after this call.
|
|
|
|
static void Detach(PlatformThreadHandle thread_handle);
|
|
|
|
|
2019-06-12 13:10:48 +03:00
|
|
|
// Returns true if SetCurrentThreadPriority() should be able to increase the
|
|
|
|
// priority of a thread to |priority|.
|
|
|
|
static bool CanIncreaseThreadPriority(ThreadPriority priority);
|
2017-03-29 16:23:17 +03:00
|
|
|
|
2017-10-26 17:10:41 +03:00
|
|
|
// Toggles the current thread's priority at runtime.
|
|
|
|
//
|
|
|
|
// A thread may not be able to raise its priority back up after lowering it if
|
|
|
|
// the process does not have a proper permission, e.g. CAP_SYS_NICE on Linux.
|
|
|
|
// A thread may not be able to lower its priority back down after raising it
|
|
|
|
// to REALTIME_AUDIO.
|
|
|
|
//
|
|
|
|
// This function must not be called from the main thread on Mac. This is to
|
|
|
|
// avoid performance regressions (https://crbug.com/601270).
|
|
|
|
//
|
2016-09-06 10:57:21 +03:00
|
|
|
// Since changing other threads' priority is not permitted in favor of
|
|
|
|
// security, this interface is restricted to change only the current thread
|
|
|
|
// priority (https://crbug.com/399473).
|
|
|
|
static void SetCurrentThreadPriority(ThreadPriority priority);
|
|
|
|
|
|
|
|
static ThreadPriority GetCurrentThreadPriority();
|
2013-10-29 01:54:42 +04:00
|
|
|
|
2017-03-29 16:23:17 +03:00
|
|
|
#if defined(OS_LINUX)
|
|
|
|
// Toggles a specific thread's priority at runtime. This can be used to
|
|
|
|
// change the priority of a thread in a different process and will fail
|
|
|
|
// if the calling process does not have proper permissions. The
|
|
|
|
// SetCurrentThreadPriority() function above is preferred in favor of
|
|
|
|
// security but on platforms where sandboxed processes are not allowed to
|
|
|
|
// change priority this function exists to allow a non-sandboxed process
|
|
|
|
// to change the priority of sandboxed threads for improved performance.
|
|
|
|
// Warning: Don't use this for a main thread because that will change the
|
|
|
|
// whole thread group's (i.e. process) priority.
|
|
|
|
static void SetThreadPriority(PlatformThreadId thread_id,
|
|
|
|
ThreadPriority priority);
|
|
|
|
#endif
|
|
|
|
|
2019-06-12 13:10:48 +03:00
|
|
|
// Returns the default thread stack size set by chrome. If we do not
|
|
|
|
// explicitly set default size then returns 0.
|
|
|
|
static size_t GetDefaultThreadStackSize();
|
|
|
|
|
2013-10-29 01:54:42 +04:00
|
|
|
private:
|
2019-06-12 13:10:48 +03:00
|
|
|
static void SetCurrentThreadPriorityImpl(ThreadPriority priority);
|
|
|
|
|
2013-10-29 01:54:42 +04:00
|
|
|
DISALLOW_IMPLICIT_CONSTRUCTORS(PlatformThread);
|
|
|
|
};
|
|
|
|
|
2019-06-12 13:10:48 +03:00
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
// Initializes the "ThreadPriorities" feature. The feature state is only taken
|
|
|
|
// into account after this initialization. This initialization must be
|
|
|
|
// synchronized with calls to PlatformThread::SetCurrentThreadPriority().
|
|
|
|
void InitializeThreadPrioritiesFeature();
|
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
|
2013-10-29 01:54:42 +04:00
|
|
|
} // namespace base
|
|
|
|
|
|
|
|
#endif // BASE_THREADING_PLATFORM_THREAD_H_
|