source: trunk/kernel/libk/rwlock.h @ 136

Last change on this file since 136 was 14, checked in by alain, 8 years ago

Bugs fix.

File size: 4.6 KB
Line 
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 ******************************************************************************************/
43
44/****     Forward declarations    ****/
45
46struct thread_s;
47
48/*******************************************************************************************
49 * This structure defines a local rwlock.
50 ******************************************************************************************/
51
52typedef struct rwlock_s
53{
54        uint32_t            ticket;           /*! first free ticket index                     */
55    uint32_t            current;          /*! ticket index of current owner               */
56    uint32_t            count;            /*! number of simultaneous readers threads      */
57        struct thread_s   * owner;            /*! pointer on curent writer thread             */
58}
59rwlock_t;
60
61/*******************************************************************************************
62 * This function initializes a local rwlock.
63 *******************************************************************************************
64 * @ lock       : pointer on rwlock
65 ******************************************************************************************/
66void rwlock_init( rwlock_t * lock );
67
68/*******************************************************************************************
69 * This function get access to a local rwlock for a reader.
70 *******************************************************************************************
71 * @ lock       : pointer on rwlock
72 ******************************************************************************************/
73void rwlock_rd_lock( rwlock_t * lock );
74
75/*******************************************************************************************
76 * This function get access to a local rwlock for a writer.
77 *******************************************************************************************
78 * @ lock       : pointer on rwlock
79 ******************************************************************************************/
80void rwlock_wr_lock( rwlock_t * lock );
81
82/*******************************************************************************************
83 * This function unlocks a local rwlock for a reader.
84 *******************************************************************************************
85 * @ lock       : pointer on rwlock
86 ******************************************************************************************/
87void rwlock_rd_unlock( rwlock_t * lock );
88
89/*******************************************************************************************
90 * This function unlocks a local rwlock for a writer.
91 *******************************************************************************************
92 * @ lock       : pointer on rwlock
93 ******************************************************************************************/
94void rwlock_wr_unlock( rwlock_t * lock );
95
96
97#endif  /* _RWLOCK_H_ */
Note: See TracBrowser for help on using the repository browser.