blob: d73bb111cc70d562bc1d462bf73b9bc891a688d5 [file] [log] [blame]
Erik Gilling196b3a52012-03-07 15:30:33 -08001/*
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>
Elliott Hughesa744b052015-01-28 11:37:57 -080020#include <malloc.h>
Erik Gilling196b3a52012-03-07 15:30:33 -080021#include <stdint.h>
22#include <string.h>
23
Jamie Gennis3be33e42012-06-13 16:40:54 -070024#include <linux/sync.h>
25#include <linux/sw_sync.h>
26
Erik Gilling196b3a52012-03-07 15:30:33 -080027#include <sys/ioctl.h>
28#include <sys/stat.h>
29#include <sys/types.h>
30
Erik Gilling984d3572012-08-21 18:21:18 -070031int sync_wait(int fd, int timeout)
Erik Gilling196b3a52012-03-07 15:30:33 -080032{
Erik Gilling984d3572012-08-21 18:21:18 -070033 __s32 to = timeout;
Erik Gilling196b3a52012-03-07 15:30:33 -080034
35 return ioctl(fd, SYNC_IOC_WAIT, &to);
36}
37
38int sync_merge(const char *name, int fd1, int fd2)
39{
40 struct sync_merge_data data;
41 int err;
42
43 data.fd2 = fd2;
44 strlcpy(data.name, name, sizeof(data.name));
45
46 err = ioctl(fd1, SYNC_IOC_MERGE, &data);
47 if (err < 0)
48 return err;
49
50 return data.fence;
51}
52
53struct sync_fence_info_data *sync_fence_info(int fd)
54{
55 struct sync_fence_info_data *info;
56 int err;
57
58 info = malloc(4096);
59 if (info == NULL)
60 return NULL;
61
62 info->len = 4096;
63 err = ioctl(fd, SYNC_IOC_FENCE_INFO, info);
64 if (err < 0) {
65 free(info);
66 return NULL;
67 }
68
69 return info;
70}
71
72struct sync_pt_info *sync_pt_info(struct sync_fence_info_data *info,
73 struct sync_pt_info *itr)
74{
75 if (itr == NULL)
76 itr = (struct sync_pt_info *) info->pt_info;
77 else
78 itr = (struct sync_pt_info *) ((__u8 *)itr + itr->len);
79
80 if ((__u8 *)itr - (__u8 *)info >= (int)info->len)
81 return NULL;
82
83 return itr;
84}
85
86void sync_fence_info_free(struct sync_fence_info_data *info)
87{
88 free(info);
89}
90
91
92int sw_sync_timeline_create(void)
93{
94 return open("/dev/sw_sync", O_RDWR);
95}
96
97int sw_sync_timeline_inc(int fd, unsigned count)
98{
99 __u32 arg = count;
100
101 return ioctl(fd, SW_SYNC_IOC_INC, &arg);
102}
103
104int sw_sync_fence_create(int fd, const char *name, unsigned value)
105{
106 struct sw_sync_create_fence_data data;
107 int err;
108
109 data.value = value;
110 strlcpy(data.name, name, sizeof(data.name));
111
112 err = ioctl(fd, SW_SYNC_IOC_CREATE_FENCE, &data);
113 if (err < 0)
114 return err;
115
116 return data.fence;
117}