libcm is a C development framework with an emphasis on audio signal processing applications.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

clock_gettime_stub.c 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (c), MM Weiss
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * 3. Neither the name of the MM Weiss nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software without
  17. * specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
  20. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  21. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  22. * SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  24. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  26. * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  27. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. /*
  30. * clock_gettime_stub.c
  31. * gcc -Wall -c clock_gettime_stub.c
  32. * posix realtime functions; MacOS user space glue
  33. */
  34. /* @comment
  35. * other possible implementation using intel builtin rdtsc
  36. * rdtsc-workaround: http://www.mcs.anl.gov/~kazutomo/rdtsc.html
  37. *
  38. * we could get the ticks by doing this
  39. *
  40. * __asm __volatile("mov %%ebx, %%esi\n\t"
  41. * "cpuid\n\t"
  42. * "xchg %%esi, %%ebx\n\t"
  43. * "rdtsc"
  44. * : "=a" (a),
  45. * "=d" (d)
  46. * );
  47. * we could even replace our tricky sched_yield call by assembly code to get a better accurency,
  48. * anyway the following C stub will satisfy 99% of apps using posix clock_gettime call,
  49. * moreover, the setter version (clock_settime) could be easly written using mach primitives:
  50. * http://www.opensource.apple.com/source/xnu/xnu-${VERSION}/osfmk/man/ (clock_[set|get]_time)
  51. *
  52. * hackers don't be crackers, don't you use a flush toilet?
  53. *
  54. *
  55. * @see draft: ./posix-realtime-stub/posix-realtime-stub.c
  56. *
  57. */
  58. #ifdef OS_OSX
  59. #pragma weak clock_gettime
  60. #include <sys/time.h>
  61. #include <sys/resource.h>
  62. #include <mach/mach.h>
  63. #include <mach/clock.h>
  64. #include <mach/mach_time.h>
  65. #include <errno.h>
  66. #include <unistd.h>
  67. #include <sched.h>
  68. #include "clock_gettime_stub.h"
  69. /*
  70. typedef enum {
  71. CLOCK_REALTIME,
  72. CLOCK_MONOTONIC,
  73. CLOCK_PROCESS_CPUTIME_ID,
  74. CLOCK_THREAD_CPUTIME_ID
  75. } clockid_t;
  76. */
  77. static mach_timebase_info_data_t __clock_gettime_inf;
  78. int clock_gettime(clockid_t clk_id, struct timespec *tp) {
  79. kern_return_t ret;
  80. clock_serv_t clk;
  81. clock_id_t clk_serv_id;
  82. mach_timespec_t tm;
  83. uint64_t start, end, delta, nano;
  84. //task_basic_info_data_t tinfo;
  85. //task_thread_times_info_data_t ttinfo;
  86. //mach_msg_type_number_t tflag;
  87. int retval = -1;
  88. switch (clk_id) {
  89. case CLOCK_REALTIME:
  90. case CLOCK_MONOTONIC:
  91. clk_serv_id = clk_id == CLOCK_REALTIME ? CALENDAR_CLOCK : SYSTEM_CLOCK;
  92. if (KERN_SUCCESS == (ret = host_get_clock_service(mach_host_self(), clk_serv_id, &clk))) {
  93. if (KERN_SUCCESS == (ret = clock_get_time(clk, &tm))) {
  94. tp->tv_sec = tm.tv_sec;
  95. tp->tv_nsec = tm.tv_nsec;
  96. retval = 0;
  97. }
  98. }
  99. if (KERN_SUCCESS != ret) {
  100. errno = EINVAL;
  101. retval = -1;
  102. }
  103. break;
  104. case CLOCK_PROCESS_CPUTIME_ID:
  105. case CLOCK_THREAD_CPUTIME_ID:
  106. start = mach_absolute_time();
  107. if (clk_id == CLOCK_PROCESS_CPUTIME_ID) {
  108. getpid();
  109. } else {
  110. sched_yield();
  111. }
  112. end = mach_absolute_time();
  113. delta = end - start;
  114. if (0 == __clock_gettime_inf.denom) {
  115. mach_timebase_info(&__clock_gettime_inf);
  116. }
  117. nano = delta * __clock_gettime_inf.numer / __clock_gettime_inf.denom;
  118. tp->tv_sec = nano * 1e-9;
  119. tp->tv_nsec = nano - (tp->tv_sec * 1e9);
  120. retval = 0;
  121. break;
  122. default:
  123. errno = EINVAL;
  124. retval = -1;
  125. }
  126. return retval;
  127. }
  128. #endif // __APPLE__
  129. /* EOF */