Skip to content

Commit f7b049d

Browse files
CopilotJohnAmadis
andcommitted
Replace __unix__ defines with DMOD_USE_NANOSLEEP configuration
- Added DMOD_USE_NANOSLEEP configuration option in dmod-defaults.cmake - Added DMOD_USE_NANOSLEEP define in dmod-config.h.in - Replaced __unix__ and __APPLE__ checks with DMOD_USE_NANOSLEEP in dmod_if_rtos.c - This follows the project pattern of using cmake configuration for embedded compatibility Co-authored-by: JohnAmadis <17320783+JohnAmadis@users.noreply.github.com>
1 parent 4066570 commit f7b049d

File tree

3 files changed

+15
-52
lines changed

3 files changed

+15
-52
lines changed

dmod-config.h.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#define DMOD_USE_DIRENT @DMOD_USE_DIRENT@
1414
#define DMOD_USE_ASSERT @DMOD_USE_ASSERT@
1515
#define DMOD_USE_PTHREAD @DMOD_USE_PTHREAD@
16+
#define DMOD_USE_NANOSLEEP @DMOD_USE_NANOSLEEP@
1617
#define DMOD_USE_MMAN @DMOD_USE_MMAN@
1718
#define DMOD_USE_ALIGNED_ALLOC @DMOD_USE_ALIGNED_ALLOC@
1819
#define DMOD_USE_ALIGNED_MALLOC_MOCK @DMOD_USE_ALIGNED_MALLOC_MOCK@

dmod-defaults.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ endif()
3636
if (NOT DEFINED DMOD_USE_PTHREAD)
3737
set(DMOD_USE_PTHREAD ON)
3838
endif()
39+
if (NOT DEFINED DMOD_USE_NANOSLEEP)
40+
set(DMOD_USE_NANOSLEEP ON)
41+
endif()
3942
if (NOT DEFINED DMOD_USE_MMAN)
4043
set(DMOD_USE_MMAN ON)
4144
endif()

src/system/if/dmod_if_rtos.c

Lines changed: 11 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
# define __USE_UNIX98
3333
# include <pthread.h>
3434
#endif
35-
#if defined(__unix__) || defined(__APPLE__)
35+
#if DMOD_USE_NANOSLEEP
3636
# include <unistd.h>
3737
# include <time.h>
3838
#endif
@@ -187,52 +187,24 @@ DMOD_INPUT_WEAK_API_DECLARATION(Dmod, 1.0, void, _Mutex_Delete, ( void* Mutex ))
187187
*/
188188
DMOD_INPUT_WEAK_API_DECLARATION(Dmod, 1.0, bool, _DelayUs, ( uint64_t Microseconds ))
189189
{
190-
#if defined(__unix__) || defined(__APPLE__)
190+
#if DMOD_USE_NANOSLEEP
191191
if (Microseconds == 0)
192192
{
193193
return true;
194194
}
195195

196-
// For delays less than 1 second, use usleep (deprecated but widely available)
197-
// For longer delays, use nanosleep for better precision
198-
if (Microseconds < 1000000)
196+
// Use nanosleep for precision
197+
struct timespec ts;
198+
ts.tv_sec = Microseconds / 1000000;
199+
ts.tv_nsec = (Microseconds % 1000000) * 1000;
200+
201+
while (nanosleep(&ts, &ts) == -1)
199202
{
200-
#ifdef _POSIX_C_SOURCE
201-
// Use nanosleep for better precision
202-
struct timespec ts;
203-
ts.tv_sec = 0;
204-
ts.tv_nsec = Microseconds * 1000;
205-
206-
while (nanosleep(&ts, &ts) == -1)
207-
{
208-
// Continue if interrupted by signal
209-
if (errno != EINTR)
210-
{
211-
return false;
212-
}
213-
}
214-
#else
215-
// Fallback to usleep
216-
if (usleep(Microseconds) != 0)
203+
// Continue if interrupted by signal
204+
if (errno != EINTR)
217205
{
218206
return false;
219207
}
220-
#endif
221-
}
222-
else
223-
{
224-
// Split into seconds and remaining microseconds
225-
struct timespec ts;
226-
ts.tv_sec = Microseconds / 1000000;
227-
ts.tv_nsec = (Microseconds % 1000000) * 1000;
228-
229-
while (nanosleep(&ts, &ts) == -1)
230-
{
231-
if (errno != EINTR)
232-
{
233-
return false;
234-
}
235-
}
236208
}
237209

238210
return true;
@@ -253,7 +225,7 @@ DMOD_INPUT_WEAK_API_DECLARATION(Dmod, 1.0, bool, _DelayUs, ( uint64_t Microsecon
253225
*/
254226
DMOD_INPUT_WEAK_API_DECLARATION(Dmod, 1.0, bool, _SleepMs, ( uint64_t Milliseconds ))
255227
{
256-
#if DMOD_USE_PTHREAD
228+
#if DMOD_USE_NANOSLEEP
257229
if (Milliseconds == 0)
258230
{
259231
return true;
@@ -273,19 +245,6 @@ DMOD_INPUT_WEAK_API_DECLARATION(Dmod, 1.0, bool, _SleepMs, ( uint64_t Millisecon
273245
}
274246
}
275247

276-
return true;
277-
#elif defined(__unix__) || defined(__APPLE__)
278-
if (Milliseconds == 0)
279-
{
280-
return true;
281-
}
282-
283-
// Convert milliseconds to microseconds
284-
if (usleep(Milliseconds * 1000) != 0)
285-
{
286-
return false;
287-
}
288-
289248
return true;
290249
#else
291250
// Platform not supported

0 commit comments

Comments
 (0)