"Fossies" - the Fresh Open Source Software Archive

Member "openpa-1.0.4/src/primitives/opa_gcc_intrinsics.h" (5 Dec 2012, 3668 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_gcc_intrinsics.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 /* FIXME needs to be converted to new style functions with OPA_int_t/OPA_ptr_t-types */
    8 #ifndef OPA_GCC_INTRINSICS_H_INCLUDED
    9 #define OPA_GCC_INTRINSICS_H_INCLUDED
   10 
   11 /* FIXME do we need to align these? */
   12 typedef struct { volatile int v;    } OPA_int_t;
   13 typedef struct { void * volatile v; } OPA_ptr_t;
   14 
   15 #define OPA_INT_T_INITIALIZER(val_) { (val_) }
   16 #define OPA_PTR_T_INITIALIZER(val_) { (val_) }
   17 
   18 /* Assume that loads/stores are atomic on the current platform, even though this
   19    may not be true at all. */
   20 static _opa_inline int OPA_load_int(_opa_const OPA_int_t *ptr)
   21 {
   22     return ptr->v;
   23 }
   24 
   25 static _opa_inline void OPA_store_int(OPA_int_t *ptr, int val)
   26 {
   27     ptr->v = val;
   28 }
   29 
   30 static _opa_inline void *OPA_load_ptr(_opa_const OPA_ptr_t *ptr)
   31 {
   32     return ptr->v;
   33 }
   34 
   35 static _opa_inline void OPA_store_ptr(OPA_ptr_t *ptr, void *val)
   36 {
   37     ptr->v = val;
   38 }
   39 
   40 static _opa_inline int OPA_load_acquire_int(_opa_const OPA_int_t *ptr)
   41 {
   42     volatile int i = 0;
   43     int tmp;
   44     tmp = ptr->v;
   45     __sync_lock_test_and_set(&i, 1); /* guarantees acquire semantics */
   46     return tmp;
   47 }
   48 
   49 static _opa_inline void OPA_store_release_int(OPA_int_t *ptr, int val)
   50 {
   51     volatile int i = 1;
   52     __sync_lock_release(&i); /* guarantees release semantics */
   53     ptr->v = val;
   54 }
   55 
   56 static _opa_inline void *OPA_load_acquire_ptr(_opa_const OPA_ptr_t *ptr)
   57 {
   58     volatile int i = 0;
   59     void *tmp;
   60     tmp = ptr->v;
   61     __sync_lock_test_and_set(&i, 1); /* guarantees acquire semantics */
   62     return tmp;
   63 }
   64 
   65 static _opa_inline void OPA_store_release_ptr(OPA_ptr_t *ptr, void *val)
   66 {
   67     volatile int i = 1;
   68     __sync_lock_release(&i); /* guarantees release semantics */
   69     ptr->v = val;
   70 }
   71 
   72 
   73 /* gcc atomic intrinsics accept an optional list of variables to be
   74    protected by a memory barrier.  These variables are labeled
   75    below by "protected variables :". */
   76 
   77 static _opa_inline int OPA_fetch_and_add_int(OPA_int_t *ptr, int val)
   78 {
   79     return __sync_fetch_and_add(&ptr->v, val, /* protected variables: */ &ptr->v);
   80 }
   81 
   82 static _opa_inline int OPA_decr_and_test_int(OPA_int_t *ptr)
   83 {
   84     return __sync_sub_and_fetch(&ptr->v, 1, /* protected variables: */ &ptr->v) == 0;
   85 }
   86 
   87 #define OPA_fetch_and_incr_int_by_faa OPA_fetch_and_incr_int
   88 #define OPA_fetch_and_decr_int_by_faa OPA_fetch_and_decr_int
   89 #define OPA_add_int_by_faa OPA_add_int
   90 #define OPA_incr_int_by_fai OPA_incr_int
   91 #define OPA_decr_int_by_fad OPA_decr_int
   92 
   93 
   94 static _opa_inline void *OPA_cas_ptr(OPA_ptr_t *ptr, void *oldv, void *newv)
   95 {
   96     return __sync_val_compare_and_swap(&ptr->v, oldv, newv, /* protected variables: */ &ptr->v);
   97 }
   98 
   99 static _opa_inline int OPA_cas_int(OPA_int_t *ptr, int oldv, int newv)
  100 {
  101     return __sync_val_compare_and_swap(&ptr->v, oldv, newv, /* protected variables: */ &ptr->v);
  102 }
  103 
  104 #ifdef SYNC_LOCK_TEST_AND_SET_IS_SWAP
  105 static _opa_inline void *OPA_swap_ptr(OPA_ptr_t *ptr, void *val)
  106 {
  107     return __sync_lock_test_and_set(&ptr->v, val, /* protected variables: */ &ptr->v);
  108 }
  109 
  110 static _opa_inline int OPA_swap_int(OPA_int_t *ptr, int val)
  111 {
  112     return __sync_lock_test_and_set(&ptr->v, val, /* protected variables: */ &ptr->v);
  113 }
  114 
  115 #else
  116 #define OPA_swap_ptr_by_cas OPA_swap_ptr
  117 #define OPA_swap_int_by_cas OPA_swap_int 
  118 #endif
  119 
  120 #define OPA_write_barrier()      __sync_synchronize()
  121 #define OPA_read_barrier()       __sync_synchronize()
  122 #define OPA_read_write_barrier() __sync_synchronize()
  123 #define OPA_compiler_barrier()   __asm__ __volatile__  ( ""  ::: "memory" )
  124 
  125 
  126 
  127 #include"opa_emulated.h"
  128 
  129 #endif /* OPA_GCC_INTRINSICS_H_INCLUDED */