mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-22 01:11:42 +00:00
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).
69 lines
2.0 KiB
C++
69 lines
2.0 KiB
C++
/*----------------------------------------------------------------------------*/
|
|
/* Copyright (c) FIRST 2015. All Rights Reserved. */
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
/* the project. */
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
#ifndef NT_NOTIFIER_H_
|
|
#define NT_NOTIFIER_H_
|
|
|
|
#include <functional>
|
|
|
|
#include "atomic_static.h"
|
|
#include "ntcore_cpp.h"
|
|
#include "SafeThread.h"
|
|
|
|
namespace nt {
|
|
|
|
class Notifier {
|
|
friend class NotifierTest;
|
|
public:
|
|
static Notifier& GetInstance() {
|
|
ATOMIC_STATIC(Notifier, instance);
|
|
return instance;
|
|
}
|
|
~Notifier();
|
|
|
|
void Start();
|
|
void Stop();
|
|
|
|
bool local_notifiers() const { return m_local_notifiers; }
|
|
static bool destroyed() { return s_destroyed; }
|
|
|
|
void SetOnStart(std::function<void()> on_start) { m_on_start = on_start; }
|
|
void SetOnExit(std::function<void()> on_exit) { m_on_exit = on_exit; }
|
|
|
|
unsigned int AddEntryListener(StringRef prefix,
|
|
EntryListenerCallback callback,
|
|
unsigned int flags);
|
|
void RemoveEntryListener(unsigned int entry_listener_uid);
|
|
|
|
void NotifyEntry(StringRef name, std::shared_ptr<Value> value,
|
|
unsigned int flags, EntryListenerCallback only = nullptr);
|
|
|
|
unsigned int AddConnectionListener(ConnectionListenerCallback callback);
|
|
void RemoveConnectionListener(unsigned int conn_listener_uid);
|
|
|
|
void NotifyConnection(bool connected, const ConnectionInfo& conn_info,
|
|
ConnectionListenerCallback only = nullptr);
|
|
|
|
private:
|
|
Notifier();
|
|
|
|
class Thread;
|
|
SafeThreadOwner<Thread> m_owner;
|
|
|
|
std::atomic_bool m_local_notifiers;
|
|
|
|
std::function<void()> m_on_start;
|
|
std::function<void()> m_on_exit;
|
|
|
|
ATOMIC_STATIC_DECL(Notifier)
|
|
static bool s_destroyed;
|
|
};
|
|
|
|
} // namespace nt
|
|
|
|
#endif // NT_NOTIFIER_H_
|