Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e4b9fcd6 authored by Zach Johnson's avatar Zach Johnson
Browse files

Add SentinelWorkItem, to allow posting a work item and waiting for completion

Stopgap until we have a better way to handle threads and fuzzing.
Test: fuzz test I'm working on

Change-Id: Ie1b3c0775dfbf40045d0322bd98cadc845d411dc
parent b718d671
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@
 */

#include "fuzzing/helpers.h"
#include "common/bind.h"

namespace bluetooth {
namespace fuzzing {
@@ -36,5 +37,16 @@ std::vector<std::vector<uint8_t>> SplitInput(const uint8_t* data, size_t size, c
  return result;
}

void SentinelWorkItem::notify_handler_quiesced() {
  quiesce_promise_->set_value();
}

void SentinelWorkItem::WaitUntilFinishedOn(os::Handler* handler) {
  quiesce_promise_ = std::make_unique<std::promise<void>>();
  handler->Post(common::Bind(&SentinelWorkItem::notify_handler_quiesced, common::Unretained(this)));
  quiesce_promise_->get_future().wait_for(std::chrono::milliseconds(300));
  quiesce_promise_ = nullptr;
}

}  // namespace fuzzing
}  // namespace bluetooth
+9 −0
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@

#include <cstdint>
#include <vector>
#include "os/handler.h"

namespace bluetooth {
namespace fuzzing {
@@ -25,5 +26,13 @@ namespace fuzzing {
std::vector<std::vector<uint8_t>> SplitInput(const uint8_t* data, size_t size, const uint8_t* separator,
                                             size_t separatorSize);

class SentinelWorkItem {
 public:
  void WaitUntilFinishedOn(os::Handler* handler);

 private:
  void notify_handler_quiesced();
  std::unique_ptr<std::promise<void>> quiesce_promise_;
};
}
}  // namespace bluetooth