2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2019-09-28 15:43:24 -07:00
|
|
|
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "wpi/hal/Main.h"
|
2019-09-28 15:43:24 -07:00
|
|
|
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "wpi/util/condition_variable.hpp"
|
|
|
|
|
#include "wpi/util/mutex.hpp"
|
2019-09-28 15:43:24 -07:00
|
|
|
|
|
|
|
|
static void DefaultMain(void*);
|
|
|
|
|
static void DefaultExit(void*);
|
|
|
|
|
|
|
|
|
|
static bool gHasMain = false;
|
|
|
|
|
static void* gMainParam = nullptr;
|
|
|
|
|
static void (*gMainFunc)(void*) = DefaultMain;
|
|
|
|
|
static void (*gExitFunc)(void*) = DefaultExit;
|
|
|
|
|
static bool gExited = false;
|
|
|
|
|
struct MainObj {
|
2025-11-07 20:00:05 -05:00
|
|
|
wpi::util::mutex gExitMutex;
|
|
|
|
|
wpi::util::condition_variable gExitCv;
|
2019-09-28 15:43:24 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static MainObj* mainObj;
|
|
|
|
|
|
|
|
|
|
static void DefaultMain(void*) {
|
|
|
|
|
std::unique_lock lock{mainObj->gExitMutex};
|
|
|
|
|
mainObj->gExitCv.wait(lock, [] { return gExited; });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void DefaultExit(void*) {
|
|
|
|
|
std::lock_guard lock{mainObj->gExitMutex};
|
|
|
|
|
gExited = true;
|
|
|
|
|
mainObj->gExitCv.notify_all();
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
namespace wpi::hal::init {
|
2019-09-28 15:43:24 -07:00
|
|
|
void InitializeMain() {
|
|
|
|
|
static MainObj mO;
|
|
|
|
|
mainObj = &mO;
|
|
|
|
|
}
|
2025-11-07 20:00:05 -05:00
|
|
|
} // namespace wpi::hal::init
|
2019-09-28 15:43:24 -07:00
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
|
|
void HAL_SetMain(void* param, void (*mainFunc)(void*),
|
|
|
|
|
void (*exitFunc)(void*)) {
|
|
|
|
|
gHasMain = true;
|
|
|
|
|
gMainParam = param;
|
|
|
|
|
gMainFunc = mainFunc;
|
|
|
|
|
gExitFunc = exitFunc;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
HAL_Bool HAL_HasMain(void) {
|
|
|
|
|
return gHasMain;
|
|
|
|
|
}
|
2019-09-28 15:43:24 -07:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
void HAL_RunMain(void) {
|
|
|
|
|
gMainFunc(gMainParam);
|
|
|
|
|
}
|
2019-09-28 15:43:24 -07:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
void HAL_ExitMain(void) {
|
|
|
|
|
gExitFunc(gMainParam);
|
|
|
|
|
}
|
2019-09-28 15:43:24 -07:00
|
|
|
|
|
|
|
|
} // extern "C"
|