Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
emper
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Lehrstuhl für Informatik 4 (Systemsoftware)
manycore
emper
Merge requests
!415
Add WaitFreeCountingSemaphore
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Add WaitFreeCountingSemaphore
flow/emper:wait-free-counting-semaphore
into
master
Overview
0
Commits
1
Pipelines
3
Changes
5
Merged
Florian Schmaus
requested to merge
flow/emper:wait-free-counting-semaphore
into
master
2 years ago
Overview
0
Commits
1
Pipelines
3
Changes
5
Expand
0
0
Merge request reports
Compare
master
version 2
0a3530c8
2 years ago
version 1
42cb15e1
2 years ago
master (base)
and
latest version
latest version
81259105
1 commit,
6 months ago
version 2
0a3530c8
1 commit,
2 years ago
version 1
42cb15e1
1 commit,
2 years ago
5 files
+
164
−
0
Side-by-side
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
5
Search (e.g. *.vue) (Ctrl+P)
emper/WaitFreeCountingPrivateSemaphore.cpp
0 → 100644
+
63
−
0
Options
// SPDX-License-Identifier: LGPL-3.0-or-later
// Copyright © 2020-2022 Florian Schmaus
#include
"WaitFreeCountingPrivateSemaphore.hpp"
#include
<cassert>
#include
<limits>
#include
<ostream>
#include
"Context.hpp"
#include
"Debug.hpp"
WaitFreeCountingPrivateSemaphore
::
WaitFreeCountingPrivateSemaphore
()
:
WaitFreeCountingPrivateSemaphore
(
0
)
{}
WaitFreeCountingPrivateSemaphore
::
WaitFreeCountingPrivateSemaphore
(
unsigned
int
counter
)
:
requiredSignalCount
(
counter
),
counter
(
std
::
numeric_limits
<
unsigned
int
>::
max
()),
blockedContext
(
nullptr
)
{}
void
WaitFreeCountingPrivateSemaphore
::
incrementCounterByOne
()
{
requiredSignalCount
++
;
}
void
WaitFreeCountingPrivateSemaphore
::
incrementCounter
(
unsigned
int
count
)
{
requiredSignalCount
+=
count
;
}
void
WaitFreeCountingPrivateSemaphore
::
wait
()
{
unsigned
int
delta
=
std
::
numeric_limits
<
unsigned
int
>::
max
()
-
requiredSignalCount
;
LOGD
(
"wait() counter: "
<<
counter
<<
" delta: "
<<
delta
);
if
(
counter
-
delta
==
0
)
{
return
;
}
Context
*
blockedContext
=
Context
::
getCurrentContext
();
block
([
this
,
blockedContext
,
delta
]
{
assert
(
blockedContext
>
(
Context
*
)
4096
);
// this->blockedContext.store(blockedContext); // MO relaxed possible
this
->
blockedContext
=
blockedContext
;
unsigned
int
oldCounter
=
counter
.
fetch_sub
(
delta
);
// MO release
LOGD
(
"wait() block oldCounter: "
<<
oldCounter
<<
" delta: "
<<
delta
);
if
(
oldCounter
==
delta
)
{
unblock
(
blockedContext
);
}
});
}
auto
WaitFreeCountingPrivateSemaphore
::
signalInternal
()
->
Context
*
{
unsigned
int
oldCounter
=
counter
.
fetch_sub
(
1
);
assert
(
oldCounter
>=
1
);
LOGD
(
"signalInternal(): oldCounter: "
<<
oldCounter
);
// If the counter is still non-zero after the decrement, somebody
// else is responsible for scheduling the fiber.
if
(
oldCounter
>
1
)
{
return
nullptr
;
}
// Context* context = blockedContext.load();
Context
*
context
=
blockedContext
;
EMPER_ASSERT_MSG
(
!
context
||
context
>
(
Context
*
)
4096
,
"Unexpected context value: "
<<
context
);
return
context
;
}
Loading