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