ALib C++ Library
Library Version: 2510 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
dbgcriticalsections.cpp
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header-file is part of module \alib_threads of the \aliblong.
4///
5/// \emoji :copyright: 2013-2025 A-Worx GmbH, Germany.
6/// Published under \ref mainpage_license "Boost Software License".
7//==================================================================================================
8#include "alib_precompile.hpp"
9#if !defined(ALIB_C20_MODULES) || ((ALIB_C20_MODULES != 0) && (ALIB_C20_MODULES != 1))
10# error "Symbol ALIB_C20_MODULES has to be given to the compiler as either 0 or 1"
11#endif
12#if ALIB_C20_MODULES
13 module;
14#endif
15// ====================================== Global Fragment ======================================
16#include "alib/alib.inl"
17// =========================================== Module ==========================================
18#if ALIB_C20_MODULES
19 module ALib.Lang;
20#else
21# include "ALib.Lang.H"
22#endif
23// ====================================== Implementation =======================================
24
25#if ALIB_DEBUG_CRITICAL_SECTIONS
26namespace alib::lang {
27
29
30
32"Assertion in Critical Section \"{}\"" "\n"
33" Message: {}" "\n"
34" In (Member-)Function: {}" "\n"
35" Is Owned: {} ({})" "\n"
36" Is Shared Owned: {} ({})" "\n"
37 "\n"
38" Called By: {}::{}" "\n"
39" At: {}:{}" "\n"
40" Thread: {}" "\n"
41 "\n"
42" Latest Acquisition By: {}::{}" "\n"
43" At: {}:{}" "\n"
44" Thread: {}" "\n"
45" Latest Release By: {}::{}" "\n"
46" At: {}:{}" "\n"
47" Thread: {}" "\n"
48 "\n"
49" Latest Shared Acquisition By: {}::{}" "\n"
50" At: {}:{}" "\n"
51" Thread: {}" "\n"
52" Latest SharedRelease By: {}::{}" "\n"
53" At: {}:{}" "\n"
54" Thread: {}" "\n"
55;
56
57void DbgCriticalSections::doAssert( bool cond, const CallerInfo& ciAssert, const CallerInfo& ci,
58 const char* headline ) const
59{
60 if (cond) return;
61
62 assert::Raise( ciAssert, 0, "THREADS", ASSERTION_FORMAT,
63 DCSName, headline, ciAssert.Func,
64 DCSWriterCnt.load() > 0, DCSWriterCnt.load(),
65 DCSReaderCnt.load() > 0, DCSReaderCnt.load(),
66 ci.TypeInfo, ci.Func, ci.File, ci.Line, ci.ThreadID,
67 DCSAcq.TypeInfo, DCSAcq.Func, DCSAcq.File, DCSAcq.Line, DCSAcq.ThreadID,
68 DCSRel.TypeInfo, DCSRel.Func, DCSRel.File, DCSRel.Line, DCSRel.ThreadID,
69 DCSSAcq.TypeInfo,DCSSAcq.Func,DCSSAcq.File,DCSSAcq.Line,DCSSAcq.ThreadID,
70 DCSSRel.TypeInfo,DCSSRel.Func,DCSSRel.File,DCSSRel.Line,DCSSRel.ThreadID );
71}
72
74{
75 doAssert( !DCSLock || DCSLock->DCSIsAcquired(), ALIB_CALLER, ci, "Acquire: Associated Lock not acquired." );
76 doAssert( DCSWriterCnt.load() == 0
77 || DCSAcq.ThreadID == std::this_thread::get_id(), ALIB_CALLER, ci, "Acquired by other thread.");
78 doAssert( DCSReaderCnt.load() == 0, ALIB_CALLER, ci, "Acquired by reader.");
79
80 DCSWriterCnt.fetch_add(1);
81 DCSAcq= ci;
83}
84
86{
87 doAssert(!DCSLock || DCSLock->DCSIsAcquired() , ALIB_CALLER, ci, "Release: Associated lock not acquired." );
88 doAssert(DCSAcq.ThreadID == std::this_thread::get_id(), ALIB_CALLER, ci, "Release: Acquired by other thread.");
90 DCSRel= ci;
91 DCSWriterCnt.fetch_sub(1);
92}
93
95{
96 doAssert( !DCSLock || DCSLock->DCSIsSharedAcquired(), ALIB_CALLER, ci,
97 "AcquireShared: Associated lock not shared-acquired." );
98
100 int wCnt = DCSWriterCnt.load();
101 doAssert(wCnt <= 0 || DCSAcq.ThreadID == std::this_thread::get_id(), ALIB_CALLER, ci,
102 "AcquireShared: Acquired by different thread.");
103 DCSReaderCnt.fetch_add(1);
104 DCSSAcq= ci;
105}
106
108{
109 yieldOrSleep();
110 doAssert( !DCSLock || DCSLock->DCSIsSharedAcquired(), ALIB_CALLER, ci,
111 "ReleaseShared: Associated lock not shared-acquired." );
112
113 doAssert(DCSWriterCnt.load() <= 0 || DCSAcq.ThreadID == std::this_thread::get_id(), ALIB_CALLER, ci,
114 "ReleaseShared: Internal error. Acquired by different thread while shared release.");
115 int prevRCnt= DCSReaderCnt.fetch_sub(1);
116 doAssert(prevRCnt > 0, ALIB_CALLER, ci, "ReleaseShared: No shared acquirement.");
117 DCSSRel= ci;
118}
119
120} // namespace [alib::lang]
121
122#endif // ALIB_DEBUG_CRITICAL_SECTIONS
123
#define ALIB_CALLER
Definition alib.inl:1001
void Raise(const lang::CallerInfo &ci, int type, std::string_view domain, TArgs &&... args)
Definition assert.inl:181
unsigned int DBG_CRITICAL_SECTION_YIELD_OR_SLEEP_TIME_IN_NS
const char * File
The name of the source file as given by compiler.
const std::type_info * TypeInfo
The calling type.
int Line
The line number within File.
std::thread::id ThreadID
The ID of the calling thread.
CallerInfo DCSSRel
Source location of the last "reader" seen.
ALIB_DLL void Acquire(const CallerInfo &ci) const
ALIB_DLL void doAssert(bool cond, const CallerInfo &ciAssert, const CallerInfo &ci, const char *headline) const
CallerInfo DCSRel
Source location of the last "reader" seen.
const char * DCSName
The name of this DCS. Used for debug-output.
static ALIB_DLL const char * ASSERTION_FORMAT
CallerInfo DCSSAcq
Source location of acquirement.
CallerInfo DCSAcq
Source location of acquirement.
ALIB_DLL void Release(const CallerInfo &ci) const
ALIB_DLL void AcquireShared(const CallerInfo &ci) const
std::atomic< int > DCSReaderCnt
Tracks enter/exit calls of readers.
ALIB_DLL void ReleaseShared(const CallerInfo &ci) const
std::atomic< int > DCSWriterCnt
Tracks enter/exit calls (including readers)