| 1 |
/* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */ |
| 2 |
/* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */ |
| 3 |
|
| 4 |
/* |
| 5 |
* atomic.c - atomic operations |
| 6 |
* |
| 7 |
* Copyright (c) 1984 Taiichi Yuasa and Masami Hagiya |
| 8 |
* Copyright (c) 1990 Giuseppe Attardi |
| 9 |
* Copyright (c) 2001 Juan Jose Garcia Ripoll |
| 10 |
* |
| 11 |
* See file 'LICENSE' for the copyright details. |
| 12 |
* |
| 13 |
*/ |
| 14 |
|
| 15 |
#include <ecl/ecl.h> |
| 16 |
#include <ecl/internal.h> |
| 17 |
|
| 18 |
#ifdef ECL_THREADS |
| 19 |
|
| 20 |
cl_object |
| 21 |
ecl_atomic_get(cl_object *slot) |
| 22 |
{ |
| 23 |
cl_object old; |
| 24 |
do { |
| 25 |
old = (cl_object)AO_load((AO_t*)slot); |
| 26 |
} while (!AO_compare_and_swap_full((AO_t*)slot, (AO_t)old, (AO_t)ECL_NIL)); |
| 27 |
return old; |
| 28 |
} |
| 29 |
|
| 30 |
cl_object |
| 31 |
ecl_atomic_psh(cl_object *slot, cl_object cons) |
| 32 |
{ |
| 33 |
cl_object cdr; |
| 34 |
do { |
| 35 |
cdr = (cl_object)AO_load((AO_t*)slot); |
| 36 |
ECL_RPLACD(cons, cdr); |
| 37 |
} while (!AO_compare_and_swap_full((AO_t*)slot, (AO_t)cdr, (AO_t)cons)); |
| 38 |
return cdr; |
| 39 |
} |
| 40 |
|
| 41 |
cl_object |
| 42 |
ecl_atomic_pop(cl_object *slot) |
| 43 |
{ |
| 44 |
cl_object cons, rest; |
| 45 |
do { |
| 46 |
cons = (cl_object)AO_load((AO_t*)slot); |
| 47 |
rest = CDR(cons); |
| 48 |
} while (!AO_compare_and_swap_full((AO_t*)slot, (AO_t)cons, (AO_t)rest)); |
| 49 |
return cons; |
| 50 |
} |
| 51 |
|
| 52 |
cl_index |
| 53 |
ecl_atomic_index_incf(cl_index *slot) |
| 54 |
{ |
| 55 |
AO_t old; |
| 56 |
AO_t next; |
| 57 |
do { |
| 58 |
old = AO_load((AO_t*)slot); |
| 59 |
next = old+1; |
| 60 |
} while (!AO_compare_and_swap_full((AO_t*)slot, (AO_t)old, (AO_t)next)); |
| 61 |
return (cl_index)next; |
| 62 |
} |
| 63 |
|
| 64 |
#endif /* ECL_THREADS */ |