1 module creator.core.taskstack;
2 import core.thread.fiber;
3 
4 private {
5 __gshared:
6     Task[] tasks;
7     string status_ = "No pending tasks...";
8     float progress_ = -1;
9 }
10 
11 public:
12 /**
13     A task
14 */
15 struct Task {
16     /**
17         The name of the task
18     */
19     string name;
20 
21     /**
22         The task's worker
23     */
24     Fiber worker;
25 }
26 
27 /**
28     Adds task to the list
29 */
30 void incTaskAdd(string name, void delegate() worker) {
31     tasks ~= Task(name, new Fiber(worker));
32 }
33 
34 /**
35     Sets the status of task
36 */
37 void incTaskStatus(string status) {
38     status_ = status;
39 }
40 
41 /**
42     Gets the curently posted status
43 */
44 string incTaskGetStatus() {
45     return status_;
46 }
47 
48 /**
49     Gets the current progress of the current task
50 */
51 float incTaskGetProgress() {
52     return progress_;
53 }
54 
55 /**
56     Sets the progress of the current task
57 */
58 void incTaskProgress(float progress) {
59     progress_ = progress;
60 }
61 
62 /**
63     Gets count of pending tasks
64 */
65 size_t incTaskLength() {
66     return tasks.length;
67 }
68 
69 /**
70     Yields a task/fiber
71 */
72 void incTaskYield() {
73     Fiber.yield();
74 }
75 
76 /**
77     Updates tasks
78 */
79 void incTaskUpdate() {
80     if (tasks.length > 0) {
81         if (tasks[0].worker.state != Fiber.State.TERM) {
82             tasks[0].worker.call();
83         } else {
84             tasks = tasks[1..$];
85             progress_ = -1;
86         }
87 
88         if (tasks.length == 0) {
89             incTaskStatus("No pending tasks...");
90         }
91     }
92 }