Skip to content

Commit

Permalink
keep reading/readers relationship
Browse files Browse the repository at this point in the history
  • Loading branch information
wlawt committed Apr 17, 2024
1 parent bc8b7b0 commit 897c043
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ func (e *Executor) work() {
type task struct {
id int
f func() error
// shared are the tasks that this task is using non-exclusively.
shared []*task
// reading are the tasks that this task is using non-exclusively.
reading []*task

l sync.Mutex
executed bool
Expand All @@ -95,12 +95,12 @@ func (e *Executor) runTask(t *task) {
// to ensure we can exit.
defer func() {
// Notify other tasks that we are done reading them
for _, rt := range t.shared {
for _, rt := range t.reading {
rt.l.Lock()
delete(rt.readers, t.id)
rt.l.Unlock()
}
t.shared = nil
t.reading = nil

// Nodify blocked tasks that they can execute
t.l.Lock()
Expand Down Expand Up @@ -145,9 +145,9 @@ func (e *Executor) Run(keys state.Keys, f func() error) {
id := e.tasks
e.tasks++
t := &task{
id: id,
f: f,
shared: []*task{},
id: id,
f: f,
reading: []*task{},

blocked: make(map[int]*task),
readers: make(map[int]*task),
Expand All @@ -170,14 +170,14 @@ func (e *Executor) Run(keys state.Keys, f func() error) {
if v == state.Read {
// If we don't need exclusive access to a key, just mark
// that we are reading it and that we are a reader of it.
t.shared = append(t.shared, lt)
t.reading = append(t.reading, lt)
lt.readers[id] = t
} else {
// If we do need exclusive access to a key, we need to
// mark ourselves blocked on all readers ahead of us.
//
// If a task is a reader, that means it is not executed yet
// and can't mark itself as executed until all [shared] are
// and can't mark itself as executed until all [reading] are
// cleared (which can't be done while we hold the lock for [lt]).
for _, rt := range lt.readers {
// Don't block on ourself if we just recorded us reading
Expand Down

0 comments on commit 897c043

Please sign in to comment.