Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals  

rwlocks.h File Reference

Go to the source code of this file.

Defines

#define ARTP_RWLOCKS_H   1
#define RW_INIT(x, y, z, wsem, rsem, wcount, rcount)
#define READERS_LOCK(x, y, z, wsem, rsem, wcount, rcount)
#define READERS_UNLOCK(x, y, z, wsem, rsem, wcount, rcount)
#define WRITERS_LOCK(x, y, z, wsem, rsem, wcount, rcount)
#define WRITERS_UNLOCK(x, y, z, wsem, rsem, wcount, rcount)


Detailed Description

Help macros for readers/writers problem.
Author:
Tomas Rebok
Date:
2004

Define Documentation

#define READERS_LOCK x,
y,
z,
wsem,
rsem,
wcount,
rcount   
 

Value:

{\
    pthread_mutex_lock(&z);\
        pthread_mutex_lock(&rsem);\
            pthread_mutex_lock(&x);\
                (rcount)++;\
                if ((rcount) == 1)\
                    pthread_mutex_lock(&wsem);\
            pthread_mutex_unlock(&x);\
        pthread_mutex_unlock(&rsem);\
    pthread_mutex_unlock(&z);\
}
Readers lock. This macro is used for readers. There can be more than one reader simultaneously, but first writer stops new readers till the end of the writer(s) code.

#define READERS_UNLOCK x,
y,
z,
wsem,
rsem,
wcount,
rcount   
 

Value:

{\
    pthread_mutex_lock(&x);\
        (rcount)--;\
        if ((rcount) == 0)\
            pthread_mutex_unlock(&wsem);\
    pthread_mutex_unlock(&x);\
}
Readers unlock. This macro unlocks previously locked readers lock. It decreases the count of readers, too.

#define RW_INIT x,
y,
z,
wsem,
rsem,
wcount,
rcount   
 

Value:

{\
    if ((pthread_mutex_init(&x, NULL) != 0)\
            || (pthread_mutex_init(&y, NULL) != 0)\
            || (pthread_mutex_init(&z, NULL) != 0)\
            || (pthread_mutex_init(&wsem, NULL) != 0)\
            || (pthread_mutex_init(&rsem, NULL) != 0))\
        return E_MEMORY_FAIL;\
    wcount = 0;\
    rcount = 0;\
}
Initialize all necessary structures. This macro initializes all necessary mutexes and counters.

#define WRITERS_LOCK x,
y,
z,
wsem,
rsem,
wcount,
rcount   
 

Value:

{\
    pthread_mutex_lock(&y);\
        (wcount)++;\
        if ((wcount) == 1)\
            pthread_mutex_lock(&rsem);\
    pthread_mutex_unlock(&y);\
    pthread_mutex_lock(&wsem);\
}
Writers lock. This macro is used for writers to lock. There can be only one writer at the moment, no readers are accepted, too. Waiting writer stops all new readers till its end.

#define WRITERS_UNLOCK x,
y,
z,
wsem,
rsem,
wcount,
rcount   
 

Value:

{\
    pthread_mutex_unlock(&wsem);\
    pthread_mutex_lock(&y);\
        (wcount)--;\
        if ((wcount) == 0)\
            pthread_mutex_unlock(&rsem);\
    pthread_mutex_unlock(&y);\
}
Writers unlock. This macro unlocks previously locked writers lock. It decreases the count of writers, too.


Generated on Fri May 21 07:50:25 2004 for Active Router Transport Protocol (ARTP) by doxygen1.2.15