source: mainline/uspace/lib/cpp/src/mutex.cpp@ 9fe2fd7

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9fe2fd7 was b3b8405, checked in by Dzejrou <dzejrou@…>, 7 years ago

cpp: split <mutex> into <mutex> and <shared_mutex>

  • Property mode set to 100644
File size: 4.4 KB
Line 
1/*
2 * Copyright (c) 2018 Jaroslav Jindrak
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <mutex>
30
31namespace std
32{
33 void mutex::lock()
34 {
35 aux::threading::mutex::lock(mtx_);
36 }
37
38 bool mutex::try_lock()
39 {
40 return aux::threading::mutex::try_lock(mtx_);
41 }
42
43 void mutex::unlock()
44 {
45 aux::threading::mutex::unlock(mtx_);
46 }
47
48 mutex::native_handle_type mutex::native_handle()
49 {
50 return &mtx_;
51 }
52
53 recursive_mutex::~recursive_mutex()
54 { /* DUMMY BODY */ }
55
56 void recursive_mutex::lock()
57 {
58 if (owner_ != this_thread::get_id())
59 {
60 aux::threading::mutex::lock(mtx_);
61 owner_ = this_thread::get_id();
62 lock_level_ = 1;
63 }
64 else
65 ++lock_level_;
66 }
67
68 bool recursive_mutex::try_lock() noexcept
69 {
70 if (owner_ != this_thread::get_id())
71 {
72 bool res = aux::threading::mutex::try_lock(mtx_);
73 if (res)
74 {
75 owner_ = this_thread::get_id();
76 lock_level_ = 1;
77 }
78
79 return res;
80 }
81 else
82 ++lock_level_;
83
84 return true;
85 }
86
87 void recursive_mutex::unlock()
88 {
89 if (owner_ != this_thread::get_id())
90 return;
91 else if (--lock_level_ == 0)
92 aux::threading::mutex::unlock(mtx_);
93 }
94
95 recursive_mutex::native_handle_type recursive_mutex::native_handle()
96 {
97 return &mtx_;
98 }
99
100 timed_mutex::timed_mutex() noexcept
101 : mtx_{}
102 {
103 aux::threading::mutex::init(mtx_);
104 }
105
106 timed_mutex::~timed_mutex()
107 { /* DUMMY BODY */ }
108
109 void timed_mutex::lock()
110 {
111 aux::threading::mutex::lock(mtx_);
112 }
113
114 bool timed_mutex::try_lock()
115 {
116 return aux::threading::mutex::try_lock(mtx_);
117 }
118
119 void timed_mutex::unlock()
120 {
121 aux::threading::mutex::unlock(mtx_);
122 }
123
124 timed_mutex::native_handle_type timed_mutex::native_handle()
125 {
126 return &mtx_;
127 }
128
129 recursive_timed_mutex::~recursive_timed_mutex()
130 { /* DUMMY BODY */ }
131
132 void recursive_timed_mutex::lock()
133 {
134 if (owner_ != this_thread::get_id())
135 {
136 aux::threading::mutex::lock(mtx_);
137 owner_ = this_thread::get_id();
138 lock_level_ = 1;
139 }
140 else
141 ++lock_level_;
142 }
143
144 bool recursive_timed_mutex::try_lock() noexcept
145 {
146 if (owner_ != this_thread::get_id())
147 {
148 bool res = aux::threading::mutex::try_lock(mtx_);
149 if (res)
150 {
151 owner_ = this_thread::get_id();
152 lock_level_ = 1;
153 }
154
155 return res;
156 }
157 else
158 ++lock_level_;
159
160 return true;
161 }
162
163 void recursive_timed_mutex::unlock()
164 {
165 if (owner_ != this_thread::get_id())
166 return;
167 else if (--lock_level_ == 0)
168 aux::threading::mutex::unlock(mtx_);
169 }
170
171 recursive_timed_mutex::native_handle_type recursive_timed_mutex::native_handle()
172 {
173 return &mtx_;
174 }
175}
Note: See TracBrowser for help on using the repository browser.