mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-21 01:01:43 +00:00
Add SafeThread to fix thread JNI shutdown races.
During JVM shutdown, some JNI calls may not return, so it's not possible to reliably perform a join() during static variable destruction (which occurs as the JVM unloads the JNI module). Also, due to static variable destruction, it's not safe to use any members of a static class instance from a separate thread of execution. SafeThread is a templated thread class and a related owner class that's designed for safe operation and shutdown of threads in the presence of callbacks that may not return. It also passes ownership of variables from the static instance to the thread, so the thread can safely operate until it exits (the last operation of the thread being to destroy its instance). Notifiers, RpcServer, and Logger now use SafeThread to ensure race-free destruction in both C++ and Java. All Java callback threads are now marked as Java daemon threads so they don't keep the JVM running after main() terminates. All Java callback threads are now named so their purpose is more easily identified in a debugger. Add SetRpcServerOnStart and SetRpcServerOnExit (similar to Listener).
This commit is contained in:
@@ -75,9 +75,8 @@ void SetListenerOnExit(std::function<void()> on_exit) {
|
||||
|
||||
unsigned int AddEntryListener(StringRef prefix, EntryListenerCallback callback,
|
||||
unsigned int flags) {
|
||||
Notifier& notifier = Notifier::GetInstance();
|
||||
unsigned int uid = notifier.AddEntryListener(prefix, callback, flags);
|
||||
notifier.Start();
|
||||
unsigned int uid =
|
||||
Notifier::GetInstance().AddEntryListener(prefix, callback, flags);
|
||||
if ((flags & NT_NOTIFY_IMMEDIATE) != 0)
|
||||
Storage::GetInstance().NotifyEntries(prefix, callback);
|
||||
return uid;
|
||||
@@ -89,9 +88,7 @@ void RemoveEntryListener(unsigned int entry_listener_uid) {
|
||||
|
||||
unsigned int AddConnectionListener(ConnectionListenerCallback callback,
|
||||
bool immediate_notify) {
|
||||
Notifier& notifier = Notifier::GetInstance();
|
||||
unsigned int uid = notifier.AddConnectionListener(callback);
|
||||
Notifier::GetInstance().Start();
|
||||
unsigned int uid = Notifier::GetInstance().AddConnectionListener(callback);
|
||||
if (immediate_notify) Dispatcher::GetInstance().NotifyConnections(callback);
|
||||
return uid;
|
||||
}
|
||||
@@ -106,6 +103,14 @@ bool NotifierDestroyed() { return Notifier::destroyed(); }
|
||||
* Remote Procedure Call Functions
|
||||
*/
|
||||
|
||||
void SetRpcServerOnStart(std::function<void()> on_start) {
|
||||
RpcServer::GetInstance().SetOnStart(on_start);
|
||||
}
|
||||
|
||||
void SetRpcServerOnExit(std::function<void()> on_exit) {
|
||||
RpcServer::GetInstance().SetOnExit(on_exit);
|
||||
}
|
||||
|
||||
void CreateRpc(StringRef name, StringRef def, RpcCallback callback) {
|
||||
Storage::GetInstance().CreateRpc(name, def, callback);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user