GNU/Weeb Mailing List <[email protected]>
 help / color / mirror / Atom feed
* [RFC PATCH v1 0/3] Fix undefined behavior in the C++ mutex implementation
@ 2023-03-11 11:28 Alviro Iskandar Setiawan
  2023-03-11 11:28 ` [RFC PATCH v1 1/3] MAINTAINERS: Add myself as the thread maintainer Alviro Iskandar Setiawan
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Alviro Iskandar Setiawan @ 2023-03-11 11:28 UTC (permalink / raw)
  To: Ammar Faizi
  Cc: Alviro Iskandar Setiawan, Irvan Malik Azantha, GNU/Weeb Mailing List

Hi,

The current C++ mutex implementation is undefined behavior, specifically
in the cond_wait() function because std::unique_lock<std::mutex> is
constructed with std::defer_lock while the same thread has acquired the
lock. Also, right after that defer_lock, std::condition_variable calls
wait() with a unique lock, not in a locked state.

In such a situation, the correct construction is using std::adopt_lock.
However, using std::adopt_lock leads to another issue. The issue is the
lock will be released upon return in the cond_wait(). To solve the
problem, introduce a new helper function, __cond_wait() which will
release the lock with std::adopt_lock and then call it from cond_wait().
The cond_wait() then acquires the lock again before it returns. The
result is that we correctly fulfill the __must_hold() semantic while
conforming to the C++ mutex implementation.

Signed-off-by: Alviro Iskandar Setiawan <[email protected]>
---
Alviro Iskandar Setiawan (3):
  MAINTAINERS: Add myself as the thread maintainer
  core/thread: Fix undefined behavior in the C++ mutex implementation
  configure: Introduce `--cpp-thread` option

 MAINTAINERS         |  7 +++++++
 configure           |  8 ++++++++
 core/thread.cc      | 17 +++++++++++++----
 include/gw/thread.h |  2 +-
 4 files changed, 29 insertions(+), 5 deletions(-)


base-commit: 2ca56f61c307813ad7069cf08c350f2ff61fc615
-- 
Alviro Iskandar Setiawan


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-03-12 19:40 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-11 11:28 [RFC PATCH v1 0/3] Fix undefined behavior in the C++ mutex implementation Alviro Iskandar Setiawan
2023-03-11 11:28 ` [RFC PATCH v1 1/3] MAINTAINERS: Add myself as the thread maintainer Alviro Iskandar Setiawan
2023-03-11 11:28 ` [RFC PATCH v1 2/3] core/thread: Fix undefined behavior in the C++ mutex implementation Alviro Iskandar Setiawan
2023-03-11 11:28 ` [RFC PATCH v1 3/3] configure: Introduce `--cpp-thread` option Alviro Iskandar Setiawan
2023-03-12 19:40 ` [RFC PATCH v1 0/3] Fix undefined behavior in the C++ mutex implementation Ammar Faizi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox