lockingipcfreebsdrwlock

How do I make an interprocess rwlock in FreeBSD?


What is the way to make an interprocess read-write lock in FreeBSD? I'm looking for something like pthread's rwlock that could be set to be shared between processes by "pthread_rwlockattr_setpshared" and PTHREAD_PROCESS_SHARED flag which successfully worked in Linux. But FreeBSD doesn't support this flag. Thank you in advance.


Solution

  • Found a simple and portable solution built on posix semaphores here: http://stupefydeveloper.blogspot.com/2009/03/nix-rw-lock-with-semaphores.html

    #include <semaphore.h>
    #include <errno.h>
    
    struct rwlock {
        sem_t rlock;
        sem_t wlock;
    };
    
    void init_rwlock(struct rwlock *rw) {
        sem_init(&rw->rlock, 1, 0);
        sem_init(&rw->wlock, 1, 1);
    }
    
    void destroy_rwlock(struct rwlock *rw) {
        wlock(rw);
        sem_destroy(&rw->rlock);
        sem_destroy(&rw->wlock);
    }
    
    void rlock(struct rwlock *rw){
        sem_wait(&rw->wlock);
        sem_post(&rw->rlock);
        sem_post(&rw->wlock);
    }
    
    void wlock(struct rwlock *rw) {
        int readers = 0;
        sem_wait(&rw->wlock);
    
        do {
            if (sem_getvalue(&rw->rlock, &readers) != -1) {
                if (readers == 0)
                    break;
            }
            usleep(1);
        } while (1);
    }
    
    void runlock(struct rwlock *rw) {
        do {
            if (sem_trywait(&rw->rlock) == -1) {
                if (errno != EAGAIN)
                    continue;
            }
            break;
        } while (1);
    }
    
    void wunlock(struct rwlock *rw) {
        sem_post(&rw->wlock);
    }