Skip to content

Commit 6ed35ad

Browse files
author
Rafał Hibner
committed
Remove <mutex> from public header
1 parent a6f8c63 commit 6ed35ad

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
lines changed

cpp/src/arrow/acero/concurrent_queue.h

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
#pragma once
1919

2020
#include <condition_variable>
21-
#include <mutex>
2221
#include <queue>
2322
#include "arrow/acero/backpressure_handler.h"
23+
#include "arrow/util/mutex.h"
2424

2525
namespace arrow::acero {
2626

@@ -34,32 +34,32 @@ class ConcurrentQueue {
3434
// Pops the last item from the queue but waits if the queue is empty until new items are
3535
// pushed.
3636
T WaitAndPop() {
37-
std::unique_lock<std::mutex> lock(mutex_);
38-
WaitUntilNonEmpty(lock);
37+
arrow::util::Mutex::Guard guard = mutex_.Lock();
38+
WaitUntilNonEmpty(guard);
3939
return PopUnlocked();
4040
}
4141

4242
// Pops the last item from the queue, or returns a nullopt if empty
4343
std::optional<T> TryPop() {
44-
std::unique_lock<std::mutex> lock(mutex_);
44+
arrow::util::Mutex::Guard guard = mutex_.Lock();
4545
return TryPopUnlocked();
4646
}
4747

4848
// Pushes an item to the queue
4949
void Push(const T& item) {
50-
std::unique_lock<std::mutex> lock(mutex_);
50+
arrow::util::Mutex::Guard guard = mutex_.Lock();
5151
return PushUnlocked(item);
5252
}
5353

5454
// Clears the queue
5555
void Clear() {
56-
std::unique_lock<std::mutex> lock(mutex_);
56+
arrow::util::Mutex::Guard guard = mutex_.Lock();
5757
ClearUnlocked();
5858
}
5959

6060
// Checks if the queue is empty
6161
bool Empty() const {
62-
std::unique_lock<std::mutex> lock(mutex_);
62+
arrow::util::Mutex::Guard guard = mutex_.Lock();
6363
return queue_.empty();
6464
}
6565

@@ -69,17 +69,17 @@ class ConcurrentQueue {
6969
// Need to lock the queue because `front()` may be implemented in terms
7070
// of `begin()`, which isn't safe with concurrent calls to e.g. `push()`.
7171
// (see GH-44846)
72-
std::unique_lock<std::mutex> lock(mutex_);
72+
arrow::util::Mutex::Guard guard = mutex_.Lock();
7373
return queue_.front();
7474
}
7575

7676
protected:
77-
std::mutex& GetMutex() { return mutex_; }
77+
arrow::util::Mutex::Guard Lock() { return mutex_.Lock(); }
7878

7979
size_t SizeUnlocked() const { return queue_.size(); }
8080

81-
void WaitUntilNonEmpty(std::unique_lock<std::mutex>& lock) {
82-
cond_.wait(lock, [&] { return !queue_.empty(); });
81+
void WaitUntilNonEmpty(arrow::util::Mutex::Guard& guard) {
82+
guard.wait(cond_, [&] { return !queue_.empty(); });
8383
}
8484

8585
T PopUnlocked() {
@@ -108,7 +108,7 @@ class ConcurrentQueue {
108108
std::queue<T> queue_;
109109

110110
private:
111-
mutable std::mutex mutex_;
111+
mutable arrow::util::Mutex mutex_;
112112
std::condition_variable cond_;
113113
};
114114

@@ -137,29 +137,29 @@ class BackpressureConcurrentQueue : public ConcurrentQueue<T> {
137137
// Pops the last item from the queue but waits if the queue is empty until new items are
138138
// pushed.
139139
T WaitAndPop() {
140-
std::unique_lock<std::mutex> lock(ConcurrentQueue<T>::GetMutex());
141-
ConcurrentQueue<T>::WaitUntilNonEmpty(lock);
140+
arrow::util::Mutex::Guard guard = ConcurrentQueue<T>::Lock();
141+
ConcurrentQueue<T>::WaitUntilNonEmpty(guard);
142142
DoHandle do_handle(*this);
143143
return ConcurrentQueue<T>::PopUnlocked();
144144
}
145145

146146
// Pops the last item from the queue, or returns a nullopt if empty
147147
std::optional<T> TryPop() {
148-
std::unique_lock<std::mutex> lock(ConcurrentQueue<T>::GetMutex());
148+
arrow::util::Mutex::Guard guard = ConcurrentQueue<T>::Lock();
149149
DoHandle do_handle(*this);
150150
return ConcurrentQueue<T>::TryPopUnlocked();
151151
}
152152

153153
// Pushes an item to the queue
154154
void Push(const T& item) {
155-
std::unique_lock<std::mutex> lock(ConcurrentQueue<T>::GetMutex());
155+
arrow::util::Mutex::Guard guard = ConcurrentQueue<T>::Lock();
156156
DoHandle do_handle(*this);
157157
ConcurrentQueue<T>::PushUnlocked(item);
158158
}
159159

160160
// Clears the queue
161161
void Clear() {
162-
std::unique_lock<std::mutex> lock(ConcurrentQueue<T>::GetMutex());
162+
arrow::util::Mutex::Guard guard = ConcurrentQueue<T>::Lock();
163163
DoHandle do_handle(*this);
164164
ConcurrentQueue<T>::ClearUnlocked();
165165
}

cpp/src/arrow/util/mutex.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ Mutex::Guard::Guard(Mutex* locked)
4343
}) {
4444
}
4545

46+
void Mutex::Guard::wait(std::condition_variable& cond) {
47+
std::unique_lock lock(locked_->impl_->mutex_, std::adopt_lock);
48+
cond.wait(lock);
49+
lock.release();
50+
}
51+
void Mutex::Guard::wait(std::condition_variable& cond, std::function<bool()> pred) {
52+
std::unique_lock lock(locked_->impl_->mutex_, std::adopt_lock);
53+
cond.wait(lock, pred);
54+
lock.release();
55+
}
56+
4657
Mutex::Guard Mutex::TryLock() {
4758
DCHECK_NE(impl_, nullptr);
4859
if (impl_->mutex_.try_lock()) {

cpp/src/arrow/util/mutex.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717

1818
#pragma once
1919

20+
#include <condition_variable>
21+
#include <functional>
2022
#include <memory>
21-
2223
#include "arrow/util/macros.h"
2324
#include "arrow/util/visibility.h"
2425

@@ -45,6 +46,9 @@ class ARROW_EXPORT Mutex {
4546

4647
void Unlock() { locked_.reset(); }
4748

49+
void wait(std::condition_variable& cond);
50+
void wait(std::condition_variable& cond, std::function<bool()> pred);
51+
4852
private:
4953
explicit Guard(Mutex* locked);
5054

0 commit comments

Comments
 (0)