artf4107: GetInstance() calls are now atomic

C++11 guarantees construction of static variables to be atomic, so this patch takes advantage of that.

Change-Id: I4a3db0f19c5fa5461fef3b6b63d7b8fec596c962
This commit is contained in:
Tyler Veness
2015-06-24 04:25:10 -07:00
parent 368ad30d37
commit faedfa6ed4
11 changed files with 14 additions and 43 deletions

View File

@@ -50,7 +50,6 @@ class Scheduler : public ErrorBase, public NamedSendable {
void ProcessCommandAddition(Command *command);
static Scheduler *_instance;
Command::SubsystemSet m_subsystems;
MUTEX_ID m_buttonsLock = nullptr;
typedef std::vector<ButtonScheduler *> ButtonVector;

View File

@@ -16,8 +16,6 @@
#include <set>
#include <algorithm>
Scheduler *Scheduler::_instance = nullptr;
Scheduler::Scheduler() {
m_buttonsLock = initializeMutexNormal();
m_additionsLock = initializeMutexNormal();
@@ -38,8 +36,8 @@ Scheduler::~Scheduler() {
* @return the {@link Scheduler}
*/
Scheduler *Scheduler::GetInstance() {
if (_instance == nullptr) _instance = new Scheduler();
return _instance;
static Scheduler instance;
return &instance;
}
void Scheduler::SetEnabled(bool enabled) { m_enabled = enabled; }

View File

@@ -10,9 +10,8 @@
* how many times GetInstance is called.
*/
LiveWindow *LiveWindow::GetInstance() {
static LiveWindow *instance = new LiveWindow();
return instance;
static LiveWindow instance;
return &instance;
}
/**

View File

@@ -55,8 +55,6 @@ class CameraServer : public ErrorBase {
uint32_t size;
};
static CameraServer* s_instance;
public:
static CameraServer* GetInstance();
void SetImage(Image const* image);

View File

@@ -82,8 +82,6 @@ class Preferences : public ErrorBase, public ITableListener {
return 0;
}
static Preferences *_instance;
/** The semaphore for accessing the file */
MUTEX_ID m_fileLock = nullptr;
/** The semaphore for beginning reads and writes to the file */

View File

@@ -10,13 +10,10 @@
#include <netdb.h>
constexpr uint8_t CameraServer::kMagicNumber[];
CameraServer* CameraServer::s_instance = nullptr;
CameraServer* CameraServer::GetInstance() {
if (s_instance == nullptr) {
s_instance = new CameraServer;
}
return s_instance;
static CameraServer instance;
return &instance;
}
CameraServer::CameraServer()

View File

@@ -28,7 +28,6 @@ const double JOYSTICK_UNPLUGGED_MESSAGE_INTERVAL = 1.0;
Log().Get(level)
const uint32_t DriverStation::kJoystickPorts;
DriverStation* DriverStation::m_instance = nullptr;
/**
* DriverStation constructor.
@@ -73,7 +72,6 @@ DriverStation::DriverStation() {
DriverStation::~DriverStation() {
m_task.Stop();
m_instance = nullptr;
deleteMultiWait(m_waitForDataSem);
// Unregister our semaphore.
HALSetNewDataSem(nullptr);
@@ -111,10 +109,8 @@ void DriverStation::Run() {
* @return Pointer to the DS instance
*/
DriverStation* DriverStation::GetInstance() {
if (m_instance == nullptr) {
m_instance = new DriverStation();
}
return m_instance;
static DriverStation instance;
return &instance;
}
/**

View File

@@ -24,8 +24,6 @@ static const char *kFileName = "/home/lvuser/wpilib-preferences.ini";
static const char *kValuePrefix = "=\"";
/** The characters to put after the value */
static const char *kValueSuffix = "\"\n";
/** The singleton instance */
Preferences *Preferences::_instance = nullptr;
Preferences::Preferences()
: m_readTask("PreferencesReadTask", (FUNCPTR)Preferences::InitReadTask),
@@ -54,8 +52,8 @@ Preferences::~Preferences() {
* @return pointer to the {@link Preferences}
*/
Preferences *Preferences::GetInstance() {
if (_instance == nullptr) _instance = new Preferences;
return _instance;
static Preferences instance;
return &instance;
}
/**

View File

@@ -10,10 +10,8 @@ using namespace gazebo;
class MainNode {
public:
static MainNode* GetInstance() {
if (instance == nullptr) {
instance = new MainNode();
}
return instance;
static MainNode instance;
return &instance;
}
template<typename M>
@@ -39,8 +37,6 @@ public:
transport::NodePtr main;
private:
static MainNode* instance;
MainNode() {
gazebo::transport::init();
main = transport::NodePtr(new transport::Node());

View File

@@ -26,7 +26,6 @@ const uint32_t DriverStation::kBatteryChannel;
const uint32_t DriverStation::kJoystickPorts;
const uint32_t DriverStation::kJoystickAxes;
constexpr float DriverStation::kUpdatePeriod;
DriverStation* DriverStation::m_instance = nullptr;
uint8_t DriverStation::m_updateNumber = 0;
/**
@@ -70,7 +69,6 @@ DriverStation::DriverStation()
DriverStation::~DriverStation()
{
m_instance = nullptr;
deleteMultiWait(m_waitForDataSem);
deleteMutex(m_waitForDataMutex);
// TODO: Release m_stateSemaphore and m_joystickSemaphore?
@@ -81,11 +79,8 @@ DriverStation::~DriverStation()
*/
DriverStation* DriverStation::GetInstance()
{
if (m_instance == nullptr)
{
m_instance = new DriverStation();
}
return m_instance;
static DriverStation instance;
return &instance;
}
/**

View File

@@ -1,3 +0,0 @@
#include "simulation/MainNode.h"
MainNode* MainNode::instance = nullptr;