1 | /* |
---|
2 | * readlock.h - kernel readlock 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 _READLOCK_H_ |
---|
25 | #define _READLOCK_H_ |
---|
26 | |
---|
27 | #include <kernel_config.h> |
---|
28 | #include <hal_types.h> |
---|
29 | |
---|
30 | /******************************************************************************************* |
---|
31 | * This structure defines a local readlock, that supports several simultaneous read |
---|
32 | * accesses, but only one write access. |
---|
33 | * - A reader does not take the exclusive ownership of the lock, but it must atomically |
---|
34 | * increment the registered readers count in the readlock_t structure before starting |
---|
35 | * access and decrement it when access is completed. |
---|
36 | * - A writer must take exclusive ownership before access, and must wait completion |
---|
37 | * of all current read access before starting its own access. |
---|
38 | * TODO : add a "list" field to register a taken lock in the owner thread list. |
---|
39 | ******************************************************************************************/ |
---|
40 | |
---|
41 | /**** Forward declarations ****/ |
---|
42 | |
---|
43 | struct thread_s; |
---|
44 | |
---|
45 | /******************************************************************************************* |
---|
46 | * This structure defines a local readlock. |
---|
47 | ******************************************************************************************/ |
---|
48 | |
---|
49 | typedef struct readlock_s |
---|
50 | { |
---|
51 | volatile uint32_t taken; /*! state : free if zero / taken if non zero */ |
---|
52 | volatile uint32_t count; /*! current number of readers */ |
---|
53 | struct thread_s * owner; /*! pointer on curent owner thread */ |
---|
54 | } |
---|
55 | readlock_t; |
---|
56 | |
---|
57 | /******************************************************************************************* |
---|
58 | * This function initializes a local readlock. |
---|
59 | ******************************************************************************************* |
---|
60 | * @ lock : pointer on readlock |
---|
61 | ******************************************************************************************/ |
---|
62 | void readlock_init( readlock_t * lock ); |
---|
63 | |
---|
64 | /******************************************************************************************* |
---|
65 | * This function get access to a local readlock for a reader. |
---|
66 | ******************************************************************************************* |
---|
67 | * @ lock : pointer on readlock |
---|
68 | ******************************************************************************************/ |
---|
69 | void readlock_rd_lock( readlock_t * lock ); |
---|
70 | |
---|
71 | /******************************************************************************************* |
---|
72 | * This function get access to a local readlock for a writer. |
---|
73 | ******************************************************************************************* |
---|
74 | * @ lock : pointer on readlock |
---|
75 | ******************************************************************************************/ |
---|
76 | void readlock_wr_lock( readlock_t * lock ); |
---|
77 | |
---|
78 | /******************************************************************************************* |
---|
79 | * This function unlocks a local readlock for a reader. |
---|
80 | ******************************************************************************************* |
---|
81 | * @ lock : pointer on readlock |
---|
82 | ******************************************************************************************/ |
---|
83 | void readlock_rd_unlock( readlock_t * lock ); |
---|
84 | |
---|
85 | /******************************************************************************************* |
---|
86 | * This function unlocks a local readlock for a writer. |
---|
87 | ******************************************************************************************* |
---|
88 | * @ lock : pointer on readlock |
---|
89 | ******************************************************************************************/ |
---|
90 | void readlock_wr_unlock( readlock_t * lock ); |
---|
91 | |
---|
92 | |
---|
93 | #endif /* _READLOCK_H_ */ |
---|