Home | History | Annotate | Download | only in libsync
      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 <linux/sync.h>
     24 #include <linux/sw_sync.h>
     25 
     26 #include <sys/ioctl.h>
     27 #include <sys/stat.h>
     28 #include <sys/types.h>
     29 
     30 int sync_wait(int fd, int timeout)
     31 {
     32     __s32 to = timeout;
     33 
     34     return ioctl(fd, SYNC_IOC_WAIT, &to);
     35 }
     36 
     37 int sync_merge(const char *name, int fd1, int fd2)
     38 {
     39     struct sync_merge_data data;
     40     int err;
     41 
     42     data.fd2 = fd2;
     43     strlcpy(data.name, name, sizeof(data.name));
     44 
     45     err = ioctl(fd1, SYNC_IOC_MERGE, &data);
     46     if (err < 0)
     47         return err;
     48 
     49     return data.fence;
     50 }
     51 
     52 struct sync_fence_info_data *sync_fence_info(int fd)
     53 {
     54     struct sync_fence_info_data *info;
     55     int err;
     56 
     57     info = malloc(4096);
     58     if (info == NULL)
     59         return NULL;
     60 
     61     info->len = 4096;
     62     err = ioctl(fd, SYNC_IOC_FENCE_INFO, info);
     63     if (err < 0) {
     64         free(info);
     65         return NULL;
     66     }
     67 
     68     return info;
     69 }
     70 
     71 struct sync_pt_info *sync_pt_info(struct sync_fence_info_data *info,
     72                                   struct sync_pt_info *itr)
     73 {
     74     if (itr == NULL)
     75         itr = (struct sync_pt_info *) info->pt_info;
     76     else
     77         itr = (struct sync_pt_info *) ((__u8 *)itr + itr->len);
     78 
     79     if ((__u8 *)itr - (__u8 *)info >= (int)info->len)
     80         return NULL;
     81 
     82     return itr;
     83 }
     84 
     85 void sync_fence_info_free(struct sync_fence_info_data *info)
     86 {
     87     free(info);
     88 }
     89 
     90 
     91 int sw_sync_timeline_create(void)
     92 {
     93     return open("/dev/sw_sync", O_RDWR);
     94 }
     95 
     96 int sw_sync_timeline_inc(int fd, unsigned count)
     97 {
     98     __u32 arg = count;
     99 
    100     return ioctl(fd, SW_SYNC_IOC_INC, &arg);
    101 }
    102 
    103 int sw_sync_fence_create(int fd, const char *name, unsigned value)
    104 {
    105     struct sw_sync_create_fence_data data;
    106     int err;
    107 
    108     data.value = value;
    109     strlcpy(data.name, name, sizeof(data.name));
    110 
    111     err = ioctl(fd, SW_SYNC_IOC_CREATE_FENCE, &data);
    112     if (err < 0)
    113         return err;
    114 
    115     return data.fence;
    116 }
    117