Skip to content

Commit

Permalink
USe RWMutex to optimize read speed
Browse files Browse the repository at this point in the history
  • Loading branch information
j0hax committed Dec 27, 2023
1 parent 39df784 commit 11c2973
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
4 changes: 2 additions & 2 deletions agenda/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ func (h *Agenda) PrettyPrint(output io.Writer) {
w := tabwriter.NewWriter(output, 0, 0, 1, ' ', 0)

// Ensure our queue is not modified
h.mu.Lock()
defer h.mu.Unlock()
h.mu.RLock()
defer h.mu.RUnlock()

for _, event := range h.queue {
// Establish formats
Expand Down
10 changes: 5 additions & 5 deletions agenda/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ type EventEntry struct {
// Agenda is a minheap of calendar events.
type Agenda struct {
queue []EventEntry
mu sync.Mutex
mu sync.RWMutex
}

func (el *Agenda) Len() int {
el.mu.Lock()
defer el.mu.Unlock()
el.mu.RLock()
defer el.mu.RUnlock()
return len(el.queue)
}

func (el *Agenda) Less(i, j int) bool {
el.mu.Lock()
defer el.mu.Unlock()
el.mu.RLock()
defer el.mu.RUnlock()
return el.queue[i].Start.Before(*el.queue[j].Start)
}

Expand Down

0 comments on commit 11c2973

Please sign in to comment.