From 40ce7088f470f97b821746a14077bc10870468ea Mon Sep 17 00:00:00 2001 From: "alexey.lysiuk" Date: Thu, 23 Feb 2023 15:15:52 +0200 Subject: [PATCH] quasi-glib: add test of all defined functions use c++ 17 for library and test --- patch/quasi-glib/CMakeLists.txt | 9 ++- patch/quasi-glib/quasi-glib-test.cpp | 95 ++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 patch/quasi-glib/quasi-glib-test.cpp diff --git a/patch/quasi-glib/CMakeLists.txt b/patch/quasi-glib/CMakeLists.txt index dc3ec4dc..5904a187 100644 --- a/patch/quasi-glib/CMakeLists.txt +++ b/patch/quasi-glib/CMakeLists.txt @@ -1,5 +1,12 @@ cmake_minimum_required(VERSION 3.1) project(quasi-glib) add_library(quasi-glib quasi-glib.cpp) -set_property(TARGET quasi-glib PROPERTY CXX_STANDARD 11) +set_property(TARGET quasi-glib PROPERTY CXX_STANDARD 17) install(TARGETS quasi-glib) + +add_executable(quasi-glib-test quasi-glib-test.cpp) +find_package(PkgConfig REQUIRED) +pkg_check_modules(GLIB REQUIRED glib-2.0) +set_property(TARGET quasi-glib-test PROPERTY CXX_STANDARD 17) +target_include_directories(quasi-glib-test PRIVATE ${GLIB_INCLUDE_DIRS}) +target_link_libraries(quasi-glib-test PRIVATE quasi-glib) diff --git a/patch/quasi-glib/quasi-glib-test.cpp b/patch/quasi-glib/quasi-glib-test.cpp new file mode 100644 index 00000000..96798811 --- /dev/null +++ b/patch/quasi-glib/quasi-glib-test.cpp @@ -0,0 +1,95 @@ + +#include +#include + +constexpr int REC_MUTEX_LOCK_COUNT = 4; + +struct ThreadData +{ + GCond* cond; + GMutex* mutex; + GRecMutex* recMutex; + GPrivate* tls; + bool flag; +}; + +static void* ThreadFunc(void* arg) +{ + ThreadData* threadData = reinterpret_cast(arg); + GPrivate* tls = threadData->tls; + + { + assert(g_private_get(tls) == nullptr); + + const gpointer TLS_REF_VALUE = gpointer(0xFEDCBA0987654321); + g_private_set(tls, TLS_REF_VALUE); + + assert(g_private_get(tls) == TLS_REF_VALUE); + } + + GMutex* mutex = threadData->mutex; + g_mutex_lock(mutex); + threadData->flag = true; + g_cond_signal(threadData->cond); + g_mutex_unlock(mutex); + + GRecMutex* recMutex = threadData->recMutex; + g_rec_mutex_lock(recMutex); + g_rec_mutex_unlock(recMutex); + + return nullptr; +} + +int main() +{ + g_clear_error(nullptr); + g_return_if_fail_warning("", "", ""); + assert(g_file_test(nullptr, GFileTest(0)) == 0); + assert(g_get_monotonic_time() > 0); + + GRecMutex recMutex; + g_rec_mutex_init(&recMutex); + + for (int i = 0; i < REC_MUTEX_LOCK_COUNT; ++i) + g_rec_mutex_lock(&recMutex); + + GCond cond; + g_cond_init(&cond); + + GMutex mutex; + g_mutex_init(&mutex); + + GPrivate tls = G_PRIVATE_INIT(nullptr); + { + assert(g_private_get(&tls) == nullptr); + + const gpointer TLS_REF_VALUE = gpointer(0x1234567890ABCDEF); + g_private_set(&tls, TLS_REF_VALUE); + + assert(g_private_get(&tls) == TLS_REF_VALUE); + } + + ThreadData threadData = { &cond, &mutex, &recMutex, &tls, false }; + + GThread* thread = g_thread_try_new("", ThreadFunc, &threadData, nullptr); + assert(thread); + + g_usleep(10'000); + + g_mutex_lock(&mutex); + while (!threadData.flag) + g_cond_wait(&cond, &mutex); + g_mutex_unlock(&mutex); + + g_cond_broadcast(&cond); + g_cond_clear(&cond); + + for (int i = 0; i < REC_MUTEX_LOCK_COUNT; ++i) + g_rec_mutex_unlock(&recMutex); + + g_thread_join(thread); + g_thread_unref(thread); + + g_mutex_clear(&mutex); + g_rec_mutex_clear(&recMutex); +}