0

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.

Nikita Kanashin
  • 125
  • 1
  • 7

1 Answers1

0

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);
}
Nikita Kanashin
  • 125
  • 1
  • 7
  • Whilst this may theoretically answer the question, [it would be preferable](http://meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Lix Mar 13 '12 at 11:23