PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/src/3rdparty/libraries/helix/src/common/system/platform/unix/gettickcount.c

https://github.com/muromec/qtopia-ezx
C | 200 lines | 111 code | 13 blank | 76 comment | 12 complexity | f70c67f2c564c9b86a0a1579e1299906 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0, AGPL-1.0, LGPL-2.1, BSD-3-Clause
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Source last modified: $Id: gettickcount.c,v 1.8 2006/06/30 12:53:02 jagmeet Exp $
  3. *
  4. * Portions Copyright (c) 1995-2004 RealNetworks, Inc. All Rights Reserved.
  5. *
  6. * The contents of this file, and the files included with this file,
  7. * are subject to the current version of the RealNetworks Public
  8. * Source License (the "RPSL") available at
  9. * http://www.helixcommunity.org/content/rpsl unless you have licensed
  10. * the file under the current version of the RealNetworks Community
  11. * Source License (the "RCSL") available at
  12. * http://www.helixcommunity.org/content/rcsl, in which case the RCSL
  13. * will apply. You may also obtain the license terms directly from
  14. * RealNetworks. You may not use this file except in compliance with
  15. * the RPSL or, if you have a valid RCSL with RealNetworks applicable
  16. * to this file, the RCSL. Please see the applicable RPSL or RCSL for
  17. * the rights, obligations and limitations governing use of the
  18. * contents of the file.
  19. *
  20. * Alternatively, the contents of this file may be used under the
  21. * terms of the GNU General Public License Version 2 or later (the
  22. * "GPL") in which case the provisions of the GPL are applicable
  23. * instead of those above. If you wish to allow use of your version of
  24. * this file only under the terms of the GPL, and not to allow others
  25. * to use your version of this file under the terms of either the RPSL
  26. * or RCSL, indicate your decision by deleting the provisions above
  27. * and replace them with the notice and other provisions required by
  28. * the GPL. If you do not delete the provisions above, a recipient may
  29. * use your version of this file under the terms of any one of the
  30. * RPSL, the RCSL or the GPL.
  31. *
  32. * This file is part of the Helix DNA Technology. RealNetworks is the
  33. * developer of the Original Code and owns the copyrights in the
  34. * portions it created.
  35. *
  36. * This file, and the files included with this file, is distributed
  37. * and made available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY
  38. * KIND, EITHER EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS
  39. * ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES
  40. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET
  41. * ENJOYMENT OR NON-INFRINGEMENT.
  42. *
  43. * Technology Compatibility Kit Test Suite(s) Location:
  44. * http://www.helixcommunity.org/content/tck
  45. *
  46. * Contributor(s):
  47. *
  48. * ***** END LICENSE BLOCK ***** */
  49. #ifndef _VXWORKS
  50. #include <sys/time.h>
  51. #ifdef HELIX_CONFIG_USE_CLOCK_GETTIME
  52. #include <time.h>
  53. #elif defined HELIX_CONFIG_USE_TIMES_SYSCALL
  54. #include <sys/times.h>
  55. #endif
  56. #endif
  57. #include <unistd.h>
  58. #include "hxtypes.h"
  59. #include "hxheap.h"
  60. #include "hxtick.h"
  61. #include <stdio.h>
  62. #ifdef _VXWORKS
  63. void g_RegisterGlobalPtr(void *ptr);
  64. #include <kernel/cClock.h>
  65. #endif
  66. #ifndef _VXWORKS
  67. #ifdef HELIX_CONFIG_USE_TIMES_SYSCALL
  68. inline ULONG32 GetClockTicksPerSec()
  69. {
  70. static ULONG32 clockTicksPerSec = 0;
  71. if (clockTicksPerSec == 0)
  72. clockTicksPerSec = (ULONG32)sysconf(_SC_CLK_TCK);
  73. return clockTicksPerSec;
  74. }
  75. #endif
  76. /*
  77. *************************IMPORTANT******************************************
  78. The current implementation of making use of external clock depends on setting
  79. of these two variables g_ulHXExternalTimerTick, g_bExternalTimerUsed declared
  80. in gettickcount.c. Since each shared library/dll gets its own version of these
  81. vars, they need to be set in each of the shard libs/dll to turn on this code.
  82. In the current incarnation, only the client core dll sets these. This is
  83. because this support is currently used only by helixsim that makes use of
  84. null renderer, so the only component that makes use of HX_GETTICKCOUNT
  85. extensively is client core.
  86. Please note that if this feature is used by some other app that uses actual
  87. renderer and other plugins, calls to HX_GET_TICKCOUNT in those components
  88. will still make use of gettimeofday() even if HELIX_FEATURE_USEEXTERNALCLOCK
  89. is turned on.
  90. The "proper fix" to propage use of externally set clock to all the plugins
  91. is yet to be implemented.
  92. *************************IMPORTANT*******************************************
  93. */
  94. #if defined(HELIX_FEATURE_SYSTEM_EXTERNAL_TIMER)
  95. ULONG32 g_ulHXExternalTimerTick = 0;
  96. HXBOOL g_bExternalTimerUsed = FALSE;
  97. #endif //defined(HELIX_FEATURE_SYSTEM_EXTERNAL_TIMER)
  98. ULONG32
  99. GetTickCount()
  100. {
  101. #ifdef HELIX_CONFIG_USE_CLOCK_GETTIME
  102. struct timespec tp;
  103. static ULONG32 ulLastTick = 0;
  104. static ULONG32 ulBaseTick = 0;
  105. static ULONG32 ulRollTick = 0;
  106. ULONG32 ulNow = 0;
  107. ULONG32 ulTmp = 0;
  108. clock_gettime(CLOCK_MONOTONIC, &tp);
  109. ulNow = (ULONG32)(tp.tv_sec * 1000 + tp.tv_nsec / 1000000);
  110. ulTmp = ulNow-ulRollTick+ulBaseTick;
  111. if( ulTmp < ulLastTick )
  112. {
  113. //roll over or system clock was set back.
  114. ulRollTick = ulNow;
  115. ulBaseTick = ulLastTick;
  116. ulTmp = ulLastTick;
  117. }
  118. ulLastTick = ulTmp;
  119. return ulLastTick;
  120. #elif defined HELIX_CONFIG_USE_TIMES_SYSCALL
  121. return (ULONG32)((((ULONG32)times(NULL)) * 1000)/ GetClockTicksPerSec());
  122. #else
  123. #if defined(HELIX_FEATURE_SYSTEM_EXTERNAL_TIMER)
  124. if(g_bExternalTimerUsed)
  125. {
  126. return g_ulHXExternalTimerTick;
  127. }
  128. #endif //defined(HELIX_FEATURE_SYSTEM_EXTERNAL_TIMER)
  129. struct timeval tv;
  130. gettimeofday( &tv, NULL );
  131. // this will rollover ~ every 49.7 days and
  132. // is vulnerable to changes in system clock by user
  133. return (ULONG32)(tv.tv_sec * 1000 + tv.tv_usec / 1000);
  134. #endif //HELIX_FEATURE_USE_CLOCK_GETTIME
  135. }
  136. // return microseconds
  137. UINT32
  138. GetTickCountInUSec()
  139. {
  140. struct timeval tv;
  141. gettimeofday( &tv, NULL );
  142. // this will rollover ~ every 49.7 days
  143. return (ULONG32)(tv.tv_sec * 1000 * 1000 + tv.tv_usec);
  144. }
  145. #else
  146. static Clock *_clock = NULL;
  147. ULONG32 GetTickCount()
  148. {
  149. TickSpec ticks;
  150. TimeSpec time;
  151. if (_clock == NULL)
  152. {
  153. _clock = ClockCreate(SYSTEM_CLOCK);
  154. if (_clock == NULL)
  155. {
  156. return 0;
  157. }
  158. g_RegisterGlobalPtr(&_clock);
  159. }
  160. ClockGetTime(_clock, &ticks);
  161. ClockConvertToTime(_clock, &ticks, &time);
  162. return (time.tv_sec * 1000) + (time.tv_nsec / 1000000);
  163. }
  164. // return microseconds
  165. UINT32
  166. GetTickCountInUSec()
  167. {
  168. TickSpec ticks;
  169. TimeSpec time;
  170. if (_clock == NULL)
  171. {
  172. _clock = ClockCreate(SYSTEM_CLOCK);
  173. if (_clock == NULL)
  174. {
  175. return 0;
  176. }
  177. g_RegisterGlobalPtr(&_clock);
  178. }
  179. ClockGetTime(_clock, &ticks);
  180. ClockConvertToTime(_clock, &ticks, &time);
  181. return (time.tv_sec * 1000 * 1000) + (time.tv_nsec / 1000);
  182. }
  183. #endif