Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Multicore GPU programming
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
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
Ylva Selling
Multicore GPU programming
Commits
9e6d9be2
Commit
9e6d9be2
authored
4 years ago
by
dansa828
Browse files
Options
Downloads
Patches
Plain Diff
test aba?
parent
958dd5c1
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
Lab2/stack.c
+46
-5
46 additions, 5 deletions
Lab2/stack.c
Lab2/stack.h
+3
-0
3 additions, 0 deletions
Lab2/stack.h
Lab2/test.c
+51
-0
51 additions, 0 deletions
Lab2/test.c
with
100 additions
and
5 deletions
Lab2/stack.c
+
46
−
5
View file @
9e6d9be2
...
...
@@ -155,13 +155,14 @@ stack_pop(stack_t* stack, stack_t* free_stack)
#elif NON_BLOCKING == 1
// Implement a harware CAS-based stack
while
(
1
)
{
node_t
*
top
Node
=
stack
->
head
;
node_t
*
old
Node
=
stack
->
head
;
if
(
stack
->
head
==
NULL
)
return
NULL
;
node_t
*
newTop
=
topNode
->
next
;
if
(
cas
((
size_t
*
)
&
stack
->
head
,
(
size_t
)
topNode
,
(
size_t
)
newTop
)
!=
(
size_t
)
topNode
){
stack_push_free
(
free_stack
,
topNode
);
return
topNode
->
value
;
node_t
*
newNode
=
oldNode
->
next
;
if
(
cas
((
size_t
*
)
&
stack
->
head
,
(
size_t
)
oldNode
,
(
size_t
)
newNode
)
!=
(
size_t
)
oldNode
){
stack_push_free
(
free_stack
,
oldNode
);
stack
->
size
--
;
return
oldNode
->
value
;
}
}
#else
...
...
@@ -195,3 +196,43 @@ node_t* allocate_node(stack_t* stack){
stack
->
size
++
;
return
newNode
;
}
void
stack_push_ABA
(
stack_t
*
stack
,
stack_t
*
free_stack
,
int
id
)
{
node_t
*
newNode
=
stack_pop_free
(
free_stack
);
newNode
->
value
=
id
;
node_t
*
oldNode
;
do
{
oldNode
=
stack
->
head
;
newNode
->
next
=
oldNode
;
}
while
(
cas
((
size_t
*
)
&
stack
->
head
,
(
size_t
)
oldNode
,
(
size_t
)
newNode
)
!=
(
size_t
)
oldNode
);
stack
->
size
++
;
}
int
stack_pop_ABA
(
stack_t
*
stack
,
stack_t
*
free_stack
,
int
id
)
{
while
(
1
)
{
node_t
*
oldNode
=
stack
->
head
;
if
(
stack
->
head
==
NULL
)
return
NULL
;
node_t
*
newNode
=
oldNode
->
next
;
//if(id == 0)
if
(
cas
((
size_t
*
)
&
stack
->
head
,
(
size_t
)
oldNode
,
(
size_t
)
newNode
)
!=
(
size_t
)
oldNode
)
{
stack_push_free
(
free_stack
,
oldNode
);
stack
->
size
--
;
return
oldNode
->
value
;
}
}
}
This diff is collapsed.
Click to expand it.
Lab2/stack.h
+
3
−
0
View file @
9e6d9be2
...
...
@@ -45,6 +45,9 @@ void stack_init(stack_t* stack, int no_of_nodes) ;
void
stack_push
(
stack_t
*
stack
,
stack_t
*
free_stack
,
int
data
);
int
stack_pop
(
stack_t
*
stack
,
stack_t
*
free_stack
);
void
stack_push_ABA
(
stack_t
*
stack
,
stack_t
*
free_stack
,
int
id
);
int
stack_pop_ABA
(
stack_t
*
stack
,
stack_t
*
free_stack
,
int
id
);
void
stack_push_free
(
stack_t
*
stack
,
node_t
*
node
);
node_t
*
stack_pop_free
(
stack_t
*
stack
);
...
...
This diff is collapsed.
Click to expand it.
Lab2/test.c
+
51
−
0
View file @
9e6d9be2
...
...
@@ -85,6 +85,13 @@ stack_t *stack;
data_t
data
;
stack_t
*
free_stacks
[
NB_THREADS
];
struct
stack_test_ABA_arg
{
int
id
;
stack_t
*
stack
;
stack_t
*
free_stack
;
};
typedef
struct
stack_test_ABA_arg
stack_test_ABA_arg_t
;
#if MEASURE != 0
struct
stack_measure_arg
...
...
@@ -219,12 +226,56 @@ test_pop_safe()
// 3 Threads should be enough to raise and detect the ABA problem
#define ABA_NB_THREADS 3
//unpack args
void
*
test_push_ABA
(
void
*
arg
)
{
stack_test_ABA_arg_t
*
args
=
(
stack_test_ABA_arg_t
*
)
arg
;
stack_push_ABA
(
args
->
stack
,
args
->
free_stack
,
args
->
id
);
return
NULL
;
}
void
*
test_pop_ABA
(
void
*
arg
)
{
stack_test_ABA_arg_t
*
args
=
(
stack_test_ABA_arg_t
*
)
arg
;
stack_pop_ABA
(
args
->
stack
,
args
->
free_stack
,
args
->
id
);
return
NULL
;
}
int
test_aba
()
{
#if NON_BLOCKING == 1 || NON_BLOCKING == 2
node_t
*
C
=
allocate_node
(
stack
);
node_t
*
B
=
allocate_node
(
stack
);
node_t
*
A
=
allocate_node
(
stack
);
int
success
,
aba_detected
=
0
;
// Write here a test for the ABA problem
int
i
;
pthread_t
thread
[
ABA_NB_THREADS
];
pthread_attr_t
attr
;
stack_test_ABA_arg_t
arg
[
ABA_NB_THREADS
];
pthread_attr_init
(
&
attr
);
for
(
i
=
0
;
i
<
ABA_NB_THREADS
;
i
++
)
{
arg
[
i
].
id
=
i
;
arg
[
i
].
stack
=
stack
;
arg
[
i
].
free_stack
=
free_stacks
[
i
];
}
//Lock thread 0 until thread 1 has pushed
pthread_mutex_lock
(
&
);
pthread_create
(
&
thread
[
0
],
&
attr
,
test_pop_ABA
,
(
void
*
)
&
arg
[
0
]);
pthread_create
(
&
thread
[
1
],
&
attr
,
test_pop_ABA
,
(
void
*
)
&
arg
[
1
]);
pthread_create
(
&
thread
[
2
],
&
attr
,
test_pop_ABA
,
(
void
*
)
&
arg
[
2
]);
pthread_create
(
&
thread
[
1
],
&
attr
,
test_push_ABA
,
(
void
*
)
&
arg
[
1
]);
for
(
i
=
0
;
i
<
ABA_NB_THREADS
;
i
++
)
{
pthread_join
(
thread
[
i
],
NULL
);
}
success
=
aba_detected
;
return
success
;
#else
...
...
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