1/*
2 * Copyright (c) 2024 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 "ecmascript/platform/ffrt.h"
17#include "libpandabase/os/thread.h"
18
19#if defined(ENABLE_FFRT_INTERFACES)
20#include "ffrt.h"
21#include "c/executor_task.h"
22#endif
23
24namespace panda::ecmascript {
25ThreadId GetCurrentThreadOrTaskId()
26{
27#if defined(ENABLE_FFRT_INTERFACES)
28    ThreadId id = ffrt_this_task_get_id();
29    if (id != 0) {
30        return id;
31    } else {
32        return os::thread::GetCurrentThreadId();
33    }
34#else
35    return os::thread::GetCurrentThreadId();
36#endif
37}
38
39ThreadId GetThreadIdOrCachedTaskId()
40{
41#if defined(ENABLE_FFRT_INTERFACES)
42    // If the current task id is zero,
43    // ffrt_get_cur_cached_task_id() returns the id of the last task that ran in this thread
44    ThreadId id = ffrt_get_cur_cached_task_id();
45    if (id != 0) {
46        return id;
47    } else {
48        return os::thread::GetCurrentThreadId();
49    }
50#else
51    return os::thread::GetCurrentThreadId();
52#endif
53}
54} // namespace panda::ecmascript
55
56