forked from vanadium/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread.go
93 lines (80 loc) · 2.2 KB
/
thread.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
// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package concurrency
import (
"fmt"
)
// TID is the thread identifier type.
type TID int
// Increasing is used to sort thread identifiers in an increasing order.
type IncreasingTID []TID
// SORT INTERFACE IMPLEMENTATION
func (tids IncreasingTID) Len() int {
return len(tids)
}
func (tids IncreasingTID) Less(i, j int) bool {
return tids[i] < tids[j]
}
func (tids IncreasingTID) Swap(i, j int) {
tids[i], tids[j] = tids[j], tids[i]
}
// TIDGenerator is used for generating unique thread identifiers.
func TIDGenerator() func() TID {
var n int = 0
return func() TID {
n++
return TID(n)
}
}
// thread records the abstract state of a thread during an execution
// of the test.
type thread struct {
// tid is the thread identifier.
tid TID
// clock is a vector clock that keeps track of the logical time of
// this thread.
clock clock
// ready is a channel that can be used to schedule execution of the
// thread.
ready chan struct{}
// req holds the current scheduling request of the thread.
request request
}
// newThread is the thread factory.
func newThread(tid TID, clock clock) *thread {
return &thread{
tid: tid,
clock: clock.clone(),
}
}
// enabled checks if the thread can be scheduled given the current
// execution context.
func (t *thread) enabled(ctx *context) bool {
if t.request == nil {
panic(fmt.Sprintf("Thread %v has no request.", t.tid))
}
return t.request.enabled(ctx)
}
// kind returns the kind of the thread transition.
func (t *thread) kind() transitionKind {
if t.request == nil {
panic(fmt.Sprintf("Thread %v has no request.", t.tid))
}
return t.request.kind()
}
// readSet returns the set of abstract resources read by the thread.
func (t *thread) readSet() resourceSet {
if t.request == nil {
panic(fmt.Sprintf("Thread %v has no request.", t.tid))
}
return t.request.readSet()
}
// writeSet returns the set of abstract resources written by the
// thread.
func (t *thread) writeSet() resourceSet {
if t.request == nil {
panic(fmt.Sprintf("Thread %v has no request.", t.tid))
}
return t.request.writeSet()
}