-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstance.go
More file actions
55 lines (46 loc) · 907 Bytes
/
Copy pathinstance.go
File metadata and controls
55 lines (46 loc) · 907 Bytes
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
package sango
import (
"context"
"errors"
"sync"
"github.com/tetratelabs/wazero/api"
)
var (
ErrReleased = errors.New("sango: instance already released")
ErrConcurrentEval = errors.New("sango: concurrent eval on single instance")
)
type Instance struct {
mod api.Module
adapter Adapter
mu sync.Mutex
busy bool
released bool
}
func (i *Instance) Eval(ctx context.Context, code []byte) (Result, error) {
i.mu.Lock()
if i.released {
i.mu.Unlock()
return Result{}, ErrReleased
}
if i.busy {
i.mu.Unlock()
return Result{}, ErrConcurrentEval
}
i.busy = true
i.mu.Unlock()
defer func() {
i.mu.Lock()
i.busy = false
i.mu.Unlock()
}()
return i.adapter.Eval(ctx, i.mod, code)
}
func (i *Instance) Release() error {
i.mu.Lock()
defer i.mu.Unlock()
if i.released {
return ErrReleased
}
i.released = true
return i.mod.Close(context.Background())
}