intuicio_frontend_simpleton/library/
jobs.rs

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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
use super::closure::Closure;
use crate::{Array, Function, Integer, Real, Reference, Transferable};
use intuicio_core::{
    context::Context, function::FunctionQuery, host::HostProducer, registry::Registry,
    IntuicioStruct,
};
use intuicio_derive::{intuicio_method, intuicio_methods, IntuicioStruct};
use std::{
    collections::VecDeque,
    sync::{
        atomic::{AtomicBool, Ordering},
        Arc, RwLock,
    },
    thread::{available_parallelism, spawn, JoinHandle},
    time::Duration,
};

type WorkerQueue = Arc<RwLock<VecDeque<JobRequest>>>;
type JobResult = Arc<RwLock<JobState>>;

#[derive(IntuicioStruct, Default)]
#[intuicio(name = "Jobs", module_name = "jobs", override_send = false)]
pub struct Jobs {
    #[intuicio(ignore)]
    workers: Vec<Worker>,
}

#[intuicio_methods(module_name = "jobs")]
impl Jobs {
    pub const HOST_PRODUCER_CUSTOM: &'static str = "Jobs::host_producer";

    #[allow(clippy::new_ret_no_self)]
    #[intuicio_method(use_context, use_registry)]
    pub fn new(context: &Context, registry: &Registry, workers_count: Reference) -> Reference {
        let host_producer = match context.custom::<HostProducer>(Self::HOST_PRODUCER_CUSTOM) {
            Some(host_producer) => host_producer.clone(),
            None => return Reference::null(),
        };
        let workers_count = workers_count
            .read::<Integer>()
            .map(|count| *count as usize)
            .unwrap_or_else(|| {
                available_parallelism()
                    .map(|count| count.get())
                    .unwrap_or_default()
            });
        Reference::new(
            Self {
                workers: (0..workers_count)
                    .map(|_| Worker::new(host_producer.clone()))
                    .collect(),
            },
            registry,
        )
    }

    #[intuicio_method()]
    pub fn sleep(seconds: Reference) -> Reference {
        std::thread::sleep(Duration::from_secs_f64(*seconds.read::<Real>().unwrap()));
        Reference::null()
    }

    #[intuicio_method(use_registry)]
    pub fn workers(registry: &Registry, jobs: Reference) -> Reference {
        let jobs = jobs.read::<Jobs>().unwrap();
        Reference::new_integer(jobs.workers.len() as Integer, registry)
    }

    #[intuicio_method(use_registry)]
    pub fn workers_alive(registry: &Registry, jobs: Reference) -> Reference {
        let jobs = jobs.read::<Jobs>().unwrap();
        Reference::new_array(
            jobs.workers
                .iter()
                .map(|worker| {
                    Reference::new_boolean(worker.is_running.load(Ordering::SeqCst), registry)
                })
                .collect(),
            registry,
        )
    }

    #[intuicio_method(use_registry)]
    pub fn schedule(
        registry: &Registry,
        jobs: Reference,
        executor: Reference,
        arguments: Reference,
    ) -> Reference {
        let jobs = jobs.read::<Jobs>().unwrap();
        let arguments = arguments.read::<Array>().unwrap();
        let (function_name, function_module_name, captured) =
            if let Some(function) = executor.read::<Function>() {
                let signature = function.handle().unwrap().signature();
                (
                    signature.name.to_owned(),
                    signature.module_name.to_owned(),
                    vec![],
                )
            } else if let Some(closure) = executor.read::<Closure>() {
                let signature = closure.function.handle().unwrap().signature();
                (
                    signature.name.to_owned(),
                    signature.module_name.to_owned(),
                    closure.captured.to_owned(),
                )
            } else {
                return Reference::null();
            };
        let worker = jobs
            .workers
            .iter()
            .filter(|worker| {
                worker
                    .handle
                    .as_ref()
                    .map(|handle| !handle.is_finished())
                    .unwrap_or_default()
            })
            .min_by(|a, b| {
                let a = a
                    .queue
                    .try_read()
                    .map(|queue| queue.len())
                    .unwrap_or_default();
                let b = b
                    .queue
                    .try_read()
                    .map(|queue| queue.len())
                    .unwrap_or_default();
                a.cmp(&b)
            });
        if let Some(worker) = worker {
            return Reference::new(
                worker.schedule(function_name, function_module_name, &captured, &arguments),
                registry,
            );
        }
        Reference::null()
    }
}

struct Worker {
    handle: Option<JoinHandle<()>>,
    is_running: Arc<AtomicBool>,
    queue: WorkerQueue,
    _running_job_result: Arc<RwLock<Option<JobResult>>>,
}

impl Drop for Worker {
    fn drop(&mut self) {
        self.is_running.store(false, Ordering::SeqCst);
        if let Some(handle) = self.handle.take() {
            let _ = handle.join();
        }
    }
}

impl Worker {
    pub fn new(host_producer: HostProducer) -> Self {
        let queue = WorkerQueue::default();
        let queue_ = queue.clone();
        let is_running = Arc::new(AtomicBool::new(false));
        let is_running_ = is_running.clone();
        let _running_job_result: Arc<RwLock<Option<JobResult>>> = Default::default();
        let running_job_result = _running_job_result.clone();
        Self {
            handle: Some(spawn(move || {
                Self::worker_thread(host_producer, is_running_, queue_, running_job_result);
            })),
            is_running,
            queue,
            _running_job_result,
        }
    }

    pub fn schedule(
        &self,
        function_name: String,
        function_module_name: Option<String>,
        captured: &[Reference],
        arguments: &[Reference],
    ) -> Job {
        let result = Job::default();
        if let Ok(mut queue) = self.queue.write() {
            queue.push_back(JobRequest {
                function_name,
                function_module_name,
                arguments: captured
                    .iter()
                    .chain(arguments.iter())
                    .map(|argument| Transferable::from(argument.clone()))
                    .collect(),
                result: result.result.clone(),
            });
        }
        result
    }

    fn consume_requests(
        is_running: &Arc<AtomicBool>,
        queue: &Arc<RwLock<VecDeque<JobRequest>>>,
        running_job_result: &Arc<RwLock<Option<JobResult>>>,
    ) {
        is_running.store(false, Ordering::SeqCst);
        if let Ok(mut result) = running_job_result.write() {
            if let Some(result) = result.as_mut() {
                if let Ok(mut result) = result.write() {
                    *result = JobState::Consumed;
                }
            }
        }
        if let Ok(mut queue) = queue.write() {
            while let Some(request) = queue.pop_front() {
                if let Ok(mut result) = request.result.write() {
                    *result = JobState::Consumed;
                }
            }
        }
    }

    fn worker_thread(
        host_producer: HostProducer,
        is_running: Arc<AtomicBool>,
        queue: Arc<RwLock<VecDeque<JobRequest>>>,
        running_job_result: Arc<RwLock<Option<JobResult>>>,
    ) {
        let panic_hook = std::panic::take_hook();
        let is_running_ = is_running.clone();
        let queue_ = queue.clone();
        let running_job_result_ = running_job_result.clone();
        std::panic::set_hook(Box::new(move |info| {
            Self::consume_requests(&is_running_, &queue_, &running_job_result_);
            panic_hook(info);
        }));

        let mut host = host_producer.produce();
        host.context()
            .set_custom(Jobs::HOST_PRODUCER_CUSTOM, host_producer);
        is_running.store(true, Ordering::SeqCst);
        while is_running.load(Ordering::SeqCst) {
            let request = queue
                .try_write()
                .ok()
                .and_then(|mut queue| queue.pop_front());
            if let Some(request) = request {
                let (context, registry) = host.context_and_registry();
                if let Some(function) = registry.find_function(FunctionQuery {
                    name: Some(request.function_name.into()),
                    module_name: request.function_module_name.map(|name| name.into()),
                    ..Default::default()
                }) {
                    if let Ok(mut result) = running_job_result.write() {
                        *result = Some(request.result.clone());
                    }
                    if let Ok(mut result) = request.result.write() {
                        *result = JobState::Running;
                    }
                    for argument in request.arguments.into_iter().rev() {
                        context.stack().push(Reference::from(argument));
                    }
                    function.invoke(context, registry);
                    let output = Transferable::from(context.stack().pop::<Reference>().unwrap());
                    if let Ok(mut result) = request.result.write() {
                        *result = JobState::Done(output);
                    }
                    if let Ok(mut result) = running_job_result.write() {
                        *result = None;
                    }
                }
            }
        }
        is_running.store(false, Ordering::SeqCst);
        Self::consume_requests(&is_running, &queue, &running_job_result);
    }
}

struct JobRequest {
    function_name: String,
    function_module_name: Option<String>,
    arguments: Vec<Transferable>,
    result: JobResult,
}

#[derive(Default)]
enum JobState {
    #[default]
    Pending,
    Running,
    Done(Transferable),
    Consumed,
}

impl JobState {
    fn consume(&mut self) -> Reference {
        let state = std::mem::replace(self, JobState::Consumed);
        if let Self::Done(transferable) = state {
            Reference::from(transferable)
        } else {
            *self = state;
            Reference::null()
        }
    }
}

#[derive(IntuicioStruct, Default, Clone)]
#[intuicio(name = "Job", module_name = "job", override_send = false)]
pub struct Job {
    #[intuicio(ignore)]
    result: JobResult,
}

#[intuicio_methods(module_name = "job")]
impl Job {
    #[intuicio_method(use_registry)]
    pub fn is_pending(registry: &Registry, job: Reference) -> Reference {
        let job = job.read::<Job>().unwrap();
        Reference::new_boolean(
            job.result
                .try_read()
                .map(|state| matches!(*state, JobState::Pending))
                .unwrap_or_default(),
            registry,
        )
    }

    #[intuicio_method(use_registry)]
    pub fn is_running(registry: &Registry, job: Reference) -> Reference {
        let job = job.read::<Job>().unwrap();
        Reference::new_boolean(
            job.result
                .try_read()
                .map(|state| matches!(*state, JobState::Running))
                .unwrap_or_default(),
            registry,
        )
    }

    #[intuicio_method(use_registry)]
    pub fn is_done(registry: &Registry, job: Reference) -> Reference {
        let job = job.read::<Job>().unwrap();
        Reference::new_boolean(
            job.result
                .try_read()
                .map(|state| matches!(*state, JobState::Done(_)))
                .unwrap_or_default(),
            registry,
        )
    }

    #[intuicio_method(use_registry)]
    pub fn is_consumed(registry: &Registry, job: Reference) -> Reference {
        let job = job.read::<Job>().unwrap();
        Reference::new_boolean(
            job.result
                .try_read()
                .map(|state| matches!(*state, JobState::Consumed))
                .unwrap_or_default(),
            registry,
        )
    }

    #[intuicio_method()]
    pub fn consume(mut job: Reference) -> Reference {
        let job = job.write::<Job>().unwrap();
        if let Ok(mut state) = job.result.try_write() {
            return state.consume();
        }
        Reference::null()
    }

    #[intuicio_method()]
    pub fn wait_then_consume(mut job: Reference) -> Reference {
        let job = job.write::<Job>().unwrap();
        loop {
            if let Ok(mut state) = job.result.try_write() {
                if matches!(*state, JobState::Done(_)) {
                    return state.consume();
                } else if matches!(*state, JobState::Consumed) {
                    return Reference::null();
                }
            }
        }
    }
}

pub fn install(registry: &mut Registry) {
    registry.add_type(Jobs::define_struct(registry));
    registry.add_type(Job::define_struct(registry));
    registry.add_function(Jobs::new__define_function(registry));
    registry.add_function(Jobs::sleep__define_function(registry));
    registry.add_function(Jobs::workers__define_function(registry));
    registry.add_function(Jobs::workers_alive__define_function(registry));
    registry.add_function(Jobs::schedule__define_function(registry));
    registry.add_function(Job::is_pending__define_function(registry));
    registry.add_function(Job::is_running__define_function(registry));
    registry.add_function(Job::is_done__define_function(registry));
    registry.add_function(Job::is_consumed__define_function(registry));
    registry.add_function(Job::consume__define_function(registry));
    registry.add_function(Job::wait_then_consume__define_function(registry));
}