start uses the generator pattern to create the readerWriter value. It launches goroutines to process the work, returning the created ReaderWriter value.
(name string, maxReads int, maxReaders int)
| 85 | // start uses the generator pattern to create the readerWriter value. It launches |
| 86 | // goroutines to process the work, returning the created ReaderWriter value. |
| 87 | func start(name string, maxReads int, maxReaders int) *readerWriter { |
| 88 | // Create a value of readerWriter and initialize. |
| 89 | rw := readerWriter{ |
| 90 | name: name, |
| 91 | shutdown: make(chan struct{}), |
| 92 | maxReads: maxReads, |
| 93 | maxReaders: maxReaders, |
| 94 | readerControl: make(semaphore, maxReads), |
| 95 | } |
| 96 | |
| 97 | // Launch a number of reader goroutines and let them start reading. |
| 98 | rw.reportShutdown.Add(maxReaders) |
| 99 | for goroutine := 0; goroutine < maxReaders; goroutine++ { |
| 100 | go rw.reader(goroutine) |
| 101 | } |
| 102 | |
| 103 | // Launch the single writer goroutine and let it start writing. |
| 104 | rw.reportShutdown.Add(1) |
| 105 | go rw.writer() |
| 106 | |
| 107 | return &rw |
| 108 | } |
| 109 | |
| 110 | // shutdown stops all of the existing readerWriter processes concurrently. |
| 111 | func shutdown(readerWriters ...*readerWriter) { |