• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #ifndef UTILS_INCLUDE_SW_SYNC_H
17 #define UTILS_INCLUDE_SW_SYNC_H
18 
19 #include <cerrno>
20 #include <fcntl.h>
21 #include <poll.h>
22 #include <sys/ioctl.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26 
27 #include <securec.h>
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 struct sw_sync_create_fence_data {
34     unsigned int value;
35     char name[32];
36     unsigned int fence;
37 };
38 
39 #define SYNC_IOC_CREATE_SW_SYNC_FENCE _IOWR('W', 0, struct sw_sync_create_fence_data)
40 #define SYNC_IOC_INCREASE_TIMELINE    _IOW('W', 1, unsigned int)
41 
IsSupportSoftwareSync(void)42 static inline bool IsSupportSoftwareSync(void)
43 {
44     struct stat syncStatus = {};
45     if (!stat("/sys/kernel/debug/sync/sw_sync", &syncStatus)) {
46         return true;
47     }
48     return false;
49 }
50 
CreateSyncTimeline(void)51 static inline int CreateSyncTimeline(void)
52 {
53     int timeline = open("/sys/kernel/debug/sync/sw_sync", O_RDWR);
54     if (fcntl(timeline, F_GETFD, 0) < 0) {
55         return -1;
56     }
57     return timeline;
58 }
59 
IncreaseSyncPointOnTimeline(int timelineFd,unsigned int count)60 static inline int IncreaseSyncPointOnTimeline(int timelineFd, unsigned int count)
61 {
62     unsigned int count_ = count;
63 
64     int ret = ioctl(timelineFd, SYNC_IOC_INCREASE_TIMELINE, &count_);
65     return ret;
66 }
67 
CreateSyncFence(int timelineFd,const char * name,unsigned int totalSteps)68 static inline int CreateSyncFence(int timelineFd, const char* name, unsigned int totalSteps)
69 {
70     struct sw_sync_create_fence_data data = { .value = totalSteps };
71     if (strcpy_s(data.name, sizeof(data.name), name)) {
72         return -1;
73     }
74     int ret = ioctl(timelineFd, SYNC_IOC_CREATE_SW_SYNC_FENCE, &data);
75     return ret != 0 ? ret : data.fence;
76 }
77 
78 #ifdef __cplusplus
79 }
80 #endif
81 
82 #endif