2 * Copyright (C) 2012 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #ifndef AAPT_WORK_QUEUE_H
18 #define AAPT_WORK_QUEUE_H
20 #include <utils/Errors.h>
21 #include <utils/Vector.h>
22 #include <utils/threads.h>
27 * A threaded work queue.
29 * This class is designed to make it easy to run a bunch of isolated work
30 * units in parallel, using up to the specified number of threads.
31 * To use it, write a loop to post work units to the work queue, then synchronize
32 * on the queue at the end.
39 virtual ~WorkUnit() { }
43 * If the result is 'true' then the work queue continues scheduling work as usual.
44 * If the result is 'false' then the work queue is canceled.
46 virtual bool run() = 0;
49 /* Creates a work queue with the specified maximum number of work threads. */
50 WorkQueue(size_t maxThreads, bool canCallJava = true);
52 /* Destroys the work queue.
53 * Cancels pending work and waits for all remaining threads to complete.
57 /* Posts a work unit to run later.
58 * If the work queue has been canceled or is already finished, returns INVALID_OPERATION
59 * and does not take ownership of the work unit (caller must destroy it itself).
60 * Otherwise, returns OK and takes ownership of the work unit (the work queue will
61 * destroy it automatically).
63 * For flow control, this method blocks when the size of the pending work queue is more
64 * 'backlog' times the number of threads. This condition reduces the rate of entry into
65 * the pending work queue and prevents it from growing much more rapidly than the
66 * work threads can actually handle.
68 * If 'backlog' is 0, then no throttle is applied.
70 status_t schedule(WorkUnit* workUnit, size_t backlog = 2);
72 /* Cancels all pending work.
73 * If the work queue is already finished, returns INVALID_OPERATION.
74 * If the work queue is already canceled, returns OK and does nothing else.
75 * Otherwise, returns OK, discards all pending work units and prevents additional
76 * work units from being scheduled.
78 * Call finish() after cancel() to wait for all remaining work to complete.
82 /* Waits for all work to complete.
83 * If the work queue is already finished, returns INVALID_OPERATION.
84 * Otherwise, waits for all work to complete and returns OK.
89 class WorkThread : public Thread {
91 WorkThread(WorkQueue* workQueue, bool canCallJava);
92 virtual ~WorkThread();
95 virtual bool threadLoop();
97 WorkQueue* const mWorkQueue;
100 status_t cancelLocked();
101 bool threadLoop(); // called from each work thread
103 const size_t mMaxThreads;
104 const bool mCanCallJava;
107 Condition mWorkChangedCondition;
108 Condition mWorkDequeuedCondition;
113 Vector<sp<WorkThread> > mWorkThreads;
114 Vector<WorkUnit*> mWorkUnits;
117 }; // namespace android
119 #endif // AAPT_WORK_QUEUE_H