1 /*
2 * sync.c
3 *
4 * Copyright 2012 Google, Inc
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 #include <fcntl.h>
20 #include <stdint.h>
21 #include <string.h>
22
23 #include <sys/ioctl.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26
27 #include <sync/sync.h>
28
sync_wait(int fd,unsigned int timeout)29 int sync_wait(int fd, unsigned int timeout)
30 {
31 __u32 to = timeout;
32
33 return ioctl(fd, SYNC_IOC_WAIT, &to);
34 }
35
sync_merge(const char * name,int fd1,int fd2)36 int sync_merge(const char *name, int fd1, int fd2)
37 {
38 struct sync_merge_data data;
39 int err;
40
41 data.fd2 = fd2;
42 strlcpy(data.name, name, sizeof(data.name));
43
44 err = ioctl(fd1, SYNC_IOC_MERGE, &data);
45 if (err < 0)
46 return err;
47
48 return data.fence;
49 }
50
sync_fence_info(int fd)51 struct sync_fence_info_data *sync_fence_info(int fd)
52 {
53 struct sync_fence_info_data *info;
54 int err;
55
56 info = malloc(4096);
57 if (info == NULL)
58 return NULL;
59
60 info->len = 4096;
61 err = ioctl(fd, SYNC_IOC_FENCE_INFO, info);
62 if (err < 0) {
63 free(info);
64 return NULL;
65 }
66
67 return info;
68 }
69
sync_pt_info(struct sync_fence_info_data * info,struct sync_pt_info * itr)70 struct sync_pt_info *sync_pt_info(struct sync_fence_info_data *info,
71 struct sync_pt_info *itr)
72 {
73 if (itr == NULL)
74 itr = (struct sync_pt_info *) info->pt_info;
75 else
76 itr = (struct sync_pt_info *) ((__u8 *)itr + itr->len);
77
78 if ((__u8 *)itr - (__u8 *)info >= (int)info->len)
79 return NULL;
80
81 return itr;
82 }
83
sync_fence_info_free(struct sync_fence_info_data * info)84 void sync_fence_info_free(struct sync_fence_info_data *info)
85 {
86 free(info);
87 }
88
89
sw_sync_timeline_create(void)90 int sw_sync_timeline_create(void)
91 {
92 return open("/dev/sw_sync", O_RDWR);
93 }
94
sw_sync_timeline_inc(int fd,unsigned count)95 int sw_sync_timeline_inc(int fd, unsigned count)
96 {
97 __u32 arg = count;
98
99 return ioctl(fd, SW_SYNC_IOC_INC, &arg);
100 }
101
sw_sync_fence_create(int fd,const char * name,unsigned value)102 int sw_sync_fence_create(int fd, const char *name, unsigned value)
103 {
104 struct sw_sync_create_fence_data data;
105 int err;
106
107 data.value = value;
108 strlcpy(data.name, name, sizeof(data.name));
109
110 err = ioctl(fd, SW_SYNC_IOC_CREATE_FENCE, &data);
111 if (err < 0)
112 return err;
113
114 return data.fence;
115 }
116