/* Mecanisme de synchronisation pour les lectures/ecritures */
/* Copyright (C) 2008 Emmanuel Varoquaux
This file is part of XOS.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see . */
/* L'acces en lecture est autorise en concurrence, mais l'acces en ecriture est
* exclusif.
* Les demandes d'acces en ecriture sont prioritaires sur les demandes d'acces
* en lecture. */
#include
#include
void rw_lock_init(struct rw_lock_struct *rw_lock)
{
rw_lock->count = 0;
condition_init(&rw_lock->read_condition);
condition_init(&rw_lock->write_condition);
}
int rw_lock_lock_read(struct rw_lock_struct *rw_lock)
{
while (rw_lock->count < 0)
if (!condition_wait_interruptible(&rw_lock->read_condition))
return 0;
rw_lock->count++;
return 1;
}
int rw_lock_lock_write(struct rw_lock_struct *rw_lock)
{
while (rw_lock->count)
if (!condition_wait_interruptible(&rw_lock->write_condition))
return 0;
rw_lock->count = -1;
return 1;
}
void rw_lock_unlock_read(struct rw_lock_struct *rw_lock)
{
rw_lock->count--;
if (!rw_lock->count)
condition_signal(&rw_lock->write_condition);
}
void rw_lock_unlock_write(struct rw_lock_struct *rw_lock)
{
rw_lock->count = 0;
condition_signal(&rw_lock->write_condition);
condition_signal_all(&rw_lock->read_condition);
}