Exemplo n.º 1
0
void * test_and_set_thr(void * id)
{
  unsigned long i;

  for (i = 0; i < NITERS/10; ++i)
    {
      while (AO_test_and_set_acquire(&lock) != AO_TS_CLEAR);
      ++locked_counter;
      if (locked_counter != 1)
        {
          fprintf(stderr, "Test and set failure 1, counter = %ld, id = %d\n",
                  (long)locked_counter, (int)(AO_PTRDIFF_T)id);
          abort();
        }
      locked_counter *= 2;
      locked_counter -= 1;
      locked_counter *= 5;
      locked_counter -= 4;
      if (locked_counter != 1)
        {
          fprintf(stderr, "Test and set failure 2, counter = %ld, id = %d\n",
                  (long)locked_counter, (int)(AO_PTRDIFF_T)id);
          abort();
        }
      --locked_counter;
      AO_CLEAR(&lock);
      /* Spend a bit of time outside the lock. */
        junk *= 17;
        junk *= 17;
    }
  return 0;
}
Exemplo n.º 2
0
static void lock_ool(volatile AO_TS_t *l)
{
  int i = 0;

  while (AO_test_and_set_acquire(l) == AO_TS_SET)
    AO_pause(++i);
}
void spinlock_lock(AO_TS_t * lock) {
  // your implementation here
while(AO_test_and_set_acquire(lock))
{
}

}
static void lock_ool(volatile AO_TS_T *l)
{
    int i = 0;
    struct timeval tv;

    while (AO_test_and_set_acquire(l) == AO_TS_SET) {
        if (++i < 12)
            spin(i);
        else
        {
            /* Short async-signal-safe sleep. */
            tv.tv_sec = 0;
            tv.tv_usec = (i > 28? 100000 : (1 << (i - 12)));
            select(0, 0, 0, 0, &tv);
        }
    }
}
AO_INLINE void lock(volatile AO_TS_T *l)
{
    if (AO_test_and_set_acquire(l) == AO_TS_SET)
        lock_ool(l);
}