OSDN Git Service

formatted all files with 'astyle -A8 -HUpc -k3 -z2 -r ./*.cpp ./*.c ./*.h'
[ultramonkey-l7/ultramonkey-l7-v3.git] / l7vsd / include / wrlock.h
1 /*!
2  *    @file    utility.h
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.
6  *
7  * L7VSD: Linux Virtual Server for Layer7 Load Balancing
8  * Copyright (C) 2009  NTT COMWARE Corporation.
9  *
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.
14  *
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.
19  *
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
23  * 02110-1301 USA
24  *
25  **********************************************************************/
26
27 #ifndef    WRLOCK_H
28 #define    WRLOCK_H
29
30 #include <pthread.h>
31 #include <boost/noncopyable.hpp>
32
33 namespace l7vs
34 {
35
36 //! read - write mutex class
37 //! non copy class. nonPOD.
38 //! don't double lock this mutex.
39 class    wr_mutex : private boost::noncopyable
40 {
41 protected:
42         pthread_rwlock_t    mutex;    //! mutex
43 public:
44         //! default constractor
45         wr_mutex() {
46                 pthread_rwlock_init(&mutex, NULL);
47         }
48         //! destractor.
49         ~wr_mutex() {
50                 pthread_rwlock_destroy(&mutex);
51         }
52         //! reaqd lock function. non-blocking read threads.
53         //! be blocked write thread mutex.
54         void    rdlock() {
55                 pthread_rwlock_rdlock(&mutex);
56         }
57         //! read - write lock function. blocking all rdlock and wrlock
58         void    wrlock() {
59                 pthread_rwlock_wrlock(&mutex);
60         }
61         //! unlock funtion. unlock rdlock and wrlock.
62         void    unlock() {
63                 pthread_rwlock_unlock(&mutex);
64         }
65 };
66
67
68 //!    readable scoped lock class
69 //!    usage:
70 //!        l7vs::rw_mutex    mtx;
71 //!        {    <---- readable lock zone start
72 //!            l7vs]::rd_scoped_lock    lock( mtx );
73 //!        }    <---- readable lock zone end.
74 template< class T >
75 class    read_scoped_lock : private boost::noncopyable
76 {
77 protected:
78         T    &mutex;    //! mutex reference.
79 public:
80         //!    constractor. use explicit keyword.
81         explicit read_scoped_lock(T &m) : mutex(m) {
82                 mutex.rdlock();
83         }
84         //! destractor.
85         ~read_scoped_lock() {
86                 mutex.unlock();
87         }
88 };
89
90 //! using wr_mutex using template typedef.
91 typedef    read_scoped_lock<wr_mutex>    rd_scoped_lock;
92
93
94 //! read - write scoped lock.
95 //! usage:
96 //!    l7vs::wr_mutex    mtx;
97 //!    {    <---- read - write lock zone start. all using mutex blocking.
98 //!        l7vs::wr_scoped_lock    lock( mtx );
99 //!    }
100 template< class T >
101 class    readwrite_scoped_lock : private boost::noncopyable
102 {
103 protected:
104         T    &mutex;                //! mutex reference.
105 public:
106         //! default constractor.
107         explicit readwrite_scoped_lock(T &m) : mutex(m) {
108                 mutex.wrlock();
109         }
110         //! default destractor
111         ~readwrite_scoped_lock() {
112                 mutex.unlock();
113         }
114 };
115
116 //! using wr_mutex using template typedef.
117 typedef    readwrite_scoped_lock<wr_mutex>    rw_scoped_lock;
118
119 }    //namespace l7vs
120 #endif    // WRLOCK