1*61046927SAndroid Build Coastguard Worker /*
2*61046927SAndroid Build Coastguard Worker * Copyright 2017 The Android Open Source Project
3*61046927SAndroid Build Coastguard Worker *
4*61046927SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*61046927SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*61046927SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*61046927SAndroid Build Coastguard Worker *
8*61046927SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*61046927SAndroid Build Coastguard Worker *
10*61046927SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*61046927SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*61046927SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*61046927SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*61046927SAndroid Build Coastguard Worker * limitations under the License.
15*61046927SAndroid Build Coastguard Worker */
16*61046927SAndroid Build Coastguard Worker
17*61046927SAndroid Build Coastguard Worker /**
18*61046927SAndroid Build Coastguard Worker * @addtogroup Sync
19*61046927SAndroid Build Coastguard Worker * @{
20*61046927SAndroid Build Coastguard Worker */
21*61046927SAndroid Build Coastguard Worker
22*61046927SAndroid Build Coastguard Worker /**
23*61046927SAndroid Build Coastguard Worker * @file sync.h
24*61046927SAndroid Build Coastguard Worker */
25*61046927SAndroid Build Coastguard Worker
26*61046927SAndroid Build Coastguard Worker #ifndef ANDROID_SYNC_H
27*61046927SAndroid Build Coastguard Worker #define ANDROID_SYNC_H
28*61046927SAndroid Build Coastguard Worker
29*61046927SAndroid Build Coastguard Worker #include <stdint.h>
30*61046927SAndroid Build Coastguard Worker #include <sys/cdefs.h>
31*61046927SAndroid Build Coastguard Worker
32*61046927SAndroid Build Coastguard Worker #include <linux/sync_file.h>
33*61046927SAndroid Build Coastguard Worker
34*61046927SAndroid Build Coastguard Worker __BEGIN_DECLS
35*61046927SAndroid Build Coastguard Worker
36*61046927SAndroid Build Coastguard Worker /* Fences indicate the status of an asynchronous task. They are initially
37*61046927SAndroid Build Coastguard Worker * in unsignaled state (0), and make a one-time transition to either signaled
38*61046927SAndroid Build Coastguard Worker * (1) or error (< 0) state. A sync file is a collection of one or more fences;
39*61046927SAndroid Build Coastguard Worker * the sync file's status is error if any of its fences are in error state,
40*61046927SAndroid Build Coastguard Worker * signaled if all of the child fences are signaled, or unsignaled otherwise.
41*61046927SAndroid Build Coastguard Worker *
42*61046927SAndroid Build Coastguard Worker * Sync files are created by various device APIs in response to submitting
43*61046927SAndroid Build Coastguard Worker * tasks to the device. Standard file descriptor lifetime syscalls like dup()
44*61046927SAndroid Build Coastguard Worker * and close() are used to manage sync file lifetime.
45*61046927SAndroid Build Coastguard Worker *
46*61046927SAndroid Build Coastguard Worker * The poll(), ppoll(), or select() syscalls can be used to wait for the sync
47*61046927SAndroid Build Coastguard Worker * file to change status, or (with a timeout of zero) to check its status.
48*61046927SAndroid Build Coastguard Worker *
49*61046927SAndroid Build Coastguard Worker * The functions below provide a few additional sync-specific operations.
50*61046927SAndroid Build Coastguard Worker */
51*61046927SAndroid Build Coastguard Worker
52*61046927SAndroid Build Coastguard Worker /**
53*61046927SAndroid Build Coastguard Worker * Merge two sync files.
54*61046927SAndroid Build Coastguard Worker *
55*61046927SAndroid Build Coastguard Worker * This produces a new sync file with the given name which has the union of the
56*61046927SAndroid Build Coastguard Worker * two original sync file's fences; redundant fences may be removed.
57*61046927SAndroid Build Coastguard Worker *
58*61046927SAndroid Build Coastguard Worker * If one of the input sync files is signaled or invalid, then this function
59*61046927SAndroid Build Coastguard Worker * may behave like dup(): the new file descriptor refers to the valid/unsignaled
60*61046927SAndroid Build Coastguard Worker * sync file with its original name, rather than a new sync file.
61*61046927SAndroid Build Coastguard Worker *
62*61046927SAndroid Build Coastguard Worker * The original fences remain valid, and the caller is responsible for closing
63*61046927SAndroid Build Coastguard Worker * them.
64*61046927SAndroid Build Coastguard Worker *
65*61046927SAndroid Build Coastguard Worker * Available since API level 26.
66*61046927SAndroid Build Coastguard Worker */
67*61046927SAndroid Build Coastguard Worker int32_t sync_merge(const char* name, int32_t fd1, int32_t fd2) __INTRODUCED_IN(26);
68*61046927SAndroid Build Coastguard Worker
69*61046927SAndroid Build Coastguard Worker /**
70*61046927SAndroid Build Coastguard Worker * Retrieve detailed information about a sync file and its fences.
71*61046927SAndroid Build Coastguard Worker *
72*61046927SAndroid Build Coastguard Worker * The returned sync_file_info must be freed by calling sync_file_info_free().
73*61046927SAndroid Build Coastguard Worker *
74*61046927SAndroid Build Coastguard Worker * Available since API level 26.
75*61046927SAndroid Build Coastguard Worker */
76*61046927SAndroid Build Coastguard Worker struct sync_file_info* sync_file_info(int32_t fd) __INTRODUCED_IN(26);
77*61046927SAndroid Build Coastguard Worker
78*61046927SAndroid Build Coastguard Worker /**
79*61046927SAndroid Build Coastguard Worker * Get the array of fence infos from the sync file's info.
80*61046927SAndroid Build Coastguard Worker *
81*61046927SAndroid Build Coastguard Worker * The returned array is owned by the parent sync file info, and has
82*61046927SAndroid Build Coastguard Worker * info->num_fences entries.
83*61046927SAndroid Build Coastguard Worker *
84*61046927SAndroid Build Coastguard Worker * Available since API level 26.
85*61046927SAndroid Build Coastguard Worker */
sync_get_fence_info(const struct sync_file_info * info)86*61046927SAndroid Build Coastguard Worker static inline struct sync_fence_info* sync_get_fence_info(const struct sync_file_info* info) {
87*61046927SAndroid Build Coastguard Worker // This header should compile in C, but some C++ projects enable
88*61046927SAndroid Build Coastguard Worker // warnings-as-error for C-style casts.
89*61046927SAndroid Build Coastguard Worker #pragma GCC diagnostic push
90*61046927SAndroid Build Coastguard Worker #pragma GCC diagnostic ignored "-Wold-style-cast"
91*61046927SAndroid Build Coastguard Worker return (struct sync_fence_info *)(uintptr_t)(info->sync_fence_info);
92*61046927SAndroid Build Coastguard Worker #pragma GCC diagnostic pop
93*61046927SAndroid Build Coastguard Worker }
94*61046927SAndroid Build Coastguard Worker
95*61046927SAndroid Build Coastguard Worker /**
96*61046927SAndroid Build Coastguard Worker * Free a struct sync_file_info structure
97*61046927SAndroid Build Coastguard Worker *
98*61046927SAndroid Build Coastguard Worker * Available since API level 26.
99*61046927SAndroid Build Coastguard Worker */
100*61046927SAndroid Build Coastguard Worker void sync_file_info_free(struct sync_file_info* info) __INTRODUCED_IN(26);
101*61046927SAndroid Build Coastguard Worker
102*61046927SAndroid Build Coastguard Worker __END_DECLS
103*61046927SAndroid Build Coastguard Worker
104*61046927SAndroid Build Coastguard Worker #endif /* ANDROID_SYNC_H */
105*61046927SAndroid Build Coastguard Worker
106*61046927SAndroid Build Coastguard Worker /** @} */
107