root/tags/rel-2-9-14/pdns/pdns/lock.hh @ 216

Revision 216, 2.8 KB (checked in by anonymous, 9 years ago)

This commit was manufactured by cvs2svn to create tag 'rel-2-9-14'.

  • Property svn:eol-style set to native
  • Property svn:keywords set to author date id revision
Line 
1/*
2    PowerDNS Versatile Database Driven Nameserver
3    Copyright (C) 2002  PowerDNS.COM BV
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18*/
19#ifndef LOCK_HH
20#define LOCK_HH
21
22#include <pthread.h>
23#include <errno.h>
24#include "misc.hh"
25#include "ahuexception.hh"
26class Lock
27{
28  pthread_mutex_t *d_lock;
29public:
30
31  Lock(pthread_mutex_t *lock) : d_lock(lock)
32  {
33    if((errno=pthread_mutex_lock(d_lock)))
34      throw AhuException("error acquiring lock: "+stringerror());
35  }
36  ~Lock()
37  {
38    pthread_mutex_unlock(d_lock);
39  }
40};
41
42class WriteLock
43{
44  pthread_rwlock_t *d_lock;
45public:
46
47  WriteLock(pthread_rwlock_t *lock) : d_lock(lock)
48  {
49    if((errno=pthread_rwlock_wrlock(d_lock))) {
50      throw AhuException("error acquiring rwlock wrlock: "+stringerror());
51    }
52  }
53  ~WriteLock()
54  {
55    pthread_rwlock_unlock(d_lock);
56  }
57};
58
59class TryWriteLock
60{
61  pthread_rwlock_t *d_lock;
62  bool d_havelock;
63public:
64
65  TryWriteLock(pthread_rwlock_t *lock) : d_lock(lock)
66  {
67    d_havelock=false;
68    if((errno=pthread_rwlock_trywrlock(d_lock)) && errno!=EBUSY)
69      throw AhuException("error acquiring rwlock tryrwlock: "+stringerror());
70    d_havelock=(errno==0);
71  }
72  ~TryWriteLock()
73  {
74    if(d_havelock)
75      pthread_rwlock_unlock(d_lock);
76  }
77  bool gotIt()
78  {
79    return d_havelock;
80  }
81};
82
83class TryReadLock
84{
85  pthread_rwlock_t *d_lock;
86  bool d_havelock;
87public:
88
89  TryReadLock(pthread_rwlock_t *lock) : d_lock(lock)
90  {
91   
92    d_havelock=false;
93    if((errno=pthread_rwlock_tryrdlock(d_lock)) && errno!=EBUSY)
94      throw AhuException("error acquiring rwlock tryrdlock: "+stringerror());
95    d_havelock=(errno==0);
96  }
97  ~TryReadLock()
98  {
99    if(d_havelock)
100      pthread_rwlock_unlock(d_lock);
101  }
102  bool gotIt()
103  {
104    return d_havelock;
105  }
106};
107
108
109class ReadLock
110{
111  pthread_rwlock_t *d_lock;
112public:
113
114  ReadLock(pthread_rwlock_t *lock) : d_lock(lock)
115  {
116    if((errno=pthread_rwlock_rdlock(d_lock)))
117      throw AhuException("error acquiring rwlock tryrwlock: "+stringerror());
118  }
119  ~ReadLock()
120  {
121    pthread_rwlock_unlock(d_lock);
122  }
123 
124  void upgrade()
125  {
126    pthread_rwlock_unlock(d_lock);
127    pthread_rwlock_wrlock(d_lock);
128  }
129
130};
131
132
133#endif
Note: See TracBrowser for help on using the browser.