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