Added option to close a queue and wait for queued up operations to finish,

rather than just closing a queue and immediately cancelling all operations.
This commit is contained in:
Simon
2016-09-02 19:21:42 -07:00
parent c93d8bdf9c
commit 9cd713439a
2 changed files with 58 additions and 15 deletions

View File

@@ -1,4 +1,4 @@
// Copyright (c) 2014 The Zcash developers
// Copyright (c) 2016 The Zcash developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
@@ -36,9 +36,12 @@ public:
void addWorker();
size_t getNumberOfWorkers() const;
bool isClosed() const;
void close();
void closeAndWait();
void cancelAllOperations();
bool isFinishing() const;
void close(); // close queue and cancel all operations
void finish(); // close queue but finishing existing operations
void closeAndWait(); // block thread until all threads have terminated.
void finishAndWait(); // block thread until existing operations have finished, threads terminated
void cancelAllOperations(); // mark all operations in the queue as cancelled
size_t getOperationCount() const;
std::shared_ptr<AsyncRPCOperation> getOperationForId(AsyncRPCOperationId) const;
std::shared_ptr<AsyncRPCOperation> popOperationForId(AsyncRPCOperationId);
@@ -46,13 +49,15 @@ public:
std::vector<AsyncRPCOperationId> getAllOperationIds() const;
private:
// addWorker() will spawn a new thread on this method
// addWorker() will spawn a new thread on run())
void run(size_t workerId);
void wait_for_worker_threads();
// Why this is not a recursive lock: http://www.zaval.org/resources/library/butenhof1.html
mutable std::mutex lock_;
std::condition_variable condition_;
bool closed_;
std::atomic<bool> closed_;
std::atomic<bool> finish_;
AsyncRPCOperationMap operation_map_;
std::queue <AsyncRPCOperationId> operation_id_queue_;
std::vector<std::thread> workers_;