File: WorkQueue.h

package info (click to toggle)
storm-lang 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,004 kB
  • sloc: ansic: 261,462; cpp: 140,405; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (91 lines) | stat: -rw-r--r-- 2,020 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#pragma once
#include "Core/Fn.h"
#include "Core/Array.h"
#include "Core/Event.h"
#include "Core/Timing.h"
#include "Compiler/Thread.h"
#include "Range.h"

namespace storm {
	namespace server {
		STORM_PKG(core.lang.server);

		class File;
		class Server;
		class WorkQueue;

		/**
		 * Work items. Subclass this class to provide custom messages.
		 *
		 * All work items which are considered equal are possibly reduced into one.
		 */
		class WorkItem : public ObjectOn<Compiler> {
			STORM_CLASS;
		public:
			// Create. Give a file that should possible be updated on completion.
			STORM_CTOR WorkItem(File *file);

			// File this item is regarding.
			File *file;

			// Execute this work. Gives a range to be updated.
			virtual Range STORM_FN run(WorkQueue *q);

			// Merge another work item with this one. Return true if the merge was possible.
			virtual Bool STORM_FN merge(WorkItem *o);
		};

		/**
		 * A queue of work to be delayed until the user is idle.
		 */
		class WorkQueue : public ObjectOn<Compiler> {
			STORM_CLASS;
		public:
			// Create.
			STORM_CTOR WorkQueue(Server *callbackTo);

			// Default idle time.
			enum {
				defaultIdleTime = 500
			};

			// How much time should pass before doing work?
			Nat idleTime;

			// Start the UThread waiting for inactivity.
			void STORM_FN start();

			// Stop the UThread waiting for inactivity.
			void STORM_FN stop();

			// Notify the wait about user activity.
			void STORM_FN poke();

			// Post a message.
			void STORM_FN post(WorkItem *item);

		private:
			// Dispatch callbacks to here.
			Server *callbackTo;

			// Is the worker thread running?
			Bool running;

			// Quit the worker thread?
			Bool quit;

			// Event used to make the worker thread wait.
			Event *event;

			// First possible moment we are allowed to do something.
			Moment startWork;

			// Work to do. Only contains unique instances of work-items.
			Array<WorkItem *> *work;

			// Main function in the worker thread.
			void CODECALL workerMain();
		};

	}
}