/*************************************************************************** * Copyright (C) 2005 by Jeff Ferr * * root@sat * * * * 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 "jcondition.h" #include "jthreadexception.h" #include namespace jthread { Condition::Condition(int nblock) { if (pthread_cond_init(&_condition, NULL) != 0) { //&cond) != 0) { throw ThreadException("Init condition error !"); } _count = 0; _nblock = nblock; } Condition::~Condition() { /* while (pthread_cond_destroy(&_condition) == EBUSY) { NotifyAll(); } */ pthread_cond_destroy(&_condition); } void Condition::Wait() { _monitor.Lock(); if (_count == 0) { if (pthread_cond_wait(&_condition, &_monitor._mutex) != 0) { throw ThreadException("Condition wait error !"); } } _count--; _monitor.Unlock(); } void Condition::Wait(struct timespec time_) { _monitor.Lock(); if (_count == 0) { if (pthread_cond_timedwait(&_condition, &_monitor._mutex, &time_) != 0) { throw ThreadException("Condition wait error !"); } } _count--; _monitor.Unlock(); } void Condition::Notify() { _monitor.Lock(); _count += _nblock; if (_count == _nblock) { if (pthread_cond_signal(&_condition) != 0) { throw ThreadException("Condition notify error !"); } } _monitor.Unlock(); } void Condition::NotifyAll() { _monitor.Lock(); _count += _nblock; if (_count == _nblock) { if (pthread_cond_broadcast(&_condition) != 0) { throw ThreadException("Condition notify all error !"); } } _monitor.Unlock(); } };