Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
TurTLE
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
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
TurTLE
TurTLE
Commits
f066e8d5
Commit
f066e8d5
authored
4 years ago
by
Berenger Bramas
Browse files
Options
Downloads
Patches
Plain Diff
Second bug solved (update the lock free bool array)
parent
73ed13b4
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Pipeline
#101776
failed
4 years ago
Stage: build
Stage: test
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
cpp/particles/lock_free_bool_array.hpp
+42
-12
42 additions, 12 deletions
cpp/particles/lock_free_bool_array.hpp
with
42 additions
and
12 deletions
cpp/particles/lock_free_bool_array.hpp
+
42
−
12
View file @
f066e8d5
...
@@ -27,31 +27,61 @@
...
@@ -27,31 +27,61 @@
#define LOCK_FREE_BOOL_ARRAY_HPP
#define LOCK_FREE_BOOL_ARRAY_HPP
#include
<vector>
#include
<vector>
#include
<memory>
#include
<atomic>
#include
<unistd.h>
#include
<cstdio>
#include
<omp.h>
class
lock_free_bool_array
{
class
lock_free_bool_array
{
std
::
vector
<
std
::
unique_ptr
<
long
int
>>
keys
;
static
const
int
Available
=
0
;
static
const
int
Busy
=
1
;
static
const
int
NoOwner
=
-
1
;
struct
Locker
{
Locker
()
:
lock
(
Available
),
ownerId
(
NoOwner
),
counter
(
0
)
{}
std
::
atomic_int
lock
;
std
::
atomic_int
ownerId
;
int
counter
;
};
std
::
vector
<
std
::
unique_ptr
<
Locker
>>
keys
;
public
:
public
:
explicit
lock_free_bool_array
(
const
long
int
inNbKeys
=
512
){
explicit
lock_free_bool_array
(
const
long
int
inNbKeys
=
1024
){
keys
.
resize
(
inNbKeys
);
keys
.
resize
(
inNbKeys
);
for
(
std
::
unique_ptr
<
long
int
>
&
k
:
keys
){
for
(
auto
&
k
:
keys
){
k
.
reset
(
new
long
int
(
0
));
k
.
reset
(
new
Locker
(
));
}
}
}
}
void
lock
(
const
long
int
inKey
){
void
lock
(
const
long
int
inKey
){
volatile
long
int
*
k
=
keys
[
inKey
%
keys
.
size
()].
get
();
Locker
*
k
=
keys
[
inKey
%
keys
.
size
()].
get
();
long
int
res
=
1
;
if
(
k
->
ownerId
.
load
()
!=
omp_get_thread_num
()){
while
(
res
==
1
){
int
expected
=
Available
;
res
=
__sync_val_compare_and_swap
(
k
,
0
,
res
);
while
(
!
std
::
atomic_compare_exchange_strong
(
&
k
->
lock
,
&
expected
,
Busy
)){
usleep
(
1
);
}
k
->
ownerId
.
store
(
omp_get_thread_num
());
k
->
counter
=
0
;
// must remain
}
}
k
->
counter
+=
1
;
assert
(
k
->
lock
.
load
()
==
Busy
);
assert
(
k
->
counter
>=
1
);
assert
(
k
->
ownerId
.
load
()
==
omp_get_thread_num
());
}
}
void
unlock
(
const
long
int
inKey
){
void
unlock
(
const
long
int
inKey
){
volatile
long
int
*
k
=
keys
[
inKey
%
keys
.
size
()].
get
();
Locker
*
k
=
keys
[
inKey
%
keys
.
size
()].
get
();
assert
(
k
&&
*
k
);
assert
(
k
->
lock
.
load
()
==
Busy
);
(
*
k
)
=
0
;
assert
(
k
->
counter
>=
1
);
assert
(
k
->
ownerId
.
load
()
==
omp_get_thread_num
());
k
->
counter
-=
1
;
if
(
k
->
counter
==
0
){
k
->
ownerId
.
store
(
NoOwner
);
k
->
lock
.
store
(
Available
);
}
}
}
};
};
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment