Author: axeld Date: 2009-12-08 10:40:07 +0100 (Tue, 08 Dec 2009) New Revision: 34548 Changeset: http://dev.haiku-os.org/changeset/34548/haiku Modified: haiku/trunk/src/servers/media/Queue.cpp haiku/trunk/src/servers/media/Queue.h Log: * Cleanup. Modified: haiku/trunk/src/servers/media/Queue.cpp =================================================================== --- haiku/trunk/src/servers/media/Queue.cpp 2009-12-08 00:01:05 UTC (rev 34547) +++ haiku/trunk/src/servers/media/Queue.cpp 2009-12-08 09:40:07 UTC (rev 34548) @@ -1,90 +1,81 @@ -/* +/* * Copyright 2002, Marcus Overhagen. All rights reserved. * Distributed under the terms of the MIT License. */ -/* This is a simple multi thread save queue. - * - * One thread calls AddItem() to add items, and when it - * is finished doing so, it calls Terminate(). Another - * thread calls RemoveItem() to remove items from the - * queue. RemoveItem() blocks when no items are available. - * As soon as Terminate() is called and the queue is empty, - * RemoveItem() returns NULL. - */ - -#include <List.h> -#include <OS.h> -#include <Locker.h> + +/*! This is a simple multi thread save queue. + + One thread calls AddItem() to add items, and when it is finished doing + so, it calls Terminate(). Another thread calls RemoveItem() to remove + items from the queue. RemoveItem() blocks when no items are available. + As soon as Terminate() is called and the queue is empty, RemoveItem() + returns NULL. +*/ + + #include "Queue.h" +#include <Autolock.h> +#include <OS.h> + + Queue::Queue() - : fList(new BList), - fLocker(new BLocker("queue locker")), + : + BLocker("queue locker"), fSem(create_sem(0, "queue sem")) { } + Queue::~Queue() { - if (fSem > 0) + if (fSem >= 0) delete_sem(fSem); - delete fLocker; - delete fList; } + status_t Queue::Terminate() { - status_t rv; + BAutolock _(this); - fLocker->Lock(); - if (fSem < 0) { - rv = B_ERROR; - } else { - delete_sem(fSem); - fSem = -1; - rv = B_OK; - } - fLocker->Unlock(); - return rv; + if (fSem < 0) + return B_ERROR; + + delete_sem(fSem); + fSem = -1; + return B_OK; } - + + status_t -Queue::AddItem(void *item) +Queue::AddItem(void* item) { - status_t rv; + BAutolock _(this); - fLocker->Lock(); - if (fSem < 0) { - rv = B_ERROR; - } else { - if (fList->AddItem(item)) { // AddItem returns a bool - release_sem(fSem); - rv = B_OK; - } else { - rv = B_ERROR; - } - } - fLocker->Unlock(); - return rv; + if (fSem < 0) + return B_ERROR; + + if (!fList.AddItem(item)) + return B_NO_MEMORY; + + release_sem(fSem); + return B_OK; } -void * + +void* Queue::RemoveItem() { - void *item; - // if the semaphore is deleted by Terminate(), // this will no longer block while (acquire_sem(fSem) == B_INTERRUPTED) ; - + + BAutolock _(this); + // if the list is empty, which can only happen after // Terminate() was called, item will be NULL - fLocker->Lock(); - item = fList->RemoveItem((int32)0); - fLocker->Unlock(); - - return item; + return fList.RemoveItem((int32)0); } Modified: haiku/trunk/src/servers/media/Queue.h =================================================================== --- haiku/trunk/src/servers/media/Queue.h 2009-12-08 00:01:05 UTC (rev 34547) +++ haiku/trunk/src/servers/media/Queue.h 2009-12-08 09:40:07 UTC (rev 34548) @@ -1,21 +1,29 @@ -/* +/* * Copyright 2002, Marcus Overhagen. All rights reserved. * Distributed under the terms of the MIT License. */ +#ifndef QUEUE_H +#define QUEUE_H -class Queue -{ + +#include <List.h> +#include <Locker.h> + + +class Queue : BLocker { public: - Queue(); - ~Queue(); + Queue(); + ~Queue(); - status_t Terminate(); - - status_t AddItem(void *item); - void * RemoveItem(); - + status_t Terminate(); + + status_t AddItem(void* item); + void* RemoveItem(); + private: - BList *fList; - BLocker *fLocker; - sem_id fSem; + BList fList; + sem_id fSem; }; + + +#endif // QUEUE_H