17ec681f3Smrg/*
27ec681f3Smrg *  Copyright 2017 The Android Open Source Project
37ec681f3Smrg *
47ec681f3Smrg *  Licensed under the Apache License, Version 2.0 (the "License");
57ec681f3Smrg *  you may not use this file except in compliance with the License.
67ec681f3Smrg *  You may obtain a copy of the License at
77ec681f3Smrg *
87ec681f3Smrg *      http://www.apache.org/licenses/LICENSE-2.0
97ec681f3Smrg *
107ec681f3Smrg *  Unless required by applicable law or agreed to in writing, software
117ec681f3Smrg *  distributed under the License is distributed on an "AS IS" BASIS,
127ec681f3Smrg *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137ec681f3Smrg *  See the License for the specific language governing permissions and
147ec681f3Smrg *  limitations under the License.
157ec681f3Smrg */
167ec681f3Smrg
177ec681f3Smrg/**
187ec681f3Smrg * @addtogroup Sync
197ec681f3Smrg * @{
207ec681f3Smrg */
217ec681f3Smrg
227ec681f3Smrg/**
237ec681f3Smrg * @file sync.h
247ec681f3Smrg */
257ec681f3Smrg
267ec681f3Smrg#ifndef ANDROID_SYNC_H
277ec681f3Smrg#define ANDROID_SYNC_H
287ec681f3Smrg
297ec681f3Smrg#include <stdint.h>
307ec681f3Smrg#include <sys/cdefs.h>
317ec681f3Smrg
327ec681f3Smrg#include <linux/sync_file.h>
337ec681f3Smrg
347ec681f3Smrg__BEGIN_DECLS
357ec681f3Smrg
367ec681f3Smrg/* Fences indicate the status of an asynchronous task. They are initially
377ec681f3Smrg * in unsignaled state (0), and make a one-time transition to either signaled
387ec681f3Smrg * (1) or error (< 0) state. A sync file is a collection of one or more fences;
397ec681f3Smrg * the sync file's status is error if any of its fences are in error state,
407ec681f3Smrg * signaled if all of the child fences are signaled, or unsignaled otherwise.
417ec681f3Smrg *
427ec681f3Smrg * Sync files are created by various device APIs in response to submitting
437ec681f3Smrg * tasks to the device. Standard file descriptor lifetime syscalls like dup()
447ec681f3Smrg * and close() are used to manage sync file lifetime.
457ec681f3Smrg *
467ec681f3Smrg * The poll(), ppoll(), or select() syscalls can be used to wait for the sync
477ec681f3Smrg * file to change status, or (with a timeout of zero) to check its status.
487ec681f3Smrg *
497ec681f3Smrg * The functions below provide a few additional sync-specific operations.
507ec681f3Smrg */
517ec681f3Smrg
527ec681f3Smrg/**
537ec681f3Smrg * Merge two sync files.
547ec681f3Smrg *
557ec681f3Smrg * This produces a new sync file with the given name which has the union of the
567ec681f3Smrg * two original sync file's fences; redundant fences may be removed.
577ec681f3Smrg *
587ec681f3Smrg * If one of the input sync files is signaled or invalid, then this function
597ec681f3Smrg * may behave like dup(): the new file descriptor refers to the valid/unsignaled
607ec681f3Smrg * sync file with its original name, rather than a new sync file.
617ec681f3Smrg *
627ec681f3Smrg * The original fences remain valid, and the caller is responsible for closing
637ec681f3Smrg * them.
647ec681f3Smrg *
657ec681f3Smrg * Available since API level 26.
667ec681f3Smrg */
677ec681f3Smrgint32_t sync_merge(const char* name, int32_t fd1, int32_t fd2) __INTRODUCED_IN(26);
687ec681f3Smrg
697ec681f3Smrg/**
707ec681f3Smrg * Retrieve detailed information about a sync file and its fences.
717ec681f3Smrg *
727ec681f3Smrg * The returned sync_file_info must be freed by calling sync_file_info_free().
737ec681f3Smrg *
747ec681f3Smrg * Available since API level 26.
757ec681f3Smrg */
767ec681f3Smrgstruct sync_file_info* sync_file_info(int32_t fd) __INTRODUCED_IN(26);
777ec681f3Smrg
787ec681f3Smrg/**
797ec681f3Smrg * Get the array of fence infos from the sync file's info.
807ec681f3Smrg *
817ec681f3Smrg * The returned array is owned by the parent sync file info, and has
827ec681f3Smrg * info->num_fences entries.
837ec681f3Smrg *
847ec681f3Smrg * Available since API level 26.
857ec681f3Smrg */
867ec681f3Smrgstatic inline struct sync_fence_info* sync_get_fence_info(const struct sync_file_info* info) {
877ec681f3Smrg// This header should compile in C, but some C++ projects enable
887ec681f3Smrg// warnings-as-error for C-style casts.
897ec681f3Smrg#pragma GCC diagnostic push
907ec681f3Smrg#pragma GCC diagnostic ignored "-Wold-style-cast"
917ec681f3Smrg    return (struct sync_fence_info *)(uintptr_t)(info->sync_fence_info);
927ec681f3Smrg#pragma GCC diagnostic pop
937ec681f3Smrg}
947ec681f3Smrg
957ec681f3Smrg/**
967ec681f3Smrg * Free a struct sync_file_info structure
977ec681f3Smrg *
987ec681f3Smrg * Available since API level 26.
997ec681f3Smrg */
1007ec681f3Smrgvoid sync_file_info_free(struct sync_file_info* info) __INTRODUCED_IN(26);
1017ec681f3Smrg
1027ec681f3Smrg__END_DECLS
1037ec681f3Smrg
1047ec681f3Smrg#endif /* ANDROID_SYNC_H */
1057ec681f3Smrg
1067ec681f3Smrg/** @} */
107