/* * Paglo Crawler * Copyright (C) 2006-2008 Paglo Labs Inc. All rights reserved. * www.paglo.com * * This program is free software; you can redistribute it and/or modify * it under the terms of version 2 of the GNU General Public License as * published by the Free Software Foundation. * * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ //--------------------------------------------------------------------------- #include "Condition.h" #include "ScanAgentUtils.h" #include "Debug.h" #include //--------------------------------------------------------------------------- TCondition::TCondition(void) { if (pthread_cond_init(&Condition, NULL) != 0) { throw runtime_error("Unable to initialize condition variable"); } if (pthread_mutex_init(&CondMutex, NULL) != 0) { throw runtime_error("Unable to initialize mutex variable"); } } //--------------------------------------------------------------------------- TCondition::~TCondition(void) { pthread_mutex_destroy(&CondMutex); pthread_cond_destroy(&Condition); } //--------------------------------------------------------------------------- void TCondition::Signal(void) { pthread_cond_signal(&Condition); } //--------------------------------------------------------------------------- void TCondition::Broadcast(void) { pthread_cond_broadcast(&Condition); } //--------------------------------------------------------------------------- void TCondition::Wait(void) { pthread_cond_wait(&Condition, &CondMutex); } //--------------------------------------------------------------------------- void TCondition::TimedWait(unsigned int Seconds) { struct timespec TimeOut; /* * The time to wakeup is specified in absolute seconds since the epoch, * so we need to add time() to it. */ TimeOut.tv_sec = time(NULL) + Seconds; TimeOut.tv_nsec = 0; // LogMesg("Waiting for %d seconds.\n", Seconds); int Ret = pthread_cond_timedwait(&Condition, &CondMutex, &TimeOut); // switch (Ret) { // case 0: // LogMesg("pthread_cond_timedwait() exited normally.\n"); // break; // case ETIMEDOUT: // LogMesg("pthread_cond_timedwait() reached time limit.\n"); // break; // case EINTR: // LogMesg("pthread_cond_timedwait() was interrupted.\n"); // break; // } } //--------------------------------------------------------------------------- void TCondition::LockMutex(void) { pthread_mutex_lock(&CondMutex); } //--------------------------------------------------------------------------- void TCondition::UnLockMutex(void) { pthread_mutex_unlock(&CondMutex); } //---------------------------------------------------------------------------