-
Notifications
You must be signed in to change notification settings - Fork 3
/
lgore.go
53 lines (41 loc) · 866 Bytes
/
lgore.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package gofine
import (
"errors"
"runtime"
"golang.org/x/sys/unix"
)
// LgoreState represents lgore's current state
type LgoreState uint8
const (
// Invalid state for non-existing lgores
Invalid LgoreState = iota
// Available represents an lgore which can be occupied
Available
// Busy represents an lgore which is occupied
Busy
)
type lgore struct {
coreId int
state LgoreState
}
func (lg *lgore) occupy() error {
if lg.state == Busy {
return errors.New("lgore is busy")
}
runtime.LockOSThread()
var cpuset unix.CPUSet
cpuset.Set(lg.coreId)
err := unix.SchedSetaffinity(0, &cpuset)
if err == nil {
lg.state = Busy
}
return err
}
func (lg *lgore) release(original unix.CPUSet) error {
if lg.state == Available {
return nil
}
defer runtime.UnlockOSThread()
lg.state = Available
return unix.SchedSetaffinity(0, &original)
}