Tweak format of command help files and do some further command cleanup
[lsnes.git] / include / core / queue.hpp
blob63209f1d237eb4141fcfaa07ebd455a3e14f0915
1 #ifndef _queue__hpp__included__
2 #define _queue__hpp__included__
4 #include "library/exrethrow.hpp"
5 #include "library/keyboard.hpp"
6 #include "library/threads.hpp"
7 #include <deque>
9 namespace command
11 class group;
14 /**
15 * Information about keypress.
17 struct keypress_info
19 /**
20 * Create null keypress (no modifiers, NULL key and released).
22 keypress_info();
23 /**
24 * Create new keypress.
26 keypress_info(keyboard::modifier_set mod, keyboard::key& _key, short _value);
27 /**
28 * Create new keypress (two keys).
30 keypress_info(keyboard::modifier_set mod, keyboard::key& _key, keyboard::key& _key2, short _value);
31 /**
32 * Modifier set.
34 keyboard::modifier_set modifiers;
35 /**
36 * The actual key (first)
38 keyboard::key* key1;
39 /**
40 * The actual key (second)
42 keyboard::key* key2;
43 /**
44 * Value for the press
46 short value;
49 template<typename T>
50 void functor_call_helper2(void* args)
52 (*reinterpret_cast<T*>(args))();
53 delete reinterpret_cast<T*>(args);
56 struct input_queue
58 struct function_queue_entry
60 std::function<void()> fn;
61 std::function<void(std::exception& e)> onerror;
64 //Queue stuff.
65 threads::lock queue_lock;
66 threads::cv queue_condition;
67 std::deque<keypress_info> keypresses;
68 std::deque<std::pair<const char*, std::string>> commands;
69 std::deque<function_queue_entry> functions;
70 volatile uint64_t functions_executed;
71 volatile uint64_t next_function;
72 volatile bool system_thread_available;
73 bool queue_function_run;
74 /**
75 * Ctor.
77 input_queue(command::group& _command);
78 /**
79 * Queue keypress.
81 * - Can be called from any thread.
83 * Parameter k: The keypress to queue.
85 void queue(const keypress_info& k) throw(std::bad_alloc);
86 /**
87 * Queue command.
89 * - Can be called from any thread.
91 * Parameter c: The command to queue.
93 void queue(const std::string& c) throw(std::bad_alloc);
94 /**
95 * Queue command and arguments.
97 * - Can be called from any thread.
99 * Parameter c: The command to queue.
100 * Parameter a: The arguments for function.
102 void queue(const char* c, const std::string& a) throw(std::bad_alloc);
104 * Queue function to be called in emulation thread.
106 * - Can be called from any thread (exception: Synchronous mode can not be used from emulation nor main threads).
108 * Parameter f: The function to execute.
109 * Parameter onerror: Function to call on error.
110 * Parameter arg: Argument to pass to the function.
111 * Parameter sync: If true, execute function call synchronously, else asynchronously.
113 void queue(std::function<void()> f, std::function<void(std::exception& e)> onerror, bool sync)
114 throw(std::bad_alloc);
116 * Run all queues.
118 void run_queues() throw();
120 * Call function synchronously in emulation thread.
122 void run(std::function<void()> fn)
124 exrethrow::storage ex;
125 queue(fn, [&ex](std::exception& e) { ex = exrethrow::storage(e); }, true);
126 if(ex) ex.rethrow();
129 * Queue asynchrous function in emulation thread.
131 void run_async(std::function<void()> fn,
132 std::function<void(std::exception& e)> onerror)
134 queue(fn, onerror, false);
137 * Run internal queues.
139 void run_queue(bool unlocked) throw();
140 private:
141 command::group& command;
144 #endif