btrfs: Attempt to fix GCC2 build.
[haiku.git] / src / servers / package / JobQueue.cpp
blobc46f9725c1ea5b2729f94d09747c51af48d94800
1 /*
2 * Copyright 2013-2014, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Ingo Weinhold <ingo_weinhold@gmx.de>
7 */
10 #include "JobQueue.h"
12 #include <PthreadMutexLocker.h>
15 // #pragma mark - JobQueue
18 JobQueue::JobQueue()
20 fMutexInitialized(false),
21 fNewJobConditionInitialized(false),
22 fJobs(),
23 fClosed(false)
28 JobQueue::~JobQueue()
30 if (fMutexInitialized) {
31 PthreadMutexLocker mutexLocker(fMutex);
32 while (Job* job = fJobs.RemoveHead())
33 job->ReleaseReference();
36 if (fNewJobConditionInitialized)
37 pthread_cond_destroy(&fNewJobCondition);
39 if (fMutexInitialized)
40 pthread_mutex_destroy(&fMutex);
44 status_t
45 JobQueue::Init()
47 status_t error = pthread_mutex_init(&fMutex, NULL);
48 if (error != B_OK)
49 return error;
50 fMutexInitialized = true;
52 error = pthread_cond_init(&fNewJobCondition, NULL);
53 if (error != B_OK)
54 return error;
55 fNewJobConditionInitialized = true;
57 return B_OK;
61 void
62 JobQueue::Close()
64 if (fMutexInitialized && fNewJobConditionInitialized) {
65 PthreadMutexLocker mutexLocker(fMutex);
66 fClosed = true;
67 pthread_cond_broadcast(&fNewJobCondition);
72 bool
73 JobQueue::QueueJob(Job* job)
75 PthreadMutexLocker mutexLocker(fMutex);
76 if (fClosed)
77 return false;
79 fJobs.Add(job);
80 job->AcquireReference();
82 pthread_cond_signal(&fNewJobCondition);
83 return true;
87 Job*
88 JobQueue::DequeueJob()
90 PthreadMutexLocker mutexLocker(fMutex);
92 while (!fClosed) {
93 Job* job = fJobs.RemoveHead();
94 if (job != NULL)
95 return job;
97 if (!fClosed)
98 pthread_cond_wait(&fNewJobCondition, &fMutex);
101 return NULL;
105 void
106 JobQueue::DeleteJobs(Filter* filter)
108 PthreadMutexLocker mutexLocker(fMutex);
110 for (JobList::Iterator it = fJobs.GetIterator(); Job* job = it.Next();) {
111 if (filter->FilterJob(job)) {
112 it.Remove();
113 delete job;
119 // #pragma mark - Filter
122 JobQueue::Filter::~Filter()