1 /*******************************************************************************
2 
3     DelegateJobNotification. Used for notifying jobs from the AsyncIO.
4     Allows the job to specify how it will be suspended and resumed via
5     delegates.
6 
7     resume_job delegate is always requried, unlike suspend_job delegate: in
8     the case where the job needs to suspend itself at the convenient
9     location, no suspend_job delegate should be passed. One usage of this
10     behaviour would be to allow resuming the job from the several
11     external systems (e.g. from the disk IO and from the network), allowing
12     the job to suspend itself at the convenient point where it's possible
13     to distinguish if the job is resumed from the network or from the
14     AsyncIO.
15 
16     Copyright:
17         Copyright (c) 2018 dunnhumby Germany GmbH.
18         All rights reserved.
19 
20     License:
21         Boost Software License Version 1.0. See LICENSE.txt for details.
22 
23 *******************************************************************************/
24 
25 module ocean.util.aio.DelegateJobNotification;
26 
27 import ocean.util.aio.JobNotification;
28 import ocean.core.Verify;
29 
30 /// ditto
31 class DelegateJobNotification: JobNotification
32 {
33     import ocean.core.Enforce: enforce;
34 
35     /***************************************************************************
36 
37         Delegate to resume the suspended job.
38 
39     ***************************************************************************/
40 
41     private void delegate() resume_job;
42 
43     /***************************************************************************
44 
45         Delegate to suspend the running job.
46 
47     ***************************************************************************/
48 
49     private void delegate() suspend_job;
50 
51 
52     /***************************************************************************
53 
54         Constructor. Initializes DelegateJobNotification.
55 
56         Params:
57             resume_job = delegate to call to resume suspended job
58             suspend_job = delegate to call to suspend current job, null
59                               if that should not be possible
60 
61     ***************************************************************************/
62 
63     public this (scope void delegate() resume_job,
64             scope void delegate() suspend_job = null)
65     {
66         this.initialise(resume_job, suspend_job);
67     }
68 
69 
70     /***************************************************************************
71 
72         Initialization method. Initializes ManualJobNotification (separated
73         from constructor for convenient use in reusable pool).
74 
75         Params:
76             resume_job = delegate to call to resume suspended job
77             suspend_job = delegate to call to suspend current job, null
78                               if that should not be possible
79 
80     ***************************************************************************/
81 
82     public typeof(this) initialise (scope void delegate() resume_job,
83             scope void delegate() suspend_job = null)
84     {
85         this.resume_job = resume_job;
86         this.suspend_job = suspend_job;
87         return this;
88     }
89 
90     /***************************************************************************
91 
92         Yields the control to the suspended job, indicating that the aio
93         operation has been done.
94 
95     ***************************************************************************/
96 
97     public override void wake_ ()
98     {
99         this.resume_job();
100     }
101 
102     /***************************************************************************
103 
104         Cedes the control from the suspendable job, waiting for the aio
105         operation to be done.
106 
107     ***************************************************************************/
108 
109     public override void wait_ ()
110     {
111         verify(&this.suspend_job !is null,
112                 "This job is not allowed to be suspended.");
113         this.suspend_job();
114     }
115 }