forked from youtube/cobalt_sandbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcondition_variable.h
125 lines (105 loc) · 4.67 KB
/
condition_variable.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright 2015 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Module Overview: Starboard Condition Variable module
//
// Defines an interface for condition variables.
#ifndef STARBOARD_CONDITION_VARIABLE_H_
#define STARBOARD_CONDITION_VARIABLE_H_
#include "starboard/export.h"
#include "starboard/mutex.h"
#include "starboard/time.h"
#include "starboard/types.h"
#ifdef __cplusplus
extern "C" {
#endif
// Max size of the SbConditionVariable type.
#define SB_CONDITION_VARIABLE_MAX_SIZE 80
// An opaque handle to a condition variable type with
// reserved memory buffer of size SB_CONDITION_VARIABLE_MAX_SIZE and
// aligned at void pointer type.
typedef union SbConditionVariable {
// Reserved memory in which the implementation should map its
// native condition variable type.
uint8_t condition_buffer[SB_CONDITION_VARIABLE_MAX_SIZE];
// Guarantees alignment of the type to a void pointer.
void* ptr;
} SbConditionVariable;
#ifdef __cplusplus
#define SB_CONDITION_VARIABLE_INITIALIZER \
{}
#else
#define SB_CONDITION_VARIABLE_INITIALIZER \
{ 0 }
#endif
// Enumeration of possible results from waiting on a condvar.
typedef enum SbConditionVariableResult {
// The wait completed because the condition variable was signaled.
kSbConditionVariableSignaled,
// The wait completed because it timed out, and was not signaled.
kSbConditionVariableTimedOut,
// The wait failed, either because a parameter wasn't valid, or the condition
// variable has already been destroyed, or something similar.
kSbConditionVariableFailed,
} SbConditionVariableResult;
// Returns whether the given result is a success.
static SB_C_INLINE bool SbConditionVariableIsSignaled(
SbConditionVariableResult result) {
return result == kSbConditionVariableSignaled;
}
// Creates a new condition variable to work with |opt_mutex|, which may be null,
// placing the newly created condition variable in |out_condition|.
//
// The return value indicates whether the condition variable could be created.
SB_EXPORT bool SbConditionVariableCreate(SbConditionVariable* out_condition,
SbMutex* opt_mutex);
// Destroys the specified SbConditionVariable. The return value indicates
// whether the destruction was successful. The behavior is undefined if other
// threads are currently waiting on this condition variable.
//
// |condition|: The SbConditionVariable to be destroyed. This invalidates the
// condition variable.
SB_EXPORT bool SbConditionVariableDestroy(SbConditionVariable* condition);
// Waits for |condition|, releasing the held lock |mutex|, blocking
// indefinitely, and returning the result. Behavior is undefined if |mutex| is
// not held.
SB_EXPORT SbConditionVariableResult
SbConditionVariableWait(SbConditionVariable* condition, SbMutex* mutex);
// Waits for |condition|, releasing the held lock |mutex|, blocking up to
// |timeout_duration|, and returning the acquisition result. Behavior is
// undefined if |mutex| is not held.
//
// |timeout_duration|: The maximum amount of time that function should wait
// for |condition|. If the |timeout_duration| value is less than or equal to
// zero, the function returns as quickly as possible with a
// kSbConditionVariableTimedOut result.
SB_EXPORT SbConditionVariableResult
SbConditionVariableWaitTimed(SbConditionVariable* condition,
SbMutex* mutex,
SbTime timeout_duration);
// Broadcasts to all current waiters of |condition| to stop waiting. This
// function wakes all of the threads waiting on |condition| while
// SbConditionVariableSignal wakes a single thread.
//
// |condition|: The condition that should no longer be waited for.
SB_EXPORT bool SbConditionVariableBroadcast(SbConditionVariable* condition);
// Signals the next waiter of |condition| to stop waiting. This function wakes
// a single thread waiting on |condition| while SbConditionVariableBroadcast
// wakes all threads waiting on it.
//
// |condition|: The condition that the waiter should stop waiting for.
SB_EXPORT bool SbConditionVariableSignal(SbConditionVariable* condition);
#ifdef __cplusplus
} // extern "C"
#endif
#endif // STARBOARD_CONDITION_VARIABLE_H_