|
20 | 20 | #define REALM_OS_BINDING_CALLBACK_THREAD_OBSERVER_HPP |
21 | 21 |
|
22 | 22 | #include <exception> |
23 | | -#include <functional> |
24 | | -#include <optional> |
25 | | - |
26 | 23 |
|
27 | 24 | namespace realm { |
28 | | -// Interface for bindings interested in registering callbacks before/after the ObjectStore thread runs. |
29 | | -// This is for example helpful to attach/detach the pthread to the JavaVM in order to be able to perform JNI calls. |
| 25 | +// Interface for observing the lifecycle of the worker thread used by |
| 26 | +// DefaultSocketProvider. This is required to be able to attach/detach the thread |
| 27 | +// to the JVM to be able to perform JNI calls. |
30 | 28 | struct BindingCallbackThreadObserver { |
31 | | - using NotificationCallback = std::function<void()>; |
32 | | - using ErrorCallback = std::function<bool(const std::exception&)>; |
33 | | - |
34 | | - // Create a BindingCallbackThreadObserver that can be used in SyncClientConfig |
35 | | - BindingCallbackThreadObserver(std::optional<NotificationCallback>&& did_create_thread, |
36 | | - std::optional<NotificationCallback>&& will_destroy_thread, |
37 | | - std::optional<ErrorCallback>&& error_handler) |
38 | | - : m_create_thread_callback{std::move(did_create_thread)} |
39 | | - , m_destroy_thread_callback{std::move(will_destroy_thread)} |
40 | | - , m_handle_error_callback{std::move(error_handler)} |
41 | | - { |
42 | | - } |
43 | | - |
44 | 29 | virtual ~BindingCallbackThreadObserver() = default; |
45 | 30 |
|
46 | | - /// |
47 | | - /// Execution Functions - check for a valid instance and if the function was set |
48 | | - /// |
49 | | - |
50 | | - // Call the stored create thread callback function with the id of this thread |
51 | | - // Can be overridden to provide a custom implementation |
52 | | - virtual void did_create_thread() |
53 | | - { |
54 | | - if (m_create_thread_callback) { |
55 | | - (*m_create_thread_callback)(); |
56 | | - } |
57 | | - } |
58 | | - |
59 | | - // Call the stored destroy thread callback function with the id of this thread |
60 | | - // Can be overridden to provide a custom implementation |
61 | | - virtual void will_destroy_thread() |
62 | | - { |
63 | | - if (m_destroy_thread_callback) { |
64 | | - (*m_destroy_thread_callback)(); |
65 | | - } |
66 | | - } |
67 | | - |
68 | | - // Call the stored handle error callback function with the id of this thread |
69 | | - // IMPORTANT: If a function is supplied that handles the exception, it must |
70 | | - // call abort() or cause the application to crash since the SyncClient will |
71 | | - // be in a bad state if this occurs and will not be able to shut down properly. |
72 | | - // Can be overridden to provide a custom implementation |
73 | | - // Return true if the exception was handled by this function, otherwise false |
74 | | - virtual bool handle_error(const std::exception& e) |
| 31 | + // Called on the thread shortly after it is created. This is guaranteed to |
| 32 | + // be called before any other callbacks to the SDK are made. |
| 33 | + virtual void did_create_thread() {} |
| 34 | + // Called on the thread shortly before it is destroyed. No further callbacks |
| 35 | + // to the SDK on the thread will be made after this is called. |
| 36 | + virtual void will_destroy_thread() {} |
| 37 | + // If has_handle_error() returns true, any uncaught exceptions from the |
| 38 | + // event loop are passed to this. If this returns true, the thread exits |
| 39 | + // cleanly, while if it returns false the exception is rethrown. |
| 40 | + virtual bool handle_error(const std::exception&) |
75 | 41 | { |
76 | | - if (!m_handle_error_callback) |
77 | | - return false; |
78 | | - |
79 | | - return (*m_handle_error_callback)(e); |
| 42 | + return false; |
80 | 43 | } |
81 | | - |
82 | | - // Return true if this event loop observer has a handle error callback defined |
83 | 44 | virtual bool has_handle_error() |
84 | 45 | { |
85 | | - return bool(m_handle_error_callback); |
| 46 | + return false; |
86 | 47 | } |
87 | | - |
88 | | -protected: |
89 | | - // Default constructor |
90 | | - BindingCallbackThreadObserver() = default; |
91 | | - |
92 | | - std::optional<NotificationCallback> m_create_thread_callback; |
93 | | - std::optional<NotificationCallback> m_destroy_thread_callback; |
94 | | - std::optional<ErrorCallback> m_handle_error_callback; |
95 | 48 | }; |
96 | 49 |
|
97 | 50 | } // namespace realm |
|
0 commit comments