#ifndef BASE_THREADING_SCOPED_THREAD_PRIORITY_H_
#define BASE_THREADING_SCOPED_THREAD_PRIORITY_H_
#include <atomic>
#include <optional>
#include "base/base_export.h"
#include "base/compiler_specific.h"
#include "base/location.h"
#include "base/macros/uniquify.h"
#include "base/memory/raw_ptr.h"
#include "base/task/task_observer.h"
#include "base/threading/thread_checker.h"
#include "build/build_config.h"
#if BUILDFLAG(IS_WIN)
#include "base/win/scoped_handle.h"
#endif
namespace base {
class Location;
enum class ThreadType : int;
#define SCOPED_MAY_LOAD_LIBRARY_AT_BACKGROUND_PRIORITY() \
static std::atomic_bool BASE_UNIQUIFY(already_loaded){false}; \
base::internal::ScopedMayLoadLibraryAtBackgroundPriority BASE_UNIQUIFY( \
scoped_may_load_library_at_background_priority)( \
FROM_HERE, &BASE_UNIQUIFY(already_loaded));
#define SCOPED_MAY_LOAD_LIBRARY_AT_BACKGROUND_PRIORITY_REPEATEDLY() \
base::internal::ScopedMayLoadLibraryAtBackgroundPriority BASE_UNIQUIFY( \
scoped_may_load_library_at_background_priority)(FROM_HERE, nullptr);
class BASE_EXPORT ScopedBoostPriority {
public:
explicit ScopedBoostPriority(ThreadType target_thread_type);
~ScopedBoostPriority();
ScopedBoostPriority(const ScopedBoostPriority&) = delete;
ScopedBoostPriority& operator=(const ScopedBoostPriority&) = delete;
private:
std::optional<ThreadType> original_thread_type_;
};
class BASE_EXPORT ScopedBoostablePriority {
public:
ScopedBoostablePriority();
~ScopedBoostablePriority();
ScopedBoostablePriority(const ScopedBoostablePriority&) = delete;
ScopedBoostablePriority& operator=(const ScopedBoostablePriority& other) =
delete;
bool BoostPriority(ThreadType target_thread_type);
private:
const ThreadType initial_thread_type_;
PlatformThreadHandle thread_handle_;
#if BUILDFLAG(IS_WIN)
win::ScopedHandle scoped_handle_;
#endif
bool did_override_priority_{false};
internal::PlatformPriorityOverride priority_override_handle_;
THREAD_CHECKER(thread_checker_);
};
class BASE_EXPORT TaskMonitoringScopedBoostPriority : public TaskObserver {
public:
explicit TaskMonitoringScopedBoostPriority(
ThreadType target_thread_type,
RepeatingCallback<bool()> should_boost_callback);
~TaskMonitoringScopedBoostPriority() override;
TaskMonitoringScopedBoostPriority(const TaskMonitoringScopedBoostPriority&) =
delete;
TaskMonitoringScopedBoostPriority& operator=(
const TaskMonitoringScopedBoostPriority&) = delete;
void WillProcessTask(const PendingTask& pending_task,
bool was_blocked_or_low_priority) override;
void DidProcessTask(const PendingTask& pending_task) override {}
private:
std::optional<ScopedBoostPriority> scoped_boost_priority_;
ThreadType target_thread_type_;
RepeatingCallback<bool()> should_boost_callback_;
};
namespace internal {
class BASE_EXPORT ScopedMayLoadLibraryAtBackgroundPriority {
public:
explicit ScopedMayLoadLibraryAtBackgroundPriority(
const Location& from_here,
std::atomic_bool* already_loaded);
ScopedMayLoadLibraryAtBackgroundPriority(
const ScopedMayLoadLibraryAtBackgroundPriority&) = delete;
ScopedMayLoadLibraryAtBackgroundPriority& operator=(
const ScopedMayLoadLibraryAtBackgroundPriority&) = delete;
~ScopedMayLoadLibraryAtBackgroundPriority();
private:
#if BUILDFLAG(IS_WIN)
std::optional<ThreadType> original_thread_type_;
const raw_ptr<std::atomic_bool> already_loaded_;
#endif
};
}
}
#endif