1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <unistd.h>
17 #ifndef _GNU_SOURCE
18 #define _GNU_SOURCE
19 #endif
20 #include <sys/time.h>
21 #include <sys/syscall.h>
22
23 #include <map>
24 #include <functional>
25 #include <linux/futex.h>
26 #include "core/task_ctx.h"
27 #include "sync.h"
28
29 #ifdef NS_PER_SEC
30 #undef NS_PER_SEC
31 #endif
32 namespace ffrt {
DelayedWakeup(const time_point_t & to,WaitEntry * we,const std::function<void (WaitEntry *)> & wakeup)33 bool DelayedWakeup(const time_point_t& to, WaitEntry* we, const std::function<void(WaitEntry*)>& wakeup)
34 {
35 static DelayedWorker w;
36 return w.dispatch(to, we, wakeup);
37 }
38
lock_contended()39 void spin_mutex::lock_contended()
40 {
41 int v = l.load(std::memory_order_relaxed);
42 do {
43 while (v != sync_detail::UNLOCK) {
44 std::this_thread::yield();
45 v = l.load(std::memory_order_relaxed);
46 }
47 } while (!l.compare_exchange_weak(v, sync_detail::LOCK, std::memory_order_acquire, std::memory_order_relaxed));
48 }
49
50 #ifndef _MSC_VER
spin()51 static void spin()
52 {
53 #if defined(__x86_64__)
54 asm volatile("pause");
55 #elif defined(__aarch64__)
56 asm volatile("isb sy");
57 #elif defined(__arm__)
58 asm volatile("yield");
59 #endif
60 }
61
lock_contended()62 void fast_mutex::lock_contended()
63 {
64 int v;
65 // lightly contended
66 for (uint32_t n = static_cast<uint32_t>(1 + rand() % 4); n <= 64; n <<= 1) {
67 for (uint32_t i = 0; i < n; ++i) {
68 spin();
69 }
70 v = __atomic_load_n(&l, __ATOMIC_RELAXED);
71 if (v == sync_detail::WAIT) {
72 break;
73 }
74 if (v == sync_detail::UNLOCK) {
75 if (__atomic_compare_exchange_n(&l, &v, sync_detail::LOCK, 0, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) {
76 return;
77 }
78 break;
79 }
80 }
81 // heavily contended
82 if (v == sync_detail::WAIT) {
83 syscall(SYS_futex, &l, FUTEX_WAIT_PRIVATE, sync_detail::WAIT, nullptr, nullptr, 0);
84 }
85 while (__atomic_exchange_n(&l, sync_detail::WAIT, __ATOMIC_ACQUIRE) != sync_detail::UNLOCK) {
86 syscall(SYS_futex, &l, FUTEX_WAIT_PRIVATE, sync_detail::WAIT, nullptr, nullptr, 0);
87 }
88 }
89 #endif
90 } // namespace ffrt
91