Symbian platform (C++)
SDK native APIs, runtime and tooling
Loading...
Searching...
No Matches
property_watch.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_PROPERTY_WATCH_H_
5#define SYMBIAN_CONCURRENCY_PROPERTY_WATCH_H_
6
7#include <atomic>
8#include <functional>
9#include <memory>
10#include <utility>
11
12#include <absl/base/nullability.h>
13
16
17#if __has_include(<symbian/runtime.h>)
18#include <symbian/runtime.h>
19#else
20#include "abi.h"
21#endif
22
23namespace symbian::concurrency {
24
25// Owns one integer Publish & Subscribe property and one outstanding change
26// request. Only the event thread calls Open, Next, Set, DispatchReady and Close.
27// Worker threads may request Future cancellation through the supplied wakeup.
29 public:
30 explicit PropertyWatch(std::function<void()> wake) : wake_(std::move(wake)) {}
31
32 PropertyWatch(const PropertyWatch&) = delete;
34
36
37 absl::Status Open(int category, unsigned int key) {
38 if (closed_ || native_ != nullptr) {
39 return absl::FailedPreconditionError("Property owner already opened");
40 }
42 SymbianRuntimePropertyCreate(category, key, &native_),
43 "RProperty define and attach");
44 }
45
47 if (closed_ || native_ == nullptr) {
48 return FailedFuture<int>(
49 absl::FailedPreconditionError("Property owner is closed"));
50 }
51 if (current_ != nullptr) {
52 return FailedFuture<int>(
53 absl::FailedPreconditionError("Property request already pending"));
54 }
55 auto entry = std::make_shared<Entry>();
56 Future<int> future = entry->promise.future();
57 entry->promise.SetCancellationCallback(
58 [entry = std::weak_ptr<Entry>(entry), wake = wake_] {
59 if (auto owned = entry.lock()) {
60 owned->cancel_requested.store(true, std::memory_order_release);
61 wake();
62 }
63 });
64 current_ = entry; // Own state before Subscribe can complete inline.
65 if (const int submitted = SymbianRuntimePropertySubscribe(native_);
66 submitted != symbian::native_error::kNone) {
67 current_.reset();
68 entry->promise.SetError(
69 symbian::StatusFromNativeError(submitted, "RProperty subscribe"));
70 }
71 return future;
72 }
73
74 absl::Status Set(int value) {
75 if (closed_ || native_ == nullptr) {
76 return absl::FailedPreconditionError("Property owner is closed");
77 }
79 SymbianRuntimePropertySet(native_, value), "RProperty set");
80 }
81
83 auto entry = current_;
84 if (closed_ || entry == nullptr) {
85 return false;
86 }
87 if (entry->cancel_requested.load(std::memory_order_acquire)) {
89 }
90 if (!SymbianRuntimePropertyIsReady(native_)) {
91 return false;
92 }
93 int value = 0;
94 const int result = SymbianRuntimePropertyResult(native_, &value);
95 current_.reset(); // Remove before an inline OnReady callback.
96 if (result == symbian::native_error::kNone) {
97 entry->promise.SetValue(value);
98 } else {
99 entry->promise.SetError(
100 symbian::StatusFromNativeError(result, "RProperty change"));
101 }
102 return true;
103 }
104
105 void Close() {
106 if (closed_) {
107 return;
108 }
109 closed_ = true;
110 if (native_ != nullptr) {
111 SymbianRuntimePropertyClose(native_); // Cancels and drains first.
112 native_ = nullptr;
113 }
114 if (auto entry = std::move(current_); entry != nullptr) {
115 entry->promise.SetError(absl::CancelledError("Property owner closed"));
116 }
117 }
118
119 private:
120 struct Entry {
121 Promise<int> promise;
122 std::atomic<bool> cancel_requested{false};
123 };
124
125 std::function<void()> wake_;
126 SymbianRuntimePropertyState* absl_nullable native_ = nullptr;
127 std::shared_ptr<Entry> current_;
128 bool closed_ = false;
129};
130
131} // namespace symbian::concurrency
132
133#endif // SYMBIAN_CONCURRENCY_PROPERTY_WATCH_H_
void SymbianRuntimePropertyClose(SymbianRuntimePropertyState *absl_nullable state)
Definition sdk_property.cc:102
void SymbianRuntimePropertyCancel(SymbianRuntimePropertyState *absl_nullable state)
Definition sdk_property.cc:95
int SymbianRuntimePropertyCreate(int category, unsigned int key, SymbianRuntimePropertyState *absl_nullable *absl_nonnull state)
Definition sdk_property.cc:17
int SymbianRuntimePropertySubscribe(SymbianRuntimePropertyState *absl_nullable state)
Definition sdk_property.cc:49
int SymbianRuntimePropertyResult(SymbianRuntimePropertyState *absl_nullable state, int *absl_nullable value)
Definition sdk_property.cc:75
bool SymbianRuntimePropertyIsReady(const SymbianRuntimePropertyState *absl_nullable state)
Definition sdk_property.cc:70
int SymbianRuntimePropertySet(SymbianRuntimePropertyState *absl_nullable state, int value)
Definition sdk_property.cc:62
Definition bounded_channel.h:41
Definition property_watch.h:28
absl::Status Open(int category, unsigned int key)
Definition property_watch.h:37
Future< int > Next()
Definition property_watch.h:46
absl::Status Set(int value)
Definition property_watch.h:74
PropertyWatch(std::function< void()> wake)
Definition property_watch.h:30
void Close()
Definition property_watch.h:105
~PropertyWatch()
Definition property_watch.h:35
bool DispatchReady()
Definition property_watch.h:82
PropertyWatch & operator=(const PropertyWatch &)=delete
PropertyWatch(const PropertyWatch &)=delete
Definition native_camera_stream.cc:19
Definition bounded_channel.h:35
constexpr int kNone
Definition native_status.h:26
absl::Status StatusFromNativeError(int native_code, absl::string_view operation)
Definition native_status.h:44
Definition sdk_property.cc:9
libusb_context *absl_nullable value
Definition usb.cc:37