NDEVR
API Documentation
Thread.h
1/*--------------------------------------------------------------------------------------------
2Copyright (c) 2019, NDEVR LLC
3tyler.parke@ndevr.org
4 __ __ ____ _____ __ __ _______
5 | \ | | | __ \ | ___|\ \ / / | __ \
6 | \ | | | | \ \ | |___ \ \ / / | |__) |
7 | . \| | | |__/ / | |___ \ V / | _ /
8 | |\ |_|_____/__|_____|___\_/____| | \ \
9 |__| \__________________________________| \__\
10
11Subject to the terms of the Enterprise+ Agreement, NDEVR hereby grants
12Licensee a limited, non-exclusive, non-transferable, royalty-free license
13(without the right to sublicense) to use the API solely for the purpose of
14Licensee's internal development efforts to develop applications for which
15the API was provided.
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
21INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
22PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
23FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25DEALINGS IN THE SOFTWARE.
26
27Library: Base
28File: Thread
29Included in API: True
30Author(s): Tyler Parke
31 *-----------------------------------------------------------------------------------------**/
32#pragma once
33#include "DLLInfo.h"
34#include <NDEVR/String.h>
35#include <NDEVR/Dictionary.h>
36#if NDEVR_SUPPORTS_THREADING
37 #include <thread>
38#endif
39namespace NDEVR
40{
41 class InfoPipe;
42 class TimeSpan;
43 class LogPtr;
48 class NDEVR_BASE_API ThreadFunctions
49 {
50 public:
55 static void SetCurrentThreadName(const StringView& name);
60 static bool IsPrimaryThread();
65 static void SetPrimaryThreadID(uint04 primary_thread);
70 static void RequestSleep(const TimeSpan& interval);
86 static void RequestNoSleep(bool request_no_sleep, const LogPtr& log);
97 static void RequestConcurrentExecution(const StringView& name, const std::function<void()>& function);
98 private:
103 static void RequestSleep(uint08 milliseconds);
104 static uint04 s_primary_thread_id;
105 };
106#if NDEVR_SUPPORTS_THREADING
112 class NDEVR_BASE_API Thread
113 {
114 public:
123 Thread(const StringView& name);
124 Thread(const Thread& name) = delete;
128 virtual ~Thread();
129
133 void start();
137 void join();
141 void detach();
145 virtual void stopThread();
149 virtual void requestStopThread();
154 bool isRunning() const;
158 void forceStop() { m_is_running = false; }
163 void setIsPrimaryThread(bool is_primary_thread);
168 virtual bool shouldExitThread() const;
173 bool isPrimaryThread() const;
178 const String& threadName() const { return m_name; }
183 void setThreadName(const StringView& name);
187 static void StopAllThreads();
206 static void SetCurrentThreadName(const StringView& name);
207 protected:
212 const std::thread& self();
216 virtual void run(){};//Override this to make the thread do something
217 private:
221 void startInThread();
222 private:
227 static uint08 GetThreadCount() { return s_thread_count; }
232 static void _setThreadName(const StringView& name);
233 private:
234 String m_name;
235 std::thread* m_tid = nullptr;
236 uint04 m_thread_id = Constant<uint04>::Invalid;
237 protected:
238 volatile bool m_is_running = false;
239 volatile bool m_is_single_run = true;
240 private:
241 volatile static uint04 s_thread_count;
242 static Dictionary<uint04, Thread*> s_thread_map;
243
244
245 };
246#endif
247}
A hash-based key-value store, useful for quick associative lookups.
Definition Dictionary.h:64
A light-weight base class for Log that allows processes to update, without the need for additional in...
A light-weight wrapper that will be a no-op if there is not a valid log reference,...
The core String View class for the NDEVR API.
Definition StringView.h:58
The core String class for the NDEVR API.
Definition String.h:95
Forward declaration of LogPtr for log access.
Definition Thread.h:49
static String CurrentThreadName()
Gets the name of the current thread.
static uint04 PrimaryThreadID()
Gets the unique identifier of the primary thread.
static void SetCurrentThreadName(const StringView &name)
Sets the name of the current thread for debugging and identification.
static void RequestSleep(const TimeSpan &interval)
Puts the current thread to sleep for a specified duration.
static bool IsPrimaryThread()
Checks whether the calling thread is the primary (main) thread.
static void SetPrimaryThreadID(uint04 primary_thread)
Sets the thread ID that should be considered the primary thread.
static void RequestNoSleep(bool request_no_sleep, const LogPtr &log)
Requests that the system prevent or allow the device to sleep.
static void RequestConcurrentExecution(const StringView &name, const std::function< void()> &function)
Requests that a function be executed concurrently on a separate thread.
static uint04 CurrentThreadID()
Gets the unique identifier of the current thread.
void forceStop()
Forces the running flag to false without waiting for the thread to finish.
Definition Thread.h:158
volatile bool m_is_running
Whether the thread is currently running.
Definition Thread.h:238
bool isRunning() const
Checks whether the thread is currently running.
virtual ~Thread()
Destroys the Thread and cleans up resources.
const std::thread & self()
Gets a const reference to the underlying std::thread.
virtual void requestStopThread()
Requests the thread to stop without blocking.
static String CurrentThreadName()
Gets the name of the current thread.
static void StopAllThreads()
Stops all tracked threads.
void join()
Blocks the calling thread until this thread finishes execution.
static void SetCurrentThreadName(const StringView &name)
Sets the name of the current thread.
virtual void stopThread()
Stops the thread and waits for it to finish.
Thread()
Constructs a Thread with a default name.
void setThreadName(const StringView &name)
Sets the name of this thread.
const String & threadName() const
Gets the name assigned to this thread.
Definition Thread.h:178
volatile bool m_is_single_run
Whether the thread should execute only once before stopping.
Definition Thread.h:239
void setIsPrimaryThread(bool is_primary_thread)
Sets whether this thread should be treated as the primary thread.
Thread(const StringView &name)
Constructs a Thread with the specified name.
static Thread & CurrentThread()
Gets a reference to the Thread object for the calling thread.
static void ServiceVirtualThreads()
Services any virtual threads that require periodic execution.
void detach()
Detaches the thread, allowing it to run independently.
virtual bool shouldExitThread() const
Checks whether the thread has been requested to exit.
bool isPrimaryThread() const
Checks whether this thread is the primary thread.
void start()
Starts the thread execution.
virtual void run()
The main execution body of the thread.
Definition Thread.h:216
Stores a time span, or difference between two times, with an optional start time.
Definition TimeSpan.h:46
The primary namespace for the NDEVR SDK.
uint64_t uint08
-Defines an alias representing an 8 byte, unsigned integer
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...