"Fossies" - the Fresh Open Source Software Archive

Member "openpa-1.0.4/src/primitives/opa_nt_intrinsics.h" (5 Dec 2012, 4389 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_nt_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 #ifndef OPA_NT_INTRINSICS_H_INCLUDED
    8 #define OPA_NT_INTRINSICS_H_INCLUDED
    9 
   10 #define WIN32_LEAN_AND_MEAN
   11 #include<windows.h>
   12 #include<intrin.h>
   13 #include "mpi.h"
   14 
   15 /* OPA_int_t uses a long because the compiler intrinsics operate on
   16  * longs instead of ints. */
   17 typedef struct { volatile long v;  } OPA_int_t;
   18 typedef struct { void * volatile v; } OPA_ptr_t;
   19 
   20 #define OPA_INT_T_INITIALIZER(val_) { (val_) }
   21 #define OPA_PTR_T_INITIALIZER(val_) { (val_) }
   22 
   23 #define OPA_write_barrier()      _WriteBarrier()
   24 #define OPA_read_barrier()       _ReadBarrier()
   25 #define OPA_read_write_barrier() _ReadWriteBarrier()
   26 /* FIXME there mut be a more efficient way to implement this.  Is "asm {};"
   27  * sufficient? */
   28 #define OPA_compiler_barrier()   _ReadWriteBarrier()
   29 
   30 static _opa_inline int OPA_load_int(_opa_const OPA_int_t *ptr)
   31 {
   32     return ((int)ptr->v);
   33 }
   34 
   35 static _opa_inline void OPA_store_int(OPA_int_t *ptr, int val)
   36 {
   37     ptr->v = (long)val;
   38 }
   39 
   40 static _opa_inline void *OPA_load_ptr(_opa_const OPA_ptr_t *ptr)
   41 {
   42     return ((void *)ptr->v);
   43 }
   44 
   45 static _opa_inline void OPA_store_ptr(OPA_ptr_t *ptr, void *val)
   46 {
   47     ptr->v = val;
   48 }
   49 
   50 /* NOTE: these acquire/release operations have not been optimized, I just threw
   51  * down a full memory barrier.  Someone with more Windows expertise should feel
   52  * free to improve these (for Windows on x86/x86_64 these almost certainly only
   53  * need to be compiler barriers). */
   54 static _opa_inline int OPA_load_acquire_int(_opa_const OPA_int_t *ptr)
   55 {
   56     int tmp;
   57     tmp = ptr->v;
   58     OPA_read_write_barrier();
   59     return tmp;
   60 }
   61 
   62 static _opa_inline void OPA_store_release_int(OPA_int_t *ptr, int val)
   63 {
   64     OPA_read_write_barrier();
   65     ptr->v = val;
   66 }
   67 
   68 static _opa_inline void *OPA_load_acquire_ptr(_opa_const OPA_ptr_t *ptr)
   69 {
   70     void *tmp;
   71     tmp = ptr->v;
   72     OPA_read_write_barrier();
   73     return tmp;
   74 }
   75 
   76 static _opa_inline void OPA_store_release_ptr(OPA_ptr_t *ptr, void *val)
   77 {
   78     OPA_read_write_barrier();
   79     ptr->v = val;
   80 }
   81 
   82 
   83 static _opa_inline void OPA_add_int(OPA_int_t *ptr, int val)
   84 {
   85     _InterlockedExchangeAdd(&(ptr->v), val);
   86 }
   87 
   88 static _opa_inline void OPA_incr_int(OPA_int_t *ptr)
   89 {
   90     _InterlockedIncrement(&(ptr->v));
   91 }
   92 
   93 static _opa_inline void OPA_decr_int(OPA_int_t *ptr)
   94 {
   95     _InterlockedDecrement(&(ptr->v));
   96 }
   97 
   98 static _opa_inline int OPA_decr_and_test_int(OPA_int_t *ptr)
   99 {
  100     return (_InterlockedDecrement(&(ptr->v)) == 0);
  101 }
  102 
  103 static _opa_inline int OPA_fetch_and_add_int(OPA_int_t *ptr, int val)
  104 {
  105     return ((int)_InterlockedExchangeAdd(&(ptr->v), (long)val));
  106 }
  107 
  108 static _opa_inline void *OPA_cas_ptr(OPA_ptr_t *ptr, void *oldv, void *newv)
  109 {
  110 #if (OPA_SIZEOF_VOID_P == 4)
  111     return ((void *)(LONG_PTR) _InterlockedCompareExchange((LONG volatile *)&(ptr->v),
  112                                                    (LONG)(LONG_PTR)newv,
  113                                                    (LONG)(LONG_PTR)oldv)
  114            );
  115 #elif (OPA_SIZEOF_VOID_P == 8)
  116     return ((void *)(LONG_PTR)_InterlockedCompareExchange64((__int64 *)&(ptr->v),
  117                                                     (INT64)(LONG_PTR)newv,
  118                                                     (INT64)(LONG_PTR)oldv)
  119            );
  120 #else
  121 #error  "OPA_SIZEOF_VOID_P not valid"
  122 #endif
  123 }
  124 
  125 static _opa_inline void *OPA_swap_ptr(OPA_ptr_t *ptr, void *val)
  126 {
  127 #if (OPA_SIZEOF_VOID_P == 4)
  128     return (void *)(LONG_PTR )_InterlockedExchange((LONG volatile *)&(ptr->v),
  129                                                     (LONG)(LONG_PTR)val);
  130 #elif (OPA_SIZEOF_VOID_P == 8)
  131     return (void *)(LONG_PTR)_InterlockedExchange64((LONG64 volatile *)&(ptr->v),
  132                                                      (INT64)(LONG_PTR)val);
  133 #else
  134 #error  "OPA_SIZEOF_VOID_P not valid"
  135 #endif
  136 }
  137 
  138 static _opa_inline int OPA_cas_int(OPA_int_t *ptr, int oldv, int newv)
  139 {
  140     return _InterlockedCompareExchange((long *)&(ptr->v), newv, oldv);
  141 }
  142 
  143 static _opa_inline int OPA_swap_int(OPA_int_t *ptr, int val)
  144 {
  145     return _InterlockedExchange(&(ptr->v), val);
  146 }
  147 
  148 /* Implement fetch_and_incr/decr using fetch_and_add (*_faa) */
  149 #define OPA_fetch_and_incr_int_by_faa OPA_fetch_and_incr_int
  150 #define OPA_fetch_and_decr_int_by_faa OPA_fetch_and_decr_int
  151 
  152 #include "opa_emulated.h"
  153 
  154 #endif /* defined(OPA_NT_INTRINSICS_H_INCLUDED) */