/* Event queues. Copyright 2002 Johan Rydberg, jrydberg@rtmk.org. 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 2 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, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include "evq.h" /* Initialize event queue EVQ. */ void evq_init (struct evq *evq) { evq->mutex = PTHREAD_MUTEX_INITIALIZER; evq->cond = PTHREAD_COND_INITIALIZER; evq->evqh = evq->evqt = evq->eventq; } /* Wait for an event to be put on EVQ. We return the event. */ unsigned int evq_wait (struct evq *evq) { unsigned int event; pthread_mutex_lock (& evq->mutex); while (evq->evqh == evq->evqt) pthread_cond_wait (& evq->cond, & evq->mutex); event = *evq->evqh++; if (evq->evqh == & evq->eventq [MAX_EVENTS]) evq->evqh = evq->eventq; pthread_mutex_unlock (& evq->mutex); return event; } /* Put EVENT on EVQ. */ void evq_signal (struct evq *evq, unsigned int event) { pthread_mutex_lock (& evq->mutex); *evq->evqt++ = event; if (evq->evqt == & evq->eventq [MAX_EVENTS]) evq->evqt = evq->eventq; pthread_mutex_unlock (& evq->mutex); pthread_cond_signal (& evq->cond); }