Tamer
C++ language extensions for event-driven programming
Loading...
Searching...
No Matches
lock.hh
Go to the documentation of this file.
1#ifndef TAMER_LOCK_HH
2#define TAMER_LOCK_HH 1
3/* Copyright (c) 2007-2015, Eddie Kohler
4 * Copyright (c) 2007, Regents of the University of California
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, subject to the conditions
9 * listed in the Tamer LICENSE file. These conditions include: you must
10 * preserve this copyright notice, and you cannot mention the copyright
11 * holders in advertising related to the Software without their permission.
12 * The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This
13 * notice is a summary of the Tamer LICENSE file; the license in that file is
14 * legally binding.
15 */
16#include <tamer/event.hh>
17namespace tamer {
18
22
23class mutex {
24 public:
25 inline mutex();
26
27 inline void acquire(event<> done);
28 inline void release();
29
30 inline void acquire_shared(event<> done);
31 inline void release_shared();
32
33 private:
34 struct wait {
35 wait **pprev;
36 wait *next;
37 event<> e;
38 };
39
40 int locked_;
41 wait *wait_;
42 wait **wait_tailp_;
43
44 void acquire(int shared, event<> done);
45 class closure__acquire__iQ_;
46 void acquire(closure__acquire__iQ_ &);
47
48 void wake() {
49 if (wait_)
50 wait_->e.trigger();
51 }
52
53 mutex(const mutex &);
54 mutex &operator=(const mutex &);
55};
56
58inline mutex::mutex()
59 : locked_(0), wait_(), wait_tailp_(&wait_) {
60}
61
67inline void mutex::acquire(event<> done) {
68 acquire(-1, done);
69}
70
75inline void mutex::release() {
76 assert(locked_ == -1);
77 ++locked_;
78 wake();
79}
80
86inline void mutex::acquire_shared(event<> done) {
87 acquire(1, done);
88}
89
94inline void mutex::release_shared() {
95 assert(locked_ != -1 && locked_ != 0);
96 --locked_;
97 wake();
98}
99
100} /* namespace tamer */
101#endif /* TAMER_LOCK_HH */
The event template classes and helper functions.
Namespace containing public Tamer classes and functions for the Tamer core.
Definition adapter.hh:17