Added in [API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Constants](https://developer.android.com/reference/android/hardware/SyncFence#constants) \| [Inherited Constants](https://developer.android.com/reference/android/hardware/SyncFence#inhconstants) \| [Fields](https://developer.android.com/reference/android/hardware/SyncFence#lfields) \| [Ctors](https://developer.android.com/reference/android/hardware/SyncFence#pubctors) \| [Methods](https://developer.android.com/reference/android/hardware/SyncFence#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/android/hardware/SyncFence#inhmethods)

# SyncFence

*** ** * ** ***

` public final class SyncFence `   
` extends https://developer.android.com/reference/java/lang/Object ` ` implements https://developer.android.com/reference/java/lang/AutoCloseable, https://developer.android.com/reference/android/os/Parcelable `

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | android.hardware.SyncFence |

<br />

*** ** * ** ***

A SyncFence represents a synchronization primitive which signals when hardware units have completed work on a particular resource. They initially start in an unsignaled state and make a one-time transition to either a signaled or error state. SyncFences are created by various device APIs in response to submitting tasks to the device. They cannot be created nor signaled by userspace. As a result, this means that a SyncFence will always make forward progress.

SyncFence's generally come in one of two varieties. "Presentation fences" refer to a SyncFence when the writing to a buffer has finished. "Release fences" then refer to when the reading from a buffer has finished.

For example, a GPU rendering to a framebuffer may generate a synchronization fence, e.g., an EGLSync or VkFence, which signals when rendering has completed. Once the fence signals, then the backing storage for the framebuffer may be safely read from, such as for display or for media encoding. This would be referred to as a "presentation fence."

Similarly when using an `https://developer.android.com/reference/android/media/ImageWriter` it is possible that an `https://developer.android.com/reference/android/media/Image` returned by `https://developer.android.com/reference/android/media/ImageWriter#dequeueInputImage()` may already have a `https://developer.android.com/reference/android/media/Image#getFence()` set on it. This would be what is referred to as either a "release fence" or an "acqurie fence" and indicates the fence that the writer must wait on before writing to the underlying buffer. In the case of ImageWriter this is done automatically when eg `https://developer.android.com/reference/android/media/Image#getPlanes()` is called, however when using `https://developer.android.com/reference/android/media/Image#getHardwareBuffer()` it is the caller's responsibility to ensure the release fence has signaled before writing to the buffer.

<br />

**See also:**

- `https://developer.android.com/reference/android/opengl/EGLExt#eglDupNativeFenceFDANDROID(android.opengl.EGLDisplay,%20android.opengl.EGLSync)`
- `https://developer.android.com/reference/android/media/Image#getFence()`

## Summary

| ### Constants ||
|---|---|
| `long` | `https://developer.android.com/reference/android/hardware/SyncFence#SIGNAL_TIME_INVALID` An invalid signal time. |
| `long` | `https://developer.android.com/reference/android/hardware/SyncFence#SIGNAL_TIME_PENDING` A pending signal time. |

### Inherited constants

From interface `https://developer.android.com/reference/android/os/Parcelable` `int` `https://developer.android.com/reference/android/os/Parcelable#CONTENTS_FILE_DESCRIPTOR`

Descriptor bit used with `https://developer.android.com/reference/android/os/Parcelable#describeContents()`: indicates that the Parcelable object's flattened representation includes a file descriptor.
`int` `https://developer.android.com/reference/android/os/Parcelable#PARCELABLE_WRITE_RETURN_VALUE`

Flag for use with `https://developer.android.com/reference/android/os/Parcelable#writeToParcel(android.os.Parcel,%20int)`: the object being written is a return value, that is the result of a function such as "`Parcelable someFunction()`", "`void someFunction(out Parcelable)`", or "`void someFunction(inout Parcelable)`".

| ### Fields ||
|---|---|
| ` public static final https://developer.android.com/reference/android/os/Parcelable.Creator<https://developer.android.com/reference/android/hardware/SyncFence>` | `https://developer.android.com/reference/android/hardware/SyncFence#CREATOR` <br /> |

| ### Public constructors ||
|---|---|
| ` https://developer.android.com/reference/android/hardware/SyncFence#SyncFence(android.hardware.SyncFence)(https://developer.android.com/reference/android/hardware/SyncFence other) ` Creates a copy of the SyncFence from an existing one. |

| ### Public methods ||
|---|---|
| ` boolean` | ` https://developer.android.com/reference/android/hardware/SyncFence#await(java.time.Duration)(https://developer.android.com/reference/java/time/Duration timeout) ` Waits for a SyncFence to signal for up to the timeout duration. |
| ` boolean` | ` https://developer.android.com/reference/android/hardware/SyncFence#awaitForever()() ` Waits forever for a SyncFence to signal. |
| ` void` | ` https://developer.android.com/reference/android/hardware/SyncFence#close()() ` Close the SyncFence. |
| ` int` | ` https://developer.android.com/reference/android/hardware/SyncFence#describeContents()() ` Describe the kinds of special objects contained in this Parcelable instance's marshaled representation. |
| ` long` | ` https://developer.android.com/reference/android/hardware/SyncFence#getSignalTime()() ` Returns the time in nanoseconds that the fence signaled in the CLOCK_MONOTONIC time domain. |
| ` boolean` | ` https://developer.android.com/reference/android/hardware/SyncFence#isValid()() ` Checks if the SyncFile object is valid. |
| ` void` | ` https://developer.android.com/reference/android/hardware/SyncFence#writeToParcel(android.os.Parcel,%20int)(https://developer.android.com/reference/android/os/Parcel out, int flags) ` Flatten this object into a Parcel. |

### Inherited methods

From class ` https://developer.android.com/reference/java/lang/Object ` ` https://developer.android.com/reference/java/lang/Object` ` https://developer.android.com/reference/java/lang/Object#clone()() `

Creates and returns a copy of this object.
` boolean` ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) `

Indicates whether some other object is "equal to" this one.
` void` ` https://developer.android.com/reference/java/lang/Object#finalize()() `

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
` final https://developer.android.com/reference/java/lang/Class<?>` ` https://developer.android.com/reference/java/lang/Object#getClass()() `

Returns the runtime class of this `Object`.
` int` ` https://developer.android.com/reference/java/lang/Object#hashCode()() `

Returns a hash code value for the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#notify()() `

Wakes up a single thread that is waiting on this object's monitor.
` final void` ` https://developer.android.com/reference/java/lang/Object#notifyAll()() `

Wakes up all threads that are waiting on this object's monitor.
` https://developer.android.com/reference/java/lang/String` ` https://developer.android.com/reference/java/lang/Object#toString()() `

Returns a string representation of the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait()() `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*.
From interface ` https://developer.android.com/reference/java/lang/AutoCloseable ` ` abstract void` ` https://developer.android.com/reference/java/lang/AutoCloseable#close()() `

Closes this resource, relinquishing any underlying resources.
From interface ` https://developer.android.com/reference/android/os/Parcelable ` ` abstract int` ` https://developer.android.com/reference/android/os/Parcelable#describeContents()() `

Describe the kinds of special objects contained in this Parcelable instance's marshaled representation.
` abstract void` ` https://developer.android.com/reference/android/os/Parcelable#writeToParcel(android.os.Parcel,%20int)(https://developer.android.com/reference/android/os/Parcel dest, int flags) `

Flatten this object in to a Parcel.

## Constants

### SIGNAL_TIME_INVALID

Added in [API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static final long SIGNAL_TIME_INVALID
```

An invalid signal time. Represents either the signal time for a SyncFence that isn't valid (that is, `https://developer.android.com/reference/android/hardware/SyncFence#isValid()` is false), or if an error occurred while attempting to retrieve the signal time.

<br />

Constant Value: -1 (0xffffffffffffffff)

### SIGNAL_TIME_PENDING

Added in [API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static final long SIGNAL_TIME_PENDING
```

A pending signal time. This is equivalent to the max value of a long, representing an infinitely far point in the future.

<br />

Constant Value: 9223372036854775807 (0x7fffffffffffffff)

## Fields

### CREATOR

Added in [API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static final Creator<SyncFence> CREATOR
```

<br />

<br />

## Public constructors

### SyncFence

Added in [API level 35](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public SyncFence (SyncFence other)
```

Creates a copy of the SyncFence from an existing one. Both fences must be closed() independently.

<br />

| Parameters ||
|---|---|
| `other` | `SyncFence`: This value cannot be `null`. <br /> |

## Public methods

### await

Added in [API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public boolean await (Duration timeout)
```

Waits for a SyncFence to signal for up to the timeout duration. An invalid SyncFence, that is if `https://developer.android.com/reference/android/hardware/SyncFence#isValid()` is false, is treated equivalently to a SyncFence that has already signaled. That is, wait() will immediately return true.

<br />

| Parameters ||
|---|---|
| `timeout` | `Duration`: The timeout duration. If the duration is negative, then this waits forever. This value cannot be `null`. <br /> |

| Returns ||
|---|---|
| `boolean` | true if the fence signaled or isn't valid, false otherwise. <br /> |

### awaitForever

Added in [API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public boolean awaitForever ()
```

Waits forever for a SyncFence to signal. An invalid SyncFence, that is if `https://developer.android.com/reference/android/hardware/SyncFence#isValid()` is false, is treated equivalently to a SyncFence that has already signaled. That is, wait() will immediately return true.

<br />

| Returns ||
|---|---|
| `boolean` | true if the fence signaled or isn't valid, false otherwise. <br /> |

### close

Added in [API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void close ()
```

Close the SyncFence. This implementation closes the underlying OS resources allocated this stream.

<br />

### describeContents

Added in [API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public int describeContents ()
```

Describe the kinds of special objects contained in this Parcelable instance's marshaled representation. For example, if the object will include a file descriptor in the output of `https://developer.android.com/reference/android/os/Parcelable#writeToParcel(android.os.Parcel,%20int)`, the return value of this method must include the `https://developer.android.com/reference/android/os/Parcelable#CONTENTS_FILE_DESCRIPTOR` bit.

<br />

| Returns ||
|---|---|
| `int` | a bitmask indicating the set of special object types marshaled by this Parcelable object instance. Value is either `0` or - `https://developer.android.com/reference/android/os/Parcelable#CONTENTS_FILE_DESCRIPTOR` |

### getSignalTime

Added in [API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public long getSignalTime ()
```

Returns the time in nanoseconds that the fence signaled in the CLOCK_MONOTONIC time domain. This corresponds to `https://developer.android.com/reference/java/lang/System#nanoTime()` but may also be compared to `https://developer.android.com/reference/android/os/SystemClock#uptimeMillis()` after adjusting for milliseconds vs. nanoseconds. If the fence isn't valid, that is if `https://developer.android.com/reference/android/hardware/SyncFence#isValid()` is false, then this returns `https://developer.android.com/reference/android/hardware/SyncFence#SIGNAL_TIME_INVALID`. Similarly, if an error occurs while trying to access the signal time, then `https://developer.android.com/reference/android/hardware/SyncFence#SIGNAL_TIME_INVALID` is also returned. If the fence hasn't yet signaled, then `https://developer.android.com/reference/android/hardware/SyncFence#SIGNAL_TIME_PENDING` is returned.

<br />

| Returns ||
|---|---|
| `long` | The time the fence signaled, `https://developer.android.com/reference/android/hardware/SyncFence#SIGNAL_TIME_INVALID` if there's an error, or `https://developer.android.com/reference/android/hardware/SyncFence#SIGNAL_TIME_PENDING` if the fence hasn't signaled yet. <br /> |

### isValid

Added in [API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public boolean isValid ()
```

Checks if the SyncFile object is valid.

<br />

| Returns ||
|---|---|
| `boolean` | `true` if the file descriptor represents a valid, open file; `false` otherwise. <br /> |

### writeToParcel

Added in [API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void writeToParcel (Parcel out, 
                int flags)
```

Flatten this object into a Parcel.

<br />

| Parameters ||
|---|---|
| `out` | `Parcel`: The Parcel in which the object should be written. This value cannot be `null`. <br /> |
| `flags` | `int`: Additional flags about how the object should be written. May be `0` or `https://developer.android.com/reference/android/os/Parcelable#PARCELABLE_WRITE_RETURN_VALUE` <br /> |