1 // Copyright 2016 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef LIB_SYNC_COMPLETION_H_
6 #define LIB_SYNC_COMPLETION_H_
7 
8 #include <zircon/compiler.h>
9 #include <zircon/types.h>
10 
11 __BEGIN_CDECLS
12 
13 typedef struct sync_completion {
14     zx_futex_t futex;
15 
16 #ifdef __cplusplus
sync_completionsync_completion17     sync_completion()
18         : futex(0) {}
19 #endif
20 } sync_completion_t;
21 
22 #if !defined(__cplusplus)
23 #define SYNC_COMPLETION_INIT ((sync_completion_t){0})
24 #endif
25 
26 // Returns ZX_ERR_TIMED_OUT if timeout elapses, and ZX_OK if woken by
27 // a call to sync_completion_signal or if the completion has already been
28 // signaled.
29 zx_status_t sync_completion_wait(sync_completion_t* completion, zx_duration_t timeout);
30 
31 // Returns ZX_ERR_TIMED_OUT if deadline elapses, and ZX_OK if woken by
32 // a call to sync_completion_signal or if the completion has already been
33 // signaled.
34 zx_status_t sync_completion_wait_deadline(sync_completion_t* completion, zx_time_t deadline);
35 
36 // Awakens all waiters on the completion, and marks it as
37 // signaled. Waits after this call but before a reset of the
38 // completion will also see the signal and immediately return.
39 void sync_completion_signal(sync_completion_t* completion);
40 
41 // Marks the completion as signaled, but doesn't awaken all waiters
42 // right away. Instead, all waiters are requeued to the |futex|.
43 // Waits after this call but before a reset of the
44 // completion will also see the signal and immediately return.
45 //
46 // Intended to be used by libsync internally, e.g. the condition variable
47 // implementation.
48 void sync_completion_signal_requeue(sync_completion_t* completion, zx_futex_t* futex);
49 
50 // Resets the completion's signaled state to unsignaled.
51 void sync_completion_reset(sync_completion_t* completion);
52 
53 __END_CDECLS
54 
55 #endif // LIB_SYNC_COMPLETION_H_
56