1/***********************license start***************
2 * Copyright (c) 2003-2010  Cavium Inc. (support@cavium.com). All rights
3 * reserved.
4 *
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 *   * Redistributions of source code must retain the above copyright
11 *     notice, this list of conditions and the following disclaimer.
12 *
13 *   * Redistributions in binary form must reproduce the above
14 *     copyright notice, this list of conditions and the following
15 *     disclaimer in the documentation and/or other materials provided
16 *     with the distribution.
17
18 *   * Neither the name of Cavium Inc. nor the names of
19 *     its contributors may be used to endorse or promote products
20 *     derived from this software without specific prior written
21 *     permission.
22
23 * This Software, including technical data, may be subject to U.S. export  control
24 * laws, including the U.S. Export Administration Act and its  associated
25 * regulations, and may be subject to export or import  regulations in other
26 * countries.
27
28 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
29 * AND WITH ALL FAULTS AND CAVIUM INC. MAKES NO PROMISES, REPRESENTATIONS OR
30 * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO
31 * THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY REPRESENTATION OR
32 * DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT DEFECTS, AND CAVIUM
33 * SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES OF TITLE,
34 * MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, LACK OF
35 * VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION OR
36 * CORRESPONDENCE TO DESCRIPTION. THE ENTIRE  RISK ARISING OUT OF USE OR
37 * PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
38 ***********************license end**************************************/
39
40
41
42
43
44
45
46/**
47 * @file
48 *
49 * This file provides reader/writer locks.
50 *
51 * <hr>$Revision: 70030 $<hr>
52 *
53 *
54 */
55
56
57#ifndef __CVMX_RWLOCK_H__
58#define __CVMX_RWLOCK_H__
59
60/* include to get atomic compare and store */
61#include "cvmx-atomic.h"
62
63#ifdef	__cplusplus
64extern "C" {
65#endif
66
67/* Flags for lock value in rw lock structure */
68#define CVMX_RWLOCK_WRITE_FLAG     0x1
69#define CVMX_RWLOCK_READ_INC       0x2
70
71
72/* Writer preference locks (wp).  Can be starved by writers.  When a writer
73 * is waiting, no readers are given the lock until all writers are done.
74 */
75typedef struct
76{
77    volatile uint32_t lock;
78    volatile uint32_t write_req;
79    volatile uint32_t write_comp;
80} cvmx_rwlock_wp_lock_t;
81
82/**
83 * Initialize a reader/writer lock.  This must be done
84 * by a single core before used.
85 *
86 * @param lock   pointer to rwlock structure
87 */
88static inline void cvmx_rwlock_wp_init(cvmx_rwlock_wp_lock_t *lock)
89{
90    lock->lock = 0;
91    lock->write_req = 0;
92    lock->write_comp = 0;
93}
94
95/**
96 * Perform a reader lock.  If a writer is pending, this
97 * will wait for that writer to complete before locking.
98 *
99 * NOTE: Each thread/process must only lock any rwlock
100 * once, or else a deadlock may result.
101 *
102 * @param lock   pointer to rwlock structure
103 */
104static inline void cvmx_rwlock_wp_read_lock(cvmx_rwlock_wp_lock_t *lock)
105{
106
107    /* Wait for outstanding write requests to be serviced */
108    while (lock->write_req != lock->write_comp)
109        ;
110    /* Add ourselves to interested reader count */
111    cvmx_atomic_add32_nosync((int32_t *)&(lock->lock), CVMX_RWLOCK_READ_INC);
112    /* Wait for writer to finish.  No writer will start again
113    ** until after we are done since we have already incremented
114    ** the reader count
115    */
116    while (lock->lock & CVMX_RWLOCK_WRITE_FLAG)
117        ;
118
119}
120
121/**
122 * Perform a reader unlock.
123 *
124 * @param lock   pointer to rwlock structure
125 */
126static inline void cvmx_rwlock_wp_read_unlock(cvmx_rwlock_wp_lock_t *lock)
127{
128    /* Remove ourselves to reader count */
129    cvmx_atomic_add32_nosync((int32_t *)&(lock->lock), -CVMX_RWLOCK_READ_INC);
130}
131
132/**
133 * Perform a writer lock.  Any readers that attempt
134 * to get a lock while there are any pending write locks
135 * will wait until all writers have completed.  Starvation
136 * of readers by writers is possible and must be avoided
137 * by the application.
138 *
139 * @param lock   pointer to rwlock structure
140 */
141static inline void cvmx_rwlock_wp_write_lock(cvmx_rwlock_wp_lock_t *lock)
142{
143    /* Get previous value of write requests */
144    uint32_t prev_writers = ((uint32_t)cvmx_atomic_fetch_and_add32((int32_t *)&(lock->write_req), 1));
145    /* Spin until our turn */
146    while (prev_writers != lock->write_comp)
147        ;
148    /* Spin until no other readers or writers, then set write flag */
149    while (!cvmx_atomic_compare_and_store32((uint32_t *)&(lock->lock), 0, CVMX_RWLOCK_WRITE_FLAG))
150        ;
151
152}
153/**
154 * Perform a writer unlock.
155 *
156 * @param lock   pointer to rwlock structure
157 */
158static inline void cvmx_rwlock_wp_write_unlock(cvmx_rwlock_wp_lock_t *lock)
159{
160    /* Remove our writer flag */
161    CVMX_SYNCWS;  /* Make sure all writes in protected region are visible before unlock */
162    cvmx_atomic_add32_nosync((int32_t *)&(lock->lock), -CVMX_RWLOCK_WRITE_FLAG);
163    cvmx_atomic_add32_nosync((int32_t *)&(lock->write_comp), 1);
164    CVMX_SYNCWS;  /* push unlock writes out, but don't stall */
165}
166
167#ifdef	__cplusplus
168}
169#endif
170
171#endif /* __CVMX_RWLOCK_H__ */
172