Mark3 Realtime Kernel
condvar.cpp
Go to the documentation of this file.
1 /*===========================================================================
2  _____ _____ _____ _____
3  ___| _|__ __|_ |__ __|__ |__ __| __ |__ ______
4 | \ / | || \ || | || |/ / ||___ |
5 | \/ | || \ || \ || \ ||___ |
6 |__/\__/|__|_||__|\__\ __||__|\__\ __||__|\__\ __||______|
7  |_____| |_____| |_____| |_____|
8 
9 --[Mark3 Realtime Platform]--------------------------------------------------
10 
11 Copyright (c) 2012 - 2018 m0slevin, all rights reserved.
12 See license.txt for more information
13 ===========================================================================*/
20 #include "mark3.h"
21 
22 namespace Mark3
23 {
24 //---------------------------------------------------------------------------
26 {
27  m_clMutex.Init();
28  m_clSemaphore.Init(0, 255);
29 }
30 
31 //---------------------------------------------------------------------------
33 {
34  KERNEL_ASSERT(nullptr != pclMutex_);
35 
36  m_clMutex.Claim();
37 
38  pclMutex_->Release();
39  m_u8Waiters++;
40 
42 
44  pclMutex_->Claim();
45 }
46 
47 //---------------------------------------------------------------------------
48 bool ConditionVariable::Wait(Mutex* pclMutex_, uint32_t u32WaitTimeMS_)
49 {
50  KERNEL_ASSERT(nullptr != pclMutex_);
51 
52  m_clMutex.Claim();
53 
54  pclMutex_->Release();
55  m_u8Waiters++;
56 
58 
59  if (!m_clSemaphore.Pend(u32WaitTimeMS_)) {
60  return false;
61  }
62  return pclMutex_->Claim(u32WaitTimeMS_);
63 }
64 
65 //---------------------------------------------------------------------------
67 {
68  m_clMutex.Claim();
69  if (m_u8Waiters) {
70  m_u8Waiters--;
72  }
74 }
75 
76 //---------------------------------------------------------------------------
78 {
79  m_clMutex.Claim();
80 
81  while (m_u8Waiters > 0) {
82  m_u8Waiters--;
84  }
85 
87 }
88 
89 } // namespace Mark3
void Release()
Release Release the mutex. When the mutex is released, another object can enter the mutex-protected r...
Definition: mutex.cpp:197
#define KERNEL_ASSERT(x)
Definition: kerneldebug.h:36
void Claim()
Claim Claim the mutex. When the mutex is claimed, no other thread can claim a region protected by the...
Definition: mutex.cpp:185
Definition: atomic.cpp:23
The Mutex Class. Class providing Mutual-exclusion locks, based on BlockingObject. ...
Definition: mutex.h:63
void Wait(Mutex *pclMutex_)
Wait Block the current thread, and wait for the object to be signalled. The specified mutex will be l...
Definition: condvar.cpp:32
Single include file given to users of the Mark3 Kernel API.
Semaphore m_clSemaphore
Definition: condvar.h:83
void Init()
Init Initialize the condition variable prior to use. Must be called before the object can be used...
Definition: condvar.cpp:25
void Signal()
Signal Signal/Unblock the next thread currently blocked on this condition variable.
Definition: condvar.cpp:66
void Pend()
Decrement the semaphore count. If the count is zero, the calling Thread will block until the semaphor...
Definition: ksemaphore.cpp:194
void Init(bool bRecursive_=true)
Init Initialize a mutex object for use - must call this function before using the object...
Definition: mutex.cpp:93
bool Post()
Increment the semaphore count. If the semaphore count is zero at the time this is called...
Definition: ksemaphore.cpp:108
void Broadcast()
Broadcast Unblock all threads currently blocked on this condition variable.
Definition: condvar.cpp:77
void Init(uint16_t u16InitVal_, uint16_t u16MaxVal_)
Initialize a semaphore before use. Must be called before attempting post/pend operations on the objec...
Definition: ksemaphore.cpp:94