OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [runtime/] [thread-linux.c] - Blame information for rev 801

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// Copyright 2009 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
#include "runtime.h"
6
 
7
#include <errno.h>
8
#include <string.h>
9
#include <time.h>
10
#include <sys/types.h>
11
#include <sys/stat.h>
12
#include <fcntl.h>
13
#include <unistd.h>
14
#include <syscall.h>
15
#include <linux/futex.h>
16
 
17
typedef struct timespec Timespec;
18
 
19
// Atomically,
20
//      if(*addr == val) sleep
21
// Might be woken up spuriously; that's allowed.
22
// Don't sleep longer than ns; ns < 0 means forever.
23
void
24
runtime_futexsleep(uint32 *addr, uint32 val, int64 ns)
25
{
26
        Timespec ts, *tsp;
27
 
28
        if(ns < 0)
29
                tsp = nil;
30
        else {
31
                ts.tv_sec = ns/1000000000LL;
32
                ts.tv_nsec = ns%1000000000LL;
33
                // Avoid overflow
34
                if(ts.tv_sec > 1<<30)
35
                        ts.tv_sec = 1<<30;
36
                tsp = &ts;
37
        }
38
 
39
        // Some Linux kernels have a bug where futex of
40
        // FUTEX_WAIT returns an internal error code
41
        // as an errno.  Libpthread ignores the return value
42
        // here, and so can we: as it says a few lines up,
43
        // spurious wakeups are allowed.
44
        syscall(__NR_futex, addr, FUTEX_WAIT, val, tsp, nil, 0);
45
}
46
 
47
// If any procs are sleeping on addr, wake up at most cnt.
48
void
49
runtime_futexwakeup(uint32 *addr, uint32 cnt)
50
{
51
        int64 ret;
52
 
53
        ret = syscall(__NR_futex, addr, FUTEX_WAKE, cnt, nil, nil, 0);
54
 
55
        if(ret >= 0)
56
                return;
57
 
58
        // I don't know that futex wakeup can return
59
        // EAGAIN or EINTR, but if it does, it would be
60
        // safe to loop and call futex again.
61
        runtime_printf("futexwakeup addr=%p returned %lld\n", addr, (long long)ret);
62
        *(int32*)0x1006 = 0x1006;
63
}
64
 
65
#ifndef O_CLOEXEC
66
#define O_CLOEXEC 0
67
#endif
68
 
69
static int32
70
getproccount(void)
71
{
72
        int32 fd, rd, cnt, cpustrlen;
73
        const char *cpustr;
74
        const byte *pos;
75
        byte *bufpos;
76
        byte buf[256];
77
 
78
        fd = open("/proc/stat", O_RDONLY|O_CLOEXEC, 0);
79
        if(fd == -1)
80
                return 1;
81
        cnt = 0;
82
        bufpos = buf;
83
        cpustr = "\ncpu";
84
        cpustrlen = strlen(cpustr);
85
        for(;;) {
86
                rd = read(fd, bufpos, sizeof(buf)-cpustrlen);
87
                if(rd == -1)
88
                        break;
89
                bufpos[rd] = 0;
90
                for(pos=buf; (pos=(const byte*)strstr((const char*)pos, cpustr)) != nil; cnt++, pos++) {
91
                }
92
                if(rd < cpustrlen)
93
                        break;
94
                memmove(buf, bufpos+rd-cpustrlen+1, cpustrlen-1);
95
                bufpos = buf+cpustrlen-1;
96
        }
97
        close(fd);
98
        return cnt ? cnt : 1;
99
}
100
 
101
void
102
runtime_osinit(void)
103
{
104
        runtime_ncpu = getproccount();
105
}
106
 
107
void
108
runtime_goenvs(void)
109
{
110
        runtime_goenvs_unix();
111
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.