1 | /* |
---|
2 | * remote_rwlock.h - kernel remote_rwlock definition. |
---|
3 | * |
---|
4 | * Authors 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-MKH; if not, write to the Free Software Foundation, |
---|
21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
22 | */ |
---|
23 | |
---|
24 | #ifndef _REMOTE_RWLOCK_H_ |
---|
25 | #define _REMOTE_RWLOCK_H_ |
---|
26 | |
---|
27 | #include <almos_config.h> |
---|
28 | #include <hal_types.h> |
---|
29 | #include <xlist.h> |
---|
30 | |
---|
31 | /*************************************************************************************** |
---|
32 | * This structure defines a remote rwdlock,that supports several simultaneous read |
---|
33 | * accesses, but only one write access. It implements a ticket based allocation policy. |
---|
34 | * It can be used to synchronize threads running in different clusters, because |
---|
35 | * all access functions use remote pointers. |
---|
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 |
---|
38 | * decrements 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 | * When the lock is taken by another thread, the new-comers use a busy waiting policy. |
---|
42 | * |
---|
43 | * TODO This could be replaced by a descheduling policy and a threads waiting queue |
---|
44 | * implemented in the lock itself: each thread releasing the lock (i.e. incrementing |
---|
45 | * the current field) activates the first waiting thread... |
---|
46 | **************************************************************************************/ |
---|
47 | |
---|
48 | typedef struct remote_rwlock_s |
---|
49 | { |
---|
50 | uint32_t ticket; /*! first free ticket index */ |
---|
51 | uint32_t current; /*! ticket index of current owner */ |
---|
52 | uint32_t count; /*! current number of reader threads */ |
---|
53 | xptr_t owner; /*! extended pointer on writer thread */ |
---|
54 | } |
---|
55 | remote_rwlock_t; |
---|
56 | |
---|
57 | /*************************************************************************************** |
---|
58 | * This function initializes a remote rwlock. |
---|
59 | *************************************************************************************** |
---|
60 | * @ lock_xp : extended pointer on the remote rwlock |
---|
61 | **************************************************************************************/ |
---|
62 | void remote_rwlock_init( xptr_t lock_xp ); |
---|
63 | |
---|
64 | /*************************************************************************************** |
---|
65 | * This blocking function get access to a remote rwlock for a reader. |
---|
66 | * It increments the calling thread locks count when the lock has been taken. |
---|
67 | *************************************************************************************** |
---|
68 | * @ lock_xp : extended pointer on the remote rwlock |
---|
69 | **************************************************************************************/ |
---|
70 | void remote_rwlock_rd_lock( xptr_t lock_xp ); |
---|
71 | |
---|
72 | /*************************************************************************************** |
---|
73 | * This function releases a remote rwlock for a reader. |
---|
74 | * It decrements the calling thread locks count when the lock has been released. |
---|
75 | *************************************************************************************** |
---|
76 | * @ lock_xp : extended pointer on the remote rwlock |
---|
77 | **************************************************************************************/ |
---|
78 | void remote_rwlock_rd_unlock( xptr_t lock_xp ); |
---|
79 | |
---|
80 | /*************************************************************************************** |
---|
81 | * This blocking function get access to a remote rwlock for a writer. |
---|
82 | * It increments the calling thread locks count when the lock has been taken. |
---|
83 | *************************************************************************************** |
---|
84 | * @ lock_xp : extended pointer on the remote rwlock |
---|
85 | **************************************************************************************/ |
---|
86 | void remote_rwlock_wr_lock( xptr_t lock_xp ); |
---|
87 | |
---|
88 | /*************************************************************************************** |
---|
89 | * This function releases a remote rwlock for a writer. |
---|
90 | * It decrements the calling thread locks count when the lock has been released. |
---|
91 | *************************************************************************************** |
---|
92 | * @ lock_xp : extended pointer on the remote rwlock |
---|
93 | **************************************************************************************/ |
---|
94 | void remote_rwlock_wr_unlock( xptr_t lock_xp ); |
---|
95 | |
---|
96 | #endif |
---|