aboutsummaryrefslogtreecommitdiff
path: root/cvmx-rwlock.h
blob: d236d1990b5cd49bbcab19d1b6c767984d130ca6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/***********************license start***************
 * Copyright (c) 2003-2010  Cavium Inc. (support@cavium.com). All rights
 * reserved.
 *
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   * Redistributions in binary form must reproduce the above
 *     copyright notice, this list of conditions and the following
 *     disclaimer in the documentation and/or other materials provided
 *     with the distribution.

 *   * Neither the name of Cavium Inc. nor the names of
 *     its contributors may be used to endorse or promote products
 *     derived from this software without specific prior written
 *     permission.

 * This Software, including technical data, may be subject to U.S. export  control
 * laws, including the U.S. Export Administration Act and its  associated
 * regulations, and may be subject to export or import  regulations in other
 * countries.

 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
 * AND WITH ALL FAULTS AND CAVIUM INC. MAKES NO PROMISES, REPRESENTATIONS OR
 * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO
 * THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY REPRESENTATION OR
 * DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT DEFECTS, AND CAVIUM
 * SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES OF TITLE,
 * MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, LACK OF
 * VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION OR
 * CORRESPONDENCE TO DESCRIPTION. THE ENTIRE  RISK ARISING OUT OF USE OR
 * PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
 ***********************license end**************************************/







/**
 * @file
 *
 * This file provides reader/writer locks.
 *
 * <hr>$Revision: 70030 $<hr>
 *
 *
 */


#ifndef __CVMX_RWLOCK_H__
#define __CVMX_RWLOCK_H__

/* include to get atomic compare and store */
#include "cvmx-atomic.h"

#ifdef	__cplusplus
extern "C" {
#endif

/* Flags for lock value in rw lock structure */
#define CVMX_RWLOCK_WRITE_FLAG     0x1
#define CVMX_RWLOCK_READ_INC       0x2


/* Writer preference locks (wp).  Can be starved by writers.  When a writer
 * is waiting, no readers are given the lock until all writers are done.
 */
typedef struct
{
    volatile uint32_t lock;
    volatile uint32_t write_req;
    volatile uint32_t write_comp;
} cvmx_rwlock_wp_lock_t;

/**
 * Initialize a reader/writer lock.  This must be done
 * by a single core before used.
 *
 * @param lock   pointer to rwlock structure
 */
static inline void cvmx_rwlock_wp_init(cvmx_rwlock_wp_lock_t *lock)
{
    lock->lock = 0;
    lock->write_req = 0;
    lock->write_comp = 0;
}

/**
 * Perform a reader lock.  If a writer is pending, this
 * will wait for that writer to complete before locking.
 *
 * NOTE: Each thread/process must only lock any rwlock
 * once, or else a deadlock may result.
 *
 * @param lock   pointer to rwlock structure
 */
static inline void cvmx_rwlock_wp_read_lock(cvmx_rwlock_wp_lock_t *lock)
{

    /* Wait for outstanding write requests to be serviced */
    while (lock->write_req != lock->write_comp)
        ;
    /* Add ourselves to interested reader count */
    cvmx_atomic_add32_nosync((int32_t *)&(lock->lock), CVMX_RWLOCK_READ_INC);
    /* Wait for writer to finish.  No writer will start again
    ** until after we are done since we have already incremented
    ** the reader count
    */
    while (lock->lock & CVMX_RWLOCK_WRITE_FLAG)
        ;

}

/**
 * Perform a reader unlock.
 *
 * @param lock   pointer to rwlock structure
 */
static inline void cvmx_rwlock_wp_read_unlock(cvmx_rwlock_wp_lock_t *lock)
{
    /* Remove ourselves to reader count */
    cvmx_atomic_add32_nosync((int32_t *)&(lock->lock), -CVMX_RWLOCK_READ_INC);
}

/**
 * Perform a writer lock.  Any readers that attempt
 * to get a lock while there are any pending write locks
 * will wait until all writers have completed.  Starvation
 * of readers by writers is possible and must be avoided
 * by the application.
 *
 * @param lock   pointer to rwlock structure
 */
static inline void cvmx_rwlock_wp_write_lock(cvmx_rwlock_wp_lock_t *lock)
{
    /* Get previous value of write requests */
    uint32_t prev_writers = ((uint32_t)cvmx_atomic_fetch_and_add32((int32_t *)&(lock->write_req), 1));
    /* Spin until our turn */
    while (prev_writers != lock->write_comp)
        ;
    /* Spin until no other readers or writers, then set write flag */
    while (!cvmx_atomic_compare_and_store32((uint32_t *)&(lock->lock), 0, CVMX_RWLOCK_WRITE_FLAG))
        ;

}
/**
 * Perform a writer unlock.
 *
 * @param lock   pointer to rwlock structure
 */
static inline void cvmx_rwlock_wp_write_unlock(cvmx_rwlock_wp_lock_t *lock)
{
    /* Remove our writer flag */
    CVMX_SYNCWS;  /* Make sure all writes in protected region are visible before unlock */
    cvmx_atomic_add32_nosync((int32_t *)&(lock->lock), -CVMX_RWLOCK_WRITE_FLAG);
    cvmx_atomic_add32_nosync((int32_t *)&(lock->write_comp), 1);
    CVMX_SYNCWS;  /* push unlock writes out, but don't stall */
}

#ifdef	__cplusplus
}
#endif

#endif /* __CVMX_RWLOCK_H__ */