* limitations under the License.
*/
+#include <chrono>
#include <iostream>
#include <thread>
EXPECT_TRUE(job.Done());
}
+TEST(SharedQueueTest, TryAndPop) {
+ tizen_base::SharedQueue<Job> queue;
+ Job job;
+ EXPECT_FALSE(queue.TryAndPop(job));
+ queue.Push(Job());
+ EXPECT_TRUE(queue.TryAndPop(job));
+}
+
+TEST(SharedQueueTest, WaitAndPopFor) {
+ tizen_base::SharedQueue<Job> queue;
+ Job job;
+ std::chrono::steady_clock::time_point begin =
+ std::chrono::steady_clock::now();
+ EXPECT_FALSE(queue.WaitAndPopFor(job, 110));
+ std::chrono::steady_clock::time_point end =
+ std::chrono::steady_clock::now();
+ auto elapsed_time = std::chrono::duration_cast<std::chrono::milliseconds>(
+ end - begin).count();
+ EXPECT_TRUE(elapsed_time > 100);
+
+ queue.Push(Job());
+ begin = std::chrono::steady_clock::now();
+ EXPECT_TRUE(queue.WaitAndPopFor(job, 100));
+ end = std::chrono::steady_clock::now();
+ elapsed_time = std::chrono::duration_cast<std::chrono::milliseconds>(
+ end - begin).count();
+ EXPECT_TRUE(elapsed_time < 100);
+}
+
TEST(SharedQueueTest, IsEmpty) {
tizen_base::SharedQueue<Job> queue;
EXPECT_TRUE(queue.IsEmpty());
cond_var_.notify_one();
}
+ bool TryAndPop(T& item) {
+ std::lock_guard<std::mutex> lock(mutex_);
+ if (queue_.empty())
+ return false;
+
+ item = queue_.front();
+ queue_.pop();
+ return true;
+ }
+
T WaitAndPop() {
std::unique_lock<std::mutex> lock(mutex_);
while (queue_.empty())
return item;
}
+ bool WaitAndPopFor(T& item, int timeout) {
+ std::unique_lock<std::mutex> lock(mutex_);
+ if (!queue_.empty()) {
+ item = queue_.front();
+ queue_.pop();
+ return true;
+ }
+
+ std::chrono::milliseconds duration(timeout);
+ if (cond_var_.wait_for(lock, duration) == std::cv_status::timeout)
+ return false;
+
+ item = queue_.front();
+ queue_.pop();
+ return true;
+ }
+
bool IsEmpty() const {
std::lock_guard<std::mutex> lock(mutex_);
return queue_.empty();