1 // Copyright 2022 The Chromium Authors.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CONTENT_ANALYSIS_DEMO_REQUST_QUEUE_H_
6 #define CONTENT_ANALYSIS_DEMO_REQUST_QUEUE_H_
8 #include <condition_variable>
13 #include "content_analysis/sdk/analysis_agent.h"
15 // This class maintains a list of outstanding content analysis requests to
16 // process. Each request is encapsulated in one ContentAnalysisEvent.
17 // Requests are handled in FIFO order.
20 using Event
= content_analysis::sdk::ContentAnalysisEvent
;
22 RequestQueue() = default;
23 virtual ~RequestQueue() = default;
25 // Push a new content analysis event into the queue.
26 void push(std::unique_ptr
<Event
> event
) {
27 std::lock_guard
<std::mutex
> lock(mutex_
);
29 events_
.push(std::move(event
));
31 // Wake before leaving to prevent unpredicatable scheduling.
35 // Pop the next request from the queue, blocking if necessary until an event
36 // is available. Returns a nullptr if the queue will produce no more
38 std::unique_ptr
<Event
> pop() {
39 std::unique_lock
<std::mutex
> lock(mutex_
);
41 while (!abort_
&& events_
.size() == 0)
44 std::unique_ptr
<Event
> event
;
46 event
= std::move(events_
.front());
53 // Marks the queue as aborted. pop() will now return nullptr.
55 std::lock_guard
<std::mutex
> lg(mutex_
);
59 // Wake before leaving to prevent unpredicatable scheduling.
64 std::queue
<std::unique_ptr
<Event
>> events_
;
66 std::condition_variable cv_
;
70 #endif // CONTENT_ANALYSIS_DEMO_REQUST_QUEUE_H_