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.h"
14
15namespace symbian::concurrency {
16
17// A bounded cross-thread dispatch mailbox for an existing OS event thread.
18// It is not A11's Post/PostAt shared pool and never consumes the native
19// request semaphore; the caller drives it beside its native request owners.
21 public:
22 explicit EventMailbox(std::function<void()> wake,
23 std::size_t max_pending = 128)
24 : wake_(std::move(wake)), queue_(max_pending) {}
25
26 EventMailbox(const EventMailbox&) = delete;
28
30
31 absl::Status Enqueue(std::function<void()> callback) {
32 absl::Status status = queue_.TryWrite(std::move(callback));
33 if (!status.ok()) {
34 return status;
35 }
36 wake_();
37 return absl::OkStatus();
38 }
39
40 // Execute no more than budget callbacks, always outside the mailbox lock.
41 // A callback that enqueues more work is processed on a later turn.
42 std::size_t DispatchReady(std::size_t budget = 64) {
43 if (budget == 0) {
44 return 0;
45 }
46 std::vector<std::function<void()>> ready;
47 const std::size_t initial = queue_.Size();
48 const std::size_t count = initial < budget ? initial : budget;
49 ready.reserve(count);
50 for (std::size_t i = 0; i < count; ++i) {
51 std::function<void()> callback;
52 absl::StatusOr<bool> read = queue_.TryRead(&callback);
53 if (!read.ok() || !read.value()) {
54 break;
55 }
56 ready.push_back(std::move(callback));
57 }
58 for (auto& callback : ready) {
59 callback();
60 }
61 if (Pending() != 0) {
62 wake_();
63 }
64 return ready.size();
65 }
66
67 std::size_t Pending() const { return queue_.Size(); }
68
69 void Close() { queue_.Discard(); }
70
71 private:
72 std::function<void()> wake_;
73 BoundedChannel<std::function<void()>> queue_;
74};
75
76} // namespace symbian::concurrency
77
78#endif // SYMBIAN_CONCURRENCY_EVENT_MAILBOX_H_
Definition bounded_channel.h:83
absl::StatusOr< bool > TryRead(T *out)
Definition bounded_channel.h:136
absl::Status TryWrite(T &&item)
Definition bounded_channel.h:97
std::size_t Size() const
Definition bounded_channel.h:191
void Discard()
Definition bounded_channel.h:180
Definition bounded_channel.h:38
Definition event_mailbox.h:20
std::size_t Pending() const
Definition event_mailbox.h:67
EventMailbox(const EventMailbox &)=delete
~EventMailbox()
Definition event_mailbox.h:29
void Close()
Definition event_mailbox.h:69
EventMailbox(std::function< void()> wake, std::size_t max_pending=128)
Definition event_mailbox.h:22
std::size_t DispatchReady(std::size_t budget=64)
Definition event_mailbox.h:42
EventMailbox & operator=(const EventMailbox &)=delete
absl::Status Enqueue(std::function< void()> callback)
Definition event_mailbox.h:31
Definition exception_count.cc:8
Definition bounded_channel.h:32