Ejemplo n.º 1
0
 //! Atomically xors a bit with 1
 inline bool xor_bit(size_t b) {
   // use CAS to set the bit
   size_t arrpos, bitpos;
   bit_to_pos(b, arrpos, bitpos);
   const size_t mask(size_t(1) << size_t(bitpos)); 
   return __sync_fetch_and_xor(array + arrpos, mask) & mask;
 }
Ejemplo n.º 2
0
static void
do_hi (void)
{
  if (__sync_fetch_and_add(AL+4, 1) != 0)
    abort ();
  if (__sync_fetch_and_add(AL+5, 4) != 0)
    abort ();
  if (__sync_fetch_and_add(AL+6, 22) != 0)
    abort ();
  if (__sync_fetch_and_sub(AL+7, 12) != 0)
    abort ();
  if (__sync_fetch_and_and(AL+8, 7) != -1)
    abort ();
  if (__sync_fetch_and_or(AL+9, 8) != 0)
    abort ();
  if (__sync_fetch_and_xor(AL+10, 9) != 0)
    abort ();
  if (__sync_fetch_and_nand(AL+11, 7) != -1)
    abort ();

  if (__sync_add_and_fetch(AL+12, 1) != 1)
    abort ();
  if (__sync_sub_and_fetch(AL+13, 12) != -12)
    abort ();
  if (__sync_and_and_fetch(AL+14, 7) != 7)
    abort ();
  if (__sync_or_and_fetch(AL+15, 8) != 8)
    abort ();
  if (__sync_xor_and_fetch(AL+16, 9) != 9)
    abort ();
  if (__sync_nand_and_fetch(AL+17, 7) != ~7)
    abort ();
}
Ejemplo n.º 3
0
static void
do_qi (void)
{
  if (__sync_fetch_and_add(AI+4, 1) != 0)
    abort ();
  if (__sync_fetch_and_add(AI+5, 4) != 0)
    abort ();
  if (__sync_fetch_and_add(AI+6, 22) != 0)
    abort ();
  if (__sync_fetch_and_sub(AI+7, 12) != 0)
    abort ();
  if (__sync_fetch_and_and(AI+8, 7) != (char)-1)
    abort ();
  if (__sync_fetch_and_or(AI+9, 8) != 0)
    abort ();
  if (__sync_fetch_and_xor(AI+10, 9) != 0)
    abort ();
  if (__sync_fetch_and_nand(AI+11, 7) != (char)-1)
    abort ();

  if (__sync_add_and_fetch(AI+12, 1) != 1)
    abort ();
  if (__sync_sub_and_fetch(AI+13, 12) != (char)-12)
    abort ();
  if (__sync_and_and_fetch(AI+14, 7) != 7)
    abort ();
  if (__sync_or_and_fetch(AI+15, 8) != 8)
    abort ();
  if (__sync_xor_and_fetch(AI+16, 9) != 9)
    abort ();
  if (__sync_nand_and_fetch(AI+17, 7) != (char)~7)
    abort ();
}
Ejemplo n.º 4
0
void test_fetch_and_op (void)
{
  sc = __sync_fetch_and_add (&sc, 11);
  uc = __sync_fetch_and_add (&uc, 11);
  ss = __sync_fetch_and_add (&ss, 11);
  us = __sync_fetch_and_add (&us, 11);
  si = __sync_fetch_and_add (&si, 11);
  ui = __sync_fetch_and_add (&ui, 11);
  sll = __sync_fetch_and_add (&sll, 11);
  ull = __sync_fetch_and_add (&ull, 11);

  sc = __sync_fetch_and_sub (&sc, 11);
  uc = __sync_fetch_and_sub (&uc, 11);
  ss = __sync_fetch_and_sub (&ss, 11);
  us = __sync_fetch_and_sub (&us, 11);
  si = __sync_fetch_and_sub (&si, 11);
  ui = __sync_fetch_and_sub (&ui, 11);
  sll = __sync_fetch_and_sub (&sll, 11);
  ull = __sync_fetch_and_sub (&ull, 11);

  sc = __sync_fetch_and_or (&sc, 11);
  uc = __sync_fetch_and_or (&uc, 11);
  ss = __sync_fetch_and_or (&ss, 11);
  us = __sync_fetch_and_or (&us, 11);
  si = __sync_fetch_and_or (&si, 11);
  ui = __sync_fetch_and_or (&ui, 11);
  sll = __sync_fetch_and_or (&sll, 11);
  ull = __sync_fetch_and_or (&ull, 11);

  sc = __sync_fetch_and_xor (&sc, 11);
  uc = __sync_fetch_and_xor (&uc, 11);
  ss = __sync_fetch_and_xor (&ss, 11);
  us = __sync_fetch_and_xor (&us, 11);
  si = __sync_fetch_and_xor (&si, 11);
  ui = __sync_fetch_and_xor (&ui, 11);
  sll = __sync_fetch_and_xor (&sll, 11);
  ull = __sync_fetch_and_xor (&ull, 11);

  sc = __sync_fetch_and_and (&sc, 11);
  uc = __sync_fetch_and_and (&uc, 11);
  ss = __sync_fetch_and_and (&ss, 11);
  us = __sync_fetch_and_and (&us, 11);
  si = __sync_fetch_and_and (&si, 11);
  ui = __sync_fetch_and_and (&ui, 11);
  sll = __sync_fetch_and_and (&sll, 11);
  ull = __sync_fetch_and_and (&ull, 11);

  sc = __sync_fetch_and_nand (&sc, 11);
  uc = __sync_fetch_and_nand (&uc, 11);
  ss = __sync_fetch_and_nand (&ss, 11);
  us = __sync_fetch_and_nand (&us, 11);
  si = __sync_fetch_and_nand (&si, 11);
  ui = __sync_fetch_and_nand (&ui, 11);
  sll = __sync_fetch_and_nand (&sll, 11);
  ull = __sync_fetch_and_nand (&ull, 11);
}
Ejemplo n.º 5
0
void test_op_ignore (void)
{
  (void) __sync_fetch_and_add (&sc, 1);
  (void) __sync_fetch_and_add (&uc, 1);
  (void) __sync_fetch_and_add (&ss, 1);
  (void) __sync_fetch_and_add (&us, 1);
  (void) __sync_fetch_and_add (&si, 1);
  (void) __sync_fetch_and_add (&ui, 1);
  (void) __sync_fetch_and_add (&sll, 1);
  (void) __sync_fetch_and_add (&ull, 1);

  (void) __sync_fetch_and_sub (&sc, 1);
  (void) __sync_fetch_and_sub (&uc, 1);
  (void) __sync_fetch_and_sub (&ss, 1);
  (void) __sync_fetch_and_sub (&us, 1);
  (void) __sync_fetch_and_sub (&si, 1);
  (void) __sync_fetch_and_sub (&ui, 1);
  (void) __sync_fetch_and_sub (&sll, 1);
  (void) __sync_fetch_and_sub (&ull, 1);

  (void) __sync_fetch_and_or (&sc, 1);
  (void) __sync_fetch_and_or (&uc, 1);
  (void) __sync_fetch_and_or (&ss, 1);
  (void) __sync_fetch_and_or (&us, 1);
  (void) __sync_fetch_and_or (&si, 1);
  (void) __sync_fetch_and_or (&ui, 1);
  (void) __sync_fetch_and_or (&sll, 1);
  (void) __sync_fetch_and_or (&ull, 1);

  (void) __sync_fetch_and_xor (&sc, 1);
  (void) __sync_fetch_and_xor (&uc, 1);
  (void) __sync_fetch_and_xor (&ss, 1);
  (void) __sync_fetch_and_xor (&us, 1);
  (void) __sync_fetch_and_xor (&si, 1);
  (void) __sync_fetch_and_xor (&ui, 1);
  (void) __sync_fetch_and_xor (&sll, 1);
  (void) __sync_fetch_and_xor (&ull, 1);

  (void) __sync_fetch_and_and (&sc, 1);
  (void) __sync_fetch_and_and (&uc, 1);
  (void) __sync_fetch_and_and (&ss, 1);
  (void) __sync_fetch_and_and (&us, 1);
  (void) __sync_fetch_and_and (&si, 1);
  (void) __sync_fetch_and_and (&ui, 1);
  (void) __sync_fetch_and_and (&sll, 1);
  (void) __sync_fetch_and_and (&ull, 1);

  (void) __sync_fetch_and_nand (&sc, 1);
  (void) __sync_fetch_and_nand (&uc, 1);
  (void) __sync_fetch_and_nand (&ss, 1);
  (void) __sync_fetch_and_nand (&us, 1);
  (void) __sync_fetch_and_nand (&si, 1);
  (void) __sync_fetch_and_nand (&ui, 1);
  (void) __sync_fetch_and_nand (&sll, 1);
  (void) __sync_fetch_and_nand (&ull, 1);
}
Ejemplo n.º 6
0
//============================================================================
//		NTargetThread::AtomicXor32 : Atomic 32-bit xor.
//----------------------------------------------------------------------------
void NTargetThread::AtomicXor32(UInt32 &theValue, UInt32 theMask)
{


	// Validate our parameters
	NN_ASSERT_ALIGNED_4(&theValue);



	// XOR the value
	__sync_fetch_and_xor(&theValue, theMask);
}
Ejemplo n.º 7
0
unsigned
Atomic::XOR(unsigned value)
{
#if defined(_OPENTHREADS_ATOMIC_USE_GCC_BUILTINS)
    return __sync_fetch_and_xor(&_value, value);
#elif defined(_OPENTHREADS_ATOMIC_USE_WIN32_INTERLOCKED)
    return _InterlockedXor(&_value, value);
#elif defined(_OPENTHREADS_ATOMIC_USE_BSD_ATOMIC)
    return OSAtomicXor32((uint32_t)value, (uint32_t *)&_value);
#else
# error This implementation should happen inline in the include file
#endif
}
Ejemplo n.º 8
0
void test_fetch_and_op (void) // CHECK-LABEL: define void @test_fetch_and_op
{
  sc = __sync_fetch_and_add (&sc, 11); // CHECK: atomicrmw add
  uc = __sync_fetch_and_add (&uc, 11); // CHECK: atomicrmw add
  ss = __sync_fetch_and_add (&ss, 11); // CHECK: atomicrmw add
  us = __sync_fetch_and_add (&us, 11); // CHECK: atomicrmw add
  si = __sync_fetch_and_add (&si, 11); // CHECK: atomicrmw add
  ui = __sync_fetch_and_add (&ui, 11); // CHECK: atomicrmw add
  sll = __sync_fetch_and_add (&sll, 11); // CHECK: atomicrmw add
  ull = __sync_fetch_and_add (&ull, 11); // CHECK: atomicrmw add

  sc = __sync_fetch_and_sub (&sc, 11); // CHECK: atomicrmw sub
  uc = __sync_fetch_and_sub (&uc, 11); // CHECK: atomicrmw sub
  ss = __sync_fetch_and_sub (&ss, 11); // CHECK: atomicrmw sub
  us = __sync_fetch_and_sub (&us, 11); // CHECK: atomicrmw sub
  si = __sync_fetch_and_sub (&si, 11); // CHECK: atomicrmw sub
  ui = __sync_fetch_and_sub (&ui, 11); // CHECK: atomicrmw sub
  sll = __sync_fetch_and_sub (&sll, 11); // CHECK: atomicrmw sub
  ull = __sync_fetch_and_sub (&ull, 11); // CHECK: atomicrmw sub

  sc = __sync_fetch_and_or (&sc, 11); // CHECK: atomicrmw or
  uc = __sync_fetch_and_or (&uc, 11); // CHECK: atomicrmw or
  ss = __sync_fetch_and_or (&ss, 11); // CHECK: atomicrmw or
  us = __sync_fetch_and_or (&us, 11); // CHECK: atomicrmw or
  si = __sync_fetch_and_or (&si, 11); // CHECK: atomicrmw or
  ui = __sync_fetch_and_or (&ui, 11); // CHECK: atomicrmw or
  sll = __sync_fetch_and_or (&sll, 11); // CHECK: atomicrmw or
  ull = __sync_fetch_and_or (&ull, 11); // CHECK: atomicrmw or

  sc = __sync_fetch_and_xor (&sc, 11); // CHECK: atomicrmw xor
  uc = __sync_fetch_and_xor (&uc, 11); // CHECK: atomicrmw xor
  ss = __sync_fetch_and_xor (&ss, 11); // CHECK: atomicrmw xor
  us = __sync_fetch_and_xor (&us, 11); // CHECK: atomicrmw xor
  si = __sync_fetch_and_xor (&si, 11); // CHECK: atomicrmw xor
  ui = __sync_fetch_and_xor (&ui, 11); // CHECK: atomicrmw xor
  sll = __sync_fetch_and_xor (&sll, 11); // CHECK: atomicrmw xor
  ull = __sync_fetch_and_xor (&ull, 11); // CHECK: atomicrmw xor

  sc = __sync_fetch_and_and (&sc, 11); // CHECK: atomicrmw and
  uc = __sync_fetch_and_and (&uc, 11); // CHECK: atomicrmw and
  ss = __sync_fetch_and_and (&ss, 11); // CHECK: atomicrmw and
  us = __sync_fetch_and_and (&us, 11); // CHECK: atomicrmw and
  si = __sync_fetch_and_and (&si, 11); // CHECK: atomicrmw and
  ui = __sync_fetch_and_and (&ui, 11); // CHECK: atomicrmw and
  sll = __sync_fetch_and_and (&sll, 11); // CHECK: atomicrmw and
  ull = __sync_fetch_and_and (&ull, 11); // CHECK: atomicrmw and

}
Ejemplo n.º 9
0
void test_op_ignore (void) // CHECK-LABEL: define void @test_op_ignore
{
  (void) __sync_fetch_and_add (&sc, 1); // CHECK: atomicrmw add i8
  (void) __sync_fetch_and_add (&uc, 1); // CHECK: atomicrmw add i8
  (void) __sync_fetch_and_add (&ss, 1); // CHECK: atomicrmw add i16
  (void) __sync_fetch_and_add (&us, 1); // CHECK: atomicrmw add i16
  (void) __sync_fetch_and_add (&si, 1); // CHECK: atomicrmw add i32
  (void) __sync_fetch_and_add (&ui, 1); // CHECK: atomicrmw add i32
  (void) __sync_fetch_and_add (&sll, 1); // CHECK: atomicrmw add i64
  (void) __sync_fetch_and_add (&ull, 1); // CHECK: atomicrmw add i64

  (void) __sync_fetch_and_sub (&sc, 1); // CHECK: atomicrmw sub i8
  (void) __sync_fetch_and_sub (&uc, 1); // CHECK: atomicrmw sub i8
  (void) __sync_fetch_and_sub (&ss, 1); // CHECK: atomicrmw sub i16
  (void) __sync_fetch_and_sub (&us, 1); // CHECK: atomicrmw sub i16
  (void) __sync_fetch_and_sub (&si, 1); // CHECK: atomicrmw sub i32
  (void) __sync_fetch_and_sub (&ui, 1); // CHECK: atomicrmw sub i32
  (void) __sync_fetch_and_sub (&sll, 1); // CHECK: atomicrmw sub i64
  (void) __sync_fetch_and_sub (&ull, 1); // CHECK: atomicrmw sub i64

  (void) __sync_fetch_and_or (&sc, 1); // CHECK: atomicrmw or i8
  (void) __sync_fetch_and_or (&uc, 1); // CHECK: atomicrmw or i8
  (void) __sync_fetch_and_or (&ss, 1); // CHECK: atomicrmw or i16
  (void) __sync_fetch_and_or (&us, 1); // CHECK: atomicrmw or i16
  (void) __sync_fetch_and_or (&si, 1); // CHECK: atomicrmw or i32
  (void) __sync_fetch_and_or (&ui, 1); // CHECK: atomicrmw or i32
  (void) __sync_fetch_and_or (&sll, 1); // CHECK: atomicrmw or i64
  (void) __sync_fetch_and_or (&ull, 1); // CHECK: atomicrmw or i64

  (void) __sync_fetch_and_xor (&sc, 1); // CHECK: atomicrmw xor i8
  (void) __sync_fetch_and_xor (&uc, 1); // CHECK: atomicrmw xor i8
  (void) __sync_fetch_and_xor (&ss, 1); // CHECK: atomicrmw xor i16
  (void) __sync_fetch_and_xor (&us, 1); // CHECK: atomicrmw xor i16
  (void) __sync_fetch_and_xor (&si, 1); // CHECK: atomicrmw xor i32
  (void) __sync_fetch_and_xor (&ui, 1); // CHECK: atomicrmw xor i32
  (void) __sync_fetch_and_xor (&sll, 1); // CHECK: atomicrmw xor i64
  (void) __sync_fetch_and_xor (&ull, 1); // CHECK: atomicrmw xor i64

  (void) __sync_fetch_and_and (&sc, 1); // CHECK: atomicrmw and i8
  (void) __sync_fetch_and_and (&uc, 1); // CHECK: atomicrmw and i8
  (void) __sync_fetch_and_and (&ss, 1); // CHECK: atomicrmw and i16
  (void) __sync_fetch_and_and (&us, 1); // CHECK: atomicrmw and i16
  (void) __sync_fetch_and_and (&si, 1); // CHECK: atomicrmw and i32
  (void) __sync_fetch_and_and (&ui, 1); // CHECK: atomicrmw and i32
  (void) __sync_fetch_and_and (&sll, 1); // CHECK: atomicrmw and i64
  (void) __sync_fetch_and_and (&ull, 1); // CHECK: atomicrmw and i64

}
Ejemplo n.º 10
0
void *async_write(void *argv)
{
    pthread_detach(pthread_self());

    struct timeval interval;
    while (1)
    {
        interval.tv_sec = 0;
        interval.tv_usec = 1000 * TIME_OUT;
        select(0, NULL, NULL, NULL, &interval);

        check_file++;
        if (log_fd_invalid != 1 && check_file > 100) //10s
        {
            check_file = 0;
            if (access(PERSIST_LOG, R_OK) != 0) //not exist
            {
                log_fd_invalid = 1;
            }
        }

        if (1 == log_fd_invalid)
        {
            if (open_log_file(PERSIST_LOG) < 0)
            {
                printf("open log file failed\n");
                continue; //logdaemon without log
            }
        }

        int index = __sync_fetch_and_xor(&g_index, 1);

        pthread_mutex_lock(&g_locks[index]);

        int wpos = g_buffer_pos[index];
        if (wpos > 0 && my_write(log_file, (char *)g_buffer[index], wpos) != 0)
        {
            printf("write error\n");
            log_fd_invalid = 1;
        }
        g_buffer_pos[index] = 0;

        pthread_mutex_unlock(&g_locks[index]);
    }
    return NULL;
}
/* Now check return values.  */
static void
do_ret_di (void)
{
  if (__sync_val_compare_and_swap (AL+0, 0x100000002ll, 0x1234567890ll) !=
	0x100000002ll) abort ();
  if (__sync_bool_compare_and_swap (AL+1, 0x200000003ll, 0x1234567890ll) !=
	1) abort ();
  if (__sync_lock_test_and_set (AL+2, 1) != 0) abort ();
  __sync_lock_release (AL+3); /* no return value, but keep to match results.  */

  /* The following tests should not change the value since the
     original does NOT match.  */
  if (__sync_val_compare_and_swap (AL+4, 0x000000002ll, 0x1234567890ll) !=
	0x100000002ll) abort ();
  if (__sync_val_compare_and_swap (AL+5, 0x100000000ll, 0x1234567890ll) !=
	0x100000002ll) abort ();
  if (__sync_bool_compare_and_swap (AL+6, 0x000000002ll, 0x1234567890ll) !=
	0) abort ();
  if (__sync_bool_compare_and_swap (AL+7, 0x100000000ll, 0x1234567890ll) !=
	0) abort ();

  if (__sync_fetch_and_add (AL+8, 1) != 0) abort ();
  if (__sync_fetch_and_add (AL+9, 0xb000e0000000ll) != 0x1000e0de0000ll) abort ();
  if (__sync_fetch_and_sub (AL+10, 22) != 42) abort ();
  if (__sync_fetch_and_sub (AL+11, 0xb000e0000000ll) != 0xc001c0de0000ll)
	abort ();

  if (__sync_fetch_and_and (AL+12, 0x300000007ll) != -1ll) abort ();
  if (__sync_fetch_and_or (AL+13, 0x500000009ll) != 0) abort ();
  if (__sync_fetch_and_xor (AL+14, 0xe00000001ll) != 0xff00ff0000ll) abort ();
  if (__sync_fetch_and_nand (AL+15, 0xa00000007ll) != -1ll) abort ();

  /* These should be the same as the fetch_and_* cases except for
     return value.  */
  if (__sync_add_and_fetch (AL+16, 1) != 1) abort ();
  if (__sync_add_and_fetch (AL+17, 0xb000e0000000ll) != 0xc001c0de0000ll)
	abort ();
  if (__sync_sub_and_fetch (AL+18, 22) != 20) abort ();
  if (__sync_sub_and_fetch (AL+19, 0xb000e0000000ll) != 0x1000e0de0000ll)
	abort ();

  if (__sync_and_and_fetch (AL+20, 0x300000007ll) != 0x300000007ll) abort ();
  if (__sync_or_and_fetch (AL+21, 0x500000009ll) != 0x500000009ll) abort ();
  if (__sync_xor_and_fetch (AL+22, 0xe00000001ll) != 0xf100ff0001ll) abort ();
  if (__sync_nand_and_fetch (AL+23, 0xa00000007ll) != ~0xa00000007ll) abort ();
}
Ejemplo n.º 12
0
static void
do_noret_di (void)
{
  __sync_val_compare_and_swap(AL+0, 0, 1);
  __sync_bool_compare_and_swap(AL+1, 0, 1);
  __sync_lock_test_and_set(AL+2, 1);
  __sync_lock_release(AL+3);

  __sync_fetch_and_add(AL+4, 1);
  __sync_fetch_and_add(AL+5, 4);
  __sync_fetch_and_add(AL+6, 22);
  __sync_fetch_and_sub(AL+7, 12);
  __sync_fetch_and_and(AL+8, 7);
  __sync_fetch_and_or(AL+9, 8);
  __sync_fetch_and_xor(AL+10, 9);
  __sync_fetch_and_nand(AL+11, 7);
}
Ejemplo n.º 13
0
static void
do_di (void)
{
  if (__sync_val_compare_and_swap(AL+0, 0, 1) != 0)
    abort ();
  if (__sync_val_compare_and_swap(AL+0, 0, 1) != 1)
    abort ();
  if (__sync_bool_compare_and_swap(AL+1, 0, 1) != 1)
    abort ();
  if (__sync_bool_compare_and_swap(AL+1, 0, 1) != 0)
    abort ();

  if (__sync_lock_test_and_set(AL+2, 1) != 0)
    abort ();

  if (__sync_fetch_and_add(AL+4, 1) != 0)
    abort ();
  if (__sync_fetch_and_add(AL+5, 4) != 0)
    abort ();
  if (__sync_fetch_and_add(AL+6, 22) != 0)
    abort ();
  if (__sync_fetch_and_sub(AL+7, 12) != 0)
    abort ();
  if (__sync_fetch_and_and(AL+8, 7) != -1)
    abort ();
  if (__sync_fetch_and_or(AL+9, 8) != 0)
    abort ();
  if (__sync_fetch_and_xor(AL+10, 9) != 0)
    abort ();
  if (__sync_fetch_and_nand(AL+11, 7) != 0)
    abort ();

  if (__sync_add_and_fetch(AL+12, 1) != 1)
    abort ();
  if (__sync_sub_and_fetch(AL+13, 12) != -12)
    abort ();
  if (__sync_and_and_fetch(AL+14, 7) != 7)
    abort ();
  if (__sync_or_and_fetch(AL+15, 8) != 8)
    abort ();
  if (__sync_xor_and_fetch(AL+16, 9) != 9)
    abort ();
  if (__sync_nand_and_fetch(AL+17, 7) != 7)
    abort ();
}
/* First check they work in terms of what they do to memory.  */
static void
do_noret_di (void)
{
  __sync_val_compare_and_swap (AL+0, 0x100000002ll, 0x1234567890ll);
  __sync_bool_compare_and_swap (AL+1, 0x200000003ll, 0x1234567890ll);
  __sync_lock_test_and_set (AL+2, 1);
  __sync_lock_release (AL+3);

  /* The following tests should not change the value since the
     original does NOT match.  */
  __sync_val_compare_and_swap (AL+4, 0x000000002ll, 0x1234567890ll);
  __sync_val_compare_and_swap (AL+5, 0x100000000ll, 0x1234567890ll);
  __sync_bool_compare_and_swap (AL+6, 0x000000002ll, 0x1234567890ll);
  __sync_bool_compare_and_swap (AL+7, 0x100000000ll, 0x1234567890ll);

  __sync_fetch_and_add (AL+8, 1);
  __sync_fetch_and_add (AL+9, 0xb000e0000000ll); /* + to both halves & carry.  */
  __sync_fetch_and_sub (AL+10, 22);
  __sync_fetch_and_sub (AL+11, 0xb000e0000000ll);

  __sync_fetch_and_and (AL+12, 0x300000007ll);
  __sync_fetch_and_or (AL+13, 0x500000009ll);
  __sync_fetch_and_xor (AL+14, 0xe00000001ll);
  __sync_fetch_and_nand (AL+15, 0xa00000007ll);

  /* These should be the same as the fetch_and_* cases except for
     return value.  */
  __sync_add_and_fetch (AL+16, 1);
  /* add to both halves & carry.  */
  __sync_add_and_fetch (AL+17, 0xb000e0000000ll);
  __sync_sub_and_fetch (AL+18, 22);
  __sync_sub_and_fetch (AL+19, 0xb000e0000000ll);

  __sync_and_and_fetch (AL+20, 0x300000007ll);
  __sync_or_and_fetch (AL+21, 0x500000009ll);
  __sync_xor_and_fetch (AL+22, 0xe00000001ll);
  __sync_nand_and_fetch (AL+23, 0xa00000007ll);
}
void *thread_fetch_and_xor(void *arg)
{
	for(int i = 0; i < 9999; ++i) // Odd number of times so that the operation doesn't cancel itself out.
		__sync_fetch_and_xor((T*)&fetch_and_xor_data, *(T*)arg);
	pthread_exit(0);
}
Ejemplo n.º 16
0
 T fetchAndXor(T value)      { return __sync_fetch_and_xor(&_value, value); }
NOMIPS16 int
f1 (int *z)
{
  return __sync_fetch_and_xor (z, 42);
}
NOMIPS16 char
f3 (char *z)
{
  return __sync_fetch_and_xor (z, 42);
}
NOMIPS16 short
f2 (short *z)
{
  return __sync_fetch_and_xor (z, 42);
}
int main()
{
	{
		T x = HILO(5, 3);
		T y = __sync_fetch_and_add(&x, DUP(1));
		assert(y == HILO(5, 3));
		assert(x == HILO(6, 4));
		volatile T n = HILO(2, 1);
		if (emscripten_has_threading_support())
		{
			for(int i = 0; i < NUM_THREADS; ++i) pthread_create(&thread[i], NULL, thread_fetch_and_add, (void*)&n);
			for(int i = 0; i < NUM_THREADS; ++i) pthread_join(thread[i], NULL);
			printf("n: %llx\n", n);
			assert(n == HILO(NUM_THREADS*10000ULL+2ULL, NUM_THREADS*10000ULL+1ULL));
		}
	}
	{
		T x = HILO(15, 13);
		T y = __sync_fetch_and_sub(&x, HILO(10, 10));
		assert(y == HILO(15, 13));
		assert(x == HILO(5, 3));
		volatile T n = HILO(NUM_THREADS*10000ULL+5ULL, NUM_THREADS*10000ULL+3ULL);
		if (emscripten_has_threading_support())
		{
			for(int i = 0; i < NUM_THREADS; ++i) pthread_create(&thread[i], NULL, thread_fetch_and_sub, (void*)&n);
			for(int i = 0; i < NUM_THREADS; ++i) pthread_join(thread[i], NULL);
			printf("n: %llx\n", n);
			assert(n == HILO(5,3));
		}
	}
	{
		T x = HILO(32768 + 5, 5);
		T y = __sync_fetch_and_or(&x, HILO(65536 + 9, 9));
		assert(y == HILO(32768 + 5, 5));
		assert(x == HILO(32768 + 65536 + 13, 13));
		if (emscripten_has_threading_support())
		{
			for(int x = 0; x < 100; ++x) // Test a few times for robustness, since this test is so short-lived.
			{
				fetch_and_or_data = HILO(65536 + (1<<NUM_THREADS), 1<<NUM_THREADS);
				for(int i = 0; i < NUM_THREADS; ++i)
				{
					threadArg[i] = DUP(1 << i);
					pthread_create(&thread[i], NULL, thread_fetch_and_or, (void*)&threadArg[i]);
				}
				for(int i = 0; i < NUM_THREADS; ++i) pthread_join(thread[i], NULL);
				assert(fetch_and_or_data == HILO(65536 + (1<<(NUM_THREADS+1))-1, (1<<(NUM_THREADS+1))-1));
			}
		}
	}
	{
		T x = HILO(32768 + 5, 5);
		T y = __sync_fetch_and_and(&x, HILO(32768 + 9, 9));
		assert(y == HILO(32768 + 5, 5));
		assert(x == HILO(32768 + 1, 1));
		if (emscripten_has_threading_support())
		{
			for(int x = 0; x < 100; ++x) // Test a few times for robustness, since this test is so short-lived.
			{
				fetch_and_and_data = HILO(65536 + (1<<(NUM_THREADS+1))-1, (1<<(NUM_THREADS+1))-1);
				for(int i = 0; i < NUM_THREADS; ++i)
				{
					threadArg[i] = DUP(~(1UL<<i));
					pthread_create(&thread[i], NULL, thread_fetch_and_and, (void*)&threadArg[i]);
				}
				for(int i = 0; i < NUM_THREADS; ++i) pthread_join(thread[i], NULL);
				assert(fetch_and_and_data == HILO(65536 + (1<<NUM_THREADS), 1<<NUM_THREADS));
			}
		}
	}
	{
		T x = HILO(32768 + 5, 5);
		T y = __sync_fetch_and_xor(&x, HILO(16384 + 9, 9));
		assert(y == HILO(32768 + 5, 5));
		assert(x == HILO(32768 + 16384 + 12, 12));
		if (emscripten_has_threading_support())
		{
			for(int x = 0; x < 100; ++x) // Test a few times for robustness, since this test is so short-lived.
			{
				fetch_and_xor_data = HILO(32768 + (1<<NUM_THREADS), 1<<NUM_THREADS);
				for(int i = 0; i < NUM_THREADS; ++i)
				{
					threadArg[i] = DUP(~(1UL<<i));
					pthread_create(&thread[i], NULL, thread_fetch_and_xor, (void*)&threadArg[i]);
				}
				for(int i = 0; i < NUM_THREADS; ++i) pthread_join(thread[i], NULL);
				assert(fetch_and_xor_data == HILO(32768 + ((1<<(NUM_THREADS+1))-1), (1<<(NUM_THREADS+1))-1));
			}
		}
	}
// XXX NAND support does not exist in Atomics API.
#if 0
	{
		T x = 5;
		T y = __sync_fetch_and_nand(&x, 9);
		assert(y == 5);
		assert(x == -2);
		const int oddNThreads = NUM_THREADS-1;
		for(int x = 0; x < 100; ++x) // Test a few times for robustness, since this test is so short-lived.
		{
			fetch_and_nand_data = 0;
			for(int i = 0; i < oddNThreads; ++i) pthread_create(&thread[i], NULL, thread_fetch_and_nand, (void*)-1);
			for(int i = 0; i < oddNThreads; ++i) pthread_join(thread[i], NULL);
			assert(fetch_and_nand_data == -1);
		}
	}
#endif

#ifdef REPORT_RESULT
	int result = 0;
	REPORT_RESULT();
#endif
}