ALib C++ Library
Library Version: 2511 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 if (cond) return;
60
61 assert::Raise( ciAssert, 0, "THREADS", ASSERTION_FORMAT,
62 DCSName, headline, ciAssert.Func,
63 DCSWriterCnt.load() > 0, DCSWriterCnt.load(),
64 DCSReaderCnt.load() > 0, DCSReaderCnt.load(),
65 ci.TypeInfo, ci.Func, ci.File, ci.Line, ci.ThreadID,
66 DCSAcq.TypeInfo, DCSAcq.Func, DCSAcq.File, DCSAcq.Line, DCSAcq.ThreadID,
67 DCSRel.TypeInfo, DCSRel.Func, DCSRel.File, DCSRel.Line, DCSRel.ThreadID,
68 DCSSAcq.TypeInfo,DCSSAcq.Func,DCSSAcq.File,DCSSAcq.Line,DCSSAcq.ThreadID,
69 DCSSRel.TypeInfo,DCSSRel.Func,DCSSRel.File,DCSSRel.Line,DCSSRel.ThreadID );
70}
71
73 doAssert( !DCSLock || DCSLock->DCSIsAcquired(), ALIB_CALLER, ci, "Acquire: Associated Lock not acquired." );
74 doAssert( DCSWriterCnt.load() == 0
75 || DCSAcq.ThreadID == std::this_thread::get_id(), ALIB_CALLER, ci, "Acquired by other thread.");
76 doAssert( DCSReaderCnt.load() == 0, ALIB_CALLER, ci, "Acquired by reader.");
77
78 DCSWriterCnt.fetch_add(1);
79 DCSAcq= ci;
81}
82
84 doAssert(!DCSLock || DCSLock->DCSIsAcquired() , ALIB_CALLER, ci, "Release: Associated lock not acquired." );
85 doAssert(DCSAcq.ThreadID == std::this_thread::get_id(), ALIB_CALLER, ci, "Release: Acquired by other thread.");
87 DCSRel= ci;
88 DCSWriterCnt.fetch_sub(1);
89}
90
92 doAssert( !DCSLock || DCSLock->DCSIsSharedAcquired(), ALIB_CALLER, ci,
93 "AcquireShared: Associated lock not shared-acquired." );
94
96 int wCnt = DCSWriterCnt.load();
97 doAssert(wCnt <= 0 || DCSAcq.ThreadID == std::this_thread::get_id(), ALIB_CALLER, ci,
98 "AcquireShared: Acquired by different thread.");
99 DCSReaderCnt.fetch_add(1);
100 DCSSAcq= ci;
101}
102
104 yieldOrSleep();
105 doAssert( !DCSLock || DCSLock->DCSIsSharedAcquired(), ALIB_CALLER, ci,
106 "ReleaseShared: Associated lock not shared-acquired." );
107
108 doAssert(DCSWriterCnt.load() <= 0 || DCSAcq.ThreadID == std::this_thread::get_id(), ALIB_CALLER, ci,
109 "ReleaseShared: Internal error. Acquired by different thread while shared release.");
110 int prevRCnt= DCSReaderCnt.fetch_sub(1);
111 doAssert(prevRCnt > 0, ALIB_CALLER, ci, "ReleaseShared: No shared acquirement.");
112 DCSSRel= ci;
113}
114
115} // namespace [alib::lang]
116
117#endif // ALIB_DEBUG_CRITICAL_SECTIONS
#define ALIB_CALLER
Definition alib.inl:1018
void Raise(const lang::CallerInfo &ci, int type, std::string_view domain, TArgs &&... args)
Definition assert.inl:181
unsigned 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)