"Fossies" - the Fresh Open Source Software Archive

Member "openpa-1.0.4/src/primitives/opa_unsafe.h" (5 Dec 2012, 3684 Bytes) of package /linux/misc/openpa-1.0.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "opa_unsafe.h" see the Fossies "Dox" file reference documentation.

    1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
    2 /*
    3  *  (C) 2008 by Argonne National Laboratory.
    4  *      See COPYRIGHT in top-level directory.
    5  */
    6 
    7 #ifndef OPA_UNSAFE_H_INCLUDED
    8 #define OPA_UNSAFE_H_INCLUDED
    9 
   10 /* NOTE: These types intentionally do not use volatile in order to provide even
   11  * better performance.  If a good use case for having unsafe operations with
   12  * volatile types comes up, we can make two definitions that are conditional on
   13  * something sort of like NDEBUG. [goodell@ 2009-08-18] */
   14 typedef struct { int v;  } OPA_int_t;
   15 typedef struct { int *v; } OPA_ptr_t;
   16 
   17 #define OPA_INT_T_INITIALIZER(val_) { (val_) }
   18 #define OPA_PTR_T_INITIALIZER(val_) { (val_) }
   19 
   20 /*
   21     Unsafe Primitives
   22     -----------------
   23 
   24     These are versions of the atomic primitives that emulate the single-threaded
   25     behavior of the primitives but do not attempt to provide any safety against
   26     concurrent use.  The primary use case for this implementation is to avoid
   27     overhead without code changes in client libraries and applications that have
   28     been compiled for single-threaded use only.  They might also be useful for
   29     testing (and testing of tests) in some cases.
   30 
   31     Thanks to Josh Haberman for inspiring this primitives implementation.
   32 */
   33 
   34 static _opa_inline int OPA_load_int(const OPA_int_t *ptr)
   35 {
   36     int retval;
   37     retval = ptr->v;
   38     return retval;
   39 }
   40 
   41 static _opa_inline void OPA_store_int(OPA_int_t *ptr, int val)
   42 {
   43     ptr->v = val;
   44 }
   45 
   46 static _opa_inline void *OPA_load_ptr(const OPA_ptr_t *ptr)
   47 {
   48     int *retval;
   49     retval = ptr->v;
   50     return retval;
   51 }
   52 
   53 static _opa_inline void OPA_store_ptr(OPA_ptr_t *ptr, void *val)
   54 {
   55     ptr->v = val;
   56 }
   57 
   58 /* no barrier needed, not even a compiler barrier */
   59 #define OPA_load_acquire_int(ptr_)       OPA_load_int((ptr_))
   60 #define OPA_store_release_int(ptr_,val_) OPA_store_int((ptr_),(val_))
   61 #define OPA_load_acquire_ptr(ptr_)       OPA_load_ptr((ptr_))
   62 #define OPA_store_release_ptr(ptr_,val_) OPA_store_ptr((ptr_),(val_))
   63 
   64 static _opa_inline void OPA_add_int(OPA_int_t *ptr, int val)
   65 {
   66     ptr->v += val;
   67 }
   68 
   69 static _opa_inline void *OPA_cas_ptr(OPA_ptr_t *ptr, int *oldv, int *newv)
   70 {
   71     int *prev;
   72     prev = ptr->v;
   73     if (prev == oldv) {
   74         ptr->v = newv;
   75     }
   76     return prev;
   77 }
   78 
   79 static _opa_inline int OPA_cas_int(OPA_int_t *ptr, int oldv, int newv)
   80 {
   81     int prev;
   82     prev = ptr->v;
   83     if (prev == oldv) {
   84         ptr->v = newv;
   85     }
   86     return prev;
   87 }
   88 
   89 static _opa_inline int OPA_decr_and_test_int(OPA_int_t *ptr)
   90 {
   91     int new_val;
   92     new_val = --(ptr->v);
   93     return (0 == new_val);
   94 }
   95 
   96 static _opa_inline void OPA_decr_int(OPA_int_t *ptr)
   97 {
   98     --(ptr->v);
   99 }
  100 
  101 static _opa_inline int OPA_fetch_and_add_int(OPA_int_t *ptr, int val)
  102 {
  103     int prev;
  104     prev = ptr->v;
  105     ptr->v += val;
  106     return prev;
  107 }
  108 
  109 static _opa_inline int OPA_fetch_and_decr_int(OPA_int_t *ptr)
  110 {
  111     int prev;
  112     prev = ptr->v;
  113     --(ptr->v);
  114     return prev;
  115 }
  116 
  117 static _opa_inline int OPA_fetch_and_incr_int(OPA_int_t *ptr)
  118 {
  119     int prev;
  120     prev = ptr->v;
  121     ++(ptr->v);
  122     return prev;
  123 }
  124 
  125 static _opa_inline void OPA_incr_int(OPA_int_t *ptr)
  126 {
  127     ++(ptr->v);
  128 }
  129 
  130 static _opa_inline void *OPA_swap_ptr(OPA_ptr_t *ptr, void *val)
  131 {
  132     int *prev;
  133     prev = ptr->v;
  134     ptr->v = val;
  135     return prev;
  136 }
  137 
  138 static _opa_inline int OPA_swap_int(OPA_int_t *ptr, int val)
  139 {
  140     int prev;
  141     prev = ptr->v;
  142     ptr->v = val;
  143     return (int)prev;
  144 }
  145 
  146 /* null barriers */
  147 #define OPA_write_barrier()      do {} while (0)
  148 #define OPA_read_barrier()       do {} while (0)
  149 #define OPA_read_write_barrier() do {} while (0)
  150 #define OPA_compiler_barrier()   do {} while (0)
  151 
  152 #endif /* !defined(OPA_UNSAFE_H_INCLUDED) */