<commit_before>#ifndef _chpl_atomics_h_
#define _chpl_atomics_h_

#include "chpltypes.h"
#include <assert.h>

// required to use __builtin_ia32_mfence
#ifdef _CRAYC
  #include <intrinsics.h>
#endif

typedef int_least8_t atomic_int_least8_t;
typedef int_least16_t atomic_int_least16_t;
typedef int_least32_t atomic_int_least32_t;
typedef int_least64_t atomic_int_least64_t;
typedef uint_least8_t atomic_uint_least8_t;
typedef uint_least16_t atomic_uint_least16_t;
typedef uint_least32_t atomic_uint_least32_t;
typedef uint_least64_t atomic_uint_least64_t;
typedef uintptr_t atomic_uintptr_t;
typedef chpl_bool atomic_flag;
typedef _real64 atomic__real64;
typedef _real32 atomic__real32;

typedef enum {
 memory_order_relaxed,
 memory_order_consume,
 memory_order_acquire,
 memory_order_release,
 memory_order_acq_rel,
 memory_order_seq_cst
} memory_order;


// Cray does not support __sync_synchronize so we use a cray specific memory
// fence. Cray also does not support __sync_bool_compare_and_swap so we 
// cheat our way around this using __sync_val_compare_and_swap
#ifdef _CRAYC
  #define full_memory_barrier __builtin_ia32_mfence
  
  # define my__sync_bool_compare_and_swap(obj, expected, desired) \
  (__sync_val_compare_and_swap(obj, expected, desired) == expected) 
#else
  #define full_memory_barrier __sync_synchronize
 
  # define my__sync_bool_compare_and_swap(obj, expected, desired) \
  __sync_bool_compare_and_swap(obj, expected, desired) 
#endif


static inline void atomic_thread_fence(memory_order order)
{
  full_memory_barrier();
}
static inline void atomic_signal_thread_fence(memory_order order)
{
  full_memory_barrier();
}


///////////////////////////////////////////////////////////////////////////////
////               Test & Set and Clear for flag(boolean)                 ////
//////////////////////////////////////////////////////////////////////////////
static inline chpl_bool atomic_flag_test_and_set_explicit(atomic_flag *obj, memory_order order) {
 return __sync_fetch_and_or(obj, true); 
}
static inline chpl_bool atomic_flag_test_and_set(atomic_flag *obj) {
  return atomic_flag_test_and_set_explicit(obj, memory_order_seq_cst);
}

static inline void atomic_flag_clear_explicit(atomic_flag *obj, memory_order order) {
  __sync_fetch_and_and(obj, false);
}
static inline void atomic_flag_clear(atomic_flag *obj) {
  atomic_flag_clear_explicit(obj, memory_order_seq_cst);
}


///////////////////////////////////////////////////////////////////////////////
////                      START OF INTEGER ATOMICS BASE                   ////
//////////////////////////////////////////////////////////////////////////////
#define DECLARE_ATOMICS_BASE(type, basetype) \
static inline chpl_bool atomic_is_lock_free_ ## type(atomic_ ## type * obj) { \
  return true; \
} \<commit_msg>Make atomic_flag_test_and_set_explicit return a dummy value<commit_after>#ifndef _chpl_atomics_h_
#define _chpl_atomics_h_

#include "chpltypes.h"
#include <assert.h>

// required to use __builtin_ia32_mfence
#ifdef _CRAYC
  #include <intrinsics.h>
#endif

typedef int_least8_t atomic_int_least8_t;
typedef int_least16_t atomic_int_least16_t;
typedef int_least32_t atomic_int_least32_t;
typedef int_least64_t atomic_int_least64_t;
typedef uint_least8_t atomic_uint_least8_t;
typedef uint_least16_t atomic_uint_least16_t;
typedef uint_least32_t atomic_uint_least32_t;
typedef uint_least64_t atomic_uint_least64_t;
typedef uintptr_t atomic_uintptr_t;
typedef chpl_bool atomic_flag;
typedef _real64 atomic__real64;
typedef _real32 atomic__real32;

typedef enum {
 memory_order_relaxed,
 memory_order_consume,
 memory_order_acquire,
 memory_order_release,
 memory_order_acq_rel,
 memory_order_seq_cst
} memory_order;


// Cray does not support __sync_synchronize so we use a cray specific memory
// fence. Cray also does not support __sync_bool_compare_and_swap so we 
// cheat our way around this using __sync_val_compare_and_swap
#ifdef _CRAYC
  #define full_memory_barrier __builtin_ia32_mfence
  
  # define my__sync_bool_compare_and_swap(obj, expected, desired) \
  (__sync_val_compare_and_swap(obj, expected, desired) == expected) 
#else
  #define full_memory_barrier __sync_synchronize
 
  # define my__sync_bool_compare_and_swap(obj, expected, desired) \
  __sync_bool_compare_and_swap(obj, expected, desired) 
#endif


static inline void atomic_thread_fence(memory_order order)
{
  full_memory_barrier();
}
static inline void atomic_signal_thread_fence(memory_order order)
{
  full_memory_barrier();
}


///////////////////////////////////////////////////////////////////////////////
////               Test & Set and Clear for flag(boolean)                 ////
//////////////////////////////////////////////////////////////////////////////
static inline chpl_bool atomic_flag_test_and_set_explicit(atomic_flag *obj, memory_order order) {
 return __sync_fetch_and_or(obj, true); 
}
static inline chpl_bool atomic_flag_test_and_set(atomic_flag *obj) {
  return atomic_flag_test_and_set_explicit(obj, memory_order_seq_cst);
}

// We don't really need the return here, but with chpl_developer set gcc
// complains about a value computed is not used 
static inline atomic_flag atomic_flag_clear_explicit(atomic_flag *obj, memory_order order) {
  return __sync_fetch_and_and(obj, false);
}
static inline void atomic_flag_clear(atomic_flag *obj) {
  atomic_flag_clear_explicit(obj, memory_order_seq_cst);
}


///////////////////////////////////////////////////////////////////////////////
////                      START OF INTEGER ATOMICS BASE                   ////
//////////////////////////////////////////////////////////////////////////////
#define DECLARE_ATOMICS_BASE(type, basetype) \
static inline chpl_bool atomic_is_lock_free_ ## type(atomic_ ## type * obj) { \
  return true; \
} \
{'fraction_non_alphanumeric': 0.16637960730279022, 'fraction_numerical': 0.013778849466069583, 'mean_word_length': 3.0722300140252456, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3011771', 'n_tokens_mistral': 1942, 'n_tokens_neox': 1786, 'n_words': 394}