tlds
Transactional Operations for Linked Data Structures
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
lock.hpp
Go to the documentation of this file.
1 /**
2  * Copyright (C) 2011
3  * University of Rochester Department of Computer Science
4  * and
5  * Lehigh University Department of Computer Science and Engineering
6  *
7  * License: Modified BSD
8  * Please see the file LICENSE.RSTM for licensing information
9  */
10 
11 /* lock.hpp
12  *
13  * Currently uses pthread mutex, with PTHREAD_MUTEX_RECURSIVE for
14  * re-entrancy.
15  */
16 
17 #ifndef LOCK_HPP__
18 #define LOCK_HPP__
19 
20 #include <pthread.h> // for pthread_mutex_t
21 
22 #include "macros.hpp"
23 
24 class d_lock {
25  pthread_mutex_t mutex;
26 public:
27  void acquire() {
28  VERIFY(pthread_mutex_lock(&mutex));
29  }
30  void release() {
31  VERIFY(pthread_mutex_unlock(&mutex));
32  }
33  d_lock() {
34  pthread_mutexattr_t attrs;
35  VERIFY(pthread_mutexattr_init(&attrs));
36  VERIFY(pthread_mutexattr_settype(&attrs, PTHREAD_MUTEX_RECURSIVE));
37  VERIFY(pthread_mutex_init(&mutex, &attrs));
38  }
39  ~d_lock() {
40  VERIFY(pthread_mutex_destroy(&mutex));
41  }
42 };
43 
44 // Declaring one of these makes a scope a critical section.
45 //
46 class with_lock {
48 public:
50  my_lock = &l;
51  my_lock->acquire();
52  }
54  my_lock->release();
55  }
56 };
57 
58 #endif // LOCK_HPP__
#define VERIFY(E)
Definition: macros.hpp:25
Definition: defs.h:133
void acquire()
Definition: lock.hpp:27
Definition: lock.hpp:24
d_lock * my_lock
Definition: lock.hpp:47
Definition: lock.hpp:46
~with_lock()
Definition: lock.hpp:53
void release()
Definition: lock.hpp:30
pthread_mutex_t mutex
Definition: lock.hpp:25
~d_lock()
Definition: lock.hpp:39
with_lock(d_lock &l)
Definition: lock.hpp:49
d_lock()
Definition: lock.hpp:33
bool set_op int op_size set_t * l
Definition: stmskip.cc:240