/**
 * @file rwlock.h
 * @brief Header file for rwlock.c
 * 
 */
 
#ifndef RWLOCK_H_
#define RWLOCK_H_

#include "thread.h"
#include "tqueue.h"
#include "io.h"


struct rwlock {
	/** count of writing thread recursion lock*/
	unsigned int w_count;
	/** count of reading threads */
	unsigned int r_count;
	/** pointer to actually writing thread - for detect recursions */
	thread_t writing_thread;
	/** pointer to queue of threads waiting for write */
	thread_queue_t w_queue;
	/** pointer to queue of threads waiting for read */
	thread_queue_t r_queue;
};

typedef struct rwlock rwlock_t;

void rwlock_init (rwlock_t * rwl);
void rwlock_destroy(rwlock_t * rwl);
void rwlock_read_lock (rwlock_t * rwl);
void rwlock_write_lock (rwlock_t * rwl);
int rwlock_read_timeout (rwlock_t * rwl, const unsigned int usec);
int rwlock_write_timeout (rwlock_t * rwl, const unsigned int usec);
void rwlock_write_unlock (rwlock_t * rwl);
void rwlock_read_unlock (rwlock_t * rwl);

#endif /*RWLOCK_H_*/
