-
Notifications
You must be signed in to change notification settings - Fork 390
/
universe.go
66 lines (53 loc) · 1.26 KB
/
universe.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
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"sync"
)
import "C"
var universe = NewUniverse()
// Handle is a generic handle.
type Handle = C.long
// Universe stores different Go values that need to be accessed from Go side.
type Universe struct {
lock sync.Mutex
nextid Handle
values map[Handle]interface{}
}
// NewUniverse creates a place to store go files by handle.
func NewUniverse() *Universe {
return &Universe{
values: make(map[Handle]interface{}),
}
}
// Add adds a value to the table.
func (m *Universe) Add(x interface{}) Handle {
m.lock.Lock()
defer m.lock.Unlock()
m.nextid++
m.values[m.nextid] = x
return m.nextid
}
// Get gets a value.
func (m *Universe) Get(x Handle) interface{} {
m.lock.Lock()
defer m.lock.Unlock()
return m.values[x]
}
// Del deletes the value
func (m *Universe) Del(x Handle) {
m.lock.Lock()
defer m.lock.Unlock()
delete(m.values, x)
}
// Empty returns whether the universe is empty.
func (m *Universe) Empty() bool {
m.lock.Lock()
defer m.lock.Unlock()
return len(m.values) == 0
}
//export internal_UniverseIsEmpty
// internal_UniverseIsEmpty returns true if nothing is stored in the global map.
func internal_UniverseIsEmpty() bool {
return universe.Empty()
}