OSDN Git Service

runtime: If O_CLOEXEC is not defined, define it as 0.
[pf3gnuchains/gcc-fork.git] / libgo / runtime / yield.c
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
5 #define _GNU_SOURCE
6
7 #include "config.h"
8
9 #include <stddef.h>
10 #include <sys/types.h>
11 #include <sys/time.h>
12 #include <sched.h>
13 #include <unistd.h>
14
15 #ifdef HAVE_SYS_SELECT_H
16 #include <sys/select.h>
17 #endif
18
19 #include "runtime.h"
20
21 /* Spin wait.  */
22
23 void
24 runtime_procyield (uint32 cnt)
25 {
26   volatile uint32 i;
27
28   for (i = 0; i < cnt; ++i)
29     {
30 #if defined (__i386__) || defined (__x86_64__)
31       __builtin_ia32_pause ();
32 #endif
33     }
34 }
35
36 /* Ask the OS to reschedule this thread.  */
37
38 void
39 runtime_osyield (void)
40 {
41   sched_yield ();
42 }
43
44 /* Sleep for some number of microseconds.  */
45
46 void
47 runtime_usleep (uint32 us)
48 {
49   struct timeval tv;
50
51   tv.tv_sec = us / 1000000;
52   tv.tv_usec = us % 1000000;
53   select (0, NULL, NULL, NULL, &tv);
54 }