1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 
17 #define DEBUG false  // STOPSHIP if true
18 #include "Log.h"
19 
20 #include "LogEventQueue.h"
21 
22 namespace android {
23 namespace os {
24 namespace statsd {
25 
26 using std::unique_lock;
27 using std::unique_ptr;
28 
waitPop()29 unique_ptr<LogEvent> LogEventQueue::waitPop() {
30     std::unique_lock<std::mutex> lock(mMutex);
31 
32     if (mQueue.empty()) {
33         mCondition.wait(lock, [this] { return !this->mQueue.empty(); });
34     }
35 
36     unique_ptr<LogEvent> item = std::move(mQueue.front());
37     mQueue.pop();
38 
39     return item;
40 }
41 
push(unique_ptr<LogEvent> item,int64_t * oldestTimestampNs)42 bool LogEventQueue::push(unique_ptr<LogEvent> item, int64_t* oldestTimestampNs) {
43     bool success;
44     {
45         std::unique_lock<std::mutex> lock(mMutex);
46         if (mQueue.size() < mQueueLimit) {
47             mQueue.push(std::move(item));
48             success = true;
49         } else {
50             // safe operation as queue must not be empty.
51             *oldestTimestampNs = mQueue.front()->GetElapsedTimestampNs();
52             success = false;
53         }
54     }
55 
56     mCondition.notify_one();
57     return success;
58 }
59 
60 }  // namespace statsd
61 }  // namespace os
62 }  // namespace android
63