1 //===-- Linux implementation of the mtx_lock function ---------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "config/linux/syscall.h" // For syscall functions.
10 #include "include/sys/syscall.h"  // For syscall numbers.
11 #include "include/threads.h"      // For mtx_t definition.
12 #include "src/__support/common.h"
13 #include "src/threads/linux/thread_utils.h"
14 
15 #include <linux/futex.h> // For futex operations.
16 #include <stdatomic.h>   // For atomic_compare_exchange_strong.
17 
18 namespace __llvm_libc {
19 
20 // The implementation currently handles only plain mutexes.
LLVM_LIBC_ENTRYPOINT(mtx_lock)21 int LLVM_LIBC_ENTRYPOINT(mtx_lock)(mtx_t *mutex) {
22   FutexData *futex_data = reinterpret_cast<FutexData *>(mutex->__internal_data);
23   while (true) {
24     uint32_t mutex_status = MS_Free;
25     uint32_t locked_status = MS_Locked;
26 
27     if (atomic_compare_exchange_strong(futex_data, &mutex_status, MS_Locked))
28       return thrd_success;
29 
30     switch (mutex_status) {
31     case MS_Waiting:
32       // If other threads are waiting already, then join them. Note that the
33       // futex syscall will block if the futex data is still `MS_Waiting` (the
34       // 4th argument to the syscall function below.)
35       __llvm_libc::syscall(SYS_futex, futex_data, FUTEX_WAIT_PRIVATE,
36                            MS_Waiting, 0, 0, 0);
37       // Once woken up/unblocked, try everything all over.
38       continue;
39     case MS_Locked:
40       // Mutex has been locked by another thread so set the status to
41       // MS_Waiting.
42       if (atomic_compare_exchange_strong(futex_data, &locked_status,
43                                          MS_Waiting)) {
44         // If we are able to set the futex data to `MS_Waiting`, then we will
45         // wait for the futex to be woken up. Note again that the following
46         // syscall will block only if the futex data is still `MS_Waiting`.
47         __llvm_libc::syscall(SYS_futex, futex_data, FUTEX_WAIT_PRIVATE,
48                              MS_Waiting, 0, 0, 0);
49       }
50       continue;
51     case MS_Free:
52       // If it was MS_Free, we shouldn't be here at all.
53       [[clang::fallthrough]];
54     default:
55       // Mutex status cannot be anything else. So control should not reach
56       // here at all.
57       return thrd_error;
58     }
59   }
60 }
61 
62 } // namespace __llvm_libc
63