Skip to content

Commit

Permalink
libdeng2: Added FIFO, Waitable::reset()
Browse files Browse the repository at this point in the history
WaitableFIFO automatically updates the Waitable semaphore as the
FIFO is used.
  • Loading branch information
skyjake committed May 1, 2013
1 parent 3df4625 commit 17611c7
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 9 deletions.
2 changes: 2 additions & 0 deletions doomsday/libdeng2/data.pri
Expand Up @@ -14,6 +14,7 @@ HEADERS += \
include/de/Counted \
include/de/Date \
include/de/DictionaryValue \
include/de/FIFO \
include/de/FixedByteArray \
include/de/Guard \
include/de/IBlock \
Expand Down Expand Up @@ -62,6 +63,7 @@ HEADERS += \
include/de/data/counted.h \
include/de/data/date.h \
include/de/data/dictionaryvalue.h \
include/de/data/fifo.h \
include/de/data/fixedbytearray.h \
include/de/data/guard.h \
include/de/data/huffman.h \
Expand Down
121 changes: 121 additions & 0 deletions doomsday/libdeng2/include/de/data/fifo.h
@@ -0,0 +1,121 @@
/*
* The Doomsday Engine Project -- libdeng2
*
* Copyright (c) 2004-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* 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, see <http://www.gnu.org/licenses/>.
*/

#ifndef LIBDENG2_FIFO_H
#define LIBDENG2_FIFO_H

#include "../Lockable"
#include "../Guard"

#include <list>

namespace de {

/**
* A template FIFO buffer that maintains pointers to objects. This is a
* thread-safe implementation: lock() and unlock() are automatically called
* when necessary.
*
* @ingroup data
*/
template <typename Type>
class FIFO : public Lockable
{
public:
enum PutMode {
PutHead,
PutTail
};

public:
FIFO() : Lockable() {}

virtual ~FIFO() {
DENG2_GUARD(this);
for(typename Objects::iterator i = _objects.begin(); i != _objects.end(); ++i) {
delete *i;
}
}

/**
* Insert a new object to the buffer.
*
* @param object Object to add to the buffer. FIFO gets ownership.
* @param mode Where to insert the object:
* - PutHead: (default) object is put to the head of the buffer.
* - PutTail: object is put to the tail, meaning it will be the
* next one to come out.
*/
void put(Type *object, PutMode mode = PutHead) {
DENG2_GUARD(this);
if(mode == PutHead) {
_objects.push_front(object);
}
else {
_objects.push_back(object);
}
}

/**
* Takes the oldest object in the buffer.
*
* @return The oldest object in the buffer, or NULL if the buffer is empty.
* Caller gets ownership of the returned object.
*/
Type *take() {
DENG2_GUARD(this);
if(_objects.empty()) return NULL;
Type *last = _objects.back();
_objects.pop_back();
return last;
}

/**
* Returns the oldest object in the buffer.
*
* @return The oldest object in the buffer, or NULL if the buffer is empty.
* The object is not removed from the buffer.
*/
Type* tail() const {
DENG2_GUARD(this);
if(_objects.empty()) return NULL;
return _objects.back();
}

/**
* Determines whether the buffer is empty.
*/
bool isEmpty() const {
DENG2_GUARD(this);
return _objects.empty();
}

void clear() {
DENG2_GUARD(this);
while(!isEmpty()) delete take();
}

private:
typedef std::list<Type *> Objects;
Objects _objects;
};

} // namespace de

#endif // LIBDENG2_FIFO_H
5 changes: 4 additions & 1 deletion doomsday/libdeng2/include/de/data/waitable.h
Expand Up @@ -45,7 +45,10 @@ class DENG2_PUBLIC Waitable
Waitable(duint initialValue = 0);
virtual ~Waitable();

/// Wait until the resource becomes available.
/// Resets the semaphore to zero.
void reset();

/// Wait until the resource becomes available. Waits indefinitely.
void wait();

/// Wait for the specified period of time to secure the
Expand Down
12 changes: 11 additions & 1 deletion doomsday/libdeng2/include/de/data/waitablefifo.h
Expand Up @@ -36,8 +36,18 @@ class WaitableFIFO : public FIFO<Type>, public Waitable
{
public:
WaitableFIFO() {}

void put(Type *object, typename FIFO<Type>::PutMode mode = FIFO<Type>::PutHead) {
FIFO<Type>::put(object, mode);
post();
}

Type *take() {
wait();
return FIFO<Type>::take();
}
};

} // namespace de

#endif /* LIBDENG2_WAITABLEFIFO_H */
#endif // LIBDENG2_WAITABLEFIFO_H
24 changes: 17 additions & 7 deletions doomsday/libdeng2/src/data/waitable.cpp
Expand Up @@ -22,26 +22,36 @@

using namespace de;

#define WAITABLE_TIMEOUT 10

Waitable::Waitable(duint initialValue) : _semaphore(initialValue)
{}

Waitable::~Waitable()
{}

void Waitable::reset()
{
_semaphore.tryAcquire(_semaphore.available());
}

void Waitable::wait()
{
wait(WAITABLE_TIMEOUT);
wait(0.0);
}

void Waitable::wait(TimeDelta const &timeOut)
{
// Wait until the resource becomes available.
if(!_semaphore.tryAcquire(1, int(timeOut.asMilliSeconds())))
if(timeOut <= 0.0)
{
_semaphore.acquire();
}
else
{
/// @throw WaitError Failed to secure the resource due to an error.
throw WaitError("Waitable::wait", "Timed out");
// Wait until the resource becomes available.
if(!_semaphore.tryAcquire(1, int(timeOut.asMilliSeconds())))
{
/// @throw WaitError Failed to secure the resource due to an error.
throw WaitError("Waitable::wait", "Timed out");
}
}
}

Expand Down

0 comments on commit 17611c7

Please sign in to comment.