/
gonewrong.go
84 lines (66 loc) · 1.77 KB
/
gonewrong.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
package gonewrong
/*
#include <errno.h>
#include <stdint.h>
int64_t get_errno() { return errno; }
*/
import "C"
import rt "runtime"
// ******* Thunks ************************************************************
// A simple argumentless function with no return value
type Thunk func()
// Wrap thunk in calls that lock the executing go routine to some OSThread
func (p Thunk) WithOSThread() Thunk {
return Thunk(func() {
rt.LockOSThread()
defer rt.UnlockOSThread()
p()
})
}
// Helper for calling thunk within a separate go routine bound to a
// fixed OSThread
func (p Thunk) RunInOSThread() {
go (p.WithOSThread())()
}
// Wrap thunk such that it sends msg on ch after finishing
// (May discard errors!)
func (p Thunk) SendOnFinish(ch chan interface{}, msg interface{}) Thunk {
return Thunk(func() {
defer func() { ch <- msg }()
p()
})
}
// ******* Error Handling ****************************************************
type ErrKnow interface {
GetError() error
OkIf(cond bool) error
ErrorIf(cond bool) error
}
// If cond is true returns nil, error otherwise
func OkIf(cond bool, error error) error {
return ErrorIf(!cond, error)
}
// If cond is true returns error, nil otherwise
func ErrorIf(cond bool, error error) error {
if cond {
return error
}
return nil
}
// Panics if err is != nil
func PanicUnlessNil(err error) {
if err != nil {
panic(err)
}
}
// Returns true if ptr is C NULL (== 0)
// (Spec doesnt define go nil to be == NULL)
func IsCNullPtr(ptr uintptr) bool {
return ptr == uintptr(0)
}
// Returns errno from C for the current thread
//
// Safe use may require locking this go routine to the underlying OSThread
func GetCErrno() int64 {
return int64(C.get_errno())
}