//https://github.com/cloudwu/skynet/blob/master/skynet-src/rwlock.h
struct rwlock { 
    int write; 
 int read;
};

void rwlock_init(struct rwlock *lock)

    lock->write = 0; 
    lock->read = 0;
}

void rwlock_rlock(struct rwlock *lock)
{
    for (;;) {
        while(lock->write)
  {
            __sync_synchronize();
        }
        __sync_add_and_fetch(&lock->read,1);
        if (lock->write)
        {
            __sync_sub_and_fetch(&lock->read,1);
        } else {
            break;
        } 
    }
}

void rwlock_wlock(struct rwlock *lock)

    while (__sync_lock_test_and_set(&lock->write,1)) {} 
 while(lock->read) {
        __sync_synchronize();
    }
}

void rwlock_wunlock(struct rwlock *lock)
{
    __sync_lock_release(&lock->write);
}

void rwlock_runlock(struct rwlock *lock)
{
    __sync_sub_and_fetch(&lock->read,1);
}

 

arrow
arrow
    全站熱搜

    hedgezzz 發表在 痞客邦 留言(0) 人氣()