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 "delayed_worker.h"
27 #include "util/ffrt_facade.h"
28 #include "sync.h"
29
30 #ifdef NS_PER_SEC
31 #undef NS_PER_SEC
32 #endif
33 namespace ffrt {
DelayedWakeup(const TimePoint & to,WaitEntry * we,const std::function<void (WaitEntry *)> & wakeup)34 bool DelayedWakeup(const TimePoint& to, WaitEntry* we, const std::function<void(WaitEntry*)>& wakeup)
35 {
36 return FFRTFacade::GetDWInstance().dispatch(to, we, wakeup);
37 }
38
DelayedRemove(const TimePoint & to,WaitEntry * we)39 bool DelayedRemove(const TimePoint& to, WaitEntry* we)
40 {
41 return FFRTFacade::GetDWInstance().remove(to, we);
42 }
43
lock_contended()44 void spin_mutex::lock_contended()
45 {
46 int v = l.load(std::memory_order_relaxed);
47 do {
48 while (v != sync_detail::UNLOCK) {
49 std::this_thread::yield();
50 v = l.load(std::memory_order_relaxed);
51 }
52 } while (!l.compare_exchange_weak(v, sync_detail::LOCK, std::memory_order_acquire, std::memory_order_relaxed));
53 }
54
spin()55 static void spin()
56 {
57 #if defined(__x86_64__)
58 asm volatile("pause");
59 #elif defined(__aarch64__)
60 asm volatile("isb sy");
61 #elif defined(__arm__)
62 asm volatile("yield");
63 #endif
64 }
65
lock_contended()66 void fast_mutex::lock_contended()
67 {
68 int v = 0;
69 // lightly contended
70 for (uint32_t n = static_cast<uint32_t>(1 + rand() % 4); n <= 64; n <<= 1) {
71 for (uint32_t i = 0; i < n; ++i) {
72 spin();
73 }
74 v = __atomic_load_n(&l, __ATOMIC_RELAXED);
75 if (v == sync_detail::WAIT) {
76 break;
77 }
78 if (v == sync_detail::UNLOCK) {
79 if (__atomic_compare_exchange_n(&l, &v, sync_detail::LOCK, 0, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) {
80 return;
81 }
82 break;
83 }
84 }
85 // heavily contended
86 if (v == sync_detail::WAIT) {
87 syscall(SYS_futex, &l, FUTEX_WAIT_PRIVATE, sync_detail::WAIT, nullptr, nullptr, 0);
88 }
89 while (__atomic_exchange_n(&l, sync_detail::WAIT, __ATOMIC_ACQUIRE) != sync_detail::UNLOCK) {
90 syscall(SYS_futex, &l, FUTEX_WAIT_PRIVATE, sync_detail::WAIT, nullptr, nullptr, 0);
91 }
92 }
93 } // namespace ffrt
94