Mark3 Realtime Kernel
readerwriter.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 - 2019 m0slevin, all rights reserved.
12 See license.txt for more information
13 ===========================================================================*/
20 #include "mark3.h"
21 namespace Mark3
22 {
23 //---------------------------------------------------------------------------
25 {
26  m_u8ReadCount = 0;
29 }
30 
31 //---------------------------------------------------------------------------
33 {
34  AcquireReader_i(0);
35 }
36 
37 //---------------------------------------------------------------------------
38 bool ReaderWriterLock::AcquireReader(uint32_t u32TimeoutMs_)
39 {
40  return AcquireReader_i(u32TimeoutMs_);
41 }
42 
43 //---------------------------------------------------------------------------
45 {
47  m_u8ReadCount--;
48  if (0 == m_u8ReadCount) {
50  }
52 }
53 
54 //---------------------------------------------------------------------------
56 {
57  AcquireWriter_i(0);
58 }
59 
60 //---------------------------------------------------------------------------
61 bool ReaderWriterLock::AcquireWriter(uint32_t u32TimeoutMs_)
62 {
63  return AcquireWriter_i(u32TimeoutMs_);
64 }
65 
66 //---------------------------------------------------------------------------
68 {
70 }
71 
72 //---------------------------------------------------------------------------
73 bool ReaderWriterLock::AcquireReader_i(uint32_t u32TimeoutMs_)
74 {
75  auto rc = true;
76  if (!m_clReaderMutex.Claim(u32TimeoutMs_)) {
77  return false;
78  }
79 
80  m_u8ReadCount++;
81  if (1 == m_u8ReadCount) {
82  rc = m_clGlobalMutex.Claim(u32TimeoutMs_);
83  }
84 
86  return rc;
87 }
88 
89 //---------------------------------------------------------------------------
90 bool ReaderWriterLock::AcquireWriter_i(uint32_t u32TimeoutMs_)
91 {
92  return m_clGlobalMutex.Claim(u32TimeoutMs_);
93 }
94 } // namespace Mark3
uint8_t m_u8ReadCount
Number of concurrent readers.
Definition: readerwriter.h:118
Mutex m_clGlobalMutex
Mutex used to lock the object against concurrent read + write.
Definition: readerwriter.h:116
void Release()
Release Release the mutex. When the mutex is released, another object can enter the mutex-protected r...
Definition: mutex.cpp:197
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
void ReleaseWriter()
ReleaseWriter Release the currently held writer, allowing other readers/writers to access the object...
Definition: atomic.cpp:23
Single include file given to users of the Mark3 Kernel API.
bool AcquireReader_i(uint32_t u32TimeoutMs_)
AcquireReader_i Internal helper function for AcquireReaer.
void Init()
Init Initialize the reader-writer lock before use. Must be called before attempting any other operati...
void AcquireReader()
AcquireReader Acquire the object's reader lock. Multiple concurrent readers are allowed. If the writer lock is currently held, the calling thread will wait until the writer lock is relinquished.
void AcquireWriter()
AcquireWriter Acquire the writer lock. Only a single writer is allowed to access the object at a time...
bool AcquireWriter_i(uint32_t u32TimeoutMs_)
AcquireWriter_i Internal helper function for AcquireWriter.
void Init(bool bRecursive_=true)
Init Initialize a mutex object for use - must call this function before using the object...
Definition: mutex.cpp:93
void ReleaseReader()
ReleaseReader Release a previously-held reader lock.
Mutex m_clReaderMutex
Mutex used to lock object for readers.
Definition: readerwriter.h:117