MeshLib Documentation
Loading...
Searching...
No Matches
MRCommandLoop.h
Go to the documentation of this file.
1#pragma once
2#include "exports.h"
3#include <queue>
4#include <functional>
5#include <condition_variable>
6#include <mutex>
7#include <thread>
8
9namespace MR
10{
11
12// Additional command loop for external app control
14{
15public:
16 using CommandFunc = std::function<void()>;
17
18 // Specify execution in specific time of application start
19 enum class StartPosition
20 {
21 AfterWindowInit, // executes right after window is initialized
22 AfterSplashAppear, // executes after splash appeared
23 AfterPluginInit, // executes during splash, after plugins init)
24 AfterSplashHide, // executes after splash, to have valid main window context
25 AfterWindowAppear // executes after window appeared to have valid opengl context
26 };
27
28 // This function setups main thread id, it should be called before any command
29 MRVIEWER_API static void setMainThreadId( const std::thread::id& id );
30 MRVIEWER_API static std::thread::id getMainThreadId();
31 // Update state of command loop, only can rise
32 MRVIEWER_API static void setState( StartPosition state );
33
34 // Adds command to the end of command loop, can be performed from any thread
35 // do not block, so be careful with lambda captures
36 // note: state - specify execution in specific time of application start
37 MRVIEWER_API static void appendCommand( CommandFunc func, StartPosition state = StartPosition::AfterSplashHide );
38
39 // If caller thread is main - instantly run command, otherwise add command to the end of loop with
40 // StartPosition state = StartPosition::AfterSplash and blocks caller thread until command is done
41 MRVIEWER_API static void runCommandFromGUIThread( CommandFunc func );
42
43 // Execute all commands from loop
44 MRVIEWER_API static void processCommands();
45
46 // Clears the queue without executing the commands
47 // if closeLoop is true, does not accept any new commands
48 MRVIEWER_API static void removeCommands( bool closeLoop );
49
50private:
51 CommandLoop() = default;
53
54 static CommandLoop& instance_();
55
56 static void addCommand_( CommandFunc func, bool blockThread, StartPosition state );
57
58 struct Command
59 {
60 CommandFunc func;
62 std::condition_variable callerThreadCV;
63 std::thread::id threadId;
64 };
65
67
68 // if set then cannot accept new commands
69 bool queueClosed_{ false }; // marked true in `removeCommands`
70 std::thread::id mainThreadId_;
71 std::queue<std::shared_ptr<Command>> commands_;
72 std::mutex mutex_;
73};
74
75}
Definition MRCommandLoop.h:14
static MRVIEWER_API void runCommandFromGUIThread(CommandFunc func)
StartPosition
Definition MRCommandLoop.h:20
static MRVIEWER_API void processCommands()
std::function< void()> CommandFunc
Definition MRCommandLoop.h:16
static MRVIEWER_API void setMainThreadId(const std::thread::id &id)
static MRVIEWER_API void setState(StartPosition state)
static MRVIEWER_API void appendCommand(CommandFunc func, StartPosition state=StartPosition::AfterSplashHide)
static MRVIEWER_API std::thread::id getMainThreadId()
static MRVIEWER_API void removeCommands(bool closeLoop)
Definition MRCameraOrientationPlugin.h:8