/* * 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 "PacketQueue.h" #include "Debug.h" //--------------------------------------------------------------------------- TPacketQueue::TPacketQueue(void) { pthread_mutex_init(&QueueMutex, NULL); // use default mutex type QueueCond = new TCondition(); ExternalCond = false; } //--------------------------------------------------------------------------- TPacketQueue::~TPacketQueue(void) { if (!ExternalCond) { delete QueueCond; } Purge(); } //--------------------------------------------------------------------------- void TPacketQueue::Put(TPacket *Pkt) { QueueCond->LockMutex(); pthread_mutex_lock(&QueueMutex); PktQueue.push(Pkt); pthread_mutex_unlock(&QueueMutex); QueueCond->UnLockMutex(); QueueCond->Broadcast(); } //--------------------------------------------------------------------------- TPacket *TPacketQueue::Get(void) { TPacket *Pkt; pthread_mutex_lock(&QueueMutex); if(PktQueue.empty()) { Pkt = NULL; } else { Pkt = PktQueue.front(); PktQueue.pop(); } pthread_mutex_unlock(&QueueMutex); return Pkt; } //--------------------------------------------------------------------------- void TPacketQueue::Purge(void) { TPacket *Pkt; while (!PktQueue.empty()) { Pkt = PktQueue.front(); PktQueue.pop(); delete Pkt; } } //--------------------------------------------------------------------------- void TPacketQueue::SetCond(TCondition *Cond) { if (QueueCond && !ExternalCond) { delete QueueCond; } QueueCond = Cond; ExternalCond = true; } //---------------------------------------------------------------------------