From d7dfc6ca12cef5528761bb42198bc06ecc0d1477 Mon Sep 17 00:00:00 2001 From: Enrico Maria Crisostomo Date: Tue, 31 Dec 2024 22:47:26 +0100 Subject: [PATCH] Refactor threading in tests to use C++11 std::thread and update build scripts for CMake --- configure.ac | 1 - full-build-with-cmake.zsh | 143 ++++++++++++++++++ libfswatch/CMakeLists.txt | 3 - test/src/CMakeLists.txt | 2 +- test/src/{fswatch_test.c => fswatch_test.cpp} | 20 +-- 5 files changed, 149 insertions(+), 20 deletions(-) create mode 100755 full-build-with-cmake.zsh rename test/src/{fswatch_test.c => fswatch_test.cpp} (84%) diff --git a/configure.ac b/configure.ac index 62d13034..4e94fb1e 100644 --- a/configure.ac +++ b/configure.ac @@ -103,7 +103,6 @@ AX_CXX_COMPILE_STDCXX([17], [noext], [mandatory]) AX_CXXFLAGS_WARN_ALL # Checks for libraries. -AC_SEARCH_LIBS([pthread_create], [pthread]) # Check for optional header files. AC_CHECK_HEADERS([getopt.h]) diff --git a/full-build-with-cmake.zsh b/full-build-with-cmake.zsh new file mode 100755 index 00000000..bd1226bd --- /dev/null +++ b/full-build-with-cmake.zsh @@ -0,0 +1,143 @@ +#!/bin/zsh +# -*- vim:fenc=utf-8:et:sw=2:ts=2:sts=2 +# +# Copyright (c) 2014-2024 Enrico M. Crisostomo +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free Software +# Foundation; either version 3, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +# details. +# +# You should have received a copy of the GNU General Public License along with +# this program. If not, see . +# +setopt local_options +setopt local_traps +unsetopt glob_subst + +set -o errexit +set -o nounset + +PROGNAME=${0:t} +PROGDIR=${0:h} +BUG_REPORT=enrico.m.crisostomo@gmail.com +PACKAGE_VERSION=1.0.0 +typeset -r LIBFSWATCH_DOC_DIR=libfswatch/doc/doxygen +typeset -r FSWATCH_DOC_DIR=fswatch/doc +typeset -i ARGS_PROCESSED=0 +typeset -a help_flag +typeset -a version_flag +typeset -a configure_opts +REQUIRED_PROGS=( git ) + +for p in ${REQUIRED_PROGS} +do + command -v ${p} > /dev/null 2>&1 || + { + >&2 print -- Cannot find required program: ${p} + exit 1 + } +done + +print_version() +{ + print -- "${PROGNAME} ${PACKAGE_VERSION}" + print -- "Copyright (C) 2024 Enrico M. Crisostomo" + print -- "License GPLv3+: GNU GPL version 3 or later ." + print -- "This is free software: you are free to change and redistribute it." + print -- "There is NO WARRANTY, to the extent permitted by law." + print + print -- "Written by Enrico M. Crisostomo" +} + +print_usage() +{ + print -- "${PROGNAME}" + print + print -- "Usage:" + print -- "${PROGNAME}" + print + print -- "Build the package from scratch using CMake:" + print -- " - Clean the source tree using: git clean -xfd" + print -- " - Bootstrap the CMake build in build/" + print -- " - Make the package" + print + print -- "Options:" + print -- " --version Print the program version." +} + +parse_opts() +{ + while getopts ":" opt + do + case $opt in + \?) + >&2 print -- Invalid option -${OPTARG}. + exit 1 + ;; + esac + done + + ARGS_PROCESSED=$((OPTIND - 1)) +} + +# main +zparseopts -D \ + h=help_flag -help=help_flag \ + -version=version_flag + +if (( ${+help_flag[1]} > 0 )) +then + print_usage + exit 0 +fi + +if (( ${+version_flag[1]} > 0 )) +then + print_version + exit 0 +fi + +parse_opts $* && shift ${ARGS_PROCESSED} + +(( $# == 0 )) || + { + >&2 print -- "Invalid number of arguments." + exit 1 + } + +print -- "This script will clean up the source tree, bootstrap the CMake build," +print -- "and build fswatch. Are you sure you want to continue? [y/N]" +read -k1 -q + +if [[ ${REPLY} == [yY] ]] ; then + print -- "Cleaning up the source tree..." + git clean -xfd + print -- "Bootstrapping the CMake build..." + mkdir build + print -- "Configuring the package..." + mkdir -p build + cd build + cmake .. + print -- "Building the package..." + make + print -- "Running the tests..." + make test + print -- "Build was successful." +else + print -- "Aborted." +fi + +# Local variables: +# coding: utf-8 +# mode: sh +# eval: (sh-set-shell "zsh") +# tab-width: 2 +# indent-tabs-mode: nil +# sh-basic-offset: 2 +# sh-indentation: 2 +# End: diff --git a/libfswatch/CMakeLists.txt b/libfswatch/CMakeLists.txt index 17a1847c..ae6240b0 100644 --- a/libfswatch/CMakeLists.txt +++ b/libfswatch/CMakeLists.txt @@ -73,9 +73,6 @@ set(LIB_SOURCE_FILES check_struct_has_member("struct stat" st_mtime sys/stat.h HAVE_STRUCT_STAT_ST_MTIME) check_struct_has_member("struct stat" st_mtimespec sys/stat.h HAVE_STRUCT_STAT_ST_MTIMESPEC) -find_library(PTHREAD_LIBRARY pthread) -set(EXTRA_LIBS ${EXTRA_LIBS} ${PTHREAD_LIBRARY}) - check_include_file_cxx(sys/inotify.h HAVE_SYS_INOTIFY_H) if (HAVE_SYS_INOTIFY_H) diff --git a/test/src/CMakeLists.txt b/test/src/CMakeLists.txt index 020c7f14..f7df586a 100644 --- a/test/src/CMakeLists.txt +++ b/test/src/CMakeLists.txt @@ -14,7 +14,7 @@ # this program. If not, see . # set(SOURCE_FILES - fswatch_test.c + fswatch_test.cpp ${PROJECT_BINARY_DIR}/libfswatch/libfswatch_config.h) add_executable(fswatch_test ${SOURCE_FILES}) diff --git a/test/src/fswatch_test.c b/test/src/fswatch_test.cpp similarity index 84% rename from test/src/fswatch_test.c rename to test/src/fswatch_test.cpp index 10f01b34..7d4fd781 100644 --- a/test/src/fswatch_test.c +++ b/test/src/fswatch_test.cpp @@ -1,7 +1,7 @@ #include #include #include -#include +#include #include /** @@ -79,15 +79,9 @@ int main(int argc, char **argv) fsw_set_allow_overflow(handle, 0); - pthread_t start_thread; + std::thread start_thread(start_monitor, &handle); - if (pthread_create(&start_thread, NULL, start_monitor, (void *) &handle)) - { - fprintf(stderr, "Error creating thread\n"); - return 1; - } - - sleep(5); + std::this_thread::sleep_for(std::chrono::seconds(5)); if (FSW_OK != fsw_stop_monitor(handle)) { @@ -95,7 +89,7 @@ int main(int argc, char **argv) return 1; } - sleep(3); + std::this_thread::sleep_for(std::chrono::seconds(3)); if (FSW_OK != fsw_destroy_session(handle)) { @@ -104,11 +98,7 @@ int main(int argc, char **argv) } // Wait for the monitor thread to finish - if (pthread_join(start_thread, NULL)) - { - fprintf(stderr, "Error joining monitor thread\n"); - return 2; - } + start_thread.join(); return 0; }