55
66#include < seastar/util/later.hh>
77
8- seastar::future<> read_lock::lock ()
8+ SET_SUBSYS (osd);
9+ // TODO: SET_SUBSYS(crimson_tri_mutex);
10+
11+ std::optional<seastar::future<>>
12+ read_lock::lock ()
913{
1014 return static_cast <tri_mutex*>(this )->lock_for_read ();
1115}
@@ -15,7 +19,8 @@ void read_lock::unlock()
1519 static_cast <tri_mutex*>(this )->unlock_for_read ();
1620}
1721
18- seastar::future<> write_lock::lock ()
22+ std::optional<seastar::future<>>
23+ write_lock::lock ()
1924{
2025 return static_cast <tri_mutex*>(this )->lock_for_write ();
2126}
@@ -25,7 +30,8 @@ void write_lock::unlock()
2530 static_cast <tri_mutex*>(this )->unlock_for_write ();
2631}
2732
28- seastar::future<> excl_lock::lock ()
33+ std::optional<seastar::future<>>
34+ excl_lock::lock ()
2935{
3036 return static_cast <tri_mutex*>(this )->lock_for_excl ();
3137}
@@ -57,30 +63,40 @@ void excl_lock_from_write::unlock()
5763
5864tri_mutex::~tri_mutex ()
5965{
66+ LOG_PREFIX (tri_mutex::~tri_mutex ());
67+ DEBUGDPP (" " , *this );
6068 assert (!is_acquired ());
6169}
6270
63- seastar::future<> tri_mutex::lock_for_read ()
71+ std::optional<seastar::future<>>
72+ tri_mutex::lock_for_read ()
6473{
74+ LOG_PREFIX (tri_mutex::lock_for_read ());
75+ DEBUGDPP (" " , *this );
6576 if (try_lock_for_read ()) {
66- return seastar::now ();
77+ DEBUGDPP (" lock_for_read successfully" , *this );
78+ return std::nullopt ;
6779 }
68- waiters.emplace_back (seastar::promise<>(), type_t ::read);
80+ DEBUGDPP (" can't lock_for_read, adding to waiters" , *this );
81+ waiters.emplace_back (seastar::promise<>(), type_t ::read, name);
6982 return waiters.back ().pr .get_future ();
7083}
7184
7285bool tri_mutex::try_lock_for_read () noexcept
7386{
87+ LOG_PREFIX (tri_mutex::try_lock_for_read ());
88+ DEBUGDPP (" " , *this );
7489 if (!writers && !exclusively_used && waiters.empty ()) {
7590 ++readers;
7691 return true ;
77- } else {
78- return false ;
7992 }
93+ return false ;
8094}
8195
8296void tri_mutex::unlock_for_read ()
8397{
98+ LOG_PREFIX (tri_mutex::unlock_for_read ());
99+ DEBUGDPP (" " , *this );
84100 assert (readers > 0 );
85101 if (--readers == 0 ) {
86102 wake ();
@@ -89,40 +105,51 @@ void tri_mutex::unlock_for_read()
89105
90106void tri_mutex::promote_from_read ()
91107{
108+ LOG_PREFIX (tri_mutex::promote_from_read ());
109+ DEBUGDPP (" " , *this );
92110 assert (readers == 1 );
93111 --readers;
94112 exclusively_used = true ;
95113}
96114
97115void tri_mutex::demote_to_read ()
98116{
117+ LOG_PREFIX (tri_mutex::demote_to_read ());
118+ DEBUGDPP (" " , *this );
99119 assert (exclusively_used);
100120 exclusively_used = false ;
101121 ++readers;
102122}
103123
104- seastar::future<> tri_mutex::lock_for_write ()
124+ std::optional<seastar::future<>>
125+ tri_mutex::lock_for_write ()
105126{
127+ LOG_PREFIX (tri_mutex::lock_for_write ());
128+ DEBUGDPP (" " , *this );
106129 if (try_lock_for_write ()) {
107- return seastar::now ();
130+ DEBUGDPP (" lock_for_write successfully" , *this );
131+ return std::nullopt ;
108132 }
109- waiters.emplace_back (seastar::promise<>(), type_t ::write);
133+ DEBUGDPP (" can't lock_for_write, adding to waiters" , *this );
134+ waiters.emplace_back (seastar::promise<>(), type_t ::write, name);
110135 return waiters.back ().pr .get_future ();
111136}
112137
113138bool tri_mutex::try_lock_for_write () noexcept
114139{
115- if (!readers && !exclusively_used) {
116- if (waiters. empty ()) {
117- ++writers;
118- return true ;
119- }
140+ LOG_PREFIX ( tri_mutex::try_lock_for_write ());
141+ DEBUGDPP ( " " , * this );
142+ if (!readers && !exclusively_used && waiters. empty ()) {
143+ ++writers ;
144+ return true ;
120145 }
121146 return false ;
122147}
123148
124149void tri_mutex::unlock_for_write ()
125150{
151+ LOG_PREFIX (tri_mutex::unlock_for_write ());
152+ DEBUGDPP (" " , *this );
126153 assert (writers > 0 );
127154 if (--writers == 0 ) {
128155 wake ();
@@ -131,30 +158,41 @@ void tri_mutex::unlock_for_write()
131158
132159void tri_mutex::promote_from_write ()
133160{
161+ LOG_PREFIX (tri_mutex::promote_from_write ());
162+ DEBUGDPP (" " , *this );
134163 assert (writers == 1 );
135164 --writers;
136165 exclusively_used = true ;
137166}
138167
139168void tri_mutex::demote_to_write ()
140169{
170+ LOG_PREFIX (tri_mutex::demote_to_write ());
171+ DEBUGDPP (" " , *this );
141172 assert (exclusively_used);
142173 exclusively_used = false ;
143174 ++writers;
144175}
145176
146177// for exclusive users
147- seastar::future<> tri_mutex::lock_for_excl ()
178+ std::optional<seastar::future<>>
179+ tri_mutex::lock_for_excl ()
148180{
181+ LOG_PREFIX (tri_mutex::lock_for_excl ());
182+ DEBUGDPP (" " , *this );
149183 if (try_lock_for_excl ()) {
150- return seastar::now ();
184+ DEBUGDPP (" lock_for_excl, successfully" , *this );
185+ return std::nullopt ;
151186 }
152- waiters.emplace_back (seastar::promise<>(), type_t ::exclusive);
187+ DEBUGDPP (" can't lock_for_excl, adding to waiters" , *this );
188+ waiters.emplace_back (seastar::promise<>(), type_t ::exclusive, name);
153189 return waiters.back ().pr .get_future ();
154190}
155191
156192bool tri_mutex::try_lock_for_excl () noexcept
157193{
194+ LOG_PREFIX (tri_mutex::try_lock_for_excl ());
195+ DEBUGDPP (" " , *this );
158196 if (readers == 0u && writers == 0u && !exclusively_used) {
159197 exclusively_used = true ;
160198 return true ;
@@ -165,13 +203,17 @@ bool tri_mutex::try_lock_for_excl() noexcept
165203
166204void tri_mutex::unlock_for_excl ()
167205{
206+ LOG_PREFIX (tri_mutex::unlock_for_excl ());
207+ DEBUGDPP (" " , *this );
168208 assert (exclusively_used);
169209 exclusively_used = false ;
170210 wake ();
171211}
172212
173213bool tri_mutex::is_acquired () const
174214{
215+ LOG_PREFIX (tri_mutex::is_acquired ());
216+ DEBUGDPP (" " , *this );
175217 if (readers != 0u ) {
176218 return true ;
177219 } else if (writers != 0u ) {
@@ -185,6 +227,8 @@ bool tri_mutex::is_acquired() const
185227
186228void tri_mutex::wake ()
187229{
230+ LOG_PREFIX (tri_mutex::wake ());
231+ DEBUGDPP (" " , *this );
188232 assert (!readers && !writers && !exclusively_used);
189233 type_t type = type_t ::none;
190234 while (!waiters.empty ()) {
@@ -210,7 +254,9 @@ void tri_mutex::wake()
210254 default :
211255 assert (0 );
212256 }
257+ DEBUGDPP (" waking up {}" , *this , waiter.waiter_name );
213258 waiter.pr .set_value ();
214259 waiters.pop_front ();
215260 }
261+ DEBUGDPP (" no waiters" , *this );
216262}
0 commit comments