| 1 | /* |
|---|
| 2 | * rwlock.h - kernel read/write lock definition. |
|---|
| 3 | * |
|---|
| 4 | * Author Alain Greiner (2016) |
|---|
| 5 | * |
|---|
| 6 | * Copyright (c) UPMC Sorbonne Universites |
|---|
| 7 | * |
|---|
| 8 | * This file is part of ALMOS-MKH. |
|---|
| 9 | * |
|---|
| 10 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
|---|
| 11 | * under the terms of the GNU General Public License as published by |
|---|
| 12 | * the Free Software Foundation; version 2.0 of the License. |
|---|
| 13 | * |
|---|
| 14 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
|---|
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 17 | * General Public License for more details. |
|---|
| 18 | * |
|---|
| 19 | * You should have received a copy of the GNU General Public License |
|---|
| 20 | * along with ALMOS-kernel; if not, write to the Free Software Foundation, |
|---|
| 21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 22 | */ |
|---|
| 23 | |
|---|
| 24 | #ifndef _RWLOCK_H_ |
|---|
| 25 | #define _RWLOCK_H_ |
|---|
| 26 | |
|---|
| 27 | #include <kernel_config.h> |
|---|
| 28 | #include <hal_types.h> |
|---|
| 29 | |
|---|
| 30 | /******************************************************************************************* |
|---|
| 31 | * This structure defines a local rwlock, that supports several simultaneous read |
|---|
| 32 | * accesses, but only one write access. It implements a ticket based allocation policy. |
|---|
| 33 | * Both readers and writers must take a ticket before doing anything else, and access |
|---|
| 34 | * are done in same order as requests (for both read an write ). |
|---|
| 35 | * - A reader take the lock to atomically increments the registered readers count. |
|---|
| 36 | * Then it release the lock and access the protected structure. It atomically decrement |
|---|
| 37 | * the readers count without taking the lock when access is completed. |
|---|
| 38 | * - A writer take the lock and keep it, but must wait completion of all current read |
|---|
| 39 | * accesses before starting its own access. |
|---|
| 40 | * As this local lock is only accessed by the local threads, if the lock is taken, |
|---|
| 41 | * the new-comers use a busy waiting policy with a delay between retry. |
|---|
| 42 | * TODO : Introduce the rwlocks in the list of locks taken by a given thread for debug. |
|---|
| 43 | ******************************************************************************************/ |
|---|
| 44 | |
|---|
| 45 | /**** Forward declarations ****/ |
|---|
| 46 | |
|---|
| 47 | struct thread_s; |
|---|
| 48 | |
|---|
| 49 | /******************************************************************************************* |
|---|
| 50 | * This structure defines a local rwlock. |
|---|
| 51 | * The "owner" and "list" fields are used for debug. |
|---|
| 52 | ******************************************************************************************/ |
|---|
| 53 | |
|---|
| 54 | typedef struct rwlock_s |
|---|
| 55 | { |
|---|
| 56 | uint32_t ticket; /*! first free ticket index */ |
|---|
| 57 | uint32_t current; /*! ticket index of current owner */ |
|---|
| 58 | uint32_t count; /*! number of simultaneous readers threads */ |
|---|
| 59 | |
|---|
| 60 | #if CONFIG_LOCKS_DEBUG |
|---|
| 61 | struct thread_s * owner; /*! pointer on curent writer thread */ |
|---|
| 62 | list_entry_t list; /*! member of list of locks taken by owner */ |
|---|
| 63 | #endif |
|---|
| 64 | |
|---|
| 65 | } |
|---|
| 66 | rwlock_t; |
|---|
| 67 | |
|---|
| 68 | /******************************************************************************************* |
|---|
| 69 | * This function initializes a local rwlock. |
|---|
| 70 | ******************************************************************************************* |
|---|
| 71 | * @ lock : pointer on rwlock |
|---|
| 72 | ******************************************************************************************/ |
|---|
| 73 | void rwlock_init( rwlock_t * lock ); |
|---|
| 74 | |
|---|
| 75 | /******************************************************************************************* |
|---|
| 76 | * This function get access to a local rwlock for a reader. |
|---|
| 77 | ******************************************************************************************* |
|---|
| 78 | * @ lock : pointer on rwlock |
|---|
| 79 | ******************************************************************************************/ |
|---|
| 80 | void rwlock_rd_lock( rwlock_t * lock ); |
|---|
| 81 | |
|---|
| 82 | /******************************************************************************************* |
|---|
| 83 | * This function get access to a local rwlock for a writer. |
|---|
| 84 | ******************************************************************************************* |
|---|
| 85 | * @ lock : pointer on rwlock |
|---|
| 86 | ******************************************************************************************/ |
|---|
| 87 | void rwlock_wr_lock( rwlock_t * lock ); |
|---|
| 88 | |
|---|
| 89 | /******************************************************************************************* |
|---|
| 90 | * This function unlocks a local rwlock for a reader. |
|---|
| 91 | ******************************************************************************************* |
|---|
| 92 | * @ lock : pointer on rwlock |
|---|
| 93 | ******************************************************************************************/ |
|---|
| 94 | void rwlock_rd_unlock( rwlock_t * lock ); |
|---|
| 95 | |
|---|
| 96 | /******************************************************************************************* |
|---|
| 97 | * This function unlocks a local rwlock for a writer. |
|---|
| 98 | ******************************************************************************************* |
|---|
| 99 | * @ lock : pointer on rwlock |
|---|
| 100 | ******************************************************************************************/ |
|---|
| 101 | void rwlock_wr_unlock( rwlock_t * lock ); |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | #endif /* _RWLOCK_H_ */ |
|---|