This repository was archived by the owner on Jan 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsem_linux.go
150 lines (118 loc) · 3.84 KB
/
sem_linux.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright (C) 2003-2014 Free Software Foundation, Inc.
// This file is part of the GNU C Library.
// Contributed by Paul Mackerras <paulus@au.ibm.com>, 2003.
//
// The GNU C Library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// The GNU C Library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with the GNU C Library; if not, see
// <http://www.gnu.org/licenses/>.
// +build linux,!386,!amd64
package sem
/*
#include <semaphore.h> // For sem_*
#include <linux/futex.h> // For FUTEX_*
#include <bits/local_lim.h> // For SEM_VALUE_MAX
// This is pulled from glibc-2.17/nptl/sysdeps/unix/sysv/linux/internaltypes.h
// The case of field names has been changed to be consistent with cgo -godefs
struct new_sem
{
unsigned int Value;
int Private;
unsigned long int NWaiters;
};
*/
import "C"
import (
"sync/atomic"
"unsafe"
"golang.org/x/sys/unix"
)
type Semaphore C.sem_t
type newSem C.struct_new_sem
func New(value uint) (*Semaphore, error) {
sem := new(Semaphore)
if err := sem.Init(value); err != nil {
return nil, err
}
return sem, nil
}
// This mirrors atomic_decrement_if_positive from glibc-2.17/include/atomic.h
func atomicDecrementIfPositive(mem *uint32) uint32 {
for {
if old := atomic.LoadUint32(mem); old == 0 || atomic.CompareAndSwapUint32(mem, old, old-1) {
return old
}
}
}
// This (mostly?) mirrors __new_sem_wait from glibc-2.17/nptl/sysdeps/unix/sysv/linux/sem_wait.c
func (sem *Semaphore) Wait() error {
isem := (*newSem)(unsafe.Pointer(sem))
if atomicDecrementIfPositive((*uint32)(&isem.Value)) > 0 {
return nil
}
atomic.AddUintptr((*uintptr)(unsafe.Pointer(&isem.NWaiters)), 1)
for {
//err = do_futex_wait(isem);
if _, _, err := unix.Syscall6(unix.SYS_FUTEX, uintptr(unsafe.Pointer(&isem.Value)), uintptr(C.FUTEX_WAIT), 0, 0, 0, 0); err != 0 && err != unix.EWOULDBLOCK {
atomic.AddUintptr((*uintptr)(unsafe.Pointer(&isem.NWaiters)), ^uintptr(0))
return err
}
if atomicDecrementIfPositive((*uint32)(&isem.Value)) > 0 {
atomic.AddUintptr((*uintptr)(unsafe.Pointer(&isem.NWaiters)), ^uintptr(0))
return nil
}
}
}
// This (loosely?) mirrors __new_sem_trywait from glibc-2.17/nptl/sysdeps/unix/sysv/linux/sem_trywait.c
func (sem *Semaphore) TryWait() error {
isem := (*newSem)(unsafe.Pointer(sem))
if atomicDecrementIfPositive((*uint32)(&isem.Value)) > 0 {
return nil
}
return unix.EAGAIN
}
// This mirrors __new_sem_post from glibc-2.17/nptl/sysdeps/unix/sysv/linux/sem_post.c
func (sem *Semaphore) Post() error {
isem := (*newSem)(unsafe.Pointer(sem))
for {
cur := atomic.LoadUint32((*uint32)(&isem.Value))
if cur == C.SEM_VALUE_MAX {
return unix.EOVERFLOW
}
if atomic.CompareAndSwapUint32((*uint32)(&isem.Value), cur, cur+1) {
break
}
}
// atomic_full_barrier ();
if atomic.LoadUintptr((*uintptr)(unsafe.Pointer(&isem.NWaiters))) <= 0 {
return nil
}
if _, _, err := unix.Syscall6(unix.SYS_FUTEX, uintptr(unsafe.Pointer(&isem.Value)), uintptr(C.FUTEX_WAKE), 1, 0, 0, 0); err != 0 {
return err
}
return nil
}
// This mirrors __new_sem_init from glibc-2.17/nptl/sem_init.c
func (sem *Semaphore) Init(value uint) error {
if value > C.SEM_VALUE_MAX {
return unix.EINVAL
}
isem := (*newSem)(unsafe.Pointer(sem))
isem.Value = C.uint(value)
isem.Private = 0
isem.NWaiters = 0
return nil
}
// This mirrors __new_sem_destroy from glibc-2.17/nptl/sem_destroy.c
func (sem *Semaphore) Destroy() error {
return nil
}