1 /*******************************************************************************
2 
3     Support for scheduler config and running an app's main logic in a Task.
4 
5     Copyright:
6         Copyright (c) 2018 dunnhumby Germany GmbH.
7         All rights reserved.
8 
9     License:
10         Boost Software License Version 1.0. See LICENSE_BOOST.txt for details.
11         Alternatively, this file may be distributed under the terms of the Tango
12         3-Clause BSD License (see LICENSE_BSD.txt for details).
13 
14 *******************************************************************************/
15 
16 module ocean.application.components.TaskScheduler;
17 
18 import ocean.meta.types.Qualifiers;
19 import ocean.core.array.Search : find;
20 import ocean.core.Enforce;
21 import ocean.meta.codegen.Identifier;
22 import ocean.task.Scheduler;
23 import ocean.task.Task;
24 import ocean.text.convert.Integer;
25 import ocean.util.config.ConfigParser;
26 
27 /*******************************************************************************
28 
29     Parses the task scheduler config from the provided config parser.
30 
31     Params:
32         parser = config parser instance
33         config = scheduler config instance to set fields of
34 
35 *******************************************************************************/
36 
37 public void parseSchedulerConfig ( ConfigParser parser,
38     ref SchedulerConfiguration config )
39 {
40     if (parser is null)
41         return;
42 
43     static immutable category = "SCHEDULER";
44 
45     foreach (idx, ref field; config.tupleof)
46     {
47         enum name = identifier!(SchedulerConfiguration.tupleof[idx]);
48         static if (name != "specialized_pools")
49         {
50             field = parser.get(
51                 category,
52                 name,
53                 field
54             );
55         }
56     }
57 
58     auto specialized_pools = parser.getList!(istring)(
59         category, "specialized_pools", null);
60 
61     foreach (line; specialized_pools)
62     {
63         if (line.length == 0)
64             continue;
65 
66         auto idx = find(line, ':');
67         enforce(
68             idx < line.length,
69             "Malformed configuration for scheduler"
70         );
71 
72         size_t size;
73         enforce(
74             toInteger(line[idx+1 .. $], size),
75             "Malformed configuration for scheduler"
76         );
77 
78         config.specialized_pools ~=
79             SchedulerConfiguration.PoolDescription(line[0 .. idx], size);
80     }
81 }
82 
83 /*******************************************************************************
84 
85     Runs the specified delegate in a task.
86 
87     Params:
88         dg = application main delegate to run in a task
89 
90     Returns:
91         exit code to return to the OS
92 
93 *******************************************************************************/
94 
95 public int runInTask ( scope int delegate () dg )
96 {
97     auto task = new class Task {
98         int delegate() dg;
99         int result = -1;
100 
101         override void run ( )
102         {
103             this.result = this.dg();
104         }
105     };
106 
107     task.dg = dg;
108     theScheduler.queue(task);
109     theScheduler.eventLoop();
110 
111     return task.result;
112 }