|
| 1 | +package gocoro |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "sync/atomic" |
| 6 | + "time" |
| 7 | +) |
| 8 | + |
| 9 | +// Coroutine represents a coroutine that executes alternately with the main / calling |
| 10 | +// thread. |
| 11 | +type Coroutine struct { |
| 12 | + routine func(*Execution) |
| 13 | + running *atomic.Bool |
| 14 | + yield chan bool |
| 15 | + execute chan bool |
| 16 | + Execution *Execution |
| 17 | +} |
| 18 | + |
| 19 | +// NewCoroutine creates and returns a new Coroutine instance. |
| 20 | +func NewCoroutine() Coroutine { |
| 21 | + co := Coroutine{ |
| 22 | + yield: make(chan bool), |
| 23 | + execute: make(chan bool), |
| 24 | + running: &atomic.Bool{}, |
| 25 | + } |
| 26 | + co.Execution = &Execution{coroutine: &co} |
| 27 | + return co |
| 28 | +} |
| 29 | + |
| 30 | +// Run runs the given coroutine function. Note that the function takes the Coroutine |
| 31 | +// as an argument to allow for a variety of methods of defining this function (as a |
| 32 | +// literal in the Run() function call, as a pointer to a pre-defined function, etc). |
| 33 | +// Run will return an error if the coroutine is already running. |
| 34 | +func (co *Coroutine) Run(coroutineFunc func(exe *Execution)) error { |
| 35 | + |
| 36 | + if !co.running.Load() { |
| 37 | + |
| 38 | + co.running.Store(true) |
| 39 | + |
| 40 | + co.routine = coroutineFunc |
| 41 | + |
| 42 | + go func() { |
| 43 | + // Send something on execute first so the script doesn't update until we |
| 44 | + // call Coroutine.Update() the first time. |
| 45 | + co.execute <- true |
| 46 | + co.routine(co.Execution) |
| 47 | + wasRunning := co.running.Load() |
| 48 | + co.running.Store(false) |
| 49 | + // If the coroutine wasn't running anymore, then we shouldn't push anything to yield to unblock the coroutine at the end |
| 50 | + if wasRunning { |
| 51 | + co.yield <- true |
| 52 | + } |
| 53 | + }() |
| 54 | + |
| 55 | + return nil |
| 56 | + |
| 57 | + } else { |
| 58 | + return errors.New("Coroutine is already running") |
| 59 | + } |
| 60 | + |
| 61 | +} |
| 62 | + |
| 63 | +// Running returns whether the Coroutine is running or not. |
| 64 | +func (co *Coroutine) Running() bool { |
| 65 | + return co.running.Load() |
| 66 | +} |
| 67 | + |
| 68 | +// Update waits for the Coroutine to pause, either as a yield or when the Coroutine is finished. |
| 69 | +func (co *Coroutine) Update() { |
| 70 | + if co.running.Load() { |
| 71 | + <-co.execute // Wait to pull from the execute channel, indicating the coroutine can run |
| 72 | + <-co.yield // Wait to pull from the yield channel, indicating the coroutine has paused / finished |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +// Stop stops running the Coroutine and allows the CoroutineExecution to pick up on it to end gracefully. |
| 77 | +// This does not kill the coroutine, which runs in a goroutine - you'll need to detect this and |
| 78 | +// end the coroutine yourself. |
| 79 | +func (co *Coroutine) Stop() { |
| 80 | + wasRunning := co.running.Load() |
| 81 | + co.running.Store(false) |
| 82 | + if wasRunning { |
| 83 | + <-co.execute // Pull from the execute channel so the coroutine can get out of the yield and realize it's borked |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +var ErrorCoroutineStopped = errors.New("Coroutine requested to be stopped") |
| 88 | + |
| 89 | +// Execution represents a means to easily and simply manipulate coroutine execution from your running coroutine function. |
| 90 | +type Execution struct { |
| 91 | + coroutine *Coroutine |
| 92 | +} |
| 93 | + |
| 94 | +// Yield yields execution in the coroutine function, allowing the main / calling thread to continue. |
| 95 | +// The coroutine will pick up from this point when Coroutine.Update() is called again. |
| 96 | +// If the Coroutine has exited already, then this will immediately return with ErrorCoroutineStopped. |
| 97 | +func (exe *Execution) Yield() error { |
| 98 | + |
| 99 | + if !exe.coroutine.Running() { |
| 100 | + return ErrorCoroutineStopped |
| 101 | + } |
| 102 | + |
| 103 | + exe.coroutine.yield <- true // Yield; we're done |
| 104 | + exe.coroutine.execute <- true // Put something in the execute channel when we're ready to pick back up if we're not done |
| 105 | + |
| 106 | + return nil |
| 107 | + |
| 108 | +} |
| 109 | + |
| 110 | +// Stopped returns true if the coroutine was requested to be stopped. You can check this in your coroutine to exit early and |
| 111 | +// clean up the coroutine as desired. |
| 112 | +func (exe *Execution) Stopped() bool { |
| 113 | + return !exe.coroutine.Running() |
| 114 | +} |
| 115 | + |
| 116 | +// Wait waits the specified duration time, yielding execution in the Coroutine if the time has yet to elapse. |
| 117 | +// Note that this function only checks the time in increments of however long the calling thread takes between calling Coroutine.Update(). |
| 118 | +// So, for example, if Coroutine.Update() is run, say, once every 20 milliseconds, then that's the fidelity of your waiting duration. |
| 119 | +// If the Coroutine has exited already, then this will immediately return with ErrorCoroutineStopped. |
| 120 | +func (exe *Execution) Wait(duration time.Duration) error { |
| 121 | + start := time.Now() |
| 122 | + for { |
| 123 | + |
| 124 | + if time.Since(start) >= duration { |
| 125 | + return nil |
| 126 | + } else { |
| 127 | + if err := exe.Yield(); err != nil { |
| 128 | + return err |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +// WaitTicks waits the specified number of ticks, yielding execution if the number of ticks have yet to elapse. A tick is defined by one instance |
| 135 | +// of Coroutine.Update() being called. |
| 136 | +// If the Coroutine has exited already, then this will immediately return with ErrorCoroutineStopped. |
| 137 | +func (exe *Execution) WaitTicks(tickCount int) error { |
| 138 | + for { |
| 139 | + |
| 140 | + if tickCount == 0 { |
| 141 | + return nil |
| 142 | + } else { |
| 143 | + tickCount-- |
| 144 | + if err := exe.Yield(); err != nil { |
| 145 | + return err |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + } |
| 150 | + |
| 151 | +} |
| 152 | + |
| 153 | +// WaitUntil pauses the Coroutine until the provided Completer's Done() function returns true. |
| 154 | +// If the Coroutine has exited already, then this will immediately return with ErrorCoroutineStopped. |
| 155 | +func (exe *Execution) WaitUntil(completer Completer) error { |
| 156 | + |
| 157 | + for { |
| 158 | + |
| 159 | + if completer.Done() { |
| 160 | + return nil |
| 161 | + } else { |
| 162 | + if err := exe.Yield(); err != nil { |
| 163 | + return err |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | +} |
| 169 | + |
| 170 | +// Do pauses the running Coroutine until the provided function returns true. |
| 171 | +// If the Coroutine has exited already, then this will immediately return with ErrorCoroutineStopped. |
| 172 | +func (exe *Execution) Do(doFunc func() bool) error { |
| 173 | + |
| 174 | + for { |
| 175 | + if doFunc() { |
| 176 | + return nil |
| 177 | + } else { |
| 178 | + if err := exe.Yield(); err != nil { |
| 179 | + return err |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | +} |
| 185 | + |
| 186 | +// Completer provides an interface of an object that can be used to pause a Coroutine until it is completed. |
| 187 | +// If the Completer's Done() function returns true, then the Coroutine will advance. |
| 188 | +type Completer interface { |
| 189 | + Done() bool |
| 190 | +} |
0 commit comments