diff --git a/debian/copyright b/debian/copyright index 391dc196..3e546eb2 100644 --- a/debian/copyright +++ b/debian/copyright @@ -60,17 +60,6 @@ License: OpenCores-I2C OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -Files: host/cmake/modules/FindLibPThreadsWin32.cmake -Copyright: 2008-2009 Hendrik Sattler - 2013-2018 Nuand LLC -License: GPL-2+ -Comment: This is file is based off of the FindLibUSB.cmake file written by - Hendrik Sattler, from the OpenOBEX project (licensed GPLv2/LGPL). (If - this is not correct, please contact us so we can attribute the author - appropriately.) - https://github.com/zuckschwerdt/openobex/blob/master/CMakeModules/FindLibUSB.cmake - http://dev.zuckschwerdt.org/openobex/ - Files: host/cmake/modules/FindLibUSB.cmake Copyright: 2008-2009 Hendrik Sattler 2013-2018 Nuand LLC diff --git a/host/CMakeLists.txt b/host/CMakeLists.txt index 5666a149..3f3562a1 100644 --- a/host/CMakeLists.txt +++ b/host/CMakeLists.txt @@ -230,29 +230,6 @@ add_custom_target(uninstall # Windows-only, Post build: Copy libraries to our build output directory ################################################################################ if(WIN32 AND NOT CMAKE_CROSSCOMPILING) - find_package(LibPThreadsWin32) - if(LIBPTHREADSWIN32_FOUND) - set(WIN_RUNTIME_ITEMS ${WIN_RUNTIME_ITEMS} - "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR}/licenses/pthreads-win32/COPYING.LIB.txt" - "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR}/pthreadVC2.dll") - - add_custom_command( - OUTPUT "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR}/licenses/pthreads-win32/COPYING.LIB.txt" - COMMAND ${CMAKE_COMMAND} -E copy_if_different - "${LIBPTHREADSWIN32_LIB_COPYING}" - "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR}/licenses/pthreads-win32/COPYING.LIB.txt" - COMMENT "Copying pthreads-win32 license to output directory..." - ) - - add_custom_command( - OUTPUT "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR}/pthreadVC2.dll" - COMMAND ${CMAKE_COMMAND} -E copy_if_different - "${LIBPTHREADSWIN32_DLL}" - "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR}/" - COMMENT "Copying pthreadVC2.dll to output directory..." - ) - endif(LIBPTHREADSWIN32_FOUND) - if(LIBUSB_FOUND) set(WIN_RUNTIME_ITEMS ${WIN_RUNTIME_ITEMS} "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR}/licenses/libusb/COPYING" diff --git a/host/README.md b/host/README.md index 120cb579..e03a3c45 100644 --- a/host/README.md +++ b/host/README.md @@ -23,9 +23,6 @@ Windows users that wish to use a Cypress driver/library based libbladeRF backend ### CMake ### [CMake][CMake.org] is used to build the items in this directory. \>= v2.8.5 is required, but the latest available version is recommended. -### pthreads-win32 - Windows-only ### -Windows users will need to get [pthreads-win32], and may (with newer Windows versions) need to build it from scratch. - ## Build ## ### Linux and OSX ### @@ -63,7 +60,6 @@ This process was most recently validated on Windows 10 Enterprise with CMake 3.9 - If you run into any failures, check these items, then re-run Configure: - LIBUSB_PATH (and ENABLE_BACKEND_LIBUSB) - for using libusb - FX3_SDK_PATH (and ENABLE_BACKEND_CYAPI) - for using the Cypress driver - - LIBPTHREADSWIN32_PATH - Adjust options as desired and re-run the "Configure" command as needed. Values in red are new, and should be reviewed before running "Configure" again to get the red out. - Click the CMake GUI's "Generate" button. - In the ```build``` directory, a Visual Studio solution will now exist. You can click "Open Project" to get there. @@ -71,21 +67,6 @@ This process was most recently validated on Windows 10 Enterprise with CMake 3.9 - Upon building the solution, build artifacts will be present in: ```build\output\``` - `````` may refer to Debug or Release, for example -#### pthreads-win32 #### -If you are using a newer version of Windows / Visual Studio and are getting ```error C0211: 'timespec': 'struct' type redefinition``` when trying to build bladeRF, you will need to rebuild pthreads-win32. The below process seems to work with pthreads-w32 2.9.1. - -- Get full source tree from [pthreads-win32] website, and unpack someplace convenient -- In Visual Studio, open `pthreads-w32-2-9-1-release\pthreads.2\pthread.dsw` (one-way upgrade is OK) -- Right-click the solution, select "Retarget solution", hit OK. -- Change "Solution Configurations" dropdown to "Release" ("Debug" doesn't work, but that's OK) -- Edit pthread.h to add `#define HAVE_STRUCT_TIMESPEC` and `#define PTW32_ARCHx64` near the top, just below the include guard -- Build the solution - -- Back over in the bladeRF CMake: - - File -> Delete Cache - - Redo the "Configure" dance as before - - Point `LIBPTHREADSWIN32_PATH` to the `pthreads-w32-2-9-1-release\pthreads.2` directory you were just working in - ## CMake Options and Flags ## Below is a list of useful and project-specific CMake options. Please see the [CMake documentation] for @@ -120,4 +101,3 @@ more information. [CMake documentation]: http://www.cmake.org/cmake/help/documentation.html (Cmake documentation) [Cypress FX3 SDK]: http://www.cypress.com/?rID=57990 (Cypress FX3 SDK) [redhat144967]: https://bugzilla.redhat.com/show_bug.cgi?id=144967 (Red Hat Bugzilla - Bug 144967) -[pthreads-win32]: https://sourceware.org/pthreads-win32/ (POSIX Threads for Win32) diff --git a/host/cmake/modules/FindLibPThreadsWin32.cmake b/host/cmake/modules/FindLibPThreadsWin32.cmake deleted file mode 100644 index 6cdfacae..00000000 --- a/host/cmake/modules/FindLibPThreadsWin32.cmake +++ /dev/null @@ -1,113 +0,0 @@ -# This is file is based off of the FindLibUSB.cmake file written by Hendrik Sattler, -# from the OpenOBEX project (licensed GPLv2/LGPL). (If this is not correct, -# please contact us so we can attribute the author appropriately.) -# -# https://github.com/zuckschwerdt/openobex/blob/master/CMakeModules/FindLibUSB.cmake -# http://dev.zuckschwerdt.org/openobex/ -# -# Find pthreads-win32 -# -# This requires the LIBPTHREADSWIN32_PATH variable to be set to the path to the -# a pthreads-win32 release, such as the Pre-built.2 directory from the 2.9.1 release: -# ftp://sourceware.org/pub/pthreads-win32/pthreads-w32-2-9-1-release.zip -# -# The following standard variables get defined: -# LIBPTHREADSWIN32_FOUND: true if LibUSB was found -# LIBPTHREADSWIN32_HEADER_FILE: the location of the C header file -# LIBPTHREADSWIN32_INCLUDE_DIRS: the directories that contain headers -# LIBPTHREADSWIN32_LIBRARIES: the library files -# LIBPTHREADSWIN32_LIB_COPYING: the license associated with the library -# LIBPTHREADSWIN32_DLL: the pthreadVC2.dll file - -if(DEFINED __INCLUDED_BLADERF_FINDLIBPTHREADSWIN32_CMAKE) - return() -endif() -set(__INCLUDED_BLADERF_FINDLIBPTHREADSWIN32_CMAKE TRUE) - -include ( CheckLibraryExists ) -include ( CheckIncludeFile ) - -set(LIBPTHREADSWIN32_PATH - "C:/Program Files (x86)/pthreads-win32" - CACHE - PATH - "Path to win-pthreads files. (This is generally only needed for Windows users who downloaded binary distributions.)" - ) - -find_file ( LIBPTHREADSWIN32_HEADER_FILE - NAMES - pthread.h - PATHS - ${LIBPTHREADSWIN32_PATH} - PATH_SUFFIXES - include include -) -mark_as_advanced ( LIBPTHREADSWIN32_HEADER_FILE ) -get_filename_component ( LIBPTHREADSWIN32_INCLUDE_DIRS "${LIBPTHREADSWIN32_HEADER_FILE}" PATH ) - -if ( WIN32 ) - if ( MSVC ) - if ( CMAKE_CL_64 ) - set ( LIBPTHREADSWIN32_LIBRARY_PATH_SUFFIX x64 ) - else ( CMAKE_CL_64 ) - set ( LIBPTHREADSWIN32_LIBRARY_PATH_SUFFIX x86 ) - endif ( CMAKE_CL_64 ) - elseif ( CMAKE_COMPILER_IS_GNUCC ) - if ( CMAKE_SIZEOF_VOID_P EQUAL 8 ) - set ( LIBPTHREADSWIN32_LIBRARY_PATH_SUFFIX x64 ) - else ( CMAKE_SIZEOF_VOID_P EQUAL 8 ) - set ( LIBPTHREADSWIN32_LIBRARY_PATH_SUFFIX x86 ) - endif ( CMAKE_SIZEOF_VOID_P EQUAL 8 ) - endif ( MSVC ) - - find_library ( PTHREAD_LIBRARY - NAMES - pthreadVC2 - PATHS - ${LIBPTHREADSWIN32_PATH} - PATH_SUFFIXES - lib/${LIBPTHREADSWIN32_LIBRARY_PATH_SUFFIX} - ) - - mark_as_advanced ( PTHREAD_LIBRARY ) - if ( PTHREAD_LIBRARY ) - set ( LIBPTHREADSWIN32_LIBRARIES ${PTHREAD_LIBRARY} ) - endif ( PTHREAD_LIBRARY ) - - find_file ( PTHREAD_DLL - NAMES - pthreadVC2.dll - PATHS - ${LIBPTHREADSWIN32_PATH} - PATH_SUFFIXES - dll/${LIBPTHREADSWIN32_LIBRARY_PATH_SUFFIX} - . - ) - - mark_as_advanced ( PTHREAD_DLL ) - if ( PTHREAD_DLL ) - set ( LIBPTHREADSWIN32_DLL ${PTHREAD_DLL} ) - endif ( PTHREAD_DLL ) - -else ( WIN32 ) - message(FATAL_ERROR "This file only supports Windows") -endif ( WIN32 ) - -set ( LIBPTHREADSWIN32_LIB_COPYING - "${LIBPTHREADSWIN32_PATH}/COPYING.LIB" ) -if ( NOT EXISTS ${LIBPTHREADSWIN32_LIB_COPYING} ) - message(FATAL_ERROR "Unable to find pthread-win32 COPYING.LIB file") -endif () - -if ( LIBPTHREADSWIN32_INCLUDE_DIRS AND LIBPTHREADSWIN32_LIBRARIES AND LIBPTHREADSWIN32_DLL ) - set ( LIBPTHREADSWIN32_FOUND true ) -endif ( LIBPTHREADSWIN32_INCLUDE_DIRS AND LIBPTHREADSWIN32_LIBRARIES AND LIBPTHREADSWIN32_DLL ) - -if ( LIBPTHREADSWIN32_FOUND ) - set ( CMAKE_REQUIRED_INCLUDES "${LIBPTHREADSWIN32_INCLUDE_DIRS}" ) - check_include_file ( "{LIBPTHREADSWIN32_HEADER_FILE}" LIBPTHREADSWIN32_FOUND ) -endif ( LIBPTHREADSWIN32_FOUND ) - -if ( NOT LIBPTHREADSWIN32_FOUND ) - message ( FATAL_ERROR "pthreads-win32 not found. If you're using a binary distribution, try setting -DLIBPTHREADSWIN32_PATH=." ) -endif ( NOT LIBPTHREADSWIN32_FOUND ) diff --git a/host/common/include/thread.h b/host/common/include/thread.h index 855a24bf..bc6f7d26 100644 --- a/host/common/include/thread.h +++ b/host/common/include/thread.h @@ -32,11 +32,30 @@ /* Currently, only pthreads is supported. In the future, native windows threads * may be used; one of the objectives of this file is to ease that transistion. */ -#include #include "rel_assert.h" -#define MUTEX pthread_mutex_t +#if (defined(_WIN32) || defined(USE_NATIVE_THREADS)) && \ + !defined(DO_NOT_USE_PTHREADS) +// Do not use pthreads on Windows +#undef USE_PTHREADS +#else +// Use pthreads on non-Windows platforms +#undef USE_PTHREADS +#define USE_PTHREADS +#endif +#ifdef USE_PTHREADS + +/* Use pthreads */ + +#include +#include +#include +#define CLOCK_REALTIME 0 + +#define THREAD pthread_t +#define MUTEX pthread_mutex_t +#define COND pthread_cond_t #ifdef ENABLE_LOCK_CHECKS # define MUTEX_INIT(m) do { \ int status; \ @@ -64,11 +83,103 @@ int status = pthread_mutex_destroy(m); \ assert(status == 0 && "Mutex destroy failure");\ } while (0) -#else -# define MUTEX_INIT(m) pthread_mutex_init(m, NULL) -# define MUTEX_LOCK(m) pthread_mutex_lock(m) -# define MUTEX_UNLOCK(m) pthread_mutex_unlock(m) -# define MUTEX_DESTROY(m) pthread_mutex_destroy(m) -#endif +#else /* ENABLE_LOCK_CHECKS */ + +#define MUTEX_INIT(m) pthread_mutex_init(m, NULL) +#define MUTEX_LOCK(m) pthread_mutex_lock(m) +#define MUTEX_UNLOCK(m) pthread_mutex_unlock(m) +#define MUTEX_DESTROY(m) pthread_mutex_destroy(m) + +#endif /* ENABLE_LOCK_CHECKS */ + +# define THREAD_CREATE(x,y,z) pthread_create(x, NULL, y, z) +# define THREAD_SUCCESS 0 +# define THREAD_TIMEOUT ETIMEDOUT +# define THREAD_CANCEL(m) pthread_cancel(m) +# define THREAD_JOIN(t, s) pthread_join(t, s) +# define THREAD_EXIT(s) pthread_exit(s) + +# define COND_INIT(m) pthread_cond_init(m, NULL) +# define COND_SIGNAL(m) pthread_cond_signal(m) +// POSIX implementation as a function +static inline int posix_cond_timedwait(pthread_cond_t *c, + pthread_mutex_t *m, + unsigned int t) +{ + struct timespec ts; + clock_gettime(CLOCK_REALTIME, &ts); + ts.tv_sec += t / 1000; + ts.tv_nsec += (t % 1000) * 1000000; + if (ts.tv_nsec >= 1000000000) { + ts.tv_sec++; + ts.tv_nsec -= 1000000000; + } + return pthread_cond_timedwait(c, m, &ts) == ETIMEDOUT ? -1 : 0; +} +#define COND_TIMED_WAIT(c, m, t) posix_cond_timedwait(c, m, t) + +#define COND_WAIT(c, m) pthread_cond_wait(c, m) + + +#else /* USE_PTHREADS */ +#include +#include + +/* Use C11 threads */ +//#include + +#define THREAD HANDLE // For thread handles +#define MUTEX CRITICAL_SECTION // For mutexes +#define COND CONDITION_VARIABLE // For condition variables + +#ifdef ENABLE_LOCK_CHECKS +# define MUTEX_INIT(m) do { \ + int status; \ + status = mtx_init(m, mtx_plain | mtx_recursive); \ + assert(status == thrd_success && "Mutex init failure"); \ + } while (0) + +# define MUTEX_LOCK(m) do { \ + int status = mtx_lock(m); \ + assert(status == thrd_success && "Mutex lock failure");\ + } while (0) + +# define MUTEX_UNLOCK(m) do { \ + int status = mtx_unlock(m); \ + assert(status == thrd_success && "Mutex unlock failure");\ + } while (0) + +# define MUTEX_DESTROY(m) do { \ + mtx_destroy(m); \ + } while (0) +#else /* ENABLE_LOCK_CHECKS */ + +#define MUTEX_INIT(m) InitializeCriticalSection(m) +#define MUTEX_LOCK(m) EnterCriticalSection(m) +#define MUTEX_UNLOCK(m) LeaveCriticalSection(m) +#define MUTEX_DESTROY(m) DeleteCriticalSection(m) + +#endif /* ENABLE_LOCK_CHECKS */ + + +# define THREAD_CREATE(handle, func, arg) \ + ((*(handle) = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)(func), \ + (LPVOID)(arg), 0, NULL)) == NULL ? GetLastError() : 0) + + +# define THREAD_SUCCESS 0 +# define THREAD_TIMEOUT WAIT_TIMEOUT +# define THREAD_CANCEL(m) TerminateThread(m, 0) +# define THREAD_JOIN(t, s) WaitForSingleObject(t, INFINITE) +# define THREAD_EXIT(s) ExitThread(s) + +# define COND_INIT(m) (InitializeConditionVariable(m), 0) +# define COND_SIGNAL(m) WakeConditionVariable(m) +# define COND_TIMED_WAIT(c, m, t) \ + (SleepConditionVariableCS(c, m, t) ? 0 : GetLastError()) +# define COND_WAIT(c, m) (!SleepConditionVariableCS(c, m, INFINITE)) + + +#endif /* USE_PTHREADS */ #endif diff --git a/host/common/include/windows/clock_gettime.h b/host/common/include/windows/clock_gettime.h index 05652ae1..f720a4db 100644 --- a/host/common/include/windows/clock_gettime.h +++ b/host/common/include/windows/clock_gettime.h @@ -25,7 +25,7 @@ #ifndef WIN_CLOCK_GETTIME_H_ #define WIN_CLOCK_GETTIME_H_ -#include +#include "thread.h" #ifdef __cplusplus extern "C" { diff --git a/host/common/include/windows/gettimeofday.h b/host/common/include/windows/gettimeofday.h index a1a4bddf..acfa22d1 100644 --- a/host/common/include/windows/gettimeofday.h +++ b/host/common/include/windows/gettimeofday.h @@ -25,7 +25,7 @@ #ifndef WIN_GETTIMEOFDAY_H_ #define WIN_GETTIMEOFDAY_H_ -#include +#include "thread.h" #ifdef __cplusplus extern "C" { diff --git a/host/common/include/windows/nanosleep.h b/host/common/include/windows/nanosleep.h index 99af6feb..011eff5c 100644 --- a/host/common/include/windows/nanosleep.h +++ b/host/common/include/windows/nanosleep.h @@ -25,7 +25,7 @@ #ifndef WIN_NANOSLEEP_H_ #define WIN_NANOSLEEP_H_ -#include +#include "thread.h" #ifdef __cplusplus extern "C" { diff --git a/host/common/include/windows/ptw32_timespec.h b/host/common/include/windows/ptw32_timespec.h index 20aa8a68..41412031 100644 --- a/host/common/include/windows/ptw32_timespec.h +++ b/host/common/include/windows/ptw32_timespec.h @@ -48,9 +48,10 @@ #endif #include +#include #include #include "host_config.h" -#include "pthread.h" +#include "thread.h" #ifdef __cplusplus extern "C" { diff --git a/host/common/thirdparty/ad936x/CMakeLists.txt b/host/common/thirdparty/ad936x/CMakeLists.txt index c134f747..158c49bd 100644 --- a/host/common/thirdparty/ad936x/CMakeLists.txt +++ b/host/common/thirdparty/ad936x/CMakeLists.txt @@ -94,16 +94,7 @@ target_include_directories(ad936x PUBLIC if(MSVC) add_definitions(-DHAVE_STRUCT_TIMESPEC) target_include_directories(ad936x PUBLIC ${MSVC_C99_INCLUDES}) - - find_package(LibPThreadsWin32 REQUIRED) - if(LIBPTHREADSWIN32_FOUND) - set(HAVE_THREADS true) - target_include_directories(ad936x PUBLIC ${LIBPTHREADSWIN32_INCLUDE_DIRS}) - set(LIBAD936X_LIBS - ${LIBAD936X_LIBS} ${LIBPTHREADSWIN32_LIBRARIES}) - else() - set(HAVE_THREADS false) - endif() + set(HAVE_THREADS true) else(MSVC) find_package(Threads REQUIRED) if(Threads_FOUND) diff --git a/host/libraries/libbladeRF/CMakeLists.txt b/host/libraries/libbladeRF/CMakeLists.txt index dbc62b60..cc93c4d6 100644 --- a/host/libraries/libbladeRF/CMakeLists.txt +++ b/host/libraries/libbladeRF/CMakeLists.txt @@ -196,16 +196,7 @@ set(LIBBLADERF_INCLUDES if(MSVC) set(LIBBLADERF_INCLUDES ${LIBBLADERF_INCLUDES} ${MSVC_C99_INCLUDES}) - find_package(LibPThreadsWin32 REQUIRED) - if(LIBPTHREADSWIN32_FOUND) - set(HAVE_THREADS true) - set(LIBBLADERF_INCLUDES - ${LIBBLADERF_INCLUDES} ${LIBPTHREADSWIN32_INCLUDE_DIRS}) - set(LIBBLADERF_LIBS - ${LIBBLADERF_LIBS} ${LIBPTHREADSWIN32_LIBRARIES}) - else() - set(HAVE_THREADS false) - endif() + set(HAVE_THREADS true) else(MSVC) find_package(Threads REQUIRED) if(Threads_FOUND) @@ -280,11 +271,6 @@ if(ENABLE_BACKEND_CYAPI) set(LIBBLADERF_INCLUDES ${LIBBLADERF_INCLUDES} ${CYAPI_INCLUDE_DIRS}) endif(ENABLE_BACKEND_CYAPI) -if(MSVC) - set(LIBBLADERF_INCLUDES ${LIBBLADERF_INCLUDES} - ${LIBPTHREADSWIN32_INCLUDE_DIRS}) -endif() - if(ENABLE_LIBBLADERF_SYNC_LOG_VERBOSE AND ENABLE_LIBBLADERF_LOGGING) add_definitions(-DENABLE_LIBBLADERF_SYNC_LOG_VERBOSE) endif() @@ -437,11 +423,9 @@ add_library(libbladerf_shared SHARED ${LIBBLADERF_SOURCE}) # Build configuration ################################################################################ -if(MSVC) - set(LIBBLADERF_LIBS ${LIBBLADERF_LIBS} ${LIBPTHREADSWIN32_LIBRARIES}) -else() +if(NOT MSVC) set(LIBBLADERF_LIBS ${LIBBLADERF_LIBS} ${CMAKE_THREAD_LIBS_INIT}) -endif(MSVC) +endif(NOT MSVC) if(ENABLE_BACKEND_LIBUSB) set(LIBBLADERF_LIBS ${LIBBLADERF_LIBS} ${LIBUSB_LIBRARIES}) diff --git a/host/libraries/libbladeRF/src/backend/usb/cyapi.c b/host/libraries/libbladeRF/src/backend/usb/cyapi.c index 48bef435..bd9e6453 100644 --- a/host/libraries/libbladeRF/src/backend/usb/cyapi.c +++ b/host/libraries/libbladeRF/src/backend/usb/cyapi.c @@ -26,7 +26,7 @@ extern "C" { #include -#include +#include "thread.h" #include #include "bladeRF.h" /* Firmware interface */ @@ -707,7 +707,7 @@ static int cyapi_stream(void *driver, struct bladerf_stream *stream, data->transfers[i].buffer = NULL; data->transfers[i].handle = NULL; data->num_avail++; - pthread_cond_signal(&stream->can_submit_buffer); + COND_SIGNAL(&stream->can_submit_buffer); if (next_buffer == BLADERF_STREAM_SHUTDOWN) { done = true; @@ -792,20 +792,20 @@ int cyapi_submit_stream_buffer(void *driver, struct bladerf_stream *stream, return BLADERF_ERR_UNEXPECTED; } - while (data->num_avail == 0 && status == 0) { - status = pthread_cond_timedwait(&stream->can_submit_buffer, + while (data->num_avail == 0 && status == THREAD_SUCCESS) { + status = COND_TIMED_WAIT(&stream->can_submit_buffer, &stream->lock, &timeout_abs); } } else { - while (data->num_avail == 0 && status == 0) { - status = pthread_cond_wait(&stream->can_submit_buffer, + while (data->num_avail == 0 && status == THREAD_SUCCESS) { + status = COND_WAIT(&stream->can_submit_buffer, &stream->lock); } } } - if (status == ETIMEDOUT) { + if (status == THREAD_TIMEOUT) { return BLADERF_ERR_TIMEOUT; } else if (status != 0) { return BLADERF_ERR_UNEXPECTED; diff --git a/host/libraries/libbladeRF/src/backend/usb/libusb.c b/host/libraries/libbladeRF/src/backend/usb/libusb.c index 6100f8ad..e853bf74 100644 --- a/host/libraries/libbladeRF/src/backend/usb/libusb.c +++ b/host/libraries/libbladeRF/src/backend/usb/libusb.c @@ -22,7 +22,7 @@ #include "host_config.h" #include #include -#include +#include "thread.h" #include #include #if 1 == BLADERF_OS_FREEBSD @@ -1054,7 +1054,7 @@ static void LIBUSB_CALL lusb_stream_cb(struct libusb_transfer *transfer) } else { stream_data->transfer_status[transfer_i] = TRANSFER_AVAIL; stream_data->num_avail++; - pthread_cond_signal(&stream->can_submit_buffer); + COND_SIGNAL(&stream->can_submit_buffer); } /* Check to see if the transfer has been cancelled or errored */ @@ -1409,7 +1409,6 @@ int lusb_submit_stream_buffer(void *driver, struct bladerf_stream *stream, { int status = 0; struct lusb_stream_data *stream_data = stream->backend_data; - struct timespec timeout_abs; if (buffer == BLADERF_STREAM_SHUTDOWN) { if (stream_data->num_avail == stream_data->num_transfers) { @@ -1430,25 +1429,20 @@ int lusb_submit_stream_buffer(void *driver, struct bladerf_stream *stream, } if (timeout_ms != 0) { - status = populate_abs_timeout(&timeout_abs, timeout_ms); - if (status != 0) { - return BLADERF_ERR_UNEXPECTED; - } - - while (stream_data->num_avail == 0 && status == 0) { - status = pthread_cond_timedwait(&stream->can_submit_buffer, + while (stream_data->num_avail == 0 && status == THREAD_SUCCESS) { + status = COND_TIMED_WAIT(&stream->can_submit_buffer, &stream->lock, - &timeout_abs); + timeout_ms); } } else { - while (stream_data->num_avail == 0 && status == 0) { - status = pthread_cond_wait(&stream->can_submit_buffer, + while (stream_data->num_avail == 0 && status == THREAD_SUCCESS) { + status = COND_WAIT(&stream->can_submit_buffer, &stream->lock); } } } - if (status == ETIMEDOUT) { + if (status == THREAD_TIMEOUT) { log_debug("%s: Timed out waiting for a transfer to become available.\n", __FUNCTION__); return BLADERF_ERR_TIMEOUT; diff --git a/host/libraries/libbladeRF/src/streaming/async.c b/host/libraries/libbladeRF/src/streaming/async.c index cffa9d21..5af70e7c 100644 --- a/host/libraries/libbladeRF/src/streaming/async.c +++ b/host/libraries/libbladeRF/src/streaming/async.c @@ -66,12 +66,12 @@ int async_init_stream(struct bladerf_stream **stream, MUTEX_INIT(&lstream->lock); - if (pthread_cond_init(&lstream->can_submit_buffer, NULL) != 0) { + if (COND_INIT(&lstream->can_submit_buffer) != THREAD_SUCCESS) { free(lstream); return BLADERF_ERR_UNEXPECTED; } - if (pthread_cond_init(&lstream->stream_started, NULL) != 0) { + if (COND_INIT(&lstream->stream_started) != THREAD_SUCCESS) { free(lstream); return BLADERF_ERR_UNEXPECTED; } @@ -203,7 +203,7 @@ int async_run_stream(struct bladerf_stream *stream, bladerf_channel_layout layou MUTEX_LOCK(&stream->lock); stream->layout = layout; stream->state = STREAM_RUNNING; - pthread_cond_signal(&stream->stream_started); + COND_SIGNAL(&stream->stream_started); MUTEX_UNLOCK(&stream->lock); status = dev->backend->stream(stream, layout); @@ -218,32 +218,23 @@ int async_submit_stream_buffer(struct bladerf_stream *stream, bool nonblock) { int status = 0; - struct timespec timeout_abs; MUTEX_LOCK(&stream->lock); if (buffer != BLADERF_STREAM_SHUTDOWN) { - if (stream->state != STREAM_RUNNING && timeout_ms != 0) { - status = populate_abs_timeout(&timeout_abs, timeout_ms); - if (status != 0) { - log_debug("Failed to populate timeout value\n"); - goto error; - } - } - while (stream->state != STREAM_RUNNING) { log_debug("Buffer submitted while stream's not running. " "Waiting for stream to start.\n"); if (timeout_ms == 0) { - status = pthread_cond_wait(&stream->stream_started, + status = COND_WAIT(&stream->stream_started, &stream->lock); } else { - status = pthread_cond_timedwait(&stream->stream_started, - &stream->lock, &timeout_abs); + status = COND_TIMED_WAIT(&stream->stream_started, + &stream->lock, timeout_ms); } - if (status == ETIMEDOUT) { + if (status == THREAD_TIMEOUT) { status = BLADERF_ERR_TIMEOUT; log_debug("%s: %u ms timeout expired", __FUNCTION__, timeout_ms); diff --git a/host/libraries/libbladeRF/src/streaming/async.h b/host/libraries/libbladeRF/src/streaming/async.h index b0fd3b8c..8d5165a3 100644 --- a/host/libraries/libbladeRF/src/streaming/async.h +++ b/host/libraries/libbladeRF/src/streaming/async.h @@ -22,8 +22,6 @@ #ifndef STREAMING_ASYNC_H_ #define STREAMING_ASYNC_H_ -#include - #include #include "thread.h" @@ -58,8 +56,8 @@ struct bladerf_stream { /* The following items must be accessed atomically */ int error_code; bladerf_stream_state state; - pthread_cond_t can_submit_buffer; - pthread_cond_t stream_started; + COND can_submit_buffer; + COND stream_started; void *backend_data; }; diff --git a/host/libraries/libbladeRF/src/streaming/sync.c b/host/libraries/libbladeRF/src/streaming/sync.c index 1b3590cc..28edce7a 100644 --- a/host/libraries/libbladeRF/src/streaming/sync.c +++ b/host/libraries/libbladeRF/src/streaming/sync.c @@ -237,7 +237,7 @@ int sync_init(struct bladerf_sync *sync, __FUNCTION__, sync->meta.samples_per_msg); MUTEX_INIT(&sync->buf_mgmt.lock); - pthread_cond_init(&sync->buf_mgmt.buf_ready, NULL); + COND_INIT(&sync->buf_mgmt.buf_ready); sync->buf_mgmt.status = (sync_buffer_status*) malloc(num_buffers * sizeof(sync_buffer_status)); if (sync->buf_mgmt.status == NULL) { @@ -332,22 +332,18 @@ static int wait_for_buffer(struct buffer_mgmt *b, unsigned int dbg_idx) { int status; - struct timespec timeout; if (timeout_ms == 0) { log_verbose("%s: Infinite wait for buffer[%d] (status: %d).\n", dbg_name, dbg_idx, b->status[dbg_idx]); - status = pthread_cond_wait(&b->buf_ready, &b->lock); + status = COND_WAIT(&b->buf_ready, &b->lock); } else { log_verbose("%s: Timed wait for buffer[%d] (status: %d).\n", dbg_name, dbg_idx, b->status[dbg_idx]); - status = populate_abs_timeout(&timeout, timeout_ms); - if (status == 0) { - status = pthread_cond_timedwait(&b->buf_ready, &b->lock, &timeout); - } + status = COND_TIMED_WAIT(&b->buf_ready, &b->lock, timeout_ms); } - if (status == ETIMEDOUT) { + if (status == THREAD_TIMEOUT) { log_error("%s: Timed out waiting for buf_ready after %d ms\n", __FUNCTION__, timeout_ms); status = BLADERF_ERR_TIMEOUT; diff --git a/host/libraries/libbladeRF/src/streaming/sync.h b/host/libraries/libbladeRF/src/streaming/sync.h index 89777919..e28d0938 100644 --- a/host/libraries/libbladeRF/src/streaming/sync.h +++ b/host/libraries/libbladeRF/src/streaming/sync.h @@ -23,7 +23,6 @@ #define STREAMING_SYNC_H_ #include -#include #include @@ -88,7 +87,7 @@ struct buffer_mgmt { MUTEX lock; - pthread_cond_t buf_ready; /**< Buffer produced by RX callback, or + COND buf_ready; /**< Buffer produced by RX callback, or * buffer emptied by TX callback */ }; diff --git a/host/libraries/libbladeRF/src/streaming/sync_worker.c b/host/libraries/libbladeRF/src/streaming/sync_worker.c index b2ec8065..cb7911a7 100644 --- a/host/libraries/libbladeRF/src/streaming/sync_worker.c +++ b/host/libraries/libbladeRF/src/streaming/sync_worker.c @@ -83,7 +83,7 @@ static void *rx_callback(struct bladerf *dev, /* This buffer is now ready for the consumer */ b->status[samples_idx] = SYNC_BUFFER_FULL; b->actual_lengths[samples_idx] = num_samples; - pthread_cond_signal(&b->buf_ready); + COND_SIGNAL(&b->buf_ready); /* Update the state of the buffer being submitted next */ next_idx = b->prod_i; @@ -155,7 +155,7 @@ static void *tx_callback(struct bladerf *dev, completed_idx = sync_buf2idx(b, samples); assert(b->status[completed_idx] == SYNC_BUFFER_IN_FLIGHT); b->status[completed_idx] = SYNC_BUFFER_EMPTY; - pthread_cond_signal(&b->buf_ready); + COND_SIGNAL(&b->buf_ready); /* If the callback is assigned to be the submitter, there are * buffers pending submission */ @@ -230,25 +230,25 @@ int sync_worker_init(struct bladerf_sync *s) MUTEX_INIT(&s->worker->state_lock); MUTEX_INIT(&s->worker->request_lock); - status = pthread_cond_init(&s->worker->state_changed, NULL); - if (status != 0) { - log_debug("%s worker: pthread_cond_init(state_changed) failed: %d\n", + status = COND_INIT(&s->worker->state_changed); + if (status != THREAD_SUCCESS) { + log_debug("%s worker: cond_init(state_changed) failed: %d\n", worker2str(s), status); status = BLADERF_ERR_UNEXPECTED; goto worker_init_out; } - status = pthread_cond_init(&s->worker->requests_pending, NULL); - if (status != 0) { - log_debug("%s worker: pthread_cond_init(requests_pending) failed: %d\n", + status = COND_INIT(&s->worker->requests_pending); + if (status != THREAD_SUCCESS) { + log_debug("%s worker: cond_init(requests_pending) failed: %d\n", worker2str(s), status); status = BLADERF_ERR_UNEXPECTED; goto worker_init_out; } - status = pthread_create(&s->worker->thread, NULL, sync_worker_task, s); - if (status != 0) { - log_debug("%s worker: pthread_create failed: %d\n", worker2str(s), + status = THREAD_CREATE(&s->worker->thread, sync_worker_task, s); + if (status != THREAD_SUCCESS) { + log_debug("%s worker: create failed: %d\n", worker2str(s), status); status = BLADERF_ERR_UNEXPECTED; goto worker_init_out; @@ -274,7 +274,7 @@ worker_init_out: } void sync_worker_deinit(struct sync_worker *w, - pthread_mutex_t *lock, pthread_cond_t *cond) + MUTEX *lock, COND *cond) { int status; @@ -289,7 +289,7 @@ void sync_worker_deinit(struct sync_worker *w, if (lock != NULL && cond != NULL) { MUTEX_LOCK(lock); - pthread_cond_signal(cond); + COND_SIGNAL(cond); MUTEX_UNLOCK(lock); } @@ -297,10 +297,10 @@ void sync_worker_deinit(struct sync_worker *w, if (status != 0) { log_warning("Timed out while stopping worker. Canceling thread.\n"); - pthread_cancel(w->thread); + THREAD_CANCEL(w->thread); } - pthread_join(w->thread, NULL); + THREAD_JOIN(w->thread, NULL); log_verbose("%s: Worker joined.\n", __FUNCTION__); async_deinit_stream(w->stream); @@ -312,7 +312,7 @@ void sync_worker_submit_request(struct sync_worker *w, unsigned int request) { MUTEX_LOCK(&w->request_lock); w->requests |= request; - pthread_cond_signal(&w->requests_pending); + COND_SIGNAL(&w->requests_pending); MUTEX_UNLOCK(&w->request_lock); } @@ -320,31 +320,14 @@ int sync_worker_wait_for_state(struct sync_worker *w, sync_worker_state state, unsigned int timeout_ms) { int status = 0; - struct timespec timeout_abs; - const int nsec_per_sec = 1000 * 1000 * 1000; if (timeout_ms != 0) { - const unsigned int timeout_sec = timeout_ms / 1000; - - status = clock_gettime(CLOCK_REALTIME, &timeout_abs); - if (status != 0) { - return BLADERF_ERR_UNEXPECTED; - } - - timeout_abs.tv_sec += timeout_sec; - timeout_abs.tv_nsec += (timeout_ms % 1000) * 1000 * 1000; - - if (timeout_abs.tv_nsec >= nsec_per_sec) { - timeout_abs.tv_sec += timeout_abs.tv_nsec / nsec_per_sec; - timeout_abs.tv_nsec %= nsec_per_sec; - } - MUTEX_LOCK(&w->state_lock); status = 0; - while (w->state != state && status == 0) { - status = pthread_cond_timedwait(&w->state_changed, + while (w->state != state && status == THREAD_SUCCESS) { + status = COND_TIMED_WAIT(&w->state_changed, &w->state_lock, - &timeout_abs); + timeout_ms); } MUTEX_UNLOCK(&w->state_lock); @@ -352,7 +335,7 @@ int sync_worker_wait_for_state(struct sync_worker *w, sync_worker_state state, MUTEX_LOCK(&w->state_lock); while (w->state != state) { log_verbose(": Waiting for state change, current = %d\n", w->state); - status = pthread_cond_wait(&w->state_changed, + status = COND_WAIT(&w->state_changed, &w->state_lock); } MUTEX_UNLOCK(&w->state_lock); @@ -362,7 +345,7 @@ int sync_worker_wait_for_state(struct sync_worker *w, sync_worker_state state, log_debug("%s: Wait on state change failed: %s\n", __FUNCTION__, strerror(status)); - if (status == ETIMEDOUT) { + if (status == THREAD_TIMEOUT) { status = BLADERF_ERR_TIMEOUT; } else { status = BLADERF_ERR_UNEXPECTED; @@ -392,7 +375,7 @@ static void set_state(struct sync_worker *w, sync_worker_state state) { MUTEX_LOCK(&w->state_lock); w->state = state; - pthread_cond_signal(&w->state_changed); + COND_SIGNAL(&w->state_changed); MUTEX_UNLOCK(&w->state_lock); } @@ -408,7 +391,7 @@ static sync_worker_state exec_idle_state(struct bladerf_sync *s) while (s->worker->requests == 0) { log_verbose("%s worker: Waiting for pending requests\n", worker2str(s)); - pthread_cond_wait(&s->worker->requests_pending, + COND_WAIT(&s->worker->requests_pending, &s->worker->request_lock); } @@ -434,7 +417,7 @@ static sync_worker_state exec_idle_state(struct bladerf_sync *s) } } - pthread_cond_signal(&s->buf_mgmt.buf_ready); + COND_SIGNAL(&s->buf_mgmt.buf_ready); } else { s->buf_mgmt.prod_i = s->stream_config.num_xfers; @@ -477,7 +460,7 @@ static void exec_running_state(struct bladerf_sync *s) * the stream error code back to the API caller */ if (status != 0) { MUTEX_LOCK(&s->buf_mgmt.lock); - pthread_cond_signal(&s->buf_mgmt.buf_ready); + COND_SIGNAL(&s->buf_mgmt.buf_ready); MUTEX_UNLOCK(&s->buf_mgmt.lock); } } diff --git a/host/libraries/libbladeRF/src/streaming/sync_worker.h b/host/libraries/libbladeRF/src/streaming/sync_worker.h index 35d5075b..75bf96e2 100644 --- a/host/libraries/libbladeRF/src/streaming/sync_worker.h +++ b/host/libraries/libbladeRF/src/streaming/sync_worker.h @@ -22,7 +22,7 @@ #include "host_config.h" #include "sync.h" #include -#include +#include "thread.h" #if BLADERF_OS_WINDOWS || BLADERF_OS_OSX #include "clock_gettime.h" @@ -49,7 +49,7 @@ typedef enum { } sync_worker_state; struct sync_worker { - pthread_t thread; + THREAD thread; struct bladerf_stream *stream; bladerf_stream_cb cb; @@ -58,7 +58,7 @@ struct sync_worker { sync_worker_state state; int err_code; MUTEX state_lock; - pthread_cond_t state_changed; /* Worker thread uses this to inform a + COND state_changed; /* Worker thread uses this to inform a * waiting main thread about a state * change */ @@ -66,7 +66,7 @@ struct sync_worker { * the sync->buf_mgmt.lock */ unsigned int requests; - pthread_cond_t requests_pending; + COND requests_pending; MUTEX request_lock; }; @@ -90,8 +90,8 @@ int sync_worker_init(struct bladerf_sync *s); * workers. */ void sync_worker_deinit(struct sync_worker *w, - pthread_mutex_t *lock, - pthread_cond_t *cond); + MUTEX *lock, + COND *cond); /** * Wait for state change with optional timeout diff --git a/host/libraries/libbladeRF_test/test_config_file/CMakeLists.txt b/host/libraries/libbladeRF_test/test_config_file/CMakeLists.txt index 0b24ddb1..4058de9b 100644 --- a/host/libraries/libbladeRF_test/test_config_file/CMakeLists.txt +++ b/host/libraries/libbladeRF_test/test_config_file/CMakeLists.txt @@ -34,21 +34,15 @@ set(SRC set(TEST_CONFIG_FILE_LIBS libbladerf_shared) if(MSVC) - find_package(LibPThreadsWin32 REQUIRED) set(TEST_CONFIG_FILE_INCLUDES ${TEST_CONFIG_FILE_INCLUDES} ${MSVC_C99_INCLUDES} - ${LIBPTHREADSWIN32_INCLUDE_DIRS} ) set(SRC ${SRC} ${BLADERF_HOST_COMMON_SOURCE_DIR}/windows/clock_gettime.c ${BLADERF_HOST_COMMON_SOURCE_DIR}/windows/mkdtemp.c ) - set(TEST_CONFIG_FILE_LIBS - ${TEST_CONFIG_FILE_LIBS} - ${LIBPTHREADSWIN32_LIBRARIES} - ) endif() add_definitions(-DLIBBLADERF_SEARCH_PREFIX="${CMAKE_INSTALL_PREFIX}") diff --git a/host/libraries/libbladeRF_test/test_ctrl/CMakeLists.txt b/host/libraries/libbladeRF_test/test_ctrl/CMakeLists.txt index d8379f5a..9967d924 100644 --- a/host/libraries/libbladeRF_test/test_ctrl/CMakeLists.txt +++ b/host/libraries/libbladeRF_test/test_ctrl/CMakeLists.txt @@ -13,14 +13,10 @@ endif() set(LIBS libbladerf_shared) -if(MSVC) - find_package(LibPThreadsWin32 REQUIRED) - set(INCLUDES ${INCLUDES} ${LIBPTHREADSWIN32_INCLUDE_DIRS}) - set(LIBS ${LIBS} ${LIBPTHREADSWIN32_LIBRARIES}) -else(MSVC) +if(NOT MSVC) find_package(Threads REQUIRED) set(LIBS ${LIBS} ${CMAKE_THREAD_LIBS_INIT}) -endif(MSVC) +endif(NOT MSVC) set(SRC src/main.c diff --git a/host/libraries/libbladeRF_test/test_ctrl/src/test_threads.c b/host/libraries/libbladeRF_test/test_ctrl/src/test_threads.c index 9eb5d413..be3b50cb 100644 --- a/host/libraries/libbladeRF_test/test_ctrl/src/test_threads.c +++ b/host/libraries/libbladeRF_test/test_ctrl/src/test_threads.c @@ -27,15 +27,15 @@ * while concurrently runing full duplex streams via the sync interface */ #include "test_ctrl.h" -#include +#include "thread.h" DECLARE_TEST_CASE(threads); struct sync_task { struct bladerf *dev; bladerf_direction direction; - pthread_t thread; - pthread_mutex_t lock; + THREAD thread; + MUTEX lock; bool launched; bool run; int status; @@ -53,7 +53,7 @@ struct thread_state { struct app_params *p; failure_count failures; struct thread_test_case const *tc; - pthread_t thread; + THREAD thread; }; static const struct thread_test_case tc[] = { @@ -69,9 +69,9 @@ static const struct thread_test_case tc[] = { static inline void get_sync_task_state(struct sync_task *t, bool *run) { - pthread_mutex_lock(&t->lock); + MUTEX_LOCK(&t->lock); *run = t->run; - pthread_mutex_unlock(&t->lock); + MUTEX_UNLOCK(&t->lock); } static void *stream_task(void *arg) @@ -129,9 +129,9 @@ out: } free(samples); - pthread_mutex_lock(&t->lock); + MUTEX_LOCK(&t->lock); t->status = status; - pthread_mutex_unlock(&t->lock); + MUTEX_UNLOCK(&t->lock); return NULL; } @@ -144,15 +144,15 @@ static void init_task(struct sync_task *t, t->run = true; t->status = 0; t->direction = dir; - pthread_mutex_init(&t->lock, NULL); + MUTEX_INIT(&t->lock); } static int launch_task(struct sync_task *t) { int status; - status = pthread_create(&t->thread, NULL, stream_task, t); - if (0 == status) { + status = THREAD_CREATE(&t->thread, stream_task, t); + if (THREAD_SUCCESS == status) { t->launched = true; } @@ -162,10 +162,10 @@ static int launch_task(struct sync_task *t) static inline int deinit_task(struct sync_task *t) { if (t->launched) { - pthread_mutex_lock(&t->lock); + MUTEX_LOCK(&t->lock); t->run = false; - pthread_mutex_unlock(&t->lock); - pthread_join(t->thread, NULL); + MUTEX_UNLOCK(&t->lock); + THREAD_JOIN(t->thread, NULL); return t->status; } else { return 0; @@ -257,9 +257,9 @@ failure_count test_threads(struct bladerf *dev, struct app_params *p, bool quiet } status = - pthread_create(&threads[i].thread, NULL, run_test_fn, &threads[i]); + THREAD_CREATE(&threads[i].thread, run_test_fn, &threads[i]); - if (0 == status) { + if (THREAD_SUCCESS == status) { PRINT(" Started test_%s thread...\n", threads[i].tc->test->name); threads[i].launched = true; } else { @@ -271,7 +271,7 @@ failure_count test_threads(struct bladerf *dev, struct app_params *p, bool quiet for (i = 0; i < num_threads; i++) { if (threads[i].launched) { - pthread_join(threads[i].thread, NULL); + THREAD_JOIN(threads[i].thread, NULL); PRINT("\n Joined test_%s thread.\n", threads[i].tc->test->name); failures += threads[i].failures; } diff --git a/host/libraries/libbladeRF_test/test_digital_loopback/CMakeLists.txt b/host/libraries/libbladeRF_test/test_digital_loopback/CMakeLists.txt index a057e46f..2ab287a7 100644 --- a/host/libraries/libbladeRF_test/test_digital_loopback/CMakeLists.txt +++ b/host/libraries/libbladeRF_test/test_digital_loopback/CMakeLists.txt @@ -14,14 +14,10 @@ endif() set(LIBS libbladerf_shared) -if(MSVC) - find_package(LibPThreadsWin32 REQUIRED) - set(INCLUDES ${INCLUDES} ${LIBPTHREADSWIN32_INCLUDE_DIRS}) - set(LIBS ${LIBS} ${LIBPTHREADSWIN32_LIBRARIES}) -else(MSVC) +if(NOT MSVC) find_package(Threads REQUIRED) set(LIBS ${LIBS} ${CMAKE_THREAD_LIBS_INIT}) -endif(MSVC) +endif(NOT MSVC) add_definitions(-DLOGGING_ENABLED=1) diff --git a/host/libraries/libbladeRF_test/test_digital_loopback/src/main.c b/host/libraries/libbladeRF_test/test_digital_loopback/src/main.c index 8bbf9749..0c1f1e75 100644 --- a/host/libraries/libbladeRF_test/test_digital_loopback/src/main.c +++ b/host/libraries/libbladeRF_test/test_digital_loopback/src/main.c @@ -29,13 +29,12 @@ #include #include -#include - #include "conversions.h" #include "log.h" #include "test_common.h" #include #include "async.h" +#include "thread.h" /****************************************************************************** * Type definitions @@ -453,7 +452,7 @@ int main(int argc, char *argv[]) struct rx_buffer_state rx_state; struct tx_buffer_state tx_state; struct bladerf_stream *rx_stream, *tx_stream; - pthread_t rx_thread, tx_thread; + THREAD rx_thread, tx_thread; bladerf_log_level level; int status; bool ok; @@ -716,9 +715,9 @@ int main(int argc, char *argv[]) goto error; } - status = pthread_create(&tx_thread, NULL, tx_streamer, tx_stream); - if (status < 0) { - log_error("pthread_create(TX): %s\n", strerror(status)); + status = THREAD_CREATE(&tx_thread, tx_streamer, tx_stream); + if (status != THREAD_SUCCESS) { + log_error("THREAD_CREATE(TX): %s\n", strerror(status)); goto error; } @@ -728,16 +727,16 @@ int main(int argc, char *argv[]) goto error; } - status = pthread_create(&rx_thread, NULL, rx_streamer, rx_stream); - if (status < 0) { - log_error("pthread_create(RX): %s\n", strerror(status)); + status = THREAD_CREATE(&rx_thread, rx_streamer, rx_stream); + if (status != THREAD_SUCCESS) { + log_error("THREAD_CREATE(RX): %s\n", strerror(status)); goto error; } log_info("Loopback test running. Press Ctrl-C to exit...\n"); - pthread_join(tx_thread, NULL); - pthread_join(rx_thread, NULL); + THREAD_JOIN(tx_thread, NULL); + THREAD_JOIN(rx_thread, NULL); log_info("IQ Matches: %zu / %zu (%.1f%%)\n", good_count, good_count + bad_count, diff --git a/host/libraries/libbladeRF_test/test_freq_hop/CMakeLists.txt b/host/libraries/libbladeRF_test/test_freq_hop/CMakeLists.txt index 666174fc..1e4c80d8 100644 --- a/host/libraries/libbladeRF_test/test_freq_hop/CMakeLists.txt +++ b/host/libraries/libbladeRF_test/test_freq_hop/CMakeLists.txt @@ -11,9 +11,6 @@ set(LIBS libbladerf_shared) if(MSVC) set(INCLUDES ${INCLUDES} ${MSVC_C99_INCLUDES}) - find_package(LibPThreadsWin32 REQUIRED) - set(INCLUDES ${INCLUDES} ${LIBPTHREADSWIN32_INCLUDE_DIRS}) - set(LIBS ${LIBS} ${LIBPTHREADSWIN32_LIBRARIES}) endif() set(SRC diff --git a/host/libraries/libbladeRF_test/test_fw_check/CMakeLists.txt b/host/libraries/libbladeRF_test/test_fw_check/CMakeLists.txt index caee11b4..7d4a9fe6 100644 --- a/host/libraries/libbladeRF_test/test_fw_check/CMakeLists.txt +++ b/host/libraries/libbladeRF_test/test_fw_check/CMakeLists.txt @@ -19,14 +19,10 @@ else() add_definitions(-DLIBBLADERF_SEARCH_PREFIX="${CMAKE_INSTALL_PREFIX}") endif() -if(MSVC) - find_package(LibPThreadsWin32 REQUIRED) - set(INCLUDES ${INCLUDES} ${LIBPTHREADSWIN32_INCLUDE_DIRS}) - set(LIBS ${LIBS} ${LIBPTHREADSWIN32_LIBRARIES}) -else(MSVC) +if(NOT MSVC) find_package(Threads REQUIRED) set(LIBS ${LIBS} ${CMAKE_THREAD_LIBS_INIT}) -endif(MSVC) +endif(NOT MSVC) set(SRC src/main.c diff --git a/host/libraries/libbladeRF_test/test_repeated_stream/CMakeLists.txt b/host/libraries/libbladeRF_test/test_repeated_stream/CMakeLists.txt index 0537f163..60435f7b 100644 --- a/host/libraries/libbladeRF_test/test_repeated_stream/CMakeLists.txt +++ b/host/libraries/libbladeRF_test/test_repeated_stream/CMakeLists.txt @@ -15,9 +15,7 @@ set(SRC set(LIBS libbladerf_shared) if(MSVC) - find_package(LibPThreadsWin32 REQUIRED) - set(INCLUDES ${INCLUDES} ${LIBPTHREADSWIN32_INCLUDE_DIRS} ${MSVC_C99_INCLUDES}) - set(LIBS ${LIBS} ${LIBPTHREADSWIN32_LIBRARIES}) + set(INCLUDES ${INCLUDES} ${MSVC_C99_INCLUDES}) set(SRC ${SRC} ${BLADERF_HOST_COMMON_SOURCE_DIR}/windows/getopt_long.c) else(MSVC) find_package(Threads REQUIRED) diff --git a/host/libraries/libbladeRF_test/test_repeated_stream/src/main.c b/host/libraries/libbladeRF_test/test_repeated_stream/src/main.c index 35e9797e..fa9c1779 100644 --- a/host/libraries/libbladeRF_test/test_repeated_stream/src/main.c +++ b/host/libraries/libbladeRF_test/test_repeated_stream/src/main.c @@ -32,12 +32,12 @@ #include #include #include -#include #include #include "devcfg.h" #include "conversions.h" #include "minmax.h" +#include "thread.h" #define OPTIONS DEVCFG_OPTIONS_BASE "r:S:" @@ -221,7 +221,7 @@ int main(int argc, char *argv[]) int status = EXIT_FAILURE; struct option *options = NULL; struct app_params params; - pthread_t rx_thread, tx_thread; + THREAD rx_thread, tx_thread; bool rx_launched = false; bool tx_launched = false; @@ -266,8 +266,8 @@ int main(int argc, char *argv[]) } if (params.rx) { - status = pthread_create(&rx_thread, NULL, rx_task, ¶ms); - if (status != 0) { + status = THREAD_CREATE(&rx_thread, rx_task, ¶ms); + if (status != THREAD_SUCCESS) { fprintf(stderr, "Failed to launch RX thread: %s\n", strerror(status)); params.rx = false; } else { @@ -276,8 +276,8 @@ int main(int argc, char *argv[]) } if (params.tx) { - status = pthread_create(&tx_thread, NULL, tx_task, ¶ms); - if (status != 0) { + status = THREAD_CREATE(&tx_thread, tx_task, ¶ms); + if (status != THREAD_SUCCESS) { fprintf(stderr, "Failed to launch TX thread: %s\n", strerror(status)); params.tx = false; } else { @@ -286,11 +286,11 @@ int main(int argc, char *argv[]) } if (rx_launched) { - pthread_join(rx_thread, NULL); + THREAD_JOIN(rx_thread, NULL); } if (tx_launched) { - pthread_join(tx_thread, NULL); + THREAD_JOIN(tx_thread, NULL); } out: diff --git a/host/libraries/libbladeRF_test/test_repeater/CMakeLists.txt b/host/libraries/libbladeRF_test/test_repeater/CMakeLists.txt index 68c5ce24..a2acef1e 100644 --- a/host/libraries/libbladeRF_test/test_repeater/CMakeLists.txt +++ b/host/libraries/libbladeRF_test/test_repeater/CMakeLists.txt @@ -1,11 +1,9 @@ cmake_minimum_required(VERSION 3.5) project(libbladeRF_test_repeater C) -if(MSVC) - find_package(LibPThreadsWin32 REQUIRED) -else(MSVC) +if(NOT MSVC) find_package(Threads REQUIRED) -endif(MSVC) +endif(NOT MSVC) set(TEST_REPEATER_SRC src/main.c @@ -28,7 +26,6 @@ set(TEST_REPEATER_INCLUDE if(MSVC) set(TEST_REPEATER_INCLUDE ${TEST_REPEATER_INCLUDE} ${BLADERF_HOST_COMMON_INCLUDE_DIRS}/windows - ${LIBPTHREADSWIN32_INCLUDE_DIRS} ${MSVC_C99_INCLUDES} ) endif() @@ -41,10 +38,5 @@ set(TEST_REPEATER_LIBS libbladerf_shared ${CMAKE_THREAD_LIBS_INIT}) - -if(LIBPTHREADSWIN32_FOUND) - set(TEST_REPEATER_LIBS ${TEST_REPEATER_LIBS} ${LIBPTHREADSWIN32_LIBRARIES}) -endif() - target_link_libraries(libbladeRF_test_repeater - ${TEST_REPEATER_LIBS}) + ${TEST_REPEATER_LIBS}) \ No newline at end of file diff --git a/host/libraries/libbladeRF_test/test_repeater/src/repeater.c b/host/libraries/libbladeRF_test/test_repeater/src/repeater.c index fcbfdcd0..011a0ead 100644 --- a/host/libraries/libbladeRF_test/test_repeater/src/repeater.c +++ b/host/libraries/libbladeRF_test/test_repeater/src/repeater.c @@ -64,10 +64,10 @@ #include #include #include -#include #include #include "rel_assert.h" +#include "thread.h" #include "repeater.h" #include "minmax.h" @@ -152,9 +152,9 @@ static char get_key() /* Thread-safe wrapper around fprintf(stderr, ...) */ #define print_error(repeater_, ...) do { \ - pthread_mutex_lock(&repeater_->stderr_lock); \ + MUTEX_LOCK(&repeater_->stderr_lock); \ fprintf(stderr, __VA_ARGS__); \ - pthread_mutex_unlock(&repeater_->stderr_lock); \ + MUTEX_UNLOCK(&repeater_->stderr_lock); \ } while (0) #define GAIN_TXVGA1_MIN -35 @@ -189,22 +189,22 @@ struct buf_mgmt size_t num_buffers; /* # of sample buffers */ /* Used to signal the TX thread when a few samplse have been buffered up */ - pthread_cond_t samples_available; + COND samples_available; - pthread_mutex_t lock; + MUTEX lock; }; struct repeater { struct bladerf *device; - pthread_t rx_task; + THREAD rx_task; struct bladerf_stream *rx_stream; - pthread_t tx_task; + THREAD tx_task; struct bladerf_stream *tx_stream; - pthread_mutex_t stderr_lock; + MUTEX stderr_lock; struct buf_mgmt buf_mgmt; @@ -247,7 +247,7 @@ static void *tx_stream_callback(struct bladerf *dev, void *ret; struct repeater *repeater = (struct repeater *)user_data; - pthread_mutex_lock(&repeater->buf_mgmt.lock); + MUTEX_LOCK(&repeater->buf_mgmt.lock); if (repeater->buf_mgmt.tx_idx < 0) { ret = NULL; @@ -271,7 +271,7 @@ static void *tx_stream_callback(struct bladerf *dev, } } - pthread_mutex_unlock(&repeater->buf_mgmt.lock); + MUTEX_UNLOCK(&repeater->buf_mgmt.lock); return ret; } @@ -284,21 +284,21 @@ void * tx_task_run(void *repeater_) /* Wait for RX task to buffer up some samples before we begin * consuming them */ - pthread_mutex_lock(&repeater->buf_mgmt.lock); + MUTEX_LOCK(&repeater->buf_mgmt.lock); while(repeater->buf_mgmt.num_filled < repeater->buf_mgmt.prefill_count && repeater->buf_mgmt.tx_idx >= 0) { - status = pthread_cond_wait(&repeater->buf_mgmt.samples_available, + status = COND_WAIT(&repeater->buf_mgmt.samples_available, &repeater->buf_mgmt.lock); - if (status != 0) { + if (status != THREAD_SUCCESS) { print_error(repeater, "TX startup wait failed (%d)\n", status); exit_early = true; } } exit_early |= repeater->buf_mgmt.tx_idx < 0; - pthread_mutex_unlock(&repeater->buf_mgmt.lock); + MUTEX_UNLOCK(&repeater->buf_mgmt.lock); if (!exit_early) { /* Call stream */ @@ -325,7 +325,7 @@ static void *rx_stream_callback(struct bladerf *dev, struct repeater *repeater = (struct repeater *)user_data; void *ret; - pthread_mutex_lock(&repeater->buf_mgmt.lock); + MUTEX_LOCK(&repeater->buf_mgmt.lock); if (repeater->buf_mgmt.rx_idx < 0) { ret = NULL; @@ -346,10 +346,10 @@ static void *rx_stream_callback(struct bladerf *dev, } else { repeater->buf_mgmt.num_filled++; } - pthread_cond_signal(&repeater->buf_mgmt.samples_available); + COND_SIGNAL(&repeater->buf_mgmt.samples_available); } - pthread_mutex_unlock(&repeater->buf_mgmt.lock); + MUTEX_UNLOCK(&repeater->buf_mgmt.lock); return ret; } @@ -372,9 +372,9 @@ static inline void repeater_init(struct repeater *repeater, { memset(repeater, 0, sizeof(*repeater)); - pthread_mutex_init(&repeater->stderr_lock, NULL); - pthread_mutex_init(&repeater->buf_mgmt.lock, NULL); - pthread_cond_init(&repeater->buf_mgmt.samples_available, NULL); + MUTEX_INIT(&repeater->stderr_lock); + MUTEX_INIT(&repeater->buf_mgmt.lock); + COND_INIT(&repeater->buf_mgmt.samples_available); repeater->buf_mgmt.num_filled = 0; repeater->buf_mgmt.num_buffers = config->num_buffers; @@ -607,15 +607,15 @@ static int start_tasks(struct repeater *repeater) { int status; - status = pthread_create(&repeater->rx_task, NULL, rx_task_run, repeater); - if (status < 0) { + status = THREAD_CREATE(&repeater->rx_task, rx_task_run, repeater); + if (status != THREAD_SUCCESS) { return -1; } - status = pthread_create(&repeater->tx_task, NULL, tx_task_run, repeater); - if (status < 0) { - pthread_cancel(repeater->rx_task); - pthread_join(repeater->rx_task, NULL); + status = THREAD_CREATE(&repeater->tx_task, tx_task_run, repeater); + if (status != THREAD_SUCCESS) { + THREAD_CANCEL(repeater->rx_task); + THREAD_JOIN(repeater->rx_task, NULL); return -1; } @@ -625,18 +625,18 @@ static int start_tasks(struct repeater *repeater) static void stop_tasks(struct repeater *repeater) { print_error(repeater, "Stoppping RX and tasks...\r\n"); - pthread_mutex_lock(&repeater->buf_mgmt.lock); + MUTEX_LOCK(&repeater->buf_mgmt.lock); repeater->buf_mgmt.tx_idx = -1; repeater->buf_mgmt.rx_idx = -1; - pthread_mutex_unlock(&repeater->buf_mgmt.lock); - pthread_join(repeater->rx_task, NULL); + MUTEX_UNLOCK(&repeater->buf_mgmt.lock); + THREAD_JOIN(repeater->rx_task, NULL); /* Fire off the "samples available" signal to the TX thread, in case * it is still awaiting for the prefill completion */ - pthread_mutex_lock(&repeater->buf_mgmt.lock); - pthread_cond_signal(&repeater->buf_mgmt.samples_available); - pthread_mutex_unlock(&repeater->buf_mgmt.lock); - pthread_join(repeater->tx_task, NULL); + MUTEX_LOCK(&repeater->buf_mgmt.lock); + COND_SIGNAL(&repeater->buf_mgmt.samples_available); + MUTEX_UNLOCK(&repeater->buf_mgmt.lock); + THREAD_JOIN(repeater->tx_task, NULL); } diff --git a/host/libraries/libbladeRF_test/test_rx_discont/CMakeLists.txt b/host/libraries/libbladeRF_test/test_rx_discont/CMakeLists.txt index ac28ca0a..d54e511b 100644 --- a/host/libraries/libbladeRF_test/test_rx_discont/CMakeLists.txt +++ b/host/libraries/libbladeRF_test/test_rx_discont/CMakeLists.txt @@ -12,14 +12,10 @@ endif() set(LIBS libbladerf_shared) -if(MSVC) - find_package(LibPThreadsWin32 REQUIRED) - set(INCLUDES ${INCLUDES} ${LIBPTHREADSWIN32_INCLUDE_DIRS}) - set(LIBS ${LIBS} ${LIBPTHREADSWIN32_LIBRARIES}) -else(MSVC) +if(NOT MSVC) find_package(Threads REQUIRED) set(LIBS ${LIBS} ${CMAKE_THREAD_LIBS_INIT}) -endif(MSVC) +endif(NOT MSVC) set(SRC src/main.c diff --git a/host/libraries/libbladeRF_test/test_scheduled_retune/CMakeLists.txt b/host/libraries/libbladeRF_test/test_scheduled_retune/CMakeLists.txt index 9788a971..0e3b3bea 100644 --- a/host/libraries/libbladeRF_test/test_scheduled_retune/CMakeLists.txt +++ b/host/libraries/libbladeRF_test/test_scheduled_retune/CMakeLists.txt @@ -14,14 +14,10 @@ endif() set(LIBS libbladerf_shared) -if(MSVC) - find_package(LibPThreadsWin32 REQUIRED) - set(INCLUDES ${INCLUDES} ${LIBPTHREADSWIN32_INCLUDE_DIRS}) - set(LIBS ${LIBS} ${LIBPTHREADSWIN32_LIBRARIES}) -else(MSVC) +if(NOT MSVC) find_package(Threads REQUIRED) set(LIBS ${LIBS} ${CMAKE_THREAD_LIBS_INIT}) -endif(MSVC) +endif(NOT MSVC) set(SRC src/main.c diff --git a/host/libraries/libbladeRF_test/test_sync/CMakeLists.txt b/host/libraries/libbladeRF_test/test_sync/CMakeLists.txt index fd3e31f9..a32603d6 100644 --- a/host/libraries/libbladeRF_test/test_sync/CMakeLists.txt +++ b/host/libraries/libbladeRF_test/test_sync/CMakeLists.txt @@ -12,14 +12,10 @@ endif() set(TEST_SYNC_LIBS libbladerf_shared) -if(MSVC) - find_package(LibPThreadsWin32 REQUIRED) - set(TEST_SYNC_INCLUDES ${TEST_SYNC_INCLUDES} ${LIBPTHREADSWIN32_INCLUDE_DIRS}) - set(TEST_SYNC_LIBS ${TEST_SYNC_LIBS} ${LIBPTHREADSWIN32_LIBRARIES}) -else(MSVC) +if(NOT MSVC) find_package(Threads REQUIRED) set(TEST_SYNC_LIBS ${TEST_SYNC_LIBS} ${CMAKE_THREAD_LIBS_INIT}) -endif(MSVC) +endif(NOT MSVC) add_definitions(-DLOGGING_ENABLED=1) diff --git a/host/libraries/libbladeRF_test/test_sync/src/test.c b/host/libraries/libbladeRF_test/test_sync/src/test.c index b9231ca2..340dab5e 100644 --- a/host/libraries/libbladeRF_test/test_sync/src/test.c +++ b/host/libraries/libbladeRF_test/test_sync/src/test.c @@ -27,7 +27,7 @@ #include #include #include -#include +#include "thread.h" #include #include @@ -40,7 +40,7 @@ static struct task_args { struct bladerf *dev; struct test_params *p; - pthread_t thread; + THREAD thread; int status; bool quit; } rx_args, tx_args; @@ -365,7 +365,7 @@ int test_run(struct test_params *p) tx_args.quit = false; log_debug("Starting TX task\n"); - if (pthread_create(&tx_args.thread, NULL, tx_task, &tx_args) != 0) { + if (THREAD_CREATE(&tx_args.thread, tx_task, &tx_args) != THREAD_SUCCESS) { fclose(p->in_file); p->in_file = NULL; } @@ -378,7 +378,7 @@ int test_run(struct test_params *p) rx_args.quit = false; log_debug("Starting RX task\n"); - if (pthread_create(&rx_args.thread, NULL, rx_task, &rx_args) != 0) { + if (THREAD_CREATE(&rx_args.thread, rx_task, &rx_args) != THREAD_SUCCESS) { fclose(p->out_file); p->out_file = NULL; } @@ -387,12 +387,12 @@ int test_run(struct test_params *p) log_debug("Running...\n"); if (p->in_file != NULL) { - pthread_join(tx_args.thread, NULL); + THREAD_JOIN(tx_args.thread, NULL); log_debug("Joined TX task\n"); } if (p->out_file != NULL) { - pthread_join(rx_args.thread, NULL); + THREAD_JOIN(rx_args.thread, NULL); log_debug("Joined RX task\n"); } diff --git a/host/libraries/libbladeRF_test/test_timestamps/CMakeLists.txt b/host/libraries/libbladeRF_test/test_timestamps/CMakeLists.txt index f1561704..c098391b 100644 --- a/host/libraries/libbladeRF_test/test_timestamps/CMakeLists.txt +++ b/host/libraries/libbladeRF_test/test_timestamps/CMakeLists.txt @@ -14,14 +14,10 @@ endif() set(LIBS libbladerf_shared) -if(MSVC) - find_package(LibPThreadsWin32 REQUIRED) - set(INCLUDES ${INCLUDES} ${LIBPTHREADSWIN32_INCLUDE_DIRS}) - set(LIBS ${LIBS} ${LIBPTHREADSWIN32_LIBRARIES}) -else() +if(NOT MSVC) find_package(Threads REQUIRED) set(LIBS ${LIBS} ${CMAKE_THREAD_LIBS_INIT}) -endif(MSVC) +endif(NOT MSVC) set(SRC diff --git a/host/libraries/libbladeRF_test/test_timestamps/src/loopback.c b/host/libraries/libbladeRF_test/test_timestamps/src/loopback.c index dd439bd3..9e4ec78a 100644 --- a/host/libraries/libbladeRF_test/test_timestamps/src/loopback.c +++ b/host/libraries/libbladeRF_test/test_timestamps/src/loopback.c @@ -284,10 +284,10 @@ void *loopback_burst_rx_task (void *args) burst_num, bladerf_strerror(status)); } else if (meta.status & BLADERF_META_STATUS_OVERRUN) { fprintf(stderr, "Error: RX overrun detected.\n"); - pthread_mutex_lock(&t->lock); + MUTEX_LOCK(&t->lock); t->stop = true; t->rx_ready = true; - pthread_mutex_unlock(&t->lock); + MUTEX_UNLOCK(&t->lock); } else { /* printf("Read %-8u samples @ 0x%08"PRIx64" (%-8"PRIu64")\n", @@ -336,9 +336,9 @@ void *loopback_burst_rx_task (void *args) * rid ourselves of any junk in the RX FIFOs and are * ready to start the test */ next_state = WAIT_FOR_BURST_START; - pthread_mutex_lock(&t->lock); + MUTEX_LOCK(&t->lock); t->rx_ready = true; - pthread_mutex_unlock(&t->lock); + MUTEX_UNLOCK(&t->lock); } } break; @@ -443,9 +443,9 @@ void *loopback_burst_rx_task (void *args) break; } - pthread_mutex_lock(&t->lock); + MUTEX_LOCK(&t->lock); stop = t->stop; - pthread_mutex_unlock(&t->lock); + MUTEX_UNLOCK(&t->lock); } if (status < 0) { @@ -459,9 +459,9 @@ void *loopback_burst_rx_task (void *args) #endif /* Ensure the TX side is signalled to stop, if it isn't already */ - pthread_mutex_lock(&t->lock); + MUTEX_LOCK(&t->lock); t->stop = true; - pthread_mutex_unlock(&t->lock); + MUTEX_UNLOCK(&t->lock); return NULL; } @@ -514,10 +514,10 @@ void *loopback_burst_rx_task_rf (void *args) burst_num, bladerf_strerror(status)); } else if (meta.status & BLADERF_META_STATUS_OVERRUN) { fprintf(stderr, "Error: RX overrun detected.\n"); - pthread_mutex_lock(&t->lock); + MUTEX_LOCK(&t->lock); t->stop = true; t->rx_ready = true; - pthread_mutex_unlock(&t->lock); + MUTEX_UNLOCK(&t->lock); } else { /* printf("Read %-8u samples @ 0x%08"PRIx64" (%-8"PRIu64")\n", @@ -565,9 +565,9 @@ void *loopback_burst_rx_task_rf (void *args) * rid ourselves of any junk in the RX FIFOs and are * ready to start the test */ next_state = WAIT_FOR_BURST_START; - pthread_mutex_lock(&t->lock); + MUTEX_LOCK(&t->lock); t->rx_ready = true; - pthread_mutex_unlock(&t->lock); + MUTEX_UNLOCK(&t->lock); } } break; @@ -672,9 +672,9 @@ void *loopback_burst_rx_task_rf (void *args) break; } - pthread_mutex_lock(&t->lock); + MUTEX_LOCK(&t->lock); stop = t->stop; - pthread_mutex_unlock(&t->lock); + MUTEX_UNLOCK(&t->lock); } if (status < 0) { @@ -688,9 +688,9 @@ void *loopback_burst_rx_task_rf (void *args) #endif /* Ensure the TX side is signalled to stop, if it isn't already */ - pthread_mutex_lock(&t->lock); + MUTEX_LOCK(&t->lock); t->stop = true; - pthread_mutex_unlock(&t->lock); + MUTEX_UNLOCK(&t->lock); return NULL; } diff --git a/host/libraries/libbladeRF_test/test_timestamps/src/loopback.h b/host/libraries/libbladeRF_test/test_timestamps/src/loopback.h index e37d488a..2d850bcc 100644 --- a/host/libraries/libbladeRF_test/test_timestamps/src/loopback.h +++ b/host/libraries/libbladeRF_test/test_timestamps/src/loopback.h @@ -28,8 +28,8 @@ #include #include #include -#include #include +#include "thread.h" #define LOOPBACK_TX_MAGNITUDE 2000 #define LOOPBACK_RX_POWER_THRESH (256 * 256) @@ -40,7 +40,7 @@ struct loopback_burst_test { struct loopback_burst *bursts; unsigned int num_bursts; - pthread_mutex_t lock; + MUTEX lock; bool stop; bool rx_ready; bladerf_frequency frequency; diff --git a/host/libraries/libbladeRF_test/test_timestamps/src/test_format_mismatch.c b/host/libraries/libbladeRF_test/test_timestamps/src/test_format_mismatch.c index f51fb1c0..08c501fd 100644 --- a/host/libraries/libbladeRF_test/test_timestamps/src/test_format_mismatch.c +++ b/host/libraries/libbladeRF_test/test_timestamps/src/test_format_mismatch.c @@ -32,7 +32,7 @@ #include #include #include -#include +#include "thread.h" #include #include "test_timestamps.h" @@ -77,8 +77,8 @@ static struct async_task size_t curr_buf_idx; size_t n_buffers; - pthread_t thread; - pthread_mutex_t lock; + THREAD thread; + MUTEX lock; } rx_task, tx_task; void *callback(struct bladerf *dev, struct bladerf_stream *stream, @@ -90,7 +90,7 @@ void *callback(struct bladerf *dev, struct bladerf_stream *stream, void *ret; - pthread_mutex_lock(&t->lock); + MUTEX_LOCK(&t->lock); t->curr_buf_idx = (t->curr_buf_idx + 1) % t->n_buffers; ret = t->buffers[t->curr_buf_idx]; @@ -112,7 +112,7 @@ void *callback(struct bladerf *dev, struct bladerf_stream *stream, ret = BLADERF_STREAM_SHUTDOWN; } - pthread_mutex_unlock(&t->lock); + MUTEX_UNLOCK(&t->lock); return ret; } @@ -146,10 +146,10 @@ static void *stream_task(void *arg) bladerf_deinit_stream(stream); out: - pthread_mutex_lock(&t->lock); + MUTEX_LOCK(&t->lock); t->state = TASK_STOPPED; t->exit_status = status; - pthread_mutex_unlock(&t->lock); + MUTEX_UNLOCK(&t->lock); return NULL; } @@ -217,8 +217,8 @@ static int init_if(struct bladerf *dev, struct app_params *p, bladerf_module m, t->fmt = fmt; t->state = state = TASK_UNINITIALIZED; - status = pthread_create(&t->thread, NULL, stream_task, t); - if (status != 0) { + status = THREAD_CREATE(&t->thread, stream_task, t); + if (status != THREAD_SUCCESS) { fprintf(stderr, "Failed to launch %s task\n", m == BLADERF_MODULE_RX ? "RX" : "TX"); return status; @@ -226,15 +226,15 @@ static int init_if(struct bladerf *dev, struct app_params *p, bladerf_module m, do { usleep(25000); - pthread_mutex_lock(&t->lock); + MUTEX_LOCK(&t->lock); state = t->state; - pthread_mutex_unlock(&t->lock); + MUTEX_UNLOCK(&t->lock); } while (state == TASK_UNINITIALIZED); /* This may have already stopped if an invalid configuration was * detected. */ if (state == TASK_STOPPED) { - pthread_join(t->thread, NULL); + THREAD_JOIN(t->thread, NULL); if (t->exit_status != exp_status) { fprintf(stderr, "Uexpected status when configuring async %s: %s\n", @@ -274,23 +274,23 @@ static int deinit_if(struct bladerf *dev, bladerf_module m, bool sync, return -1; } - pthread_mutex_lock(&t->lock); + MUTEX_LOCK(&t->lock); state = t->state; - pthread_mutex_unlock(&t->lock); + MUTEX_UNLOCK(&t->lock); while (state != TASK_STOPPED) { - pthread_mutex_lock(&t->lock); + MUTEX_LOCK(&t->lock); t->state = TASK_SHUTDOWN_REQUESTED; - pthread_mutex_unlock(&t->lock); + MUTEX_UNLOCK(&t->lock); usleep(25000); - pthread_mutex_lock(&t->lock); + MUTEX_LOCK(&t->lock); state = t->state; - pthread_mutex_unlock(&t->lock); + MUTEX_UNLOCK(&t->lock); } - pthread_join(t->thread, NULL); + THREAD_JOIN(t->thread, NULL); if (t->exit_status != exp_status) { fprintf(stderr, @@ -331,7 +331,7 @@ static void init_task_info(struct bladerf *dev, struct app_params *p, t->n_buffers = 16; t->curr_buf_idx = 0; - pthread_mutex_init(&t->lock, NULL); + MUTEX_INIT(&t->lock); } int test_fn_format_mismatch(struct bladerf *dev, struct app_params *p) diff --git a/host/libraries/libbladeRF_test/test_timestamps/src/test_loopback_onoff.c b/host/libraries/libbladeRF_test/test_timestamps/src/test_loopback_onoff.c index 7a36879a..410c2849 100644 --- a/host/libraries/libbladeRF_test/test_timestamps/src/test_loopback_onoff.c +++ b/host/libraries/libbladeRF_test/test_timestamps/src/test_loopback_onoff.c @@ -33,7 +33,6 @@ #include #include #include -#include #include #include "test_timestamps.h" #include "loopback.h" @@ -88,9 +87,9 @@ static void * tx_task(void *args) i + 1, samples_left, bladerf_strerror(status)); /* Stop the RX worker */ - pthread_mutex_lock(&t->lock); + MUTEX_LOCK(&t->lock); t->stop = true; - pthread_mutex_unlock(&t->lock); + MUTEX_UNLOCK(&t->lock); } meta.flags &= ~BLADERF_META_FLAG_TX_BURST_START; @@ -109,15 +108,15 @@ static void * tx_task(void *args) bladerf_strerror(status)); /* Stop the RX worker */ - pthread_mutex_lock(&t->lock); + MUTEX_LOCK(&t->lock); t->stop = true; - pthread_mutex_unlock(&t->lock); + MUTEX_UNLOCK(&t->lock); } } - pthread_mutex_lock(&t->lock); + MUTEX_LOCK(&t->lock); stop = t->stop; - pthread_mutex_unlock(&t->lock); + MUTEX_UNLOCK(&t->lock); } /* Wait for samples to finish */ @@ -195,10 +194,10 @@ int test_fn_loopback_onoff(struct bladerf *dev, struct app_params *p) { int status = 0; struct loopback_burst_test test; - pthread_t tx_thread; + THREAD tx_thread; bool tx_started = false; - pthread_t rx_thread; + THREAD rx_thread; bool rx_started = false; bool rx_ready = false; @@ -208,7 +207,7 @@ int test_fn_loopback_onoff(struct bladerf *dev, struct app_params *p) test.stop = false; test.rx_ready = false; - pthread_mutex_init(&test.lock, NULL); + MUTEX_INIT(&test.lock); test.bursts = (struct loopback_burst *) malloc(test.num_bursts * sizeof(test.bursts[0])); if (test.bursts == NULL) { @@ -225,8 +224,8 @@ int test_fn_loopback_onoff(struct bladerf *dev, struct app_params *p) printf("Starting bursts...\n"); - status = pthread_create(&rx_thread, NULL, loopback_burst_rx_task, &test); - if (status != 0) { + status = THREAD_CREATE(&rx_thread, loopback_burst_rx_task, &test); + if (status != THREAD_SUCCESS) { fprintf(stderr, "Failed to start RX thread: %s\n", strerror(status)); goto out; } else { @@ -235,13 +234,13 @@ int test_fn_loopback_onoff(struct bladerf *dev, struct app_params *p) while (!rx_ready) { usleep(10000); - pthread_mutex_lock(&test.lock); + MUTEX_LOCK(&test.lock); rx_ready = test.rx_ready; - pthread_mutex_unlock(&test.lock); + MUTEX_UNLOCK(&test.lock); } - status = pthread_create(&tx_thread, NULL, tx_task, &test); - if (status != 0) { + status = THREAD_CREATE(&tx_thread, tx_task, &test); + if (status != THREAD_SUCCESS) { fprintf(stderr, "Failed to start TX thread: %s\n", strerror(status)); goto out; } else { @@ -250,11 +249,11 @@ int test_fn_loopback_onoff(struct bladerf *dev, struct app_params *p) out: if (tx_started) { - pthread_join(tx_thread, NULL); + THREAD_JOIN(tx_thread, NULL); } if (rx_started) { - pthread_join(rx_thread, NULL); + THREAD_JOIN(rx_thread, NULL); } free(test.bursts); diff --git a/host/libraries/libbladeRF_test/test_timestamps/src/test_loopback_onoff_rf.c b/host/libraries/libbladeRF_test/test_timestamps/src/test_loopback_onoff_rf.c index e0ef5fbb..1b98cce5 100644 --- a/host/libraries/libbladeRF_test/test_timestamps/src/test_loopback_onoff_rf.c +++ b/host/libraries/libbladeRF_test/test_timestamps/src/test_loopback_onoff_rf.c @@ -33,7 +33,7 @@ #include #include #include -#include +#include "thread.h" #include #include "test_timestamps.h" #include "loopback.h" @@ -88,9 +88,9 @@ static void * tx_task(void *args) i + 1, samples_left, bladerf_strerror(status)); /* Stop the RX worker */ - pthread_mutex_lock(&t->lock); + MUTEX_LOCK(&t->lock); t->stop = true; - pthread_mutex_unlock(&t->lock); + MUTEX_UNLOCK(&t->lock); } meta.flags &= ~BLADERF_META_FLAG_TX_BURST_START; @@ -109,15 +109,15 @@ static void * tx_task(void *args) bladerf_strerror(status)); /* Stop the RX worker */ - pthread_mutex_lock(&t->lock); + MUTEX_LOCK(&t->lock); t->stop = true; - pthread_mutex_unlock(&t->lock); + MUTEX_UNLOCK(&t->lock); } } - pthread_mutex_lock(&t->lock); + MUTEX_LOCK(&t->lock); stop = t->stop; - pthread_mutex_unlock(&t->lock); + MUTEX_UNLOCK(&t->lock); } /* Wait for samples to finish */ @@ -199,10 +199,10 @@ int test_fn_loopback_onoff_rf(struct bladerf *dev, struct app_params *p) return 0; int status = 0; struct loopback_burst_test test; - pthread_t tx_thread; + THREAD tx_thread; bool tx_started = false; - pthread_t rx_thread; + THREAD rx_thread; bool rx_started = false; bool rx_ready = false; @@ -213,7 +213,7 @@ int test_fn_loopback_onoff_rf(struct bladerf *dev, struct app_params *p) test.rx_ready = false; test.frequency = 100e6; - pthread_mutex_init(&test.lock, NULL); + MUTEX_INIT(&test.lock); fprintf(stderr, "[WARNING] loopback_onoff_rf under construction\n"); fprintf(stderr, "[WARNING] Failure expected\n"); @@ -234,8 +234,8 @@ int test_fn_loopback_onoff_rf(struct bladerf *dev, struct app_params *p) printf("Starting bursts...\n"); - status = pthread_create(&rx_thread, NULL, loopback_burst_rx_task_rf, &test); - if (status != 0) { + status = THREAD_CREATE(&rx_thread, loopback_burst_rx_task_rf, &test); + if (status != THREAD_SUCCESS) { fprintf(stderr, "Failed to start RX thread: %s\n", strerror(status)); goto out; } else { @@ -244,13 +244,13 @@ int test_fn_loopback_onoff_rf(struct bladerf *dev, struct app_params *p) while (!rx_ready) { usleep(10000); - pthread_mutex_lock(&test.lock); + MUTEX_LOCK(&test.lock); rx_ready = test.rx_ready; - pthread_mutex_unlock(&test.lock); + MUTEX_UNLOCK(&test.lock); } - status = pthread_create(&tx_thread, NULL, tx_task, &test); - if (status != 0) { + status = THREAD_CREATE(&tx_thread, tx_task, &test); + if (status != THREAD_SUCCESS) { fprintf(stderr, "Failed to start TX thread: %s\n", strerror(status)); goto out; } else { @@ -259,11 +259,11 @@ int test_fn_loopback_onoff_rf(struct bladerf *dev, struct app_params *p) out: if (tx_started) { - pthread_join(tx_thread, NULL); + THREAD_JOIN(tx_thread, NULL); } if (rx_started) { - pthread_join(rx_thread, NULL); + THREAD_JOIN(rx_thread, NULL); } free(test.bursts); diff --git a/host/libraries/libbladeRF_test/test_timestamps/src/test_loopback_onoff_zp.c b/host/libraries/libbladeRF_test/test_timestamps/src/test_loopback_onoff_zp.c index 33e84f68..29059767 100644 --- a/host/libraries/libbladeRF_test/test_timestamps/src/test_loopback_onoff_zp.c +++ b/host/libraries/libbladeRF_test/test_timestamps/src/test_loopback_onoff_zp.c @@ -35,7 +35,7 @@ #include #include #include -#include +#include "thread.h" #include #include #include "test_timestamps.h" @@ -284,9 +284,9 @@ static void * tx_task(void *args) status = tx_samples(t, samples, &meta, t->bursts[i].duration, i); if (status != 0) { - pthread_mutex_lock(&t->lock); + MUTEX_LOCK(&t->lock); stop = t->stop = true; - pthread_mutex_unlock(&t->lock); + MUTEX_UNLOCK(&t->lock); } else { meta.flags = BLADERF_META_FLAG_TX_UPDATE_TIMESTAMP; @@ -296,9 +296,9 @@ static void * tx_task(void *args) } - pthread_mutex_lock(&t->lock); + MUTEX_LOCK(&t->lock); stop = t->stop; - pthread_mutex_unlock(&t->lock); + MUTEX_UNLOCK(&t->lock); } } @@ -341,10 +341,10 @@ int test_fn_loopback_onoff_zp(struct bladerf *dev, struct app_params *p) { int status = 0; struct loopback_burst_test test; - pthread_t tx_thread; + THREAD tx_thread; bool tx_started = false; - pthread_t rx_thread; + THREAD rx_thread; bool rx_started = false; bool rx_ready = false; @@ -354,7 +354,7 @@ int test_fn_loopback_onoff_zp(struct bladerf *dev, struct app_params *p) test.stop = false; test.rx_ready = false; - pthread_mutex_init(&test.lock, NULL); + MUTEX_INIT(&test.lock); test.bursts = (struct loopback_burst *) malloc(test.num_bursts * sizeof(test.bursts[0])); if (test.bursts == NULL) { @@ -371,8 +371,8 @@ int test_fn_loopback_onoff_zp(struct bladerf *dev, struct app_params *p) printf("Starting bursts...\n"); - status = pthread_create(&rx_thread, NULL, loopback_burst_rx_task, &test); - if (status != 0) { + status = THREAD_CREATE(&rx_thread, loopback_burst_rx_task, &test); + if (status != THREAD_SUCCESS) { fprintf(stderr, "Failed to start RX thread: %s\n", strerror(status)); goto out; } else { @@ -381,13 +381,13 @@ int test_fn_loopback_onoff_zp(struct bladerf *dev, struct app_params *p) while (!rx_ready) { usleep(10000); - pthread_mutex_lock(&test.lock); + MUTEX_LOCK(&test.lock); rx_ready = test.rx_ready; - pthread_mutex_unlock(&test.lock); + MUTEX_UNLOCK(&test.lock); } - status = pthread_create(&tx_thread, NULL, tx_task, &test); - if (status != 0) { + status = THREAD_CREATE(&tx_thread, tx_task, &test); + if (status != THREAD_SUCCESS) { fprintf(stderr, "Failed to start TX thread: %s\n", strerror(status)); goto out; } else { @@ -396,11 +396,11 @@ int test_fn_loopback_onoff_zp(struct bladerf *dev, struct app_params *p) out: if (tx_started) { - pthread_join(tx_thread, NULL); + THREAD_JOIN(tx_thread, NULL); } if (rx_started) { - pthread_join(rx_thread, NULL); + THREAD_JOIN(rx_thread, NULL); } free(test.bursts); diff --git a/host/libraries/libbladeRF_test/test_timestamps/src/test_txrx_hwloop.c b/host/libraries/libbladeRF_test/test_timestamps/src/test_txrx_hwloop.c index 560681d4..f37b9116 100644 --- a/host/libraries/libbladeRF_test/test_timestamps/src/test_txrx_hwloop.c +++ b/host/libraries/libbladeRF_test/test_timestamps/src/test_txrx_hwloop.c @@ -31,11 +31,11 @@ #include #include #include -#include #include "conversions.h" #include "test_timestamps.h" #include "test_txrx_hwloop.h" #include "minmax.h" +#include "thread.h" #ifdef _WIN32 #include "gettimeofday.h" @@ -97,8 +97,8 @@ int main(int argc, char *argv[]) { bladerf_log_level log_level; /** Threading */ - pthread_t thread_rx; - pthread_t thread_loopcompare; + THREAD thread_rx; + THREAD thread_loopcompare; thread_args args_loopbackcompare; thread_args args_rx; @@ -231,14 +231,14 @@ int main(int argc, char *argv[]) { args_rx.is_rx_device = true; args_rx.dev = dev_rx; args_rx.tc = &test; - pthread_create(&thread_rx, NULL, rx_task, &args_rx); + THREAD_CREATE(&thread_rx, rx_task, &args_rx); } if (test.compare == true) { args_loopbackcompare.is_rx_device = false; args_loopbackcompare.dev = dev_tx; args_loopbackcompare.tc = &test; - pthread_create(&thread_loopcompare, NULL, rx_task, &args_loopbackcompare); + THREAD_CREATE(&thread_loopcompare, rx_task, &args_loopbackcompare); } for (i = 0; i < test.iterations && status == 0; i++) { @@ -287,12 +287,12 @@ int main(int argc, char *argv[]) { printf("TX finished in %.4f seconds\n", time_passed); out: if (!test.just_tx) { - pthread_join(thread_rx, NULL); + THREAD_JOIN(thread_rx, NULL); bladerf_close(dev_rx); } if (test.compare) - pthread_join(thread_loopcompare, NULL); + THREAD_JOIN(thread_loopcompare, NULL); status_out = bladerf_enable_module(dev_tx, BLADERF_MODULE_TX, false); if (status_out != 0) { diff --git a/host/libraries/libbladeRF_test/test_timestamps/src/test_txrx_hwloop.h b/host/libraries/libbladeRF_test/test_timestamps/src/test_txrx_hwloop.h index c58039eb..f30de91f 100644 --- a/host/libraries/libbladeRF_test/test_timestamps/src/test_txrx_hwloop.h +++ b/host/libraries/libbladeRF_test/test_timestamps/src/test_txrx_hwloop.h @@ -24,6 +24,7 @@ */ #include #include +#include "thread.h" #include "log.h" struct test_case { @@ -326,6 +327,6 @@ void *rx_task(void *args) { cleanup: free(samples); free(file_name); - pthread_exit(NULL); + THREAD_EXIT(NULL); fclose(file); } diff --git a/host/misc/windows/scripts/build-bladeRF.bat b/host/misc/windows/scripts/build-bladeRF.bat index 31a6769f..7ad1410f 100644 --- a/host/misc/windows/scripts/build-bladeRF.bat +++ b/host/misc/windows/scripts/build-bladeRF.bat @@ -117,7 +117,7 @@ mkdir %BUILDDIR64% pushd %BUILDDIR64% echo ====================== Configuring 64-bit build ====================== cmake -G %CMAKE_GENERATOR_64% -DCMAKE_BUILD_TYPE=%BUILD_TYPE% ^ - -DLIBUSB_PATH=%LIBUSB% -DLIBPTHREADSWIN32_PATH=%PTHREADS% ^ + -DLIBUSB_PATH=%LIBUSB% ^ ..\bladeRF\host ^ || goto EXIT_FAILURE @@ -138,7 +138,7 @@ echo[ echo[ echo ====================== Configuring 32-bit build ====================== cmake -G %CMAKE_GENERATOR_32% -DCMAKE_BUILD_TYPE=%BUILD_TYPE% ^ - -DLIBUSB_PATH=%LIBUSB% -DLIBPTHREADSWIN32_PATH=%PTHREADS% ^ + -DLIBUSB_PATH=%LIBUSB% ^ ..\bladeRF\host ^ || goto EXIT_FAILURE diff --git a/host/utilities/CMakeLists.txt b/host/utilities/CMakeLists.txt index 4900c231..a5e2cd21 100644 --- a/host/utilities/CMakeLists.txt +++ b/host/utilities/CMakeLists.txt @@ -1,4 +1,7 @@ cmake_minimum_required(VERSION 3.5) add_subdirectory(bladeRF-cli) -add_subdirectory(bladeRF-fsk/c) + +if(NOT MSVC) + add_subdirectory(bladeRF-fsk/c) +endif() \ No newline at end of file diff --git a/host/utilities/bladeRF-cli/CMakeLists.txt b/host/utilities/bladeRF-cli/CMakeLists.txt index a08d058a..ea6dc2b1 100644 --- a/host/utilities/bladeRF-cli/CMakeLists.txt +++ b/host/utilities/bladeRF-cli/CMakeLists.txt @@ -28,11 +28,9 @@ configure_file( find_package(LibTecla) find_package(LibEdit) -if(MSVC) - find_package(LibPThreadsWin32 REQUIRED) -else(MSVC) +if(NOT MSVC) find_package(Threads REQUIRED) -endif(MSVC) +endif(NOT MSVC) find_program(PANDOC_EXECUTABLE pandoc) find_program(BASH_EXECUTABLE bash) @@ -169,7 +167,6 @@ endif() if(MSVC) set(CLI_INCLUDE_DIRS ${CLI_INCLUDE_DIRS} ${BLADERF_HOST_COMMON_INCLUDE_DIRS}/windows - ${LIBPTHREADSWIN32_INCLUDE_DIRS} ) endif() @@ -266,7 +263,7 @@ set(CLI_LINK_LIBRARIES if(NOT MSVC) set(CLI_LINK_LIBRARIES ${CLI_LINK_LIBRARIES} m) -endif() +endif(NOT MSVC) if(ENABLE_LIBTECLA) set(CLI_LINK_LIBRARIES ${CLI_LINK_LIBRARIES} ${LIBTECLA_LIBRARIES}) @@ -280,10 +277,6 @@ elseif(ENABLE_LIBEDIT) set(CLI_LINK_LIBRARIES ${CLI_LINK_LIBRARIES} ${LIBEDIT_LIBRARIES}) endif(ENABLE_LIBTECLA) -if(LIBPTHREADSWIN32_FOUND) - set(CLI_LINK_LIBRARIES ${CLI_LINK_LIBRARIES} ${LIBPTHREADSWIN32_LIBRARIES}) -endif() - if(LIBC_VERSION) # clock_gettime() was moved from librt -> libc in 2.17 if(${LIBC_VERSION} VERSION_LESS "2.17") diff --git a/host/utilities/bladeRF-cli/src/cmd/cmd.c b/host/utilities/bladeRF-cli/src/cmd/cmd.c index 09f1c6b8..b1ec23ed 100644 --- a/host/utilities/bladeRF-cli/src/cmd/cmd.c +++ b/host/utilities/bladeRF-cli/src/cmd/cmd.c @@ -17,7 +17,6 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include -#include #include #include #include diff --git a/host/utilities/bladeRF-cli/src/cmd/generate.c b/host/utilities/bladeRF-cli/src/cmd/generate.c index 3ffedf29..cf6cbbd4 100644 --- a/host/utilities/bladeRF-cli/src/cmd/generate.c +++ b/host/utilities/bladeRF-cli/src/cmd/generate.c @@ -19,7 +19,6 @@ */ #include #include -#include #include #include #include @@ -36,6 +35,7 @@ #include "parse.h" #include "rel_assert.h" #include "rxtx_impl.h" +#include "thread.h" #include "doc/cmd_help.h" int gen_write_fmt_sample(FILE *fp, enum rxtx_fmt fmt, int16_t s_i, int16_t s_q) { diff --git a/host/utilities/bladeRF-cli/src/cmd/rx.c b/host/utilities/bladeRF-cli/src/cmd/rx.c index 64a083cd..ea65a294 100644 --- a/host/utilities/bladeRF-cli/src/cmd/rx.c +++ b/host/utilities/bladeRF-cli/src/cmd/rx.c @@ -20,7 +20,6 @@ #include #include #include -#include #include #include #include diff --git a/host/utilities/bladeRF-cli/src/cmd/rxtx.c b/host/utilities/bladeRF-cli/src/cmd/rxtx.c index a5d1867e..64ac0d91 100644 --- a/host/utilities/bladeRF-cli/src/cmd/rxtx.c +++ b/host/utilities/bladeRF-cli/src/cmd/rxtx.c @@ -20,7 +20,6 @@ #include #include #include -#include #include #include "host_config.h" @@ -112,7 +111,7 @@ void rxtx_set_state(struct rxtx_data *rxtx, enum rxtx_state state) { MUTEX_LOCK(&rxtx->task_mgmt.lock); rxtx->task_mgmt.state = state; - pthread_cond_signal(&rxtx->task_mgmt.signal_state_change); + COND_SIGNAL(&rxtx->task_mgmt.signal_state_change); MUTEX_UNLOCK(&rxtx->task_mgmt.lock); } @@ -158,7 +157,7 @@ void rxtx_submit_request(struct rxtx_data *rxtx, unsigned char req) { MUTEX_LOCK(&rxtx->task_mgmt.lock); rxtx->task_mgmt.req |= req; - pthread_cond_signal(&rxtx->task_mgmt.signal_req); + COND_SIGNAL(&rxtx->task_mgmt.signal_req); MUTEX_UNLOCK(&rxtx->task_mgmt.lock); } @@ -415,9 +414,9 @@ struct rxtx_data *rxtx_data_alloc(bladerf_direction dir) ret->task_mgmt.state = RXTX_STATE_IDLE; ret->task_mgmt.req = 0; MUTEX_INIT(&ret->task_mgmt.lock); - pthread_cond_init(&ret->task_mgmt.signal_req, NULL); - pthread_cond_init(&ret->task_mgmt.signal_done, NULL); - pthread_cond_init(&ret->task_mgmt.signal_state_change, NULL); + COND_INIT(&ret->task_mgmt.signal_req); + COND_INIT(&ret->task_mgmt.signal_done); + COND_INIT(&ret->task_mgmt.signal_state_change); ret->task_mgmt.main_task_waiting = false; /* Initialize error management */ @@ -434,9 +433,9 @@ int rxtx_startup(struct cli_state *s, bladerf_direction dir) if (rxtx_is_tx(dir)) { rxtx_set_state(s->tx, RXTX_STATE_INIT); - status = pthread_create(&s->tx->task_mgmt.thread, NULL, tx_task, s); + status = THREAD_CREATE(&s->tx->task_mgmt.thread, tx_task, s); - if (status < 0) { + if (status != THREAD_SUCCESS) { rxtx_set_state(s->tx, RXTX_STATE_FAIL); } else { status = rxtx_wait_for_state(s->tx, RXTX_STATE_IDLE, 0); @@ -446,9 +445,8 @@ int rxtx_startup(struct cli_state *s, bladerf_direction dir) } } else { rxtx_set_state(s->rx, RXTX_STATE_INIT); - status = pthread_create(&s->rx->task_mgmt.thread, NULL, rx_task, s); - - if (status < 0) { + status = THREAD_CREATE(&s->rx->task_mgmt.thread, rx_task, s); + if (status != THREAD_SUCCESS) { rxtx_set_state(s->rx, RXTX_STATE_FAIL); } else { status = rxtx_wait_for_state(s->rx, RXTX_STATE_IDLE, 0); @@ -478,7 +476,7 @@ void rxtx_shutdown(struct cli_state *s, struct rxtx_data *rxtx) if (rxtx->task_mgmt.started && rxtx_get_state(rxtx) != RXTX_STATE_FAIL) { rxtx_submit_request(rxtx, RXTX_TASK_REQ_SHUTDOWN); - pthread_join(rxtx->task_mgmt.thread, NULL); + THREAD_JOIN(rxtx->task_mgmt.thread, NULL); } free(rxtx->file_mgmt.path); @@ -741,7 +739,7 @@ void rxtx_task_exec_idle(struct rxtx_data *rxtx, unsigned char *requests) /* Wait until we're asked to start or shutdown */ while (!(*requests & (RXTX_TASK_REQ_START | RXTX_TASK_REQ_SHUTDOWN))) { MUTEX_LOCK(&rxtx->task_mgmt.lock); - pthread_cond_wait(&rxtx->task_mgmt.signal_req, &rxtx->task_mgmt.lock); + COND_WAIT(&rxtx->task_mgmt.signal_req, &rxtx->task_mgmt.lock); *requests = rxtx->task_mgmt.req; MUTEX_UNLOCK(&rxtx->task_mgmt.lock); } @@ -791,7 +789,6 @@ int rxtx_handle_wait(struct cli_state *s, int status = CLI_RET_UNKNOWN; bool ok; unsigned int timeout_ms = 0; - struct timespec timeout_abs; enum rxtx_state state; static const struct numeric_suffix times[] = { @@ -825,29 +822,13 @@ int rxtx_handle_wait(struct cli_state *s, MUTEX_UNLOCK(&s->dev_lock); if (timeout_ms != 0) { - const unsigned int timeout_sec = timeout_ms / 1000; - - status = clock_gettime(CLOCK_REALTIME, &timeout_abs); - if (status != 0) { - goto out; - } - - timeout_abs.tv_sec += timeout_sec; - timeout_abs.tv_nsec += (timeout_ms % 1000) * 1000 * 1000; - - if (timeout_abs.tv_nsec >= NSEC_PER_SEC) { - timeout_abs.tv_sec += timeout_abs.tv_nsec / NSEC_PER_SEC; - timeout_abs.tv_nsec %= NSEC_PER_SEC; - } - MUTEX_LOCK(&rxtx->task_mgmt.lock); rxtx->task_mgmt.main_task_waiting = true; while (rxtx->task_mgmt.main_task_waiting) { - status = - pthread_cond_timedwait(&rxtx->task_mgmt.signal_done, - &rxtx->task_mgmt.lock, &timeout_abs); + status = COND_TIMED_WAIT(&rxtx->task_mgmt.signal_done, + &rxtx->task_mgmt.lock, timeout_ms); - if (status == ETIMEDOUT) { + if (status == THREAD_TIMEOUT) { rxtx->task_mgmt.main_task_waiting = false; } } @@ -857,19 +838,18 @@ int rxtx_handle_wait(struct cli_state *s, MUTEX_LOCK(&rxtx->task_mgmt.lock); rxtx->task_mgmt.main_task_waiting = true; while (rxtx->task_mgmt.main_task_waiting) { - status = pthread_cond_wait(&rxtx->task_mgmt.signal_done, + status = COND_WAIT(&rxtx->task_mgmt.signal_done, &rxtx->task_mgmt.lock); } MUTEX_UNLOCK(&rxtx->task_mgmt.lock); } -out: /* Re-acquire the device control lock, as the top-level command handler * will be unlocking this when it's done */ MUTEX_LOCK(&s->dev_lock); /* Expected and OK condition */ - if (status == ETIMEDOUT) { + if (status == THREAD_TIMEOUT) { status = 0; } @@ -887,7 +867,7 @@ bool rxtx_release_wait(struct rxtx_data *rxtx) MUTEX_LOCK(&rxtx->task_mgmt.lock); was_waiting = rxtx->task_mgmt.main_task_waiting; rxtx->task_mgmt.main_task_waiting = false; - pthread_cond_signal(&rxtx->task_mgmt.signal_done); + COND_SIGNAL(&rxtx->task_mgmt.signal_done); MUTEX_UNLOCK(&rxtx->task_mgmt.lock); return was_waiting; @@ -898,36 +878,19 @@ int rxtx_wait_for_state(struct rxtx_data *rxtx, unsigned int timeout_ms) { int status = 0; - struct timespec timeout_abs; if (timeout_ms != 0) { - const unsigned int timeout_sec = timeout_ms / 1000; - - status = clock_gettime(CLOCK_REALTIME, &timeout_abs); - if (status != 0) { - return -1; - } - - timeout_abs.tv_sec += timeout_sec; - timeout_abs.tv_nsec += (timeout_ms % 1000) * 1000 * 1000; - - if (timeout_abs.tv_nsec >= NSEC_PER_SEC) { - timeout_abs.tv_sec += timeout_abs.tv_nsec / NSEC_PER_SEC; - timeout_abs.tv_nsec %= NSEC_PER_SEC; - } - MUTEX_LOCK(&rxtx->task_mgmt.lock); - while (rxtx->task_mgmt.state != req_state && status == 0) { - status = - pthread_cond_timedwait(&rxtx->task_mgmt.signal_state_change, - &rxtx->task_mgmt.lock, &timeout_abs); + while (rxtx->task_mgmt.state != req_state && status == THREAD_SUCCESS) { + status = COND_TIMED_WAIT(&rxtx->task_mgmt.signal_state_change, + &rxtx->task_mgmt.lock, timeout_ms); } MUTEX_UNLOCK(&rxtx->task_mgmt.lock); } else { MUTEX_LOCK(&rxtx->task_mgmt.lock); while (rxtx->task_mgmt.state != req_state) { - status = pthread_cond_wait(&rxtx->task_mgmt.signal_state_change, + status = COND_WAIT(&rxtx->task_mgmt.signal_state_change, &rxtx->task_mgmt.lock); } MUTEX_UNLOCK(&rxtx->task_mgmt.lock); diff --git a/host/utilities/bladeRF-cli/src/cmd/rxtx_impl.h b/host/utilities/bladeRF-cli/src/cmd/rxtx_impl.h index 7f2c0d8c..7149fb09 100644 --- a/host/utilities/bladeRF-cli/src/cmd/rxtx_impl.h +++ b/host/utilities/bladeRF-cli/src/cmd/rxtx_impl.h @@ -102,16 +102,16 @@ struct file_mgmt { /* Task and thread management */ struct task_mgmt { - pthread_t thread; /* Handle to thread in which the task runs */ + THREAD thread; /* Handle to thread in which the task runs */ bool started; /* Has the thread been started? */ enum rxtx_state state; /* Task state */ uint8_t req; /* Requests for state change. See * RXTX_TASK_REQ_* bitmasks */ MUTEX lock; /* Must be held to access 'req' or 'state' */ - pthread_cond_t signal_req; /* Signal when a request has been made */ - pthread_cond_t signal_done; /* Signal when task finishes work */ - pthread_cond_t signal_state_change; /* Signal after state change */ + COND signal_req; /* Signal when a request has been made */ + COND signal_done; /* Signal when task finishes work */ + COND signal_state_change; /* Signal after state change */ bool main_task_waiting; /* Main task is blocked waiting */ }; diff --git a/host/utilities/bladeRF-cli/src/cmd/tx.c b/host/utilities/bladeRF-cli/src/cmd/tx.c index 03ab5904..0f1bd8d8 100644 --- a/host/utilities/bladeRF-cli/src/cmd/tx.c +++ b/host/utilities/bladeRF-cli/src/cmd/tx.c @@ -19,7 +19,6 @@ */ #include #include -#include #include #include #include @@ -31,6 +30,7 @@ #include "minmax.h" #include "parse.h" #include "rel_assert.h" +#include "thread.h" #include "rxtx_impl.h" /* The DAC range is [-2048, 2047] */ diff --git a/host/utilities/bladeRF-cli/src/common.c b/host/utilities/bladeRF-cli/src/common.c index 5c6d4a6a..86e685ba 100644 --- a/host/utilities/bladeRF-cli/src/common.c +++ b/host/utilities/bladeRF-cli/src/common.c @@ -19,7 +19,6 @@ */ #include #include -#include #include #include #include @@ -31,6 +30,7 @@ #include "cmd/rxtx.h" #include "input.h" #include "script.h" +#include "thread.h" /* There's currently only ever 1 active cli_state */ static struct cli_state *cli_state; @@ -105,7 +105,7 @@ struct cli_state *cli_state_create() cli_state->dev_info.is_bladerf_x40_x115 = false; cli_state->dev_info.is_bladerf_micro = false; - pthread_mutex_init(&cli_state->dev_lock, NULL); + MUTEX_INIT(&cli_state->dev_lock); cli_state->rx = rxtx_data_alloc(BLADERF_RX); if (!cli_state->rx) { @@ -305,25 +305,25 @@ const char *cli_strerror(int error, int lib_error) void cli_error_init(struct cli_error *e) { - pthread_mutex_init(&e->lock, NULL); + MUTEX_INIT(&e->lock); e->type = ETYPE_CLI; e->value = 0; } void set_last_error(struct cli_error *e, enum error_type type, int error) { - pthread_mutex_lock(&e->lock); + MUTEX_LOCK(&e->lock); e->type = type; e->value = error; - pthread_mutex_unlock(&e->lock); + MUTEX_UNLOCK(&e->lock); } void get_last_error(struct cli_error *e, enum error_type *type, int *error) { - pthread_mutex_lock(&e->lock); + MUTEX_LOCK(&e->lock); *type = e->type; *error = e->value; - pthread_mutex_unlock(&e->lock); + MUTEX_UNLOCK(&e->lock); } int expand_and_open(const char *filename, const char *mode, FILE **file) diff --git a/host/utilities/bladeRF-cli/src/common.h b/host/utilities/bladeRF-cli/src/common.h index 0980940a..a8fbb602 100644 --- a/host/utilities/bladeRF-cli/src/common.h +++ b/host/utilities/bladeRF-cli/src/common.h @@ -26,7 +26,7 @@ #include "host_config.h" #include "str_queue.h" #include -#include +#include "thread.h" #include #include @@ -88,7 +88,7 @@ enum error_type { struct cli_error { int value; /**< Last command error */ enum error_type type; /**< Type/source of last error */ - pthread_mutex_t lock; + MUTEX lock; }; /** @@ -106,7 +106,7 @@ struct cli_dev_info { struct cli_state { struct bladerf *dev; /**< Device currently in use */ struct cli_dev_info dev_info; /**< Cached device information */ - pthread_mutex_t dev_lock; /**< Should be held when performing + MUTEX dev_lock; /**< Should be held when performing * any "device conrol" calls */ int last_lib_error; /**< Last libbladeRF error */ diff --git a/host/utilities/bladeRF-cli/src/main.c b/host/utilities/bladeRF-cli/src/main.c index 2b3fb861..78856b40 100644 --- a/host/utilities/bladeRF-cli/src/main.c +++ b/host/utilities/bladeRF-cli/src/main.c @@ -23,7 +23,6 @@ #include #include #include -#include #include #include #include "input/input.h" @@ -32,6 +31,7 @@ #include "common.h" #include "cmd.h" #include "version.h" +#include "thread.h" #if BLADERF_OS_WINDOWS #include "setenv.h"