From 3480dc364483b448eb939d1c9b0862db50dd3ecc Mon Sep 17 00:00:00 2001 From: Gedare Bloom Date: Mon, 2 Feb 2026 18:29:50 -0700 Subject: [PATCH] cpukit/librtemscxx: reformat Fixes #5453. --- cpukit/include/rtems/error.hpp | 36 +- cpukit/include/rtems/thread.hpp | 867 +++++++++++++++----------------- cpukit/librtemscxx/error.cpp | 62 +-- cpukit/librtemscxx/thread.cpp | 595 ++++++++++------------ 4 files changed, 712 insertions(+), 848 deletions(-) diff --git a/cpukit/include/rtems/error.hpp b/cpukit/include/rtems/error.hpp index a4a6c733eb..7ef6999ae9 100644 --- a/cpukit/include/rtems/error.hpp +++ b/cpukit/include/rtems/error.hpp @@ -43,26 +43,24 @@ #include -namespace rtems -{ - class runtime_error : - public std::runtime_error - { - const rtems_status_code sc; - public: - runtime_error(const rtems_status_code sc); - runtime_error(const rtems_status_code sc, const std::string& what); - runtime_error(const rtems_status_code sc, const char* what); - ~runtime_error(); - }; +namespace rtems { +class runtime_error : public std::runtime_error { + const rtems_status_code sc; - /** - * Throw a rtems::runtime_error exception if the RTEMS status code is - * not RTEMS_SUCCESSFUL. - */ - void runtime_error_check(const rtems_status_code sc); - void runtime_error_check(const rtems_status_code sc, const std::string& what); - void runtime_error_check(const rtems_status_code sc, const char* what); +public: + runtime_error(const rtems_status_code sc); + runtime_error(const rtems_status_code sc, const std::string& what); + runtime_error(const rtems_status_code sc, const char* what); + ~runtime_error(); }; +/** + * Throw a rtems::runtime_error exception if the RTEMS status code is + * not RTEMS_SUCCESSFUL. + */ +void runtime_error_check(const rtems_status_code sc); +void runtime_error_check(const rtems_status_code sc, const std::string& what); +void runtime_error_check(const rtems_status_code sc, const char* what); +}; // namespace rtems + #endif diff --git a/cpukit/include/rtems/thread.hpp b/cpukit/include/rtems/thread.hpp index 559a3e0a32..6717c47940 100644 --- a/cpukit/include/rtems/thread.hpp +++ b/cpukit/include/rtems/thread.hpp @@ -48,457 +48,424 @@ #include #include -namespace rtems -{ - namespace thread - { - /** - * @brief Manage the attributes of a thread. - */ - class attributes - { - public: - /** - * The scheduler attribute. - */ - enum sched_attr { - sched_inherit, /**< Inherit the scheduler attributes - * from the creating thread. */ - sched_explicit /**< Explicitly set the scheduler to these - * attributes. */ - }; - - /** - * The scheduler policies. - */ - enum sched_policy { - sched_other, /**< Other scheduler policy */ - sched_fifo, /**< FIFO scheduler policy */ - sched_roundrobin, /**< Round robin scheduler policy */ - sched_sporadic /**< Sporadic scheduler policy */ - }; - - /** - * Construct a thread attributes object with the current settings of the - * executing thread. The stack size is set to the configured minimum - * stack size. - */ - attributes(); - - /* - * Copy construct the thread attributes. - * - * @param attr The attributes to copy. - */ - attributes(const attributes& attr); - - /** - * Set the name of the thread. The thread is a classic API thread and - * the name is only 4 characters. - * - * @param name The name as a string. - */ - void set_name(const std::string& name); - - /** - * Set the name of the thread. The thread is a classic API thread and - * the name is only 4 characters. - * - * @param name The name as a string. - */ - void set_name(const char* name); - - /** - * Get the name of the thread. - * - * @retval const std::string& The name of the thread. - */ - const std::string& get_name() const; - - /** - * Set the priority of the thread. - * - * @param priority The POSIX API priority of the thread. - */ - void set_priority(int priority); - - /** - * Get the POSIX API priority of the thread. - * - * @retval int The POSIX API thread priority. - */ - int get_priority() const; - - /** - * Set the stack size. If the size is less than the configured minimum - * the minimum value is used. - * - * @param size The stack size in bytes. - */ - void set_stack_size(size_t size); - - /** - * Get the stack size. - * - * @retval size_t The stack size in bytes. - */ - size_t get_stack_size() const; - - /** - * Set the scheduler name. If not set no scheduler is set. - * - * @parrm scheduler The name of the scheduler. - */ - void set_scheduler(const std::string& scheduler); - - /** - * Set the scheduler name. If not set no scheduler is set. - */ - void set_scheduler(const char* scheduler); - - /** - * Get scheduler name. - */ - const std::string& get_scheduler(); - - /** - * Get the attributes' scheduler attribute for the thread. - * - * @return sched_attr The attributes' scheduler attribute - */ - sched_attr get_scheduler_attr() const; - - /** - * Set the scheduler policy for the thread. This call sets the - * scheduler attribute to @ref sched_explicit. - * - * @param policy The scheduler policy. - */ - void set_scheduler_policy(sched_policy policy); - - /** - * Get the scheduler policy for the thread. - */ - sched_policy get_scheduler_policy() const; - - /** - * Commit any changes to the executing thread. - * - * @note only the priority and attribute of a thread can be changed. The - * name and stack size are ignored. - */ - void commit(); - - /** - * Update the attribute values from the executing thread. The attributes - * are updated from the current thread when constructed and the values - * returned are those held since then. If another thread changes the - * attributes of the current thread those changes will not be seen until - * this method is called. Except for the name and stack size any local - * changes made will lost then the update call is made. - */ - void update(); - - /** - * Copy operator. - */ - attributes& operator=(const attributes& attr); - - /** - * The comparison operator does not check the name or stack size - * of a thread. - */ - bool operator==(const attributes& attr) const; - - private: - std::string name; /**< Name of the thread */ - int priority; /**< POSIX API priority */ - size_t stack_size; /**< Stack size in bytes */ - std::string scheduler; /**< Name of the scheduler */ - sched_attr attr; /**< Scheduler's attribute */ - sched_policy policy; /**< Scheduler's policy */ - /* affinity, cpu set size is? */ - }; - - template - inline typename std::decay::type - decay_copy(T&& t) { - return std::forward(t); - } - - /** - * @brief Create a thread with thread attributes. - * - * Create a thread optionally with thread attributes. The usage of this - * class follows the C++ standard for std::thread. The standard support - * for creating a thread does not let you control the attributes of a - * thread and control is important in embedded real-time - * applications. This class lets you control a thread attributes and use - * the extensive an excellent thread support the C++ standard provides. - * - * There is no indication attribute support for threads will be added to - * the C++ standard and what it will look like. The support provided here - * is designed to make as little impact on a code base as possible. While - * the attributes supported are specific to RTEMS they are common to all - * embedded operating systems. - * - * The support provided here is specific to GCC due to the use of some - * non-standard interfaces to get the indices of the template argument - * pack in new thread's context. A standards only solution would be - * preferred. - */ - class thread - { - friend void* thread_generic_entry(void* arg); - - /** - * Base state class to interface to derived template of the thread - * state from the generic entry point for the thread. - */ - struct state_base - { - virtual ~state_base(); - virtual const attributes get_attributes() = 0; - virtual void run() = 0; - }; - - /** - * The state is passed to the new thread context as a unique - * pointer. This handles the hand over and clean up. - */ - using state_ptr = std::unique_ptr; - - public: - - /** - * Template check to see if the first argument of a thread is a set of - * attributes. - */ - template ::type> - using enable_if_attributes = typename std::enable_if - ::value>::type; - - /** - * We need our own id type so the thread class can access the pthread - * handle to initialise it. - */ - class id { - public: - id() noexcept : id_(0) { } - explicit id(pthread_t id_) : id_(id_) { } - private: - pthread_t id_; - - friend class thread; - friend bool operator==(thread::id l, thread::id r) noexcept; - - template - friend std::basic_ostream& - operator<<(std::basic_ostream& out, thread::id id_); - }; - - /** - * The default thread constructions. - */ - thread() noexcept = default; - - /** - * The std::thread equivalent constructor. The attributes will be the - * same as the executing thread with a default thread name and the - * configured minimum stack size. - */ - template - explicit thread(F&& func, Args&&... args); - - /** - * Create a thread with the provided attributes. The entry point and - * optional arguments are the same as std::thread. - */ - template > - explicit thread(A&& attr, F&& func, Args&&... args) - : id_(0) { - start_thread( - make_state(attr, - make_invoker(decay_copy(std::forward(func)), - decay_copy(std::forward(args))...)) - ); - } - - /** - * Move the thread id to this instance. - */ - thread& operator=(thread&& thread_); - - void swap(thread& thread_) noexcept; - - bool joinable() const noexcept; - - void join(); - - void detach(); - - /* - * Constrain use. These are not available. - */ - thread(thread&) = delete; - thread(const thread&) = delete; - thread(const thread&&) = delete; - thread& operator=(const thread&) = delete; - - std::thread::id get_id() const noexcept; - - private: - - id id_; - - /** - * Invoke the thread's entry point with the parameter pack in the new - * thread's context. This object holds the parameters copied onto the - * new thread's stack making them available to entry point routine. - */ - template - struct invoker { - Parms p; - - template - static std::tuple_element_t&& declval(); - - template struct _Index_tuple { }; - - template - struct _Build_index_tuple - { -#if __has_builtin(__make_integer_seq) - template - using _IdxTuple = _Index_tuple<_Indices...>; - - // Clang defines __make_integer_seq for this purpose. - using __type = __make_integer_seq<_IdxTuple, size_t, _Num>; -#else - // For GCC and other compilers, use __integer_pack instead. - using __type = _Index_tuple<__integer_pack(_Num)...>; -#endif - }; - - template - auto invoke(_Index_tuple) - noexcept(noexcept(std::invoke(declval()...))) - -> decltype(std::invoke(declval()...)) { - return std::invoke(std::get(std::move(p))...); - } - - using indices = - typename _Build_index_tuple::value>::__type; - - void run() { - invoke(indices()); - } - }; - - /** - * The state holds the invoker with the parameters. The generic entry - * point calls the virtual methods to get the attributes and to run the - * new thread in the new thread's context. - */ - template - struct state : state_base { - const attributes attr; - Invoker i; - - state(const attributes& attr, Invoker&& i) - : attr(attr), - i(std::forward(i)) { - } - - const attributes get_attributes() override { - return attr; - } - - void run() override { - i.run(); - } - }; - - /** - * Make the state. This dynamic memory is managed by the unique pointer - * and is passed to the generic thread entry point. - */ - template - static state_ptr - make_state(const attributes& attr, Invoker&& i) { - using state_impl = state; - return state_ptr{ new state_impl(attr, std::forward(i)) }; - } - - /** - * Decay the parameters so they can be correctly packed into the - * parameter tuple. - */ - template - using decayed_tuple = std::tuple::type...>; - - /** - * Make the invoker with the parameters. - */ - template - static invoker> - make_invoker(F&& func, Args&&... args) - { - return { - decayed_tuple { - std::forward(func), std::forward(args)... - } - }; - } - - /** - * Create and start the thread. - */ - void start_thread(state_ptr s); - }; - - template - thread::thread(F&& func, Args&&... args) - : id_(0) { - attributes attr; - start_thread( - make_state(attr, - make_invoker(decay_copy(std::forward(func)), - decay_copy(std::forward(args))...)) - ); - } - - inline std::thread::id thread::get_id() const noexcept { -#ifdef __clang__ - /* clang does not allow construction of std::thread::id with - an id so fail this call */ - return std::thread::id(); -#else - return std::thread::id(id_.id_); -#endif - } - - inline bool - operator==(thread::id l, thread::id r) noexcept { - return l.id_ == r.id_; - } - - inline bool - operator!=(thread::id l, thread::id r) noexcept { - return !(l == r); - } - - template - inline std::basic_ostream& - operator<<(std::basic_ostream& out, thread::id id_) { -#ifdef __clang__ - return out << id_.id_; -#else - return out << std::thread::id(id_.id_); -#endif - } +namespace rtems { +namespace thread { +/** + * @brief Manage the attributes of a thread. + */ +class attributes { +public: + /** + * The scheduler attribute. + */ + enum sched_attr { + sched_inherit, /**< Inherit the scheduler attributes + * from the creating thread. */ + sched_explicit /**< Explicitly set the scheduler to these + * attributes. */ }; + + /** + * The scheduler policies. + */ + enum sched_policy { + sched_other, /**< Other scheduler policy */ + sched_fifo, /**< FIFO scheduler policy */ + sched_roundrobin, /**< Round robin scheduler policy */ + sched_sporadic /**< Sporadic scheduler policy */ + }; + + /** + * Construct a thread attributes object with the current settings of the + * executing thread. The stack size is set to the configured minimum + * stack size. + */ + attributes(); + + /* + * Copy construct the thread attributes. + * + * @param attr The attributes to copy. + */ + attributes(const attributes& attr); + + /** + * Set the name of the thread. The thread is a classic API thread and + * the name is only 4 characters. + * + * @param name The name as a string. + */ + void set_name(const std::string& name); + + /** + * Set the name of the thread. The thread is a classic API thread and + * the name is only 4 characters. + * + * @param name The name as a string. + */ + void set_name(const char* name); + + /** + * Get the name of the thread. + * + * @retval const std::string& The name of the thread. + */ + const std::string& get_name() const; + + /** + * Set the priority of the thread. + * + * @param priority The POSIX API priority of the thread. + */ + void set_priority(int priority); + + /** + * Get the POSIX API priority of the thread. + * + * @retval int The POSIX API thread priority. + */ + int get_priority() const; + + /** + * Set the stack size. If the size is less than the configured minimum + * the minimum value is used. + * + * @param size The stack size in bytes. + */ + void set_stack_size(size_t size); + + /** + * Get the stack size. + * + * @retval size_t The stack size in bytes. + */ + size_t get_stack_size() const; + + /** + * Set the scheduler name. If not set no scheduler is set. + * + * @parrm scheduler The name of the scheduler. + */ + void set_scheduler(const std::string& scheduler); + + /** + * Set the scheduler name. If not set no scheduler is set. + */ + void set_scheduler(const char* scheduler); + + /** + * Get scheduler name. + */ + const std::string& get_scheduler(); + + /** + * Get the attributes' scheduler attribute for the thread. + * + * @return sched_attr The attributes' scheduler attribute + */ + sched_attr get_scheduler_attr() const; + + /** + * Set the scheduler policy for the thread. This call sets the + * scheduler attribute to @ref sched_explicit. + * + * @param policy The scheduler policy. + */ + void set_scheduler_policy(sched_policy policy); + + /** + * Get the scheduler policy for the thread. + */ + sched_policy get_scheduler_policy() const; + + /** + * Commit any changes to the executing thread. + * + * @note only the priority and attribute of a thread can be changed. The + * name and stack size are ignored. + */ + void commit(); + + /** + * Update the attribute values from the executing thread. The attributes + * are updated from the current thread when constructed and the values + * returned are those held since then. If another thread changes the + * attributes of the current thread those changes will not be seen until + * this method is called. Except for the name and stack size any local + * changes made will lost then the update call is made. + */ + void update(); + + /** + * Copy operator. + */ + attributes& operator=(const attributes& attr); + + /** + * The comparison operator does not check the name or stack size + * of a thread. + */ + bool operator==(const attributes& attr) const; + +private: + std::string name; /**< Name of the thread */ + int priority; /**< POSIX API priority */ + size_t stack_size; /**< Stack size in bytes */ + std::string scheduler; /**< Name of the scheduler */ + sched_attr attr; /**< Scheduler's attribute */ + sched_policy policy; /**< Scheduler's policy */ + /* affinity, cpu set size is? */ }; +template inline typename std::decay::type decay_copy(T&& t) { + return std::forward(t); +} + +/** + * @brief Create a thread with thread attributes. + * + * Create a thread optionally with thread attributes. The usage of this + * class follows the C++ standard for std::thread. The standard support + * for creating a thread does not let you control the attributes of a + * thread and control is important in embedded real-time + * applications. This class lets you control a thread attributes and use + * the extensive an excellent thread support the C++ standard provides. + * + * There is no indication attribute support for threads will be added to + * the C++ standard and what it will look like. The support provided here + * is designed to make as little impact on a code base as possible. While + * the attributes supported are specific to RTEMS they are common to all + * embedded operating systems. + * + * The support provided here is specific to GCC due to the use of some + * non-standard interfaces to get the indices of the template argument + * pack in new thread's context. A standards only solution would be + * preferred. + */ +class thread { + friend void* thread_generic_entry(void* arg); + + /** + * Base state class to interface to derived template of the thread + * state from the generic entry point for the thread. + */ + struct state_base { + virtual ~state_base(); + virtual const attributes get_attributes() = 0; + virtual void run() = 0; + }; + + /** + * The state is passed to the new thread context as a unique + * pointer. This handles the hand over and clean up. + */ + using state_ptr = std::unique_ptr; + +public: + /** + * Template check to see if the first argument of a thread is a set of + * attributes. + */ + template ::type> + using enable_if_attributes = + typename std::enable_if::value>::type; + + /** + * We need our own id type so the thread class can access the pthread + * handle to initialise it. + */ + class id { + public: + id() noexcept : id_(0) {} + explicit id(pthread_t id_) : id_(id_) {} + + private: + pthread_t id_; + + friend class thread; + friend bool operator==(thread::id l, thread::id r) noexcept; + + template + friend std::basic_ostream& + operator<<(std::basic_ostream& out, thread::id id_); + }; + + /** + * The default thread constructions. + */ + thread() noexcept = default; + + /** + * The std::thread equivalent constructor. The attributes will be the + * same as the executing thread with a default thread name and the + * configured minimum stack size. + */ + template + explicit thread(F&& func, Args&&... args); + + /** + * Create a thread with the provided attributes. The entry point and + * optional arguments are the same as std::thread. + */ + template > + explicit thread(A&& attr, F&& func, Args&&... args) : id_(0) { + start_thread(make_state( + attr, make_invoker(decay_copy(std::forward(func)), + decay_copy(std::forward(args))...))); + } + + /** + * Move the thread id to this instance. + */ + thread& operator=(thread&& thread_); + + void swap(thread& thread_) noexcept; + + bool joinable() const noexcept; + + void join(); + + void detach(); + + /* + * Constrain use. These are not available. + */ + thread(thread&) = delete; + thread(const thread&) = delete; + thread(const thread&&) = delete; + thread& operator=(const thread&) = delete; + + std::thread::id get_id() const noexcept; + +private: + id id_; + + /** + * Invoke the thread's entry point with the parameter pack in the new + * thread's context. This object holds the parameters copied onto the + * new thread's stack making them available to entry point routine. + */ + template struct invoker { + Parms p; + + template + static std::tuple_element_t&& declval(); + + template struct _Index_tuple {}; + + template struct _Build_index_tuple { +#if __has_builtin(__make_integer_seq) + template + using _IdxTuple = _Index_tuple<_Indices...>; + + // Clang defines __make_integer_seq for this purpose. + using __type = __make_integer_seq<_IdxTuple, size_t, _Num>; +#else + // For GCC and other compilers, use __integer_pack instead. + using __type = _Index_tuple<__integer_pack(_Num)...>; +#endif + }; + + template + auto invoke(_Index_tuple) noexcept( + noexcept(std::invoke(declval()...))) + -> decltype(std::invoke(declval()...)) { + return std::invoke(std::get(std::move(p))...); + } + + using indices = + typename _Build_index_tuple::value>::__type; + + void run() { invoke(indices()); } + }; + + /** + * The state holds the invoker with the parameters. The generic entry + * point calls the virtual methods to get the attributes and to run the + * new thread in the new thread's context. + */ + template struct state : state_base { + const attributes attr; + Invoker i; + + state(const attributes& attr, Invoker&& i) + : attr(attr), i(std::forward(i)) {} + + const attributes get_attributes() override { return attr; } + + void run() override { i.run(); } + }; + + /** + * Make the state. This dynamic memory is managed by the unique pointer + * and is passed to the generic thread entry point. + */ + template + static state_ptr make_state(const attributes& attr, Invoker&& i) { + using state_impl = state; + return state_ptr{new state_impl(attr, std::forward(i))}; + } + + /** + * Decay the parameters so they can be correctly packed into the + * parameter tuple. + */ + template + using decayed_tuple = std::tuple::type...>; + + /** + * Make the invoker with the parameters. + */ + template + static invoker> make_invoker(F&& func, + Args&&... args) { + return {decayed_tuple{std::forward(func), + std::forward(args)...}}; + } + + /** + * Create and start the thread. + */ + void start_thread(state_ptr s); +}; + +template +thread::thread(F&& func, Args&&... args) : id_(0) { + attributes attr; + start_thread( + make_state(attr, make_invoker(decay_copy(std::forward(func)), + decay_copy(std::forward(args))...))); +} + +inline std::thread::id thread::get_id() const noexcept { +#ifdef __clang__ + /* clang does not allow construction of std::thread::id with + an id so fail this call */ + return std::thread::id(); +#else + return std::thread::id(id_.id_); +#endif +} + +inline bool operator==(thread::id l, thread::id r) noexcept { + return l.id_ == r.id_; +} + +inline bool operator!=(thread::id l, thread::id r) noexcept { + return !(l == r); +} + +template +inline std::basic_ostream& operator<<(std::basic_ostream& out, + thread::id id_) { +#ifdef __clang__ + return out << id_.id_; +#else + return out << std::thread::id(id_.id_); +#endif +} +}; // namespace thread +}; // namespace rtems + #endif diff --git a/cpukit/librtemscxx/error.cpp b/cpukit/librtemscxx/error.cpp index 0bf2d8cfec..2582c33ea7 100644 --- a/cpukit/librtemscxx/error.cpp +++ b/cpukit/librtemscxx/error.cpp @@ -27,53 +27,35 @@ #include -namespace rtems -{ - runtime_error::runtime_error(const rtems_status_code sc) - : std::runtime_error(::rtems_status_text(sc)), - sc(sc) - { - } +namespace rtems { +runtime_error::runtime_error(const rtems_status_code sc) + : std::runtime_error(::rtems_status_text(sc)), sc(sc) {} - runtime_error::runtime_error(const rtems_status_code sc, - const std::string& what) - : std::runtime_error(what + ": " + ::rtems_status_text(sc)), - sc(sc) - { - } +runtime_error::runtime_error(const rtems_status_code sc, + const std::string& what) + : std::runtime_error(what + ": " + ::rtems_status_text(sc)), sc(sc) {} - runtime_error::runtime_error(const rtems_status_code sc, - const char* what) +runtime_error::runtime_error(const rtems_status_code sc, const char* what) : std::runtime_error(std::string(what) + ": " + ::rtems_status_text(sc)), - sc(sc) - { - } + sc(sc) {} - runtime_error::~runtime_error() - { - } +runtime_error::~runtime_error() {} - void - runtime_error_check(const rtems_status_code sc) - { - if (sc != RTEMS_SUCCESSFUL) { - throw runtime_error(sc); - } +void runtime_error_check(const rtems_status_code sc) { + if (sc != RTEMS_SUCCESSFUL) { + throw runtime_error(sc); } +} - void - runtime_error_check(const rtems_status_code sc, const std::string& what) - { - if (sc != RTEMS_SUCCESSFUL) { - throw runtime_error(sc, what); - } +void runtime_error_check(const rtems_status_code sc, const std::string& what) { + if (sc != RTEMS_SUCCESSFUL) { + throw runtime_error(sc, what); } +} - void - runtime_error_check(const rtems_status_code sc, const char* what) - { - if (sc != RTEMS_SUCCESSFUL) { - throw runtime_error(sc, what); - } +void runtime_error_check(const rtems_status_code sc, const char* what) { + if (sc != RTEMS_SUCCESSFUL) { + throw runtime_error(sc, what); } -}; +} +}; // namespace rtems diff --git a/cpukit/librtemscxx/thread.cpp b/cpukit/librtemscxx/thread.cpp index 6ef492ac8b..fe96514d35 100644 --- a/cpukit/librtemscxx/thread.cpp +++ b/cpukit/librtemscxx/thread.cpp @@ -48,8 +48,7 @@ extern "C" bool get_scheduler_name(rtems_id sid, char* name); #endif #if HAVE_GET_SCHEDULER_NAME -bool get_scheduler_name(rtems_id sid, char* name) -{ +bool get_scheduler_name(rtems_id sid, char* name) { name[0] = 'N'; name[1] = 'O'; name[2] = 'P'; @@ -60,7 +59,7 @@ bool get_scheduler_name(rtems_id sid, char* name) #ifdef __FreeBSD__ #include -static inline int pthread_getattr_np(pthread_t p, pthread_attr_t *a) { +static inline int pthread_getattr_np(pthread_t p, pthread_attr_t* a) { return ::pthread_attr_get_np(p, a); } #endif /* __FreeBSD__ */ @@ -69,404 +68,322 @@ static inline int pthread_getattr_np(pthread_t p, pthread_attr_t *a) { #define HAVE_SCHED_SPORADIC 1 #endif /* SCHED_SPORADIC */ -namespace rtems -{ - namespace thread - { - void - system_error_check(int ec, const char* what) - { - if (ec != 0) { - throw std::system_error(ec, std::system_category(), what); - } - } +namespace rtems { +namespace thread { +void system_error_check(int ec, const char* what) { + if (ec != 0) { + throw std::system_error(ec, std::system_category(), what); + } +} - attributes::attributes() - : priority(-1), - stack_size(MINIMUM_STACK_SIZE), - attr(sched_inherit), - policy(sched_fifo) - { - update(); - } +attributes::attributes() + : priority(-1), stack_size(MINIMUM_STACK_SIZE), attr(sched_inherit), + policy(sched_fifo) { + update(); +} - attributes::attributes(const attributes& attr) - : name(attr.name), - priority(attr.priority), - stack_size(attr.stack_size), - scheduler(attr.scheduler), - attr(attr.attr), - policy(attr.policy) - { - } +attributes::attributes(const attributes& attr) + : name(attr.name), priority(attr.priority), stack_size(attr.stack_size), + scheduler(attr.scheduler), attr(attr.attr), policy(attr.policy) {} - void - attributes::set_name(const std::string& name_) - { - name = name_; - } +void attributes::set_name(const std::string& name_) { + name = name_; +} - void - attributes::set_name(const char* name_) - { - name = name_; - } +void attributes::set_name(const char* name_) { + name = name_; +} - const std::string& - attributes::get_name() const - { - return name; - } +const std::string& attributes::get_name() const { + return name; +} - void - attributes::set_priority(int priority_) - { - priority = priority_; - } +void attributes::set_priority(int priority_) { + priority = priority_; +} - int - attributes::get_priority() const - { - return priority; - } +int attributes::get_priority() const { + return priority; +} - void - attributes::set_stack_size(size_t size) - { - stack_size = size; - } +void attributes::set_stack_size(size_t size) { + stack_size = size; +} - size_t - attributes::get_stack_size() const - { - return stack_size; - } +size_t attributes::get_stack_size() const { + return stack_size; +} - void - attributes::set_scheduler(const std::string& scheduler_) - { - scheduler = scheduler_; - } +void attributes::set_scheduler(const std::string& scheduler_) { + scheduler = scheduler_; +} - void - attributes::set_scheduler(const char* scheduler_) - { - scheduler = scheduler_; - } +void attributes::set_scheduler(const char* scheduler_) { + scheduler = scheduler_; +} - const std::string& - attributes::get_scheduler() - { - return scheduler; - } +const std::string& attributes::get_scheduler() { + return scheduler; +} - attributes::sched_attr - attributes::get_scheduler_attr() const - { - return attr; - } +attributes::sched_attr attributes::get_scheduler_attr() const { + return attr; +} - void - attributes::set_scheduler_policy(sched_policy policy_) - { - attr = sched_explicit; - policy = policy_; - } +void attributes::set_scheduler_policy(sched_policy policy_) { + attr = sched_explicit; + policy = policy_; +} - attributes::sched_policy - attributes::get_scheduler_policy() const - { - return policy; - } +attributes::sched_policy attributes::get_scheduler_policy() const { + return policy; +} - void - attributes::commit() - { - pthread_t pid = ::pthread_self(); +void attributes::commit() { + pthread_t pid = ::pthread_self(); - system_error_check( - ::pthread_setname_np(pid, name.c_str()), - "getting name"); + system_error_check(::pthread_setname_np(pid, name.c_str()), "getting name"); - int spolicy; - struct sched_param sched_param; + int spolicy; + struct sched_param sched_param; - system_error_check( - ::pthread_getschedparam(pid, &spolicy, &sched_param), - "getting scheduler parameters"); + system_error_check(::pthread_getschedparam(pid, &spolicy, &sched_param), + "getting scheduler parameters"); - switch (policy) { - case sched_other: - spolicy = SCHED_OTHER; - break; - case sched_fifo: - spolicy = SCHED_FIFO; - break; - case sched_roundrobin: - spolicy = SCHED_RR; - break; - case sched_sporadic: + switch (policy) { + case sched_other: + spolicy = SCHED_OTHER; + break; + case sched_fifo: + spolicy = SCHED_FIFO; + break; + case sched_roundrobin: + spolicy = SCHED_RR; + break; + case sched_sporadic: #ifdef HAVE_SCHED_SPORADIC - spolicy = SCHED_SPORADIC; - break; + spolicy = SCHED_SPORADIC; + break; #endif /* HAVE_SCHED_SPORADIC */ - default: - system_error_check(EINVAL, "get scheduler policy"); + default: + system_error_check(EINVAL, "get scheduler policy"); + break; + } + + sched_param.sched_priority = priority; + + system_error_check(::pthread_setschedparam(pid, spolicy, &sched_param), + "getting scheduler parameters"); + + if (!scheduler.empty()) { + char sname[4] = {' ', ' ', ' ', ' '}; + for (size_t c = 0; c < sizeof(sname); ++c) { + if (c >= scheduler.length()) { break; } - - sched_param.sched_priority = priority; - - system_error_check( - ::pthread_setschedparam(pid, spolicy, &sched_param), - "getting scheduler parameters"); - - if (!scheduler.empty()) { - char sname[4] = { ' ', ' ', ' ', ' ' }; - for (size_t c = 0; c < sizeof(sname); ++c) { - if (c >= scheduler.length()) { - break; - } - sname[c] = scheduler[c]; - } + sname[c] = scheduler[c]; + } #ifdef __rtems__ - rtems_name scheduler_name = rtems_build_name( - sname[0], sname[1], sname[2], sname[3]); - rtems_id scheduler_id; - runtime_error_check( - ::rtems_scheduler_ident(scheduler_name, &scheduler_id), - "get scheduler id"); - // runtime_error_check (::rtems_task_set_scheduler (RTEMS_SELF, - // scheduler_id, - // 1), - // "set scheduler id"); + rtems_name scheduler_name = + rtems_build_name(sname[0], sname[1], sname[2], sname[3]); + rtems_id scheduler_id; + runtime_error_check(::rtems_scheduler_ident(scheduler_name, &scheduler_id), + "get scheduler id"); + // runtime_error_check (::rtems_task_set_scheduler (RTEMS_SELF, + // scheduler_id, + // 1), + // "set scheduler id"); #endif /* __rtems__ */ - } - } + } +} - void - attributes::update() - { - pthread_t pid = ::pthread_self(); +void attributes::update() { + pthread_t pid = ::pthread_self(); - char buf[64]; - system_error_check( - ::pthread_getname_np(pid, buf, sizeof (buf)), - "getting name"); - name = buf; + char buf[64]; + system_error_check(::pthread_getname_np(pid, buf, sizeof(buf)), + "getting name"); + name = buf; - int spolicy; - struct sched_param sched_param; - system_error_check( - ::pthread_getschedparam(pid, &spolicy, &sched_param), - "getting scheduler parameters"); + int spolicy; + struct sched_param sched_param; + system_error_check(::pthread_getschedparam(pid, &spolicy, &sched_param), + "getting scheduler parameters"); - switch (spolicy) { - case SCHED_OTHER: - policy = sched_other; - break; - case SCHED_FIFO: - policy = sched_fifo; - break; - case SCHED_RR: - policy = sched_roundrobin; - break; + switch (spolicy) { + case SCHED_OTHER: + policy = sched_other; + break; + case SCHED_FIFO: + policy = sched_fifo; + break; + case SCHED_RR: + policy = sched_roundrobin; + break; #ifdef HAVE_SCHED_SPORADIC - case SCHED_SPORADIC: - policy = sched_sporadic; - break; + case SCHED_SPORADIC: + policy = sched_sporadic; + break; #endif /* HAVE_SCHED_SPORADIC */ - default: - system_error_check(EINVAL, "get scheduler policy"); - break; - } - priority = sched_param.sched_priority; + default: + system_error_check(EINVAL, "get scheduler policy"); + break; + } + priority = sched_param.sched_priority; - pthread_attr_t pattr; - system_error_check( - ::pthread_attr_init(&pattr), "attribute init"); + pthread_attr_t pattr; + system_error_check(::pthread_attr_init(&pattr), "attribute init"); - system_error_check( - ::pthread_getattr_np(pid, &pattr), - "getting thread attributes"); - system_error_check( - ::pthread_attr_getstacksize(&pattr, &stack_size), - "getting stack size"); - int inheritsched = 0; - system_error_check( - ::pthread_attr_getinheritsched(&pattr, &inheritsched), - "getting inherited scheduler attribute"); + system_error_check(::pthread_getattr_np(pid, &pattr), + "getting thread attributes"); + system_error_check(::pthread_attr_getstacksize(&pattr, &stack_size), + "getting stack size"); + int inheritsched = 0; + system_error_check(::pthread_attr_getinheritsched(&pattr, &inheritsched), + "getting inherited scheduler attribute"); - ::pthread_attr_destroy(&pattr); + ::pthread_attr_destroy(&pattr); - switch (inheritsched) { - case PTHREAD_INHERIT_SCHED: - attr = sched_inherit; - break; - case PTHREAD_EXPLICIT_SCHED: - attr = sched_explicit; - break; - default: - system_error_check(EINVAL, "get scheduler attribute"); - break; - } + switch (inheritsched) { + case PTHREAD_INHERIT_SCHED: + attr = sched_inherit; + break; + case PTHREAD_EXPLICIT_SCHED: + attr = sched_explicit; + break; + default: + system_error_check(EINVAL, "get scheduler attribute"); + break; + } #ifdef __rtems__ - rtems_id scheduler_id; - runtime_error_check( - ::rtems_task_get_scheduler(RTEMS_SELF, &scheduler_id)); + rtems_id scheduler_id; + runtime_error_check(::rtems_task_get_scheduler(RTEMS_SELF, &scheduler_id)); #endif /* __rtems__ */ #if HAVE_GET_SCHEDULER_NAME - char name[5]; - if (!get_scheduler_name(scheduler_id, &name[0])) { - system_error_check(ENOENT, "get scheduler name"); - } - scheduler = name; + char name[5]; + if (!get_scheduler_name(scheduler_id, &name[0])) { + system_error_check(ENOENT, "get scheduler name"); + } + scheduler = name; #endif - } +} - attributes& - attributes::operator=(const attributes& other) - { - name = other.name; - priority = other.priority; - stack_size = other.stack_size; - attr = other.attr; - policy = other.policy; - return *this; - } +attributes& attributes::operator=(const attributes& other) { + name = other.name; + priority = other.priority; + stack_size = other.stack_size; + attr = other.attr; + policy = other.policy; + return *this; +} - bool - attributes::operator==(const attributes& other) const - { - return - name == other.name && - priority == other.priority && - stack_size == other.stack_size && - attr == other.attr && - policy == other.policy; - } +bool attributes::operator==(const attributes& other) const { + return name == other.name && priority == other.priority && + stack_size == other.stack_size && attr == other.attr && + policy == other.policy; +} - void* - thread_generic_entry(void* arg) - { - thread::state_ptr s{ static_cast(arg) }; - try { - s->run(); - } catch (...) { - std::terminate(); - } - return nullptr; - } +void* thread_generic_entry(void* arg) { + thread::state_ptr s{static_cast(arg)}; + try { + s->run(); + } catch (...) { + std::terminate(); + } + return nullptr; +} - thread& - thread::operator=(thread&& thread_) - { - if (joinable()) { - std::terminate(); - } - swap(thread_); - return *this; - } +thread& thread::operator=(thread&& thread_) { + if (joinable()) { + std::terminate(); + } + swap(thread_); + return *this; +} - void - thread::swap(thread& thread_) noexcept - { - std::swap(id_, thread_.id_); - } +void thread::swap(thread& thread_) noexcept { + std::swap(id_, thread_.id_); +} - bool - thread::joinable() const noexcept - { - return !(id_ == id()); - } +bool thread::joinable() const noexcept { + return !(id_ == id()); +} - void - thread::join() - { - if (!joinable()) { - system_error_check(EINVAL, "join"); - } - system_error_check(::pthread_join(id_.id_, nullptr), "join"); - id_ = id(); - } +void thread::join() { + if (!joinable()) { + system_error_check(EINVAL, "join"); + } + system_error_check(::pthread_join(id_.id_, nullptr), "join"); + id_ = id(); +} - void - thread::detach() - { - if (!joinable()) { - system_error_check(EINVAL, "detach"); - } - system_error_check(::pthread_detach(id_.id_), "detach"); - id_ = id(); - } +void thread::detach() { + if (!joinable()) { + system_error_check(EINVAL, "detach"); + } + system_error_check(::pthread_detach(id_.id_), "detach"); + id_ = id(); +} - thread::state_base::~state_base() = default; +thread::state_base::~state_base() = default; - void - thread::start_thread(thread::state_ptr s) - { - const attributes attr = s->get_attributes(); +void thread::start_thread(thread::state_ptr s) { + const attributes attr = s->get_attributes(); - pthread_attr_t pattr; - system_error_check( - ::pthread_attr_init(&pattr), "attribute init"); + pthread_attr_t pattr; + system_error_check(::pthread_attr_init(&pattr), "attribute init"); - int spolicy; - switch (attr.get_scheduler_policy()) { - case attributes::sched_other: - spolicy = SCHED_OTHER; - break; - case attributes::sched_roundrobin: - spolicy = SCHED_RR; - break; - case attributes::sched_sporadic: + int spolicy; + switch (attr.get_scheduler_policy()) { + case attributes::sched_other: + spolicy = SCHED_OTHER; + break; + case attributes::sched_roundrobin: + spolicy = SCHED_RR; + break; + case attributes::sched_sporadic: #ifdef HAVE_SCHED_SPORADIC - spolicy = SCHED_SPORADIC; - break; + spolicy = SCHED_SPORADIC; + break; #endif /* HAVE_SCHED_SPORADIC */ - default: - spolicy = SCHED_FIFO; - break; - } - system_error_check( - ::pthread_attr_setschedpolicy(&pattr, spolicy), - "set scheduler policy"); + default: + spolicy = SCHED_FIFO; + break; + } + system_error_check(::pthread_attr_setschedpolicy(&pattr, spolicy), + "set scheduler policy"); - if (attr.get_scheduler_attr() == attributes::sched_inherit) { - ::pthread_attr_setinheritsched(&pattr, PTHREAD_INHERIT_SCHED); - } - else { - ::pthread_attr_setinheritsched(&pattr, PTHREAD_EXPLICIT_SCHED); - } + if (attr.get_scheduler_attr() == attributes::sched_inherit) { + ::pthread_attr_setinheritsched(&pattr, PTHREAD_INHERIT_SCHED); + } else { + ::pthread_attr_setinheritsched(&pattr, PTHREAD_EXPLICIT_SCHED); + } #ifndef __linux__ - struct sched_param param; - param.sched_priority = attr.get_priority(); - system_error_check( - ::pthread_attr_setschedparam(&pattr, ¶m), - "set sched param"); + struct sched_param param; + param.sched_priority = attr.get_priority(); + system_error_check(::pthread_attr_setschedparam(&pattr, ¶m), + "set sched param"); - system_error_check( - ::pthread_attr_setstacksize(&pattr, attr.get_stack_size()), - "set stack size"); + system_error_check(::pthread_attr_setstacksize(&pattr, attr.get_stack_size()), + "set stack size"); #endif /* __linux__ */ - /* - * Hold the new thread in the state's run handler until the rest - * of the thread is set up after the create call. - */ - system_error_check( - ::pthread_create(&id_.id_, &pattr, thread_generic_entry, s.get()), - "create thread"); + /* + * Hold the new thread in the state's run handler until the rest + * of the thread is set up after the create call. + */ + system_error_check( + ::pthread_create(&id_.id_, &pattr, thread_generic_entry, s.get()), + "create thread"); - system_error_check( - ::pthread_setname_np(id_.id_, attr.get_name().c_str()), - "setting thread name"); + system_error_check(::pthread_setname_np(id_.id_, attr.get_name().c_str()), + "setting thread name"); - ::pthread_attr_destroy(&pattr); + ::pthread_attr_destroy(&pattr); - s.release(); - }; - }; + s.release(); }; +}; // namespace thread +}; // namespace rtems