Skip to content

Commit

Permalink
CMake: Add support for building a thread-less library
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
pcercuei committed Jan 30, 2024
1 parent 145b555 commit c81c4fe
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 2 deletions.
18 changes: 17 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand All @@ -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.")
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion iio-config.h.cmakein
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#cmakedefine01 HAVE_DNS_SD
#cmakedefine01 HAVE_AVAHI
#cmakedefine01 WITH_ZSTD
#cmakedefine01 NO_THREADS

#cmakedefine HAS_PIPE2
#cmakedefine HAS_STRDUP
Expand All @@ -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)

Expand Down
80 changes: 80 additions & 0 deletions lock-dummy.c
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
*/

#include <iio/iio.h>
#include <iio/iio-lock.h>

#include <errno.h>
#include <stdlib.h>

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;
}

0 comments on commit c81c4fe

Please sign in to comment.