yield.c 935 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. #include "config.h"
  5. #include <stddef.h>
  6. #include <sys/types.h>
  7. #include <sys/time.h>
  8. #include <sched.h>
  9. #include <unistd.h>
  10. #ifdef HAVE_SYS_SELECT_H
  11. #include <sys/select.h>
  12. #endif
  13. #if defined (__i386__) || defined (__x86_64__)
  14. #include <xmmintrin.h>
  15. #endif
  16. #include "runtime.h"
  17. /* Spin wait. */
  18. void
  19. runtime_procyield (uint32 cnt)
  20. {
  21. volatile uint32 i;
  22. for (i = 0; i < cnt; ++i)
  23. {
  24. #if defined (__i386__) || defined (__x86_64__)
  25. _mm_pause ();
  26. #endif
  27. }
  28. }
  29. /* Ask the OS to reschedule this thread. */
  30. void
  31. runtime_osyield (void)
  32. {
  33. sched_yield ();
  34. }
  35. /* Sleep for some number of microseconds. */
  36. void
  37. runtime_usleep (uint32 us)
  38. {
  39. struct timeval tv;
  40. tv.tv_sec = us / 1000000;
  41. tv.tv_usec = us % 1000000;
  42. select (0, NULL, NULL, NULL, &tv);
  43. }