* Cleanup.
git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@34548 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
+36
-45
@@ -2,89 +2,80 @@
|
|||||||
* Copyright 2002, Marcus Overhagen. All rights reserved.
|
* Copyright 2002, Marcus Overhagen. All rights reserved.
|
||||||
* Distributed under the terms of the MIT License.
|
* 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
|
/*! This is a simple multi thread save queue.
|
||||||
* is finished doing so, it calls Terminate(). Another
|
|
||||||
* thread calls RemoveItem() to remove items from the
|
One thread calls AddItem() to add items, and when it is finished doing
|
||||||
* queue. RemoveItem() blocks when no items are available.
|
so, it calls Terminate(). Another thread calls RemoveItem() to remove
|
||||||
* As soon as Terminate() is called and the queue is empty,
|
items from the queue. RemoveItem() blocks when no items are available.
|
||||||
* RemoveItem() returns NULL.
|
As soon as Terminate() is called and the queue is empty, RemoveItem()
|
||||||
|
returns NULL.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <List.h>
|
|
||||||
#include <OS.h>
|
|
||||||
#include <Locker.h>
|
|
||||||
|
|
||||||
#include "Queue.h"
|
#include "Queue.h"
|
||||||
|
|
||||||
|
#include <Autolock.h>
|
||||||
|
#include <OS.h>
|
||||||
|
|
||||||
|
|
||||||
Queue::Queue()
|
Queue::Queue()
|
||||||
: fList(new BList),
|
:
|
||||||
fLocker(new BLocker("queue locker")),
|
BLocker("queue locker"),
|
||||||
fSem(create_sem(0, "queue sem"))
|
fSem(create_sem(0, "queue sem"))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Queue::~Queue()
|
Queue::~Queue()
|
||||||
{
|
{
|
||||||
if (fSem > 0)
|
if (fSem >= 0)
|
||||||
delete_sem(fSem);
|
delete_sem(fSem);
|
||||||
delete fLocker;
|
|
||||||
delete fList;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
status_t
|
status_t
|
||||||
Queue::Terminate()
|
Queue::Terminate()
|
||||||
{
|
{
|
||||||
status_t rv;
|
BAutolock _(this);
|
||||||
|
|
||||||
|
if (fSem < 0)
|
||||||
|
return B_ERROR;
|
||||||
|
|
||||||
fLocker->Lock();
|
|
||||||
if (fSem < 0) {
|
|
||||||
rv = B_ERROR;
|
|
||||||
} else {
|
|
||||||
delete_sem(fSem);
|
delete_sem(fSem);
|
||||||
fSem = -1;
|
fSem = -1;
|
||||||
rv = B_OK;
|
return B_OK;
|
||||||
}
|
|
||||||
fLocker->Unlock();
|
|
||||||
return rv;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
status_t
|
status_t
|
||||||
Queue::AddItem(void* item)
|
Queue::AddItem(void* item)
|
||||||
{
|
{
|
||||||
status_t rv;
|
BAutolock _(this);
|
||||||
|
|
||||||
|
if (fSem < 0)
|
||||||
|
return B_ERROR;
|
||||||
|
|
||||||
|
if (!fList.AddItem(item))
|
||||||
|
return B_NO_MEMORY;
|
||||||
|
|
||||||
fLocker->Lock();
|
|
||||||
if (fSem < 0) {
|
|
||||||
rv = B_ERROR;
|
|
||||||
} else {
|
|
||||||
if (fList->AddItem(item)) { // AddItem returns a bool
|
|
||||||
release_sem(fSem);
|
release_sem(fSem);
|
||||||
rv = B_OK;
|
return B_OK;
|
||||||
} else {
|
|
||||||
rv = B_ERROR;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fLocker->Unlock();
|
|
||||||
return rv;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void*
|
void*
|
||||||
Queue::RemoveItem()
|
Queue::RemoveItem()
|
||||||
{
|
{
|
||||||
void *item;
|
|
||||||
|
|
||||||
// if the semaphore is deleted by Terminate(),
|
// if the semaphore is deleted by Terminate(),
|
||||||
// this will no longer block
|
// this will no longer block
|
||||||
while (acquire_sem(fSem) == B_INTERRUPTED)
|
while (acquire_sem(fSem) == B_INTERRUPTED)
|
||||||
;
|
;
|
||||||
|
|
||||||
|
BAutolock _(this);
|
||||||
|
|
||||||
// if the list is empty, which can only happen after
|
// if the list is empty, which can only happen after
|
||||||
// Terminate() was called, item will be NULL
|
// Terminate() was called, item will be NULL
|
||||||
fLocker->Lock();
|
return fList.RemoveItem((int32)0);
|
||||||
item = fList->RemoveItem((int32)0);
|
|
||||||
fLocker->Unlock();
|
|
||||||
|
|
||||||
return item;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,9 +2,15 @@
|
|||||||
* Copyright 2002, Marcus Overhagen. All rights reserved.
|
* Copyright 2002, Marcus Overhagen. All rights reserved.
|
||||||
* Distributed under the terms of the MIT License.
|
* 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:
|
public:
|
||||||
Queue();
|
Queue();
|
||||||
~Queue();
|
~Queue();
|
||||||
@@ -15,7 +21,9 @@ public:
|
|||||||
void* RemoveItem();
|
void* RemoveItem();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
BList *fList;
|
BList fList;
|
||||||
BLocker *fLocker;
|
|
||||||
sem_id fSem;
|
sem_id fSem;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif // QUEUE_H
|
||||||
|
|||||||
Reference in New Issue
Block a user