1 | /* |
---|
2 | * rwlock.h - kernel read/write lock definition. |
---|
3 | * |
---|
4 | * Author Alain Greiner (2016,2017,2018) |
---|
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_kernel_types.h> |
---|
29 | #include <list.h> |
---|
30 | |
---|
31 | /******************************************************************************************* |
---|
32 | * This structure defines a local rwlock, that supports several simultaneous read |
---|
33 | * accesses, but only one write access. It implements a ticket based allocation policy. |
---|
34 | * Both readers and writers must take a ticket before doing anything else, and access |
---|
35 | * are done in same order as requests (for both read an write ). |
---|
36 | * - A reader take the lock to atomically increments the registered readers count. |
---|
37 | * Then it release the lock and access the protected structure. It atomically decrement |
---|
38 | * the readers count without taking the lock when access is completed. |
---|
39 | * - A writer take the lock and keep it, but must wait completion of all current read |
---|
40 | * accesses before starting its own access. |
---|
41 | * As this local lock is only accessed by the local threads, if the lock is taken, |
---|
42 | * the new-comers use a busy waiting policy with a delay between retry. |
---|
43 | * TODO : Introduce the rwlocks in the list of locks taken by a given thread for debug. |
---|
44 | ******************************************************************************************/ |
---|
45 | |
---|
46 | /**** Forward declarations ****/ |
---|
47 | |
---|
48 | struct thread_s; |
---|
49 | |
---|
50 | /******************************************************************************************* |
---|
51 | * This structure defines a local rwlock. |
---|
52 | * The "owner" and "list" fields are used for debug. |
---|
53 | ******************************************************************************************/ |
---|
54 | |
---|
55 | typedef struct rwlock_s |
---|
56 | { |
---|
57 | uint32_t ticket; /*! first free ticket index */ |
---|
58 | uint32_t current; /*! ticket index of current owner */ |
---|
59 | uint32_t count; /*! number of simultaneous readers threads */ |
---|
60 | |
---|
61 | #if DEBUG_RWLOCKS |
---|
62 | struct thread_s * owner; /*! pointer on curent writer thread */ |
---|
63 | list_entry_t list; /*! member of list of locks taken by owner */ |
---|
64 | #endif |
---|
65 | |
---|
66 | } |
---|
67 | rwlock_t; |
---|
68 | |
---|
69 | /******************************************************************************************* |
---|
70 | * This function initializes a local rwlock. |
---|
71 | ******************************************************************************************* |
---|
72 | * @ lock : pointer on rwlock |
---|
73 | ******************************************************************************************/ |
---|
74 | void rwlock_init( rwlock_t * lock ); |
---|
75 | |
---|
76 | /******************************************************************************************* |
---|
77 | * This function get access to a local rwlock for a reader. |
---|
78 | ******************************************************************************************* |
---|
79 | * @ lock : pointer on rwlock |
---|
80 | ******************************************************************************************/ |
---|
81 | void rwlock_rd_lock( rwlock_t * lock ); |
---|
82 | |
---|
83 | /******************************************************************************************* |
---|
84 | * This function get access to a local rwlock for a writer. |
---|
85 | ******************************************************************************************* |
---|
86 | * @ lock : pointer on rwlock |
---|
87 | ******************************************************************************************/ |
---|
88 | void rwlock_wr_lock( rwlock_t * lock ); |
---|
89 | |
---|
90 | /******************************************************************************************* |
---|
91 | * This function unlocks a local rwlock for a reader. |
---|
92 | ******************************************************************************************* |
---|
93 | * @ lock : pointer on rwlock |
---|
94 | ******************************************************************************************/ |
---|
95 | void rwlock_rd_unlock( rwlock_t * lock ); |
---|
96 | |
---|
97 | /******************************************************************************************* |
---|
98 | * This function unlocks a local rwlock for a writer. |
---|
99 | ******************************************************************************************* |
---|
100 | * @ lock : pointer on rwlock |
---|
101 | ******************************************************************************************/ |
---|
102 | void rwlock_wr_unlock( rwlock_t * lock ); |
---|
103 | |
---|
104 | |
---|
105 | #endif /* _RWLOCK_H_ */ |
---|