Timur Davydov has uploaded this change for review. ( 
https://gerrit.osmocom.org/c/osmo-trx/+/42654?usp=email )


Change subject: build: detect pthread_setname_np and sched_* APIs, guard usage
......................................................................

build: detect pthread_setname_np and sched_* APIs, guard usage

Add configure checks for pthread_setname_np and sched_* functions.
Wrap their usage with HAVE_* guards to avoid build failures on
platforms where they are unavailable.

Return -ENOTSUP for unsupported scheduler operations.

Improves portability across non-Linux and restricted environments
(e.g. WebAssembly).

Change-Id: Ic3f1492544439d88c5e6683330c6696de1c2f85b
---
M CommonLibs/Threads.cpp
M Transceiver52M/device/ipc/ipc-driver-test.c
M Transceiver52M/ms/threadsched.cpp
M Transceiver52M/ms/uhd_specific.h
M Transceiver52M/osmo-trx.cpp
M configure.ac
6 files changed, 35 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/osmo-trx refs/changes/54/42654/1

diff --git a/CommonLibs/Threads.cpp b/CommonLibs/Threads.cpp
index 91e78d7..b61a029 100644
--- a/CommonLibs/Threads.cpp
+++ b/CommonLibs/Threads.cpp
@@ -47,6 +47,7 @@

 void set_selfthread_name(const char *name)
 {
+#ifdef HAVE_PTHREAD_SETNAME_NP
        pthread_t selfid = pthread_self();
        pid_t tid = osmo_gettid();
        if (pthread_setname_np(selfid, name) == 0) {
@@ -57,6 +58,7 @@
                LOG(NOTICE) << "Thread "<< selfid << " (task " << tid << ") set 
name \"" << name << "\" failed: (" << err
                        << ") " << strerror_buf(err, err_buf, sizeof(err_buf));
        }
+#endif /* HAVE_PTHREAD_SETNAME_NP */
 }

 void thread_enable_cancel(bool cancel)
diff --git a/Transceiver52M/device/ipc/ipc-driver-test.c 
b/Transceiver52M/device/ipc/ipc-driver-test.c
index d8284c7..22395ce 100644
--- a/Transceiver52M/device/ipc/ipc-driver-test.c
+++ b/Transceiver52M/device/ipc/ipc-driver-test.c
@@ -256,7 +256,9 @@
 {
        uint32_t chann = decoded_region->num_chans;
        ul_running = true;
+#ifdef HAVE_PTHREAD_SETNAME_NP
        pthread_setname_np(pthread_self(), "uplink_rx");
+#endif /* HAVE_PTHREAD_SETNAME_NP */

        while (!ipc_exit_requested) {
                int32_t read = uhdwrap_read(global_dev, chann);
@@ -270,7 +272,9 @@
 {
        int chann = decoded_region->num_chans;
        dl_running = true;
+#ifdef HAVE_PTHREAD_SETNAME_NP
        pthread_setname_np(pthread_self(), "downlink_tx");
+#endif /* HAVE_PTHREAD_SETNAME_NP */

        while (!ipc_exit_requested) {
                bool underrun;
diff --git a/Transceiver52M/ms/threadsched.cpp 
b/Transceiver52M/ms/threadsched.cpp
index ba5cae7..fbca3a8 100644
--- a/Transceiver52M/ms/threadsched.cpp
+++ b/Transceiver52M/ms/threadsched.cpp
@@ -40,7 +40,9 @@

 void set_name_aff_sched(std::thread::native_handle_type h, const char *name, 
int cpunum, int schedtype, int prio)
 {
+#ifdef HAVE_PTHREAD_SETNAME_NP
        pthread_setname_np(h, name);
+#endif /* HAVE_PTHREAD_SETNAME_NP */

        cpu_set_t cpuset;

@@ -79,7 +81,9 @@
        if (a)
                std::cerr << "thread arg rc:" << a << std::endl;
        pthread_create(&thread, &attr, fun, arg);
+#ifdef HAVE_PTHREAD_SETNAME_NP
        pthread_setname_np(thread, name);
+#endif /* HAVE_PTHREAD_SETNAME_NP */
        pthread_attr_destroy(&attr);
        return thread;
 }
diff --git a/Transceiver52M/ms/uhd_specific.h b/Transceiver52M/ms/uhd_specific.h
index 151c002..4f1f789 100644
--- a/Transceiver52M/ms/uhd_specific.h
+++ b/Transceiver52M/ms/uhd_specific.h
@@ -235,7 +235,9 @@
                static auto rx_burst_cap_this = this;
                static auto rx_burst_cap_bh = burst_handler;
                auto fn = [](void *args) -> void * {
+#ifdef HAVE_PTHREAD_SETNAME_NP
                        pthread_setname_np(pthread_self(), "rxrun");
+#endif /* HAVE_PTHREAD_SETNAME_NP */

                        uhd::stream_cmd_t 
stream_cmd(uhd::stream_cmd_t::STREAM_MODE_START_CONTINUOUS);
                        stream_cmd.stream_now = true;
diff --git a/Transceiver52M/osmo-trx.cpp b/Transceiver52M/osmo-trx.cpp
index fbc1492..f19df0c 100644
--- a/Transceiver52M/osmo-trx.cpp
+++ b/Transceiver52M/osmo-trx.cpp
@@ -500,6 +500,7 @@

 static int set_sched_rr(unsigned int prio)
 {
+#ifdef HAVE_SCHED_SETSCHEDULER
        struct sched_param param;
        int rc;
        memset(&param, 0, sizeof(param));
@@ -513,6 +514,9 @@
                return -1;
        }
        return 0;
+#else /* HAVE_SCHED_SETSCHEDULER */
+       return -ENOTSUP;
+#endif /* HAVE_SCHED_SETSCHEDULER */
 }

 static void print_simd_info(void)
diff --git a/configure.ac b/configure.ac
index 01b2f34..fc6d6d8 100644
--- a/configure.ac
+++ b/configure.ac
@@ -277,6 +277,25 @@
     AC_DEFINE(HAVE_ATOMIC_OPS, 1, [Support all required atomic operations], 
[AC_MSG_WARN("At least one aotmic operation missing, will use mutex")])
 ])

+dnl Check for pthread_setname_np
+AX_PTHREAD
+if test "x$ax_pthread_ok" = "xyes"; then
+    saved_CFLAGS="$CFLAGS"
+    saved_LIBS="$LIBS"
+    CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
+    LIBS="$LIBS $PTHREAD_LIBS"
+
+    AC_CHECK_HEADERS([pthread.h])
+    AC_CHECK_FUNCS([pthread_setname_np])
+
+    CFLAGS="$saved_CFLAGS"
+    LIBS="$saved_LIBS"
+fi
+
+dnl Check the scheduller functions
+AC_CHECK_HEADERS([sched.h])
+AC_CHECK_FUNCS([sched_setaffinity sched_setscheduler])
+
 AM_CONDITIONAL(DEVICE_UHD, [test "x$with_uhd" = "xyes"])
 AM_CONDITIONAL(DEVICE_USRP1, [test "x$with_usrp1" = "xyes"])
 AM_CONDITIONAL(DEVICE_LMS, [test "x$with_lms" = "xyes"])

--
To view, visit https://gerrit.osmocom.org/c/osmo-trx/+/42654?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings?usp=email

Gerrit-MessageType: newchange
Gerrit-Project: osmo-trx
Gerrit-Branch: master
Gerrit-Change-Id: Ic3f1492544439d88c5e6683330c6696de1c2f85b
Gerrit-Change-Number: 42654
Gerrit-PatchSet: 1
Gerrit-Owner: Timur Davydov <[email protected]>

Reply via email to