From c81c4fe6fc5edd0a7907d388b8cd20dcb0afc616 Mon Sep 17 00:00:00 2001 From: Paul Cercueil Date: Tue, 16 Jan 2024 12:05:08 +0100 Subject: [PATCH] CMake: Add support for building a thread-less library Add support for building a thread-less Libiio. This disables all current backends, but applications are still free to provide their own external backend. This is especially useful on embedded, where threads generally aren't a thing. Signed-off-by: Paul Cercueil --- CMakeLists.txt | 18 +++++++++- iio-config.h.cmakein | 2 +- lock-dummy.c | 80 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 lock-dummy.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 0e8ec0dd6..1b82abec7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -372,6 +372,10 @@ set(LIBIIO_VERSION ${VERSION}.g${LIBIIO_VERSION_GIT}) file(WRITE ${CMAKE_BINARY_DIR}/.version ${LIBIIO_VERSION}) if(WITH_LOCAL_BACKEND) + if (NO_THREADS) + message(SEND_ERROR "Local backend require threads.") + endif() + target_sources(iio PRIVATE local.c) # Link with librt if present @@ -576,7 +580,10 @@ elseif(NEED_LIBXML2) "If you want to enable the XML backend, set WITH_XML_BACKEND=ON.") endif() -if (WIN32) +option(NO_THREADS "Build a thread-less Libiio library" OFF) +if (NO_THREADS) + target_sources(iio PRIVATE lock-dummy.c) +elseif (WIN32) target_sources(iio PRIVATE lock-windows.c) else () if (NOT ANDROID) @@ -606,6 +613,10 @@ if (IIOD_CLIENT OR WITH_IIOD) endif() if (IIOD_CLIENT) + if (NO_THREADS) + message(SEND_ERROR "Backends using iiod-client require threads.") + endif() + target_sources(iio PRIVATE iiod-client.c) target_link_libraries(iio PRIVATE iiod-responder) endif() @@ -625,6 +636,10 @@ if(WITH_EXAMPLES) add_subdirectory(examples) endif() if (WITH_IIOD) + if (NO_THREADS) + message(SEND_ERROR "IIOD require threads.") + endif() + if (NOT PTHREAD_LIBRARIES) message(SEND_ERROR "IIOD requires pthread support.\n" "If you want to disable IIOD, set WITH_IIOD=OFF.") @@ -672,6 +687,7 @@ string(REPLACE ";" "," LIBIIO_SCAN_BACKENDS "${LIBIIO_SCAN_BACKENDS}") configure_file(iio-config.h.cmakein ${CMAKE_CURRENT_BINARY_DIR}/iio-config.h @ONLY) +toggle_iio_feature("${NO_THREADS}" threadless) toggle_iio_feature("${LIBIIO_COMPAT}" compat) toggle_iio_feature("${WITH_XML_BACKEND}" xml) toggle_iio_feature("${WITH_ZSTD}" zstd) diff --git a/iio-config.h.cmakein b/iio-config.h.cmakein index 0359a826b..3f647eb09 100644 --- a/iio-config.h.cmakein +++ b/iio-config.h.cmakein @@ -41,6 +41,7 @@ #cmakedefine01 HAVE_DNS_SD #cmakedefine01 HAVE_AVAHI #cmakedefine01 WITH_ZSTD +#cmakedefine01 NO_THREADS #cmakedefine HAS_PIPE2 #cmakedefine HAS_STRDUP @@ -50,7 +51,6 @@ #cmakedefine HAS_NEWLOCALE #cmakedefine HAS_PTHREAD_SETNAME_NP #cmakedefine HAVE_IPV6 -#cmakedefine NO_THREADS #define IF_ENABLED(cfg, ptr) ((cfg) ? (ptr) : NULL) diff --git a/lock-dummy.c b/lock-dummy.c new file mode 100644 index 000000000..b944f36d1 --- /dev/null +++ b/lock-dummy.c @@ -0,0 +1,80 @@ +// SPDX-License-Identifier: LGPL-2.1-or-later +/* + * libiio - Library for interfacing industrial I/O (IIO) devices + * + * Copyright (C) 2024 Analog Devices, Inc. + * Author: Paul Cercueil + */ + +#include +#include + +#include +#include + +struct iio_mutex { + int dummy; /* Flawfinder: ignore */ +}; + +struct iio_cond { + int dummy; /* Flawfinder: ignore */ +}; + +struct iio_mutex * iio_mutex_create(void) +{ + struct iio_mutex *lock = malloc(sizeof(*lock)); + + if (!lock) + return iio_ptr(-ENOMEM); + + return lock; +} + +void iio_mutex_destroy(struct iio_mutex *lock) +{ + free(lock); +} + +void iio_mutex_lock(struct iio_mutex *lock) +{ +} + +void iio_mutex_unlock(struct iio_mutex *lock) +{ +} + +struct iio_cond * iio_cond_create(void) +{ + struct iio_cond *cond = malloc(sizeof(*cond)); + + if (!cond) + return iio_ptr(-ENOMEM); + + return cond; +} + +void iio_cond_destroy(struct iio_cond *cond) +{ + free(cond); +} + +int iio_cond_wait(struct iio_cond *cond, struct iio_mutex *lock, + unsigned int timeout_ms) +{ + return -ETIMEDOUT; +} + +void iio_cond_signal(struct iio_cond *cond) +{ +} + +struct iio_thrd * iio_thrd_create(int (*thrd)(void *), + void *d, const char *name) +{ + return iio_ptr(-ENOSYS); +} + +int iio_thrd_join_and_destroy(struct iio_thrd *thrd) +{ + return 0; +}