Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 1 | /*----------------------------------------------------------------------------*/ |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 2 | /* Copyright (c) FIRST 2008-2016. All Rights Reserved. */ |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 4 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | /* the project. */ |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 6 | /*----------------------------------------------------------------------------*/ |
Brian Silverman | 1a67511 | 2016-02-20 20:42:49 -0500 | [diff] [blame^] | 7 | |
Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame] | 8 | #pragma once |
| 9 | |
| 10 | #include "ErrorBase.h" |
| 11 | #include "HAL/Task.hpp" |
| 12 | #include <iostream> |
| 13 | #include <string> |
| 14 | #include <thread> |
| 15 | |
| 16 | /** |
| 17 | * Wrapper class around std::thread that allows changing thread priority |
| 18 | */ |
| 19 | class Task : public ErrorBase { |
| 20 | public: |
| 21 | static const uint32_t kDefaultPriority = 60; |
| 22 | |
| 23 | Task() = default; |
| 24 | Task(const Task&) = delete; |
| 25 | Task& operator=(const Task&) = delete; |
| 26 | Task& operator=(Task&& task); |
| 27 | |
| 28 | template <class Function, class... Args> |
| 29 | Task(const std::string& name, Function&& function, Args&&... args); |
| 30 | |
| 31 | virtual ~Task(); |
| 32 | |
| 33 | bool joinable() const noexcept; |
| 34 | void join(); |
| 35 | void detach(); |
| 36 | std::thread::id get_id() const noexcept; |
| 37 | std::thread::native_handle_type native_handle(); |
| 38 | |
| 39 | bool Verify(); |
| 40 | |
| 41 | int32_t GetPriority(); |
| 42 | |
| 43 | bool SetPriority(int32_t priority); |
| 44 | |
| 45 | std::string GetName() const; |
| 46 | |
| 47 | private: |
| 48 | std::thread m_thread; |
| 49 | std::string m_taskName; |
| 50 | bool HandleError(STATUS results); |
| 51 | }; |
| 52 | |
| 53 | #include "Task.inc" |