<commit_before>// Package waitqueue provides a mechanism to synchronize a master task with several related
// but unbounded subtasks such that:
//
// - When the queue is unlocked, an arbitrary number of subtasks can run concurrently without
//   any delay
// - When the queue is locked, an arbitrary number of subtasks can be submitted without locking
//   the calling thread, but will queue and executed only once the queue is later unlocked
// - When the queue is locked, a request to lock the queue again fails, returning immediately with
//   a `false` Boolean value
// - When a request is made to lock the queue, all extant subtasks already being executed are
package waitqueue

import (
	"sync"
)

// WaitQueue is a lockable work queue
type WaitQueue struct {
	lock   sync.RWMutex // Used to lock `locked`
	locked bool         // If true, the queue is locked `ExecuteOrDefer` defers the execution of closures
	work   sync.RWMutex // Used to lock extant goroutines while the queue is locked
}

// New returns a new WaitQueue ready for use.
func New() *WaitQueue {
	return &WaitQueue{}
}

// ExecuteOrDefer executes a subtask synchronously if the queue is unlocked, and returns its result. If
// the queue is locked, it instead schedules the subtask for asynchronous execution once the queue becomes
// unlocked, and returns immediately with a nil value.
func (w *WaitQueue) ExecuteOrDefer(closure func() error) error {
	// Acquire a read lock on `locked`. This ensures that:<commit_msg>- Fix typo<commit_after>// Package waitqueue provides a mechanism to synchronize a master task with several related
// but unbounded subtasks such that:
//
// - When the queue is unlocked, an arbitrary number of subtasks can run concurrently without
// any delay
// - When the queue is locked, an arbitrary number of subtasks can be submitted without locking
// the calling thread, but will queue and executed only once the queue is later unlocked
// - When the queue is locked, a request to lock the queue again fails, returning immediately with
// a `false` Boolean value
// - When a request is made to lock the queue, all extant subtasks already being executed are
// allowed to finish before the lock is obtained.
package waitqueue

import (
	"sync"
)

// WaitQueue is a lockable work queue
type WaitQueue struct {
	lock   sync.RWMutex // Used to lock `locked`
	locked bool         // If true, the queue is locked `ExecuteOrDefer` defers the execution of closures
	work   sync.RWMutex // Used to lock extant goroutines while the queue is locked
}

// New returns a new WaitQueue ready for use.
func New() *WaitQueue {
	return &WaitQueue{}
}

// ExecuteOrDefer executes a subtask synchronously if the queue is unlocked, and returns its result. If
// the queue is locked, it instead schedules the subtask for asynchronous execution once the queue becomes
// unlocked, and returns immediately with a nil value.
func (w *WaitQueue) ExecuteOrDefer(closure func() error) error {
	// Acquire a read lock on `locked`. This ensures that:
{'fraction_non_alphanumeric': 0.0628514720476348, 'fraction_numerical': 0.0, 'mean_word_length': 3.681114551083591, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '544128', 'n_tokens_mistral': 784, 'n_tokens_neox': 756, 'n_words': 437}