IrisStack

IrisStack — A lock-free stack data structure

Synopsis

#define             IRIS_TYPE_STACK
                    IrisStack;
GType               iris_stack_get_type                 (void);
IrisStack*          iris_stack_new                      (void);
void                iris_stack_push                     (IrisStack *stack,
                                                         gpointer data);
gpointer            iris_stack_pop                      (IrisStack *stack);
IrisStack*          iris_stack_ref                      (IrisStack *stack);
void                iris_stack_unref                    (IrisStack *stack);

Description

IrisStack is a lock-free stack implementation. It is not currently guaranteed to be fully correct. This is due to the classical ABA problem with many lock-free data structures. For more information on ABA, see the wikipedia page at ABA_problem.

We do try to greatly reduce the potential of this happening. This is done by aligning the destination of our internal pointers to sizeof(void*) which leaves us the two lower bits to repurpose as a counter stamp. This makes the pointer different on every compare-and-swap operation allowing us to fail and retry the operation. As you might have guessed, if the ABA problem happens 4 times within the pre-emption time of the first thread, the problem can still exist.

You can typically solve this with an indirection node.

Details

IRIS_TYPE_STACK

#define IRIS_TYPE_STACK (iris_stack_get_type())


IrisStack

typedef struct {
} IrisStack;


iris_stack_get_type ()

GType               iris_stack_get_type                 (void);

Returns :


iris_stack_new ()

IrisStack*          iris_stack_new                      (void);

Creates a new instance of an IrisStack, which is a concurrent, lock-free stack implementation.

Returns :

the newly created IrisStack instance.

iris_stack_push ()

void                iris_stack_push                     (IrisStack *stack,
                                                         gpointer data);

Pushes a new item onto the stack atomically.

stack :

An IrisStack

data :

a pointer

iris_stack_pop ()

gpointer            iris_stack_pop                      (IrisStack *stack);

Pops an item off of the stack atomically. If no item is on the stack, then NULL is returned.

stack :

An IrisStack

Returns :

the most recent item on the stack, or NULL

iris_stack_ref ()

IrisStack*          iris_stack_ref                      (IrisStack *stack);

Atomically increases the reference count of stack by one.

stack :

An IrisStack

Returns :

the passed IrisStack, which the reference count increased by one.

iris_stack_unref ()

void                iris_stack_unref                    (IrisStack *stack);

Atomically decreases the reference count of stack. If the reference count reaches zero, the object is destroyed and all its allocated resources are freed.

stack :

An IrisStack