From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on gnuweeb.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NO_DNS_FOR_FROM,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 Received: from gnuweeb.org (unknown [51.81.211.47]) by gnuweeb.org (Postfix) with ESMTPSA id B18A583213; Sat, 11 Mar 2023 11:28:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1678534101; bh=0WPX9jM+J0oaVUWqZCja/V90QKCsBdNlbOcY9En29xo=; h=From:To:Cc:Subject:Date:From; b=cVIV3rUPXvY9RLZtpoHHXuMb5yu91LuWC3xO3Lu3QXYgH+LyGa+dYvhE/6uMQQHT4 HU7lPVHszJa9UxIn0uPgB57YPL7DV1seuJBbRezGclV+KmDF+7j9kA9KIdAHzsPNSh V+84swvf2ojnZz6ORVf9YoLpok3ZRUVkpyq9IF5uwLO9HJh1wmfhzgaaH5NCCWeVV6 8M4E+B+OZx5yr1zTbFOx/ZxlSUx7qqFbDORP4sM4kzclKQ0peWM/g6nNnLY1YzUBmI AJH+gT6KhinToRzLq4zuyhw5rjCEAGI4PqGJUd17/wai4Ja+GYqVBQfx35JvDiFKfg bqEfG2YXSG3Hw== From: Alviro Iskandar Setiawan To: Ammar Faizi Cc: Alviro Iskandar Setiawan , Irvan Malik Azantha , GNU/Weeb Mailing List Subject: [RFC PATCH v1 0/3] Fix undefined behavior in the C++ mutex implementation Date: Sat, 11 Mar 2023 11:28:07 +0000 Message-Id: <20230311112810.3670483-1-alviro.iskandar@gnuweeb.org> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: Hi, The current C++ mutex implementation is undefined behavior, specifically in the cond_wait() function because std::unique_lock 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 --- 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