1 /***********************license start***************
2 * Copyright (c) 2003-2010 Cavium Networks (support@cavium.com). All rights
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
18 * * Neither the name of Cavium Networks nor the names of
19 * its contributors may be used to endorse or promote products
20 * derived from this software without specific prior written
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
28 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
29 * AND WITH ALL FAULTS AND CAVIUM NETWORKS 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**************************************/
49 * This file provides reader/writer locks.
51 * <hr>$Revision: 49448 $<hr>
57 #ifndef __CVMX_RWLOCK_H__
58 #define __CVMX_RWLOCK_H__
60 /* include to get atomic compare and store */
61 #include "cvmx-atomic.h"
67 /* Flags for lock value in rw lock structure */
68 #define CVMX_RWLOCK_WRITE_FLAG 0x1
69 #define CVMX_RWLOCK_READ_INC 0x2
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.
77 volatile uint32_t lock;
78 volatile uint32_t write_req;
79 volatile uint32_t write_comp;
80 } cvmx_rwlock_wp_lock_t;
83 * Initialize a reader/writer lock. This must be done
84 * by a single core before used.
86 * @param lock pointer to rwlock structure
88 static inline void cvmx_rwlock_wp_init(cvmx_rwlock_wp_lock_t *lock)
96 * Perform a reader lock. If a writer is pending, this
97 * will wait for that writer to complete before locking.
99 * NOTE: Each thread/process must only lock any rwlock
100 * once, or else a deadlock may result.
102 * @param lock pointer to rwlock structure
104 static inline void cvmx_rwlock_wp_read_lock(cvmx_rwlock_wp_lock_t *lock)
107 /* Wait for outstanding write requests to be serviced */
108 while (lock->write_req != lock->write_comp)
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
116 while (lock->lock & CVMX_RWLOCK_WRITE_FLAG)
122 * Perform a reader unlock.
124 * @param lock pointer to rwlock structure
126 static inline void cvmx_rwlock_wp_read_unlock(cvmx_rwlock_wp_lock_t *lock)
128 /* Remove ourselves to reader count */
129 cvmx_atomic_add32_nosync((int32_t *)&(lock->lock), -CVMX_RWLOCK_READ_INC);
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.
139 * @param lock pointer to rwlock structure
141 static inline void cvmx_rwlock_wp_write_lock(cvmx_rwlock_wp_lock_t *lock)
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)
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))
154 * Perform a writer unlock.
156 * @param lock pointer to rwlock structure
158 static inline void cvmx_rwlock_wp_write_unlock(cvmx_rwlock_wp_lock_t *lock)
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 */
171 #endif /* __CVMX_RWLOCK_H__ */