Skip to content
Snippets Groups Projects
Commit 9e6d9be2 authored by dansa828's avatar dansa828
Browse files

test aba?

parent 958dd5c1
No related branches found
No related tags found
No related merge requests found
......@@ -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 *topNode = stack->head;
node_t *oldNode = 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;
}
}
}
......@@ -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);
......
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment