On-demand answers appearing here in red
typedef struct node {
long key;
struct node *next;
struct node *mr_next; /* for limbo list */
} node_t;
/* Visible to all threads; initially NULL. */
node_t *stack_top;
void push (long t)
{
node_t *n = new_node ();
n->key = t;
do {
node->next = stack_top;
} while (!CAS(&stack_top, node, node->next));
}
CAS returns TRUE on success and FALSE on failure.
int pop(long *t)
{
node_t *first, *second;
do {
first = stack_top;
if (first != NULL) {
second = first->next;
} else return 0; /* FALSE - stack is empty, can't pop */
} while (!CAS(&stack_top, first, second));
t = first->key;
free_node_later(first);
return 1; /* TRUE - t contains value popped off stack */
}