1 // Copyright (c) 2013, Thomas Goyne <plorkyeran@aegisub.org>
2 //
3 // Permission to use, copy, modify, and distribute this software for any
4 // purpose with or without fee is hereby granted, provided that the above
5 // copyright notice and this permission notice appear in all copies.
6 //
7 // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10 // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12 // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13 // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14 //
15 // Aegisub Project http://www.aegisub.org/
16 
17 #include <functional>
18 #include <memory>
19 
20 namespace agi {
21 	namespace dispatch {
22 		typedef std::function<void()> Thunk;
23 
24 		class Queue {
25 			virtual void DoInvoke(Thunk thunk)=0;
26 		public:
~Queue()27 			virtual ~Queue() { }
28 
29 			/// Invoke the thunk on this processing queue, returning immediately
30 			void Async(Thunk thunk);
31 
32 			/// Invoke the thunk on this processing queue, returning only when
33 			/// it's complete
34 			void Sync(Thunk thunk);
35 		};
36 
37 		/// Initialize the dispatch thread pools
38 		/// @param invoke_main A function which invokes the thunk on the GUI thread
39 		void Init(std::function<void (Thunk)> invoke_main);
40 
41 		/// Get the main queue, which runs on the GUI thread
42 		Queue& Main();
43 
44 		/// Get the generic background queue, which runs thunks in parallel
45 		Queue& Background();
46 
47 		/// Create a new serial queue
48 		std::unique_ptr<Queue> Create();
49 	}
50 }
51