Skip to content

Commit 6e5ff39

Browse files
refactor: Replace starboard::ConditionVariable in OpenMaxComponentBase (#6774)
Replaces Starboard's Mutex and ConditionVariable with their `std` counterparts (`std::mutex`, `std::condition_variable`, `std::unique_lock`, and `std::lock_guard`) in `OpenMaxComponentBase`. This change also simplifies the event waiting logic in `WaitForCommandCompletion`. #vibe-coded Bug: 390503926 --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent 64bd498 commit 6e5ff39

File tree

2 files changed

+25
-28
lines changed

2 files changed

+25
-28
lines changed

starboard/raspi/shared/open_max/open_max_component_base.cc

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <pthread.h>
1818

1919
#include <algorithm>
20+
#include <mutex>
2021

2122
#include "starboard/common/check_op.h"
2223
#include "starboard/configuration.h"
@@ -51,10 +52,7 @@ void InitializeOpenMax() {
5152
} // namespace
5253

5354
OpenMaxComponentBase::OpenMaxComponentBase(const char* name)
54-
: event_condition_variable_(mutex_),
55-
handle_(NULL),
56-
input_port_(kInvalidPort),
57-
output_port_(kInvalidPort) {
55+
: handle_(NULL), input_port_(kInvalidPort), output_port_(kInvalidPort) {
5856
InitializeOpenMax();
5957

6058
OMX_CALLBACKTYPE callbacks;
@@ -100,22 +98,19 @@ void OpenMaxComponentBase::SendCommand(OMX_COMMANDTYPE command, int param) {
10098
}
10199

102100
void OpenMaxComponentBase::WaitForCommandCompletion() {
103-
for (;;) {
104-
ScopedLock scoped_lock(mutex_);
105-
for (EventDescriptions::iterator iter = event_descriptions_.begin();
106-
iter != event_descriptions_.end(); ++iter) {
107-
if (iter->event == OMX_EventCmdComplete) {
108-
event_descriptions_.erase(iter);
109-
return;
110-
}
111-
// Special case for OMX_CommandStateSet.
112-
if (iter->event == OMX_EventError && iter->data1 == OMX_ErrorSameState) {
113-
event_descriptions_.erase(iter);
114-
return;
115-
}
116-
}
117-
event_condition_variable_.Wait();
118-
}
101+
std::unique_lock lock(mutex_);
102+
EventDescriptions::iterator it;
103+
event_condition_variable_.wait(lock, [this, &it] {
104+
it = std::find_if(event_descriptions_.begin(), event_descriptions_.end(),
105+
[](const EventDescription& desc) {
106+
return desc.event == OMX_EventCmdComplete ||
107+
(desc.event == OMX_EventError &&
108+
desc.data1 == OMX_ErrorSameState);
109+
});
110+
return it != event_descriptions_.end();
111+
});
112+
113+
event_descriptions_.erase(it);
119114
}
120115

121116
void OpenMaxComponentBase::SendCommandAndWaitForCompletion(
@@ -139,14 +134,16 @@ OMX_ERRORTYPE OpenMaxComponentBase::OnEvent(OMX_EVENTTYPE event,
139134
return OMX_ErrorNone;
140135
}
141136

142-
ScopedLock scoped_lock(mutex_);
143137
EventDescription event_desc;
144138
event_desc.event = event;
145139
event_desc.data1 = data1;
146140
event_desc.data2 = data2;
147141
event_desc.event_data = event_data;
148-
event_descriptions_.push_back(event_desc);
149-
event_condition_variable_.Signal();
142+
{
143+
std::lock_guard lock(mutex_);
144+
event_descriptions_.push_back(event_desc);
145+
}
146+
event_condition_variable_.notify_one();
150147

151148
return OMX_ErrorNone;
152149
}

starboard/raspi/shared/open_max/open_max_component_base.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323
#include <interface/vcos/vcos_logging.h>
2424
#include <interface/vmcs_host/vchost.h>
2525

26+
#include <condition_variable>
27+
#include <mutex>
2628
#include <vector>
2729

28-
#include "starboard/common/condition_variable.h"
2930
#include "starboard/common/log.h"
30-
#include "starboard/common/mutex.h"
3131
#include "starboard/shared/internal_only.h"
3232

3333
namespace starboard {
@@ -126,9 +126,9 @@ class OpenMaxComponentBase {
126126
int output_port_;
127127

128128
private:
129-
Mutex mutex_;
130-
ConditionVariable event_condition_variable_;
131-
EventDescriptions event_descriptions_;
129+
std::mutex mutex_;
130+
std::condition_variable event_condition_variable_;
131+
EventDescriptions event_descriptions_; // Guarded by |mutex_|.
132132
};
133133

134134
} // namespace open_max

0 commit comments

Comments
 (0)