3 * @brief read - write spin lock class.
4 * @brief this lock used CPU power upon blocking.
5 * @brief don't used contextswitch. little clitical section use.
7 * L7VSD: Linux Virtual Server for Layer7 Load Balancing
8 * Copyright (C) 2009 NTT COMWARE Corporation.
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
25 **********************************************************************/
31 #include <boost/noncopyable.hpp>
36 //! read - write mutex class
37 //! non copy class. nonPOD.
38 //! don't double lock this mutex.
39 class wr_mutex : private boost::noncopyable
42 pthread_rwlock_t mutex; //! mutex
44 //! default constractor
46 pthread_rwlock_init(&mutex, NULL);
50 pthread_rwlock_destroy(&mutex);
52 //! reaqd lock function. non-blocking read threads.
53 //! be blocked write thread mutex.
55 pthread_rwlock_rdlock(&mutex);
57 //! read - write lock function. blocking all rdlock and wrlock
59 pthread_rwlock_wrlock(&mutex);
61 //! unlock funtion. unlock rdlock and wrlock.
63 pthread_rwlock_unlock(&mutex);
68 //! readable scoped lock class
70 //! l7vs::rw_mutex mtx;
71 //! { <---- readable lock zone start
72 //! l7vs]::rd_scoped_lock lock( mtx );
73 //! } <---- readable lock zone end.
75 class read_scoped_lock : private boost::noncopyable
78 T &mutex; //! mutex reference.
80 //! constractor. use explicit keyword.
81 explicit read_scoped_lock(T &m) : mutex(m) {
90 //! using wr_mutex using template typedef.
91 typedef read_scoped_lock<wr_mutex> rd_scoped_lock;
94 //! read - write scoped lock.
96 //! l7vs::wr_mutex mtx;
97 //! { <---- read - write lock zone start. all using mutex blocking.
98 //! l7vs::wr_scoped_lock lock( mtx );
101 class readwrite_scoped_lock : private boost::noncopyable
104 T &mutex; //! mutex reference.
106 //! default constractor.
107 explicit readwrite_scoped_lock(T &m) : mutex(m) {
110 //! default destractor
111 ~readwrite_scoped_lock() {
116 //! using wr_mutex using template typedef.
117 typedef readwrite_scoped_lock<wr_mutex> rw_scoped_lock;