Mark3 Realtime Kernel
thread.h
Go to the documentation of this file.
1 /*===========================================================================
2  _____ _____ _____ _____
3  ___| _|__ __|_ |__ __|__ |__ __| __ |__ ______
4 | \ / | || \ || | || |/ / ||___ |
5 | \/ | || \ || \ || \ ||___ |
6 |__/\__/|__|_||__|\__\ __||__|\__\ __||__|\__\ __||______|
7  |_____| |_____| |_____| |_____|
8 
9 --[Mark3 Realtime Platform]--------------------------------------------------
10 
11 Copyright (c) 2012 - 2019 m0slevin, all rights reserved.
12 See license.txt for more information
13 =========================================================================== */
34 #pragma once
35 
36 #include "kerneltypes.h"
37 #include "mark3cfg.h"
38 
39 #include "ll.h"
40 #include "threadlist.h"
41 #include "scheduler.h"
42 #include "ithreadport.h"
43 #include "quantum.h"
44 #include "autoalloc.h"
45 #include "priomap.h"
46 
47 namespace Mark3
48 {
49 class Thread;
50 
51 //---------------------------------------------------------------------------
52 using ThreadCreateCallout = void (*)(Thread* pclThread_);
53 using ThreadExitCallout = void (*)(Thread* pclThread_);
54 using ThreadContextCallout = void (*)(Thread* pclThread_);
55 
56 //---------------------------------------------------------------------------
64 class Thread : public TypedLinkListNode<Thread>
65 {
66 public:
67  void* operator new(size_t sz, void* pv) { return reinterpret_cast<Thread*>(pv); };
68  ~Thread();
69 
71 
78 
93  void Init(K_WORD* pwStack_,
94  uint16_t u16StackSize_,
95  PORT_PRIO_TYPE uXPriority_,
96  ThreadEntryFunc pfEntryPoint_,
97  void* pvArg_);
98 
116  static Thread*
117  Init(uint16_t u16StackSize_, PORT_PRIO_TYPE uXPriority_, ThreadEntryFunc pfEntryPoint_, void* pvArg_);
118 
125  void Start();
126 
132  void Stop();
133 
134 #if KERNEL_NAMED_THREADS
135 
143  void SetName(const char* szName_) { m_szName = szName_; }
149  const char* GetName() { return m_szName; }
150 #endif // #if KERNEL_NAMED_THREADS
151 
159  ThreadList* GetOwner(void) { return m_pclOwner; }
166  inline ThreadList* GetCurrent(void) { return m_pclCurrent; }
182 
183 #if KERNEL_ROUND_ROBIN
184 
190  void SetQuantum(uint16_t u16Quantum_) { m_u16Quantum = u16Quantum_; }
197  uint16_t GetQuantum(void) { return m_u16Quantum; }
198 #endif // #if KERNEL_ROUND_ROBIN
199 
206  void SetCurrent(ThreadList* pclNewList_) { m_pclCurrent = pclNewList_; }
213  void SetOwner(ThreadList* pclNewList_) { m_pclOwner = pclNewList_; }
225  void SetPriority(PORT_PRIO_TYPE uXPriority_);
226 
235  void InheritPriority(PORT_PRIO_TYPE uXPriority_);
236 
247  void Exit();
248 
256  static void Sleep(uint32_t u32TimeMs_);
257 
264  static void Yield(void);
265 
274  static void CoopYield(void);
275 
282  void SetID(uint8_t u8ID_) { m_u8ThreadID = u8ID_; }
292  uint8_t GetID() { return m_u8ThreadID; }
293 
294 #if KERNEL_STACK_CHECK
295 
306  uint16_t GetStackSlack();
307 #endif // #if KERNEL_STACK_CHECK
308 
309 #if KERNEL_EVENT_FLAGS
310 
317  uint16_t GetEventFlagMask() { return m_u16FlagMask; }
318 
323  void SetEventFlagMask(uint16_t u16Mask_) { m_u16FlagMask = u16Mask_; }
324 
331 
337 #endif // #if KERNEL_EVENT_FLAGS
338 
342  Timer* GetTimer();
343 
350  void SetExpired(bool bExpired_);
351 
357  bool GetExpired();
358 
359 #if KERNEL_EXTENDED_CONTEXT
360 
369 
380  void SetExtendedContext(void* pvData_) { m_pvExtendedContext = pvData_; }
381 #endif // #if KERNEL_EXTENDED_CONTEXT
382 
397  void SetState(ThreadState eState_) { m_eState = eState_; }
398 
403  K_WORD* GetStack() { return m_pwStack; }
404 
413 
418  uint16_t GetStackSize() { return m_u16StackSize; }
419 
424  int* ErrnoStorage() { return &m_iErrno; }
425 
426  friend class ThreadPort;
427 
428 private:
435  static void ContextSwitchSWI(void);
436 
442  void SetPriorityBase(PORT_PRIO_TYPE uXPriority_);
443 
447 
450 
453 
455  uint8_t m_u8ThreadID;
456 
459 
462 
465 
466 #if KERNEL_EXTENDED_CONTEXT
467  void* m_pvExtendedContext;
469 #endif // #if KERNEL_EXTENDED_CONTEXT
470 
471 #if KERNEL_NAMED_THREADS
472  const char* m_szName;
474 #endif // #if KERNEL_NAMED_THREADS
475 
477  uint16_t m_u16StackSize;
478 
481 
484 
487 
489  void* m_pvArg;
490 
491 #if KERNEL_ROUND_ROBIN
492  uint16_t m_u16Quantum;
494 #endif // #if KERNEL_ROUND_ROBIN
495 
496 #if KERNEL_EVENT_FLAGS
497  uint16_t m_u16FlagMask;
499 
502 #endif // #if KERNEL_EVENT_FLAGS
503 
506 
508  int m_iErrno;
509 
512 };
513 
514 } // namespace Mark3
ThreadState GetState()
GetState Returns the current state of the thread to the caller. Can be used to determine whether or n...
Definition: thread.h:389
void SetQuantum(uint16_t u16Quantum_)
SetQuantum Set the thread&#39;s round-robin execution quantum.
Definition: thread.h:190
void * GetExtendedContext()
GetExtendedContext Return the Thread object&#39;s extended-context data pointer. Used by code implementin...
Definition: thread.h:368
uint16_t m_u16Quantum
Thread quantum (in milliseconds)
Definition: thread.h:493
K_WORD * m_pwStack
Pointer to the thread&#39;s stack.
Definition: thread.h:452
EventFlagOperation
Definition: kerneltypes.h:50
#define K_WORD
Size of a data word.
Definition: portcfg.h:62
Basic data type primatives used throughout the OS.
Thread porting interface.
void Exit()
Exit. Remove the thread from being scheduled again. The thread is effectively destroyed when this occ...
Definition: thread.cpp:187
void(*)(Thread *pclThread_) ThreadExitCallout
Definition: thread.h:53
ThreadList * GetOwner(void)
GetOwner Return the ThreadList where the thread belongs when it&#39;s in the active/ready state in the sc...
Definition: thread.h:159
void(*)(Thread *pclThread_) ThreadCreateCallout
Definition: thread.h:52
PORT_PRIO_TYPE m_uXCurPriority
Current priority of the thread (priority inheritence)
Definition: thread.h:461
The ThreadList Class. This class is used for building thread-management facilities, such as schedulers, and blocking objects.
Definition: threadlist.h:38
const char * GetName()
GetName.
Definition: thread.h:149
void * m_pvExtendedContext
Pointer provided to a Thread to implement thread-local storage.
Definition: thread.h:468
PORT_PRIO_TYPE m_uXPriority
Default priority of the thread.
Definition: thread.h:458
void Init(K_WORD *pwStack_, uint16_t u16StackSize_, PORT_PRIO_TYPE uXPriority_, ThreadEntryFunc pfEntryPoint_, void *pvArg_)
Init Initialize a thread prior to its use. Initialized threads are placed in the stopped state...
Definition: thread.cpp:49
static void CoopYield(void)
CoopYield Cooperative yield - This forces the system to not only call the scheduler, but also move the currently executing thread to the back of the current thread list, allowing other same-priority threads the opportunity to run. This is used primarily for cooperative scheduling between threads in the same priority level.
Definition: thread.cpp:324
void SetState(ThreadState eState_)
SetState Set the thread&#39;s state to a new value. This is only to be used by code within the kernel...
Definition: thread.h:397
ThreadList * m_pclCurrent
Pointer to the thread-list where the thread currently resides.
Definition: thread.h:480
uint16_t m_u16StackSize
Size of the stack (in bytes)
Definition: thread.h:477
#define PORT_PRIO_TYPE
Type used for bitmap in the PriorityMap class.
Definition: portcfg.h:73
bool m_bExpired
Indicate whether or not a blocking-object timeout has occurred.
Definition: thread.h:505
K_WORD * m_pwStackTop
Pointer to the top of the thread&#39;s stack.
Definition: thread.h:449
static void ContextSwitchSWI(void)
ContextSwitchSWI This code is used to trigger the context switch interrupt. Called whenever the kerne...
Definition: thread.cpp:391
ThreadEntryFunc m_pfEntryPoint
The entry-point function called when the thread starts.
Definition: thread.h:486
bool IsInitialized()
IsInitialized Used to check whether or not a thread has been initialized prior to use...
Definition: thread.h:77
void InheritPriority(PORT_PRIO_TYPE uXPriority_)
InheritPriority Allow the thread to run at a different priority level (temporarily) for the purpose o...
Definition: thread.cpp:382
Thread scheduler function declarations This scheduler implements a very flexible type of scheduling...
void * m_pvArg
Pointer to the argument passed into the thread&#39;s entrypoint.
Definition: thread.h:489
uint16_t GetStackSlack()
GetStackSlack Performs a (somewhat lengthy) check on the thread stack to check the amount of stack ma...
Definition: atomic.cpp:23
Mark3 Kernel Configuration This file is used to configure the kernel for your specific application in...
uint16_t GetEventFlagMask()
GetEventFlagMask returns the thread&#39;s current event-flag mask, which is used in conjunction with the ...
Definition: thread.h:317
Timer m_clTimer
Timer used for blocking-object timeouts.
Definition: thread.h:511
uint16_t GetStackSize()
GetStackSize.
Definition: thread.h:418
PORT_PRIO_TYPE GetCurPriority(void)
GetCurPriority Return the priority of the current thread.
Definition: thread.h:181
EventFlagOperation m_eFlagMode
Event-flag mode.
Definition: thread.h:501
K_WORD * GetStackTop()
GetStackTop.
Definition: thread.h:412
The Thread Class. This object providing the fundamental thread control data structures and functions ...
Definition: thread.h:64
const char * m_szName
Thread name.
Definition: thread.h:473
void SetExpired(bool bExpired_)
SetExpired Set the status of the current blocking call on the thread.
Definition: thread.cpp:417
void SetCurrent(ThreadList *pclNewList_)
SetCurrent. Set the thread&#39;s current to the specified thread list.
Definition: thread.h:206
void SetEventFlagMask(uint16_t u16Mask_)
SetEventFlagMask Sets the active event flag bitfield mask.
Definition: thread.h:323
void Start()
Start Start the thread - remove it from the stopped list, add it to the scheduler&#39;s list of threads (...
Definition: thread.cpp:110
ThreadState m_eState
Enum indicating the thread&#39;s current state.
Definition: thread.h:464
static void Yield(void)
Yield Yield the thread - this forces the system to call the scheduler and determine what thread shoul...
Definition: thread.cpp:304
void SetOwner(ThreadList *pclNewList_)
SetOwner. Set the thread&#39;s owner to the specified thread list.
Definition: thread.h:213
uint16_t m_u16FlagMask
Event-flag mask.
Definition: thread.h:498
EventFlagOperation GetEventFlagMode()
GetEventFlagMode Returns the thread&#39;s event flag&#39;s operating mode.
Definition: thread.h:336
void SetName(const char *szName_)
SetName Set the name of the thread - this is purely optional, but can be useful when identifying issu...
Definition: thread.h:143
uint16_t GetQuantum(void)
GetQuantum Get the thread&#39;s round-robin execution quantum.
Definition: thread.h:197
Priority map data structure.
void SetID(uint8_t u8ID_)
SetID Set an arbitrary 8-bit ID to uniquely identify this thread.
Definition: thread.h:282
void Stop()
Stop Stop a thread that&#39;s actively scheduled without destroying its stacks. Stopped threads can be re...
Definition: thread.cpp:141
void SetEventFlagMode(EventFlagOperation eMode_)
SetEventFlagMode Sets the active event flag operation mode.
Definition: thread.h:330
void(*)(void *pvArg_) ThreadEntryFunc
Definition: kerneltypes.h:43
K_WORD * GetStack()
GetStack.
Definition: thread.h:403
static void Sleep(uint32_t u32TimeMs_)
Sleep Put the thread to sleep for the specified time (in milliseconds). Actual time slept may be long...
Definition: thread.cpp:249
Timer * GetTimer()
Definition: thread.cpp:411
bool GetExpired()
GetExpired Return the status of the most-recent blocking call on the thread.
Definition: thread.cpp:424
uint8_t GetID()
GetID Return the thread&#39;s integer ID. Note that this ID is not guaranteed to be unique when dynamic t...
Definition: thread.h:292
The Timer Class. This class provides kernel-managed timers, used to provide high-precision delays...
Definition: timer.h:68
void(*)(Thread *pclThread_) ThreadContextCallout
Definition: thread.h:54
ThreadList * GetCurrent(void)
GetCurrent Return the ThreadList where the thread is currently located.
Definition: thread.h:166
The ThreadPort Class defines the target-specific functions required by the kernel for threading...
Definition: ithreadport.h:35
void SetPriorityBase(PORT_PRIO_TYPE uXPriority_)
SetPriorityBase.
Definition: thread.cpp:331
uint8_t m_u8ThreadID
Thread ID.
Definition: thread.h:455
Automatic memory allocation for kernel objects.
PORT_PRIO_TYPE GetPriority(void)
GetPriority Return the priority of the current thread.
Definition: thread.h:174
Core linked-list declarations, used by all kernel list types At the heart of RTOS data structures are...
ThreadState
Definition: kerneltypes.h:62
int m_iErrno
Storage used to hold a thread-safe errno value.
Definition: thread.h:508
Thread Quantum declarations for Round-Robin Scheduling.
ThreadList * m_pclOwner
Pointer to the thread-list where the thread resides when active.
Definition: thread.h:483
void SetPriority(PORT_PRIO_TYPE uXPriority_)
SetPriority. Set the priority of the Thread (running or otherwise) to a different level...
Definition: thread.cpp:341
int * ErrnoStorage()
ErrnoStorage.
Definition: thread.h:424
void SetExtendedContext(void *pvData_)
SetExtendedContext Assign the Thread object&#39;s extended-context data pointer. Used by code implementin...
Definition: thread.h:380
Thread linked-list declarations.