pastebin - collaborative debugging tool
rovema.kpaste.net RSS


|clock_nanosleep()| fires too early
Posted by Anonymous on Thu 17th Oct 2024 13:45
raw | new post

  1. VOID RTLX_AlarmThread ( VOID* pAlarmFunc )
  2. {
  3.   ULONGLONG            ullCurTime   = 0;
  4.   ULONG                ulTimeOffset  = ulCycleTime ? ulCycleTime : 2000000;
  5.   INT                  iRet         = 0;
  6.   static RTLX_TIMESPEC rSleepTime;
  7.   //, remainTime;
  8.   clock_gettime(CLOCK_MONOTONIC, &rSleepTime);
  9.  
  10. #if 0
  11.   // Set sleep time (= check interval)
  12.   rSleepTime.tv_nsec = RTOS_TIME_SHIFT_NIC_TIMED / 4;
  13.   rSleepTime.tv_sec  = 0;
  14. #endif
  15. //  remainTime.tv_sec = 0;
  16.   RTLX_VERBOSE(1, "Successfully started NIC-timed alarm thread\n");
  17.  
  18.   RTOS_SetThreadPriority(RTOS_THREAD_PRIORITY_TIMER);  // was -2 !!!
  19.  
  20.  
  21.   while(TRUE)
  22.   {
  23.     // Active wait for next cycle based on NIC timer:
  24.     // poll NIC clock every rSleepTime until time reached
  25.     // Not ideal in terms of CPU utilization and jitter
  26.     // behavior, but simple and effective
  27.     iRet = RTLX_ReadTimeNic(
  28.                              &(arIgbDev[0]),
  29.                              &ullCurTime
  30.                            );
  31.     if (iRet != RTOS_RET_OK)
  32.     {
  33.       RTLX_VERBOSE(0, "Error reading time from NIC\n");
  34.     }
  35.     // sleep till next time stamp
  36.     ulTimeOffset = ullCycleOffset - ullCurTime;
  37.     rSleepTime.tv_nsec += ulTimeOffset;
  38.     rSleepTime.tv_sec  += rSleepTime.tv_nsec / NANOSEC_PER_SEC;
  39.     rSleepTime.tv_nsec %= NANOSEC_PER_SEC;
  40.     pNicAlarmFunc();
  41.     do {
  42.         iRet = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &rSleepTime, NULL);
  43.         SystemTraceDebugMarker("sleep finish -RTLX_AlarmThread NIC ulTimeOffset= %lu", ulTimeOffset );
  44.     } while (iRet == EINTR);
  45.     // Time reached, so start cycle
  46.     RTLX_SET_NIC_TIMED_OFFSET(ullCycleOffset, ulCycleTime);
  47.   }
  48. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at