From: Hwankyu Jhun Date: Tue, 10 Jan 2023 10:08:08 +0000 (+0000) Subject: Add SharedQueue library for appfw modules X-Git-Tag: accepted/tizen/7.0/unified/20230203.164128~3 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F99%2F286599%2F6;p=platform%2Fcore%2Fbase%2Fbundle.git Add SharedQueue library for appfw modules This patch adds the SharedQueue class for applying active object pattern. Change-Id: I4f05d0ebc13fa4750d3bc7d1a4bc6752ddbcaec9 Signed-off-by: Hwankyu Jhun --- diff --git a/CMakeLists.txt b/CMakeLists.txt index f58604b..7171601 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -50,6 +50,7 @@ ADD_SUBDIRECTORY(parcel) ADD_DEPENDENCIES(parcel bundle) ADD_SUBDIRECTORY(tizen-database) +ADD_SUBDIRECTORY(tizen-shared-queue) IF(NOT DEFINED MINIMUM_BUILD) ENABLE_TESTING() @@ -69,4 +70,10 @@ ADD_DEPENDENCIES(parcel_unittests parcel) 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) diff --git a/packaging/bundle.spec b/packaging/bundle.spec index 09a3bc5..b277fab 100644 --- a/packaging/bundle.spec +++ b/packaging/bundle.spec @@ -101,6 +101,26 @@ Group: Development/Libraries %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} . @@ -137,7 +157,7 @@ find . -name '*.gcno' -exec cp --parents '{}' "$gcno_obj_dir" ';' cat << EOF > run-unittest.sh #!/bin/sh GCOV_PATH="/tmp/home/abuild/rpmbuild/BUILD" -PAKCAGE="%{name}-%{version}" +PAKCAGE="-%{version}" set_perm() { ## Sets the permission for applications @@ -155,7 +175,7 @@ setup() { test_main() { echo "test_main start" export "GCOV_PREFIX=/tmp" - /usr/bin/bundle_unittests + /usr/bin/_unittests } teardown() { @@ -174,50 +194,19 @@ EOF 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//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//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//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//tizen-shared-queue/g' %{buildroot}%{_bindir}/tizen-unittests/tizen-shared-queue/run-unittest.sh %post -p /sbin/ldconfig @@ -290,3 +279,14 @@ install -m 0755 run-parcel-unittest.sh %{buildroot}%{_bindir}/tizen-unittests/ti %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 diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 2091545..bb5abb2 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,3 +1,4 @@ ADD_SUBDIRECTORY(bundle_unittests) ADD_SUBDIRECTORY(parcel_unittests) ADD_SUBDIRECTORY(tizen-database_unittests) +ADD_SUBDIRECTORY(tizen-shared-queue_unittests) diff --git a/tests/tizen-shared-queue_unittests/CMakeLists.txt b/tests/tizen-shared-queue_unittests/CMakeLists.txt new file mode 100644 index 0000000..3a17063 --- /dev/null +++ b/tests/tizen-shared-queue_unittests/CMakeLists.txt @@ -0,0 +1,25 @@ +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/) diff --git a/tests/tizen-shared-queue_unittests/test_main.cc b/tests/tizen-shared-queue_unittests/test_main.cc new file mode 100644 index 0000000..6294721 --- /dev/null +++ b/tests/tizen-shared-queue_unittests/test_main.cc @@ -0,0 +1,30 @@ +/* + * 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 + +#include +#include + +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; + } +} diff --git a/tests/tizen-shared-queue_unittests/test_shared_queue.cc b/tests/tizen-shared-queue_unittests/test_shared_queue.cc new file mode 100644 index 0000000..a20c398 --- /dev/null +++ b/tests/tizen-shared-queue_unittests/test_shared_queue.cc @@ -0,0 +1,67 @@ +/* + * 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 +#include + +#include +#include + +#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 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 queue; + EXPECT_TRUE(queue.IsEmpty()); + queue.Push(Job()); + EXPECT_FALSE(queue.IsEmpty()); +} + +TEST(SharedQueueTest, Size) { + tizen_base::SharedQueue queue; + EXPECT_EQ(queue.Size(), 0); + queue.Push(Job()); + EXPECT_EQ(queue.Size(), 1); +} diff --git a/tizen-shared-queue/CMakeLists.txt b/tizen-shared-queue/CMakeLists.txt new file mode 100644 index 0000000..8ffe1c8 --- /dev/null +++ b/tizen-shared-queue/CMakeLists.txt @@ -0,0 +1,10 @@ +### 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) diff --git a/tizen-shared-queue/shared-queue.hpp b/tizen-shared-queue/shared-queue.hpp new file mode 100644 index 0000000..648d31d --- /dev/null +++ b/tizen-shared-queue/shared-queue.hpp @@ -0,0 +1,69 @@ +/* + * 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 +#include +#include +#include +#include +#include + +namespace tizen_base { + +template +class SharedQueue { + public: + SharedQueue() = default; + virtual ~SharedQueue() = default; + + void Push(T item) { + std::lock_guard lock(mutex_); + queue_.push(item); + cond_var_.notify_one(); + } + + T WaitAndPop() { + std::unique_lock 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 lock(mutex_); + return queue_.empty(); + } + + unsigned int Size() const { + std::lock_guard lock(mutex_); + return queue_.size(); + } + + private: + std::queue queue_; + mutable std::mutex mutex_; + std::condition_variable cond_var_; +}; + +} // namespace tizen_base + +#endif // TIZEN_SHARED_QUEUE_SHARED_QUEUE_HPP_ diff --git a/tizen-shared-queue/tizen-shared-queue.pc.in b/tizen-shared-queue/tizen-shared-queue.pc.in new file mode 100644 index 0000000..22969ce --- /dev/null +++ b/tizen-shared-queue/tizen-shared-queue.pc.in @@ -0,0 +1,11 @@ +# 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