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
!101
Synchronize LockedUnboundedQueue with pthread_rwlock
Code
Review changes
Check out branch
Download
Patches
Plain diff
Closed
Synchronize LockedUnboundedQueue with pthread_rwlock
aj46ezos/emper:pthread_rwlock
into
master
Overview
0
Commits
2
Pipelines
4
Changes
4
Closed
Florian Fischer
requested to merge
aj46ezos/emper:pthread_rwlock
into
master
4 years ago
Overview
0
Commits
2
Pipelines
4
Changes
4
Expand
0
0
Merge request reports
Compare
master
version 3
8f7d1fc3
4 years ago
version 2
74745814
4 years ago
version 1
1e56e193
4 years ago
master (base)
and
latest version
latest version
bc61d41f
2 commits,
4 years ago
version 3
8f7d1fc3
2 commits,
4 years ago
version 2
74745814
2 commits,
4 years ago
version 1
1e56e193
2 commits,
4 years ago
4 files
+
64
−
19
Side-by-side
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
4
Search (e.g. *.vue) (Ctrl+P)
emper/lib/adt/LockedUnboundedQueue.hpp
+
57
−
6
Options
@@ -2,7 +2,9 @@
@@ -2,7 +2,9 @@
// Copyright © 2020 Florian Schmaus
// Copyright © 2020 Florian Schmaus
#pragma once
#pragma once
#include
<mutex>
#include
<pthread.h>
#include
<cstring>
#include
<queue>
#include
<queue>
namespace
lib
::
adt
{
namespace
lib
::
adt
{
@@ -10,23 +12,72 @@ namespace lib::adt {
@@ -10,23 +12,72 @@ namespace lib::adt {
template
<
typename
I
>
template
<
typename
I
>
class
LockedUnboundedQueue
{
class
LockedUnboundedQueue
{
private:
private:
std
::
mutex
queue_mutex
;
pthread_rwlock_t
lock
;
std
::
queue
<
I
*>
queue
;
std
::
queue
<
I
*>
queue
;
public:
public:
LockedUnboundedQueue
()
{
int
err
=
pthread_rwlock_init
(
&
lock
,
nullptr
);
if
(
unlikely
(
err
))
{
DIE_MSG
(
"pthread_rwlock_init failed: "
<<
strerror
(
err
));
}
}
~
LockedUnboundedQueue
()
{
int
err
=
pthread_rwlock_destroy
(
&
lock
);
if
(
unlikely
(
err
))
{
DIE_MSG
(
"pthread_rwlock_destroy failed: "
<<
strerror
(
err
));
}
}
void
enqueue
(
I
*
item
)
{
void
enqueue
(
I
*
item
)
{
std
::
lock_guard
<
std
::
mutex
>
lock
(
queue_mutex
);
int
err
=
pthread_rwlock_wrlock
(
&
lock
);
if
(
unlikely
(
err
))
{
DIE_MSG
(
"pthread_rwlock_wrlock failed: "
<<
strerror
(
err
));
}
queue
.
push
(
item
);
queue
.
push
(
item
);
pthread_rwlock_unlock
(
&
lock
);
}
}
auto
dequeue
()
->
I
*
{
auto
dequeue
()
->
I
*
{
std
::
lock_guard
<
std
::
mutex
>
lock
(
queue_mutex
);
I
*
res
=
nullptr
;
int
err
=
pthread_rwlock_rdlock
(
&
lock
);
if
(
unlikely
(
err
))
{
DIE_MSG
(
"pthread_rwlock_rdlock failed: "
<<
strerror
(
err
));
}
if
(
queue
.
empty
())
{
if
(
queue
.
empty
())
{
return
nullptr
;
goto
unlock_and_return
;
}
}
I
*
res
=
queue
.
front
();
// try to upgrade to wrlock
err
=
pthread_rwlock_trywrlock
(
&
lock
);
if
(
err
)
{
if
(
unlikely
(
err
!=
EBUSY
))
{
DIE_MSG
(
"pthread_rwlock_trylock failed: "
<<
strerror
(
err
));
}
// drop the read lock and aquire a write lock
pthread_rwlock_unlock
(
&
lock
);
int
err
=
pthread_rwlock_wrlock
(
&
lock
);
if
(
unlikely
(
err
))
{
DIE_MSG
(
"pthread_rwlock_wrlock failed: "
<<
strerror
(
err
));
}
if
(
queue
.
empty
())
{
goto
unlock_and_return
;
}
}
// we certainly hold the wrlock here
res
=
queue
.
front
();
queue
.
pop
();
queue
.
pop
();
unlock_and_return
:
pthread_rwlock_unlock
(
&
lock
);
return
res
;
return
res
;
}
}
};
};
Loading