readerWriter provides a structure for safely reading and writing a shared resource. It supports multiple readers and a single writer goroutine using a semaphore construct.
| 26 | // readerWriter provides a structure for safely reading and writing a shared resource. |
| 27 | // It supports multiple readers and a single writer goroutine using a semaphore construct. |
| 28 | readerWriter struct { |
| 29 | // The name of this object. |
| 30 | name string |
| 31 | |
| 32 | // write forces reading to stop to allow the write to occur safely. |
| 33 | write sync.WaitGroup |
| 34 | |
| 35 | // readerControl is a semaphore that allows a fixed number of reader goroutines |
| 36 | // to read at the same time. This is our semaphore. |
| 37 | readerControl semaphore |
| 38 | |
| 39 | // shutdown is used to signal to running goroutines to shutdown. |
| 40 | shutdown chan struct{} |
| 41 | |
| 42 | // reportShutdown is used by the goroutines to report they are shutdown. |
| 43 | reportShutdown sync.WaitGroup |
| 44 | |
| 45 | // maxReads defined the maximum number of reads that can occur at a time. |
| 46 | maxReads int |
| 47 | |
| 48 | // maxReaders defines the number of goroutines launched to perform read operations. |
| 49 | maxReaders int |
| 50 | |
| 51 | // currentReads keeps a safe count of the current number of reads occurring |
| 52 | // at any give time. |
| 53 | currentReads int32 |
| 54 | } |
| 55 | ) |
| 56 | |
| 57 | // init is called when the package is initialized. This code runs first. |
nothing calls this directly
no outgoing calls
no test coverage detected