Symbian platform (C++)
SDK native APIs, runtime and tooling
Loading...
Searching...
No Matches
event_mailbox.h
Go to the documentation of this file.
1// Copyright 2026 The Symbian SDK Authors.
2// Licensed under the Apache License, Version 2.0.
3
4#ifndef SYMBIAN_CONCURRENCY_EVENT_MAILBOX_H_
5#define SYMBIAN_CONCURRENCY_EVENT_MAILBOX_H_
6
7#include <cstddef>
8#include <functional>
9#include <utility>
10#include <vector>
11
12#include <absl/status/status_macros.h>
13#include "absl/status/status.h"
15
16namespace symbian::concurrency {
17
18// A bounded cross-thread dispatch mailbox for an existing OS event thread.
19// It is not A11's Post/PostAt shared pool and never consumes the native
20// request semaphore; the caller drives it beside its native request owners.
22 public:
23 explicit EventMailbox(std::function<void()> wake,
24 std::size_t max_pending = 128)
25 : wake_(std::move(wake)), queue_(max_pending) {}
26
27 EventMailbox(const EventMailbox&) = delete;
29
31
32 absl::Status Enqueue(std::function<void()> callback) {
33 ABSL_RETURN_IF_ERROR(queue_.TryWrite(std::move(callback)));
34 wake_();
35 return absl::OkStatus();
36 }
37
38 // Execute no more than budget callbacks, always outside the mailbox lock.
39 // A callback that enqueues more work is processed on a later turn.
40 std::size_t DispatchReady(std::size_t budget = 64) {
41 if (budget == 0) {
42 return 0;
43 }
44 std::vector<std::function<void()>> ready;
45 const std::size_t initial = queue_.Size();
46 const std::size_t count = initial < budget ? initial : budget;
47 ready.reserve(count);
48 for (std::size_t i = 0; i < count; ++i) {
49 std::function<void()> callback;
50 if (absl::StatusOr<bool> read = queue_.TryRead(&callback);
51 !read.ok() || !read.value()) {
52 break;
53 }
54 ready.push_back(std::move(callback));
55 }
56 for (auto& callback : ready) {
57 callback();
58 }
59 if (Pending() != 0) {
60 wake_();
61 }
62 return ready.size();
63 }
64
65 std::size_t Pending() const { return queue_.Size(); }
66
67 void Close() { queue_.Discard(); }
68
69 private:
70 std::function<void()> wake_;
71 BoundedChannel<std::function<void()>> queue_;
72};
73
74} // namespace symbian::concurrency
75
76#endif // SYMBIAN_CONCURRENCY_EVENT_MAILBOX_H_
Definition bounded_channel.h:90
absl::StatusOr< bool > TryRead(T *absl_nonnull out)
Definition bounded_channel.h:140
absl::Status TryWrite(T &&item)
Definition bounded_channel.h:104
std::size_t Size() const
Definition bounded_channel.h:195
void Discard()
Definition bounded_channel.h:184
Definition bounded_channel.h:41
Definition event_mailbox.h:21
std::size_t Pending() const
Definition event_mailbox.h:65
EventMailbox(const EventMailbox &)=delete
~EventMailbox()
Definition event_mailbox.h:30
void Close()
Definition event_mailbox.h:67
EventMailbox(std::function< void()> wake, std::size_t max_pending=128)
Definition event_mailbox.h:23
std::size_t DispatchReady(std::size_t budget=64)
Definition event_mailbox.h:40
EventMailbox & operator=(const EventMailbox &)=delete
absl::Status Enqueue(std::function< void()> callback)
Definition event_mailbox.h:32
Definition native_camera_stream.cc:19
Definition bounded_channel.h:35