Shad Ansari | 2f7f9be | 2017-06-07 13:34:53 -0700 | [diff] [blame^] | 1 | /* |
| 2 | <:copyright-BRCM:2016:DUAL/GPL:standard |
| 3 | |
| 4 | Broadcom Proprietary and Confidential.(c) 2016 Broadcom |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Unless you and Broadcom execute a separate written software license |
| 8 | agreement governing use of this software, this software is licensed |
| 9 | to you under the terms of the GNU General Public License version 2 |
| 10 | (the "GPL"), available at http://www.broadcom.com/licenses/GPLv2.php, |
| 11 | with the following added to such license: |
| 12 | |
| 13 | As a special exception, the copyright holders of this software give |
| 14 | you permission to link this software with independent modules, and |
| 15 | to copy and distribute the resulting executable under terms of your |
| 16 | choice, provided that you also meet, for each linked independent |
| 17 | module, the terms and conditions of the license of that module. |
| 18 | An independent module is a module which is not derived from this |
| 19 | software. The special exception does not apply to any modifications |
| 20 | of the software. |
| 21 | |
| 22 | Not withstanding the above, under no circumstances may you combine |
| 23 | this software in any way with any other Broadcom software provided |
| 24 | under a license other than the GPL, without Broadcom's express prior |
| 25 | written consent. |
| 26 | |
| 27 | :> |
| 28 | */ |
| 29 | #include "bcmos_rw_lock.h" |
| 30 | |
| 31 | struct bcmos_rw_lock |
| 32 | { |
| 33 | /* the actual lock, held by the writer, used by the reader to safely obtain a read lock, but not held to allow |
| 34 | * multiple readers */ |
| 35 | bcmos_mutex lock; |
| 36 | /* used by the writer to prevent new readers from obtaining read locks while a writer is waiting */ |
| 37 | bcmos_mutex read_lock; |
| 38 | /* used by readers to signal waiting writers that they have finished reading */ |
| 39 | bcmos_sem write_lock; |
| 40 | /* current number of readers */ |
| 41 | uint32_t readers; |
| 42 | }; |
| 43 | |
| 44 | bcmos_errno bcmos_rw_lock_create(bcmos_rw_lock **lock) |
| 45 | { |
| 46 | bcmos_errno err; |
| 47 | *lock = (bcmos_rw_lock*)bcmos_calloc(sizeof(bcmos_rw_lock)); |
| 48 | if (*lock == NULL) |
| 49 | { |
| 50 | BUG(); |
| 51 | return BCM_ERR_NOMEM; |
| 52 | } |
| 53 | if (BCM_ERR_OK != (err = bcmos_mutex_create(&(*lock)->lock, 0, "bcmos_rw_lock_create_lock"))) |
| 54 | { |
| 55 | BUG(); |
| 56 | return err; |
| 57 | } |
| 58 | if (BCM_ERR_OK != (err = bcmos_mutex_create(&(*lock)->read_lock, 0, "bcmos_rw_lock_create_rw_lock"))) |
| 59 | { |
| 60 | BUG(); |
| 61 | return err; |
| 62 | } |
| 63 | (*lock)->readers = 0; |
| 64 | if (BCM_ERR_OK != (err = bcmos_sem_create(&(*lock)->write_lock, 1, 0, "bcmos_rw_lock_create_write_lock"))) |
| 65 | { |
| 66 | BUG(); |
| 67 | return err; |
| 68 | } |
| 69 | return err; |
| 70 | } |
| 71 | |
| 72 | /*lint -e{455} suppress "thread mutex has not been locked" */ |
| 73 | void bcmos_rw_write_lock(bcmos_rw_lock* lock) |
| 74 | { |
| 75 | /* prevent any new readers from trying to obtain a read lock */ |
| 76 | bcmos_mutex_lock(&lock->read_lock); |
| 77 | /* lock the actual lock */ |
| 78 | bcmos_mutex_lock(&lock->lock); |
| 79 | while (lock->readers != 0) |
| 80 | { |
| 81 | /* there are still readers holding read locks, release the lock */ |
| 82 | bcmos_mutex_unlock(&lock->lock); |
| 83 | /* wait for the signal from the last reader before trying again */ |
| 84 | bcmos_sem_wait(&lock->write_lock, BCMOS_WAIT_FOREVER); |
| 85 | /* lock the actual lock and check for readers again */ |
| 86 | bcmos_mutex_lock(&lock->lock); |
| 87 | } |
| 88 | /* no more readers, allow new readers to wait on the lock */ |
| 89 | bcmos_mutex_unlock(&lock->read_lock); |
| 90 | } |
| 91 | /*lint +e{454} suppress "thread mutex has not been locked" */ |
| 92 | |
| 93 | void bcmos_rw_write_release(bcmos_rw_lock* lock) |
| 94 | { |
| 95 | /*lint --e{455} suppress "thread mutex has not been locked" */ |
| 96 | bcmos_mutex_unlock(&lock->lock); |
| 97 | } |
| 98 | |
| 99 | void bcmos_rw_read_lock(bcmos_rw_lock* lock) |
| 100 | { |
| 101 | /* wait for anyone trying to get a write lock */ |
| 102 | bcmos_mutex_lock(&lock->read_lock); |
| 103 | /* wait for the lock to be released */ |
| 104 | bcmos_mutex_lock(&lock->lock); |
| 105 | lock->readers++; |
| 106 | /* reset the signal to the writers */ |
| 107 | bcmos_sem_wait(&lock->write_lock, BCMOS_NO_WAIT); |
| 108 | /* all done, release everything so other readers can get a read lock */ |
| 109 | bcmos_mutex_unlock(&lock->lock); |
| 110 | bcmos_mutex_unlock(&lock->read_lock); |
| 111 | } |
| 112 | |
| 113 | void bcmos_rw_read_release(bcmos_rw_lock* lock) |
| 114 | { |
| 115 | /* get a lock to prevent interruptions */ |
| 116 | bcmos_mutex_lock(&lock->lock); |
| 117 | if (--lock->readers == 0) |
| 118 | { |
| 119 | /* if we're the last reader, signal that the lock is available for a writer */ |
| 120 | bcmos_sem_post(&lock->write_lock); |
| 121 | } |
| 122 | bcmos_mutex_unlock(&lock->lock); |
| 123 | } |
| 124 | |