This patch adds the SharedQueue class for applying active object pattern.
Change-Id: I4f05d0ebc13fa4750d3bc7d1a4bc6752ddbcaec9
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
ADD_DEPENDENCIES(parcel bundle)
ADD_SUBDIRECTORY(tizen-database)
+ADD_SUBDIRECTORY(tizen-shared-queue)
IF(NOT DEFINED MINIMUM_BUILD)
ENABLE_TESTING()
SET(TIZEN_DATABASE_UNITTESTS tizen-database_unittests)
ADD_TEST(NAME ${TIZEN_DATABASE_UNITTESTS} COMMAND ${TIZEN_DATABASE_UNITTESTS}
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tests/tizen-database_unittests)
+
+SET(TIZEN_SHARED_QUEUE_UNITTESTS tizen-shared-queue_unittests)
+ADD_TEST(NAME ${TIZEN_SHARED_QUEUE_UNITTESTS}
+ COMMAND ${TIZEN_SHARED_QUEUE_UNITTESTS}
+ WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tests/tizen-shared-queue_unittests)
+
ENDIF(NOT DEFINED MINIMUM_BUILD)
%description -n tizen-database-unittests
GTest for tizen-database
+#################################################
+## tizen-shared-queue-devel
+#################################################
+%package -n tizen-shared-queue-devel
+Summary: Tizen-shared-queue Library (devel)
+Group: Development/Libraries
+
+%description -n tizen-shared-queue-devel
+Tizen-Shared-Queue Library (devel)
+
+#################################################
+# tizen-shared-queue-unittests
+#################################################
+%package -n tizen-shared-queue-unittests
+Summary: GTest for tizen-shared-queue
+Group: Development/Libraries
+
+%description -n tizen-shared-queue-unittests
+GTest for tizen-shared-queue
+
%prep
%setup -q -n %{name}-%{version}
cp %{SOURCE1001} .
cat << EOF > run-unittest.sh
#!/bin/sh
GCOV_PATH="/tmp/home/abuild/rpmbuild/BUILD"
-PAKCAGE="%{name}-%{version}"
+PAKCAGE="<NAME>-%{version}"
set_perm() {
## Sets the permission for applications
test_main() {
echo "test_main start"
export "GCOV_PREFIX=/tmp"
- /usr/bin/bundle_unittests
+ /usr/bin/<NAME>_unittests
}
teardown() {
mkdir -p %{buildroot}%{_bindir}/tizen-unittests/%{name}
install -m 0755 run-unittest.sh %{buildroot}%{_bindir}/tizen-unittests/%{name}/
-
-cat << EOF > run-parcel-unittest.sh
-#!/bin/sh
-GCOV_PATH="/tmp/home/abuild/rpmbuild/BUILD"
-PAKCAGE="parcel-%{version}"
-
-set_perm() {
- ## Sets the permission for applications
- /usr/bin/find /tmp/home/ -print | /usr/bin/xargs -n1 /usr/bin/chsmack -a "System::Run" &> /dev/null
- /usr/bin/find /tmp/home/ -print | /usr/bin/xargs -n1 /usr/bin/chsmack -a "System::Run" -t &> /dev/null
- /usr/bin/chmod -R 777 /tmp/home/
-}
-
-setup() {
- echo "setup start"
- /usr/bin/mkdir -p "${GCOV_PATH}/${PACKAGE}"
- set_perm
-}
-
-test_main() {
- echo "test_main start"
- export "GCOV_PREFIX=/tmp"
- /usr/bin/parcel_unittests
-}
-
-teardown() {
- echo "teardown start"
- set_perm
-}
-
-main() {
- setup
- test_main
- teardown
-}
-
-main "\$*"
-EOF
+sed -i -e 's/<NAME>/bundle/g' %{buildroot}%{_bindir}/tizen-unittests/%{name}/run-unittest.sh
mkdir -p %{buildroot}%{_bindir}/tizen-unittests/parcel
-install -m 0755 run-parcel-unittest.sh %{buildroot}%{_bindir}/tizen-unittests/parcel/run-unittest.sh
+install -m 0755 run-unittest.sh %{buildroot}%{_bindir}/tizen-unittests/parcel/
+sed -i -e 's/<NAME>/parcel/g' %{buildroot}%{_bindir}/tizen-unittests/parcel/run-unittest.sh
mkdir -p %{buildroot}%{_bindir}/tizen-unittests/tizen-database
-install -m 0755 run-parcel-unittest.sh %{buildroot}%{_bindir}/tizen-unittests/tizen-database/run-unittest.sh
+install -m 0755 run-unittest.sh %{buildroot}%{_bindir}/tizen-unittests/tizen-database/
+sed -i -e 's/<NAME>/tizen-database/g' %{buildroot}%{_bindir}/tizen-unittests/tizen-database/run-unittest.sh
+
+mkdir -p %{buildroot}%{_bindir}/tizen-unittests/tizen-shared-queue
+install -m 0755 run-unittest.sh %{buildroot}%{_bindir}/tizen-unittests/tizen-shared-queue/
+sed -i -e 's/<NAME>/tizen-shared-queue/g' %{buildroot}%{_bindir}/tizen-unittests/tizen-shared-queue/run-unittest.sh
%post -p /sbin/ldconfig
%files -n tizen-database-unittests
%{_bindir}/tizen-database_unittests
%{_bindir}/tizen-unittests/tizen-database/run-unittest.sh
+
+#################################################
+# tizen-shared-queue-devel
+#################################################
+%files -n tizen-shared-queue-devel
+%{_includedir}/tizen-shared-queue/*
+%{_libdir}/pkgconfig/tizen-shared-queue.pc
+
+%files -n tizen-shared-queue-unittests
+%{_bindir}/tizen-shared-queue_unittests
+%{_bindir}/tizen-unittests/tizen-shared-queue/run-unittest.sh
ADD_SUBDIRECTORY(bundle_unittests)
ADD_SUBDIRECTORY(parcel_unittests)
ADD_SUBDIRECTORY(tizen-database_unittests)
+ADD_SUBDIRECTORY(tizen-shared-queue_unittests)
--- /dev/null
+CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
+PROJECT(tizen-shared-queue_unittests CXX)
+
+INCLUDE(FindPkgConfig)
+PKG_CHECK_MODULES(tizen-shared-queue_unittests REQUIRED gmock)
+
+SET(EXTRA_CFLAGS "")
+FOREACH(flag ${tizen-shared-queue_unittests_CFLAGS})
+ SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
+ENDFOREACH(flag)
+SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden -Wall -Werror")
+
+SET(CMAKE_CXX_FLAGS "${EXTRA_CFLAGS} -std=c++17")
+SET(CMAKE_CXX_FLAGS_DEBUG "-O0 -g")
+SET(CMAKE_CXX_FLAGS_RELEASE "-O2")
+
+INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../../)
+AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} UNITTESTS_SOURCES)
+
+ADD_EXECUTABLE(${PROJECT_NAME} ${UNITTESTS_SOURCES})
+
+SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES COMPILE_FLAGS
+ "${EXTRA_CFLAGS}")
+TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${tizen-shared-queue_unittests_LDFLAGS})
+INSTALL(TARGETS ${PROJECT_NAME} DESTINATION /usr/bin/)
--- /dev/null
+/*
+ * Copyright (c) 2023 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <exception>
+
+#include <gtest/gtest.h>
+#include <gmock/gmock.h>
+
+int main(int argc, char** argv) {
+ try {
+ testing::InitGoogleTest(&argc, argv);
+ return RUN_ALL_TESTS();
+ } catch (std::exception const &e) {
+ std::cout << "test_main caught exception: " << e.what() << std::endl;
+ return -1;
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2023 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <iostream>
+#include <thread>
+
+#include <gtest/gtest.h>
+#include <gmock/gmock.h>
+
+#include "tizen-shared-queue/shared-queue.hpp"
+
+namespace {
+
+class Job {
+ public:
+ Job() = default;
+
+ void Do() { done_ = true; }
+ bool Done() const { return done_; }
+
+ private:
+ bool done_ = false;
+};
+
+} // namespace
+
+TEST(SharedQueueTest, PushAndWaitAndPop) {
+ tizen_base::SharedQueue<Job> queue;
+ std::thread thread([&] {
+ auto job = queue.WaitAndPop();
+ job.Do();
+ queue.Push(std::move(job));
+ });
+ queue.Push(Job());
+ usleep(100);
+ auto job = queue.WaitAndPop();
+ thread.join();
+
+ EXPECT_TRUE(job.Done());
+}
+
+TEST(SharedQueueTest, IsEmpty) {
+ tizen_base::SharedQueue<Job> queue;
+ EXPECT_TRUE(queue.IsEmpty());
+ queue.Push(Job());
+ EXPECT_FALSE(queue.IsEmpty());
+}
+
+TEST(SharedQueueTest, Size) {
+ tizen_base::SharedQueue<Job> queue;
+ EXPECT_EQ(queue.Size(), 0);
+ queue.Push(Job());
+ EXPECT_EQ(queue.Size(), 1);
+}
--- /dev/null
+### Make pkgconfig file
+SET(PREFIX ${CMAKE_INSTALL_PREFIX})
+CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/tizen-shared-queue.pc.in
+ ${CMAKE_BINARY_DIR}/tizen-shared-queue.pc @ONLY)
+
+### Install
+INSTALL(FILES ${CMAKE_BINARY_DIR}/tizen-shared-queue.pc
+ DESTINATION ${LIB_INSTALL_DIR}/pkgconfig/)
+INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/shared-queue.hpp
+ DESTINATION include/tizen-shared-queue)
--- /dev/null
+/*
+ * Copyright (c) 2023 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef TIZEN_SHARED_QUEUE_SHARED_QUEUE_HPP_
+#define TIZEN_SHARED_QUEUE_SHARED_QUEUE_HPP_
+
+#include <condition_variable>
+#include <memory>
+#include <mutex>
+#include <queue>
+#include <thread>
+#include <utility>
+
+namespace tizen_base {
+
+template <class T>
+class SharedQueue {
+ public:
+ SharedQueue() = default;
+ virtual ~SharedQueue() = default;
+
+ void Push(T item) {
+ std::lock_guard<std::mutex> lock(mutex_);
+ queue_.push(item);
+ cond_var_.notify_one();
+ }
+
+ T WaitAndPop() {
+ std::unique_lock<std::mutex> lock(mutex_);
+ while (queue_.empty())
+ cond_var_.wait(lock);
+
+ auto item = std::move(queue_.front());
+ queue_.pop();
+ return item;
+ }
+
+ bool IsEmpty() const {
+ std::lock_guard<std::mutex> lock(mutex_);
+ return queue_.empty();
+ }
+
+ unsigned int Size() const {
+ std::lock_guard<std::mutex> lock(mutex_);
+ return queue_.size();
+ }
+
+ private:
+ std::queue<T> queue_;
+ mutable std::mutex mutex_;
+ std::condition_variable cond_var_;
+};
+
+} // namespace tizen_base
+
+#endif // TIZEN_SHARED_QUEUE_SHARED_QUEUE_HPP_
--- /dev/null
+# Wrapper class for sqlite3
+
+prefix=@PREFIX@
+includedir=${prefix}/include
+
+Name: tizen-shared-queue
+Description: shared queue class for active object pattern
+Version: @VERSION@
+Requires:
+Cflags: -I${includedir} -I${includedir}/tizen-shared-queue
+cppflags: -I${includedir} -I${includedir}/tizen-shared-queue