Loading...
Searching...
No Matches
XrdClSyncQueue.hh
Go to the documentation of this file.
//------------------------------------------------------------------------------
// Copyright (c) 2013 by European Organization for Nuclear Research (CERN)
// Author: Lukasz Janyst <ljanyst@cern.ch>
//------------------------------------------------------------------------------
// XRootD is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// XRootD 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 Lesser General Public License
// along with XRootD. If not, see <http://www.gnu.org/licenses/>.
//------------------------------------------------------------------------------
#ifndef __XRD_CL_SYNC_QUEUE_HH__
#define __XRD_CL_SYNC_QUEUE_HH__
#include <queue>
#include "XrdSys/XrdSysPthread.hh"
namespace XrdCl
{
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
template <typename Item>
class SyncQueue
{
public:
//------------------------------------------------------------------------
//------------------------------------------------------------------------
SyncQueue()
{
pSem = new XrdSysSemaphore(0);
};
//------------------------------------------------------------------------
//------------------------------------------------------------------------
~SyncQueue()
{
delete pSem;
}
//------------------------------------------------------------------------
//------------------------------------------------------------------------
void Put( const Item &item )
{
XrdSysMutexHelper scopedLock( pMutex );
pQueue.push( item );
pSem->Post();
}
//------------------------------------------------------------------------
//------------------------------------------------------------------------
Item Get()
{
pSem->Wait();
XrdSysMutexHelper scopedLock( pMutex );
// this is not possible, so when it happens we commit a suicide
if( pQueue.empty() )
abort();
Item i = pQueue.front();
pQueue.pop();
return i;
}
//------------------------------------------------------------------------
//------------------------------------------------------------------------
void Clear()
{
XrdSysMutexHelper scopedLock( pMutex );
while( !pQueue.empty() )
pQueue.pop();
delete pSem;
pSem = new XrdSysSemaphore(0);
}
//------------------------------------------------------------------------
//------------------------------------------------------------------------
bool IsEmpty()
{
XrdSysMutexHelper scopedLock( pMutex );
return pQueue.empty();
}
protected:
std::queue<Item> pQueue;
XrdSysMutex pMutex;
XrdSysSemaphore *pSem;
};
}
#endif // __XRD_CL_ANY_OBJECT_HH__
XrdSysPthread.hh
XrdCl::SyncQueue::IsEmpty
bool IsEmpty()
Check if the queue is empty.
Definition XrdClSyncQueue.hh:93
XrdCl::SyncQueue::Put
void Put(const Item &item)
Put the item in the queue.
Definition XrdClSyncQueue.hh:54
XrdCl::SyncQueue::Clear
void Clear()
Clear the queue.
Definition XrdClSyncQueue.hh:81
XrdCl::SyncQueue::pMutex
XrdSysMutex pMutex
Definition XrdClSyncQueue.hh:101
XrdCl::SyncQueue::SyncQueue
SyncQueue()
Constructor.
Definition XrdClSyncQueue.hh:38
XrdCl::SyncQueue::~SyncQueue
~SyncQueue()
Destructor.
Definition XrdClSyncQueue.hh:46
XrdCl::SyncQueue::pQueue
std::queue< Item > pQueue
Definition XrdClSyncQueue.hh:100
XrdCl::SyncQueue::Get
Item Get()
Get the item from the front of the queue.
Definition XrdClSyncQueue.hh:64
XrdCl::SyncQueue::pSem
XrdSysSemaphore * pSem
Definition XrdClSyncQueue.hh:102
XrdSysMutexHelper
Definition XrdSysPthread.hh:263
XrdSysMutex
Definition XrdSysPthread.hh:165
XrdSysSemaphore
Definition XrdSysPthread.hh:494
XrdCl
Definition XrdClAction.hh:34