/* Verrou d'exclusion mutuelle */ /* 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 . */ /* Note : pendant la phase d'initialisation, mutex_lock() et mutex_unlock() * peuvent etre executes avant l'initialisation des processus (printk()...). * Dans ce contexte ces fonctions sont toujours passantes, il n'y a donc aucun * risque d'acces a des donnees non initialisees. */ #include #include void mutex_init(struct mutex_struct *mutex) { mutex->locked = 0; condition_init(&mutex->condition); } int mutex_lock(struct mutex_struct *mutex) { while (mutex->locked) if (!condition_wait_interruptible(&mutex->condition)) return 0; mutex->locked = 1; return 1; } void mutex_unlock(struct mutex_struct *mutex) { mutex->locked = 0; condition_signal(&mutex->condition); }