blob: a37c2b098ccec104c8c99850882f56d82e038296 [file] [log] [blame]
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -08001/*----------------------------------------------------------------------------*/
2/* Copyright (c) 2019 FIRST. All Rights Reserved. */
3/* Open Source Software - may be modified and shared by FRC teams. The code */
4/* must be accompanied by the FIRST BSD license file in the root directory of */
5/* the project. */
6/*----------------------------------------------------------------------------*/
7
8#include "hal/Main.h"
9
10#include <wpi/condition_variable.h>
11#include <wpi/mutex.h>
12
13static void DefaultMain(void*);
14static void DefaultExit(void*);
15
16static bool gHasMain = false;
17static void* gMainParam = nullptr;
18static void (*gMainFunc)(void*) = DefaultMain;
19static void (*gExitFunc)(void*) = DefaultExit;
20static bool gExited = false;
21struct MainObj {
22 wpi::mutex gExitMutex;
23 wpi::condition_variable gExitCv;
24};
25
26static MainObj* mainObj;
27
28static void DefaultMain(void*) {
29 std::unique_lock lock{mainObj->gExitMutex};
30 mainObj->gExitCv.wait(lock, [] { return gExited; });
31}
32
33static void DefaultExit(void*) {
34 std::lock_guard lock{mainObj->gExitMutex};
35 gExited = true;
36 mainObj->gExitCv.notify_all();
37}
38
39namespace hal {
40namespace init {
41void InitializeMain() {
42 static MainObj mO;
43 mainObj = &mO;
44}
45} // namespace init
46} // namespace hal
47
48extern "C" {
49
50void HAL_SetMain(void* param, void (*mainFunc)(void*),
51 void (*exitFunc)(void*)) {
52 gHasMain = true;
53 gMainParam = param;
54 gMainFunc = mainFunc;
55 gExitFunc = exitFunc;
56}
57
58HAL_Bool HAL_HasMain(void) { return gHasMain; }
59
60void HAL_RunMain(void) { gMainFunc(gMainParam); }
61
62void HAL_ExitMain(void) { gExitFunc(gMainParam); }
63
64} // extern "C"