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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
#![feature(asm)]
#![feature(naked_functions)]
use std::ptr;
/// 256 kb stack
pub const DEFAULT_STACK_SIZE: usize = 1024 * 1024 * 2;

// TODO: Maybe make this thing thread_local?
static mut RUNTIME: usize = 0;

pub use greenie_proc::greenify;

pub struct Runtime {
    threads: Vec<Thread>,
    current: usize,
}

#[derive(PartialEq, Eq, Debug)]
pub enum State {
    Available,
    Running,
    Ready,
    Suspended,
    Sleep(std::time::Duration, std::time::Instant),
}

struct Thread {
    id: usize,
    stack: Vec<u8>,
    ctx: ThreadContext,
    state: State,
    generator: Option<Rc<Generator>>,
}

#[derive(Debug, Default)]
#[repr(C)]
struct ThreadContext {
    rsp: u64,
    r15: u64,
    r14: u64,
    r13: u64,
    r12: u64,
    rbx: u64,
    rbp: u64,
}

impl Thread {
    fn new(id: usize, stack_size: usize) -> Self {
        Thread {
            id,
            stack: vec![0_u8; stack_size],
            ctx: ThreadContext::default(),
            state: State::Available,
            generator: None,
        }
    }
}

impl Runtime {
    pub fn new(max_threads: usize, stack_size: Option<usize>) -> Self {
        let base_thread = Thread {
            id: 0,
            stack: vec![0_u8; DEFAULT_STACK_SIZE],
            ctx: ThreadContext::default(),
            state: State::Running,
            generator: None,
        };

        let mut threads = vec![base_thread];
        let mut available_threads: Vec<Thread> = (1..max_threads)
            .map(|i| {
                Thread::new(
                    i,
                    match stack_size {
                        Some(size) => size,
                        _ => DEFAULT_STACK_SIZE,
                    },
                )
            })
            .collect();
        threads.append(&mut available_threads);

        Runtime {
            threads,
            current: 0,
        }
    }

    pub fn init(&self) {
        unsafe {
            let r_ptr: *const Runtime = self;
            RUNTIME = r_ptr as usize;
        }
    }

    pub fn run(&mut self) -> ! {
        while self.t_yield() {}
        std::process::exit(0);
    }

    fn t_return(&mut self) {
        if self.current != 0 {
            self.threads[self.current].state = State::Available;
            self.threads[self.current].generator = None;
            self.t_yield();
        }
    }

    fn t_yield_generator<T: 'static>(&mut self, val: T) -> Result<(), &'static str> {
        if self.threads[self.current].generator.is_none() {
            return Err("Not a generator");
        }
        let to = if let Some(generator) = self.threads[self.current].generator.as_mut() {
            if generator.is_join {
                return Err("Not a generator");
            }
            let heap_value = Box::new(val);

            generator.state.set(GeneratorState::Yielded(
                heap_value as Box<dyn std::any::Any>,
            ));
            generator.to
        } else {
            unreachable!()
        };
        self.threads[self.current].state = State::Suspended;
        self.threads[to].state = State::Running;
        let old_pos = self.current;
        self.current = to;
        unsafe {
            switch(&mut self.threads[old_pos].ctx, &self.threads[to].ctx);
        }
        Ok(())
    }
    fn t_return_generator<T: 'static>(&mut self, val: T) -> Result<(), &'static str> {
        let to = if let Some(generator) = self.threads[self.current].generator.as_mut() {
            let heap_value = Box::new(val);

            generator.state.set(GeneratorState::Complete(
                heap_value as Box<dyn std::any::Any>,
            ));
            generator.to
        } else {
            return Err("Can't return value from green thread");
        };

        self.threads[self.current].state = State::Available;
        self.threads[self.current].generator = None;
        self.threads[to].state = State::Running;
        let old_pos = self.current;
        self.current = to;
        unsafe {
            switch(&mut self.threads[old_pos].ctx, &self.threads[to].ctx);
        }
        Ok(())
    }
    pub fn yield_to(&mut self, id: usize) {
        if self.threads[id].state == State::Suspended || self.threads[id].state == State::Ready {
            self.threads[id].state = State::Running;
            /*if self.threads[self.current].state != State::Available {
                self.threads[self.current].state = State::Suspended;
            }*/
            let old_pos = self.current;
            self.current = id;
            unsafe {
                switch(&mut self.threads[old_pos].ctx, &self.threads[id].ctx);
            }
        }
    }

    fn t_yield(&mut self) -> bool {
        let mut pos = self.current;
        while self.threads[pos].state != State::Ready {
            pos += 1;
            if pos == self.threads.len() {
                pos = 0;
            }
            if pos == self.current {
                return false;
            }

            if let State::Sleep(dur, now) = &self.threads[pos].state {
                if now.elapsed() >= *dur {
                    break;
                }
            }
        }

        if self.threads[self.current].state != State::Available {
            self.threads[self.current].state = State::Ready;
        }

        self.threads[pos].state = State::Running;
        let old_pos = self.current;
        self.current = pos;

        unsafe {
            switch(&mut self.threads[old_pos].ctx, &self.threads[pos].ctx);
        }

        true
    }

    pub fn suspend(&mut self, id: usize) {
        if id == self.current {
            self.threads[id].state = State::Suspended;
            self.t_yield();
        } else {
            self.threads[id].state = State::Suspended;
        }
    }

    pub fn resume(&mut self, id: usize) {
        if id == self.current {
            // do nothing
        } else {
            self.threads[id].state = State::Ready;
        }
    }

    pub fn spawn(&mut self, f: fn()) -> usize {
        let (i, available) = self
            .threads
            .iter_mut()
            .enumerate()
            .find(|(_, t)| t.state == State::Available)
            .expect("no available thread.");

        let size = available.stack.len();
        let s_ptr = available.stack.as_mut_ptr();

        unsafe {
            ptr::write(s_ptr.offset((size - 8) as isize) as *mut u64, guard as u64);
            ptr::write(s_ptr.offset((size - 16) as isize) as *mut u64, f as u64);
            available.ctx.rsp = s_ptr.offset((size - 16) as isize) as u64;
        }
        available.state = State::Ready;
        i
    }
}

#[cfg_attr(any(target_os = "windows", target_os = "linux"), naked)]
fn guard() {
    unsafe {
        let rt_ptr = RUNTIME as *mut Runtime;
        let rt = &mut *rt_ptr;
        rt.t_return();
    };
}

/// Yields thread
///
/// This is used when the programmer knows that the thread will have nothing to do for some time, and thus avoid wasting computing time.
pub fn yield_thread() {
    unsafe {
        let rt_ptr = RUNTIME as *mut Runtime;
        (*rt_ptr).t_yield();
    };
}

/// W.I.P, doesn't work now
/// Puts the current thread to sleep for at least the specified amount of time.
/// The thread may sleep longer than the duration specified due to scheduling specifics. It will never sleep less.
pub fn thread_sleep(dur: std::time::Duration) {
    let rt = get_rt();
    rt.threads[rt.current].state = State::Sleep(dur, std::time::Instant::now());
    yield_thread();
}
/// Spawns a new thread
///
/// Currently there are no way to specialize stack size for each thread.
pub fn spawn_greenie(f: fn()) {
    unsafe {
        let rt_ptr = RUNTIME as *mut Runtime;
        let rt = &mut *rt_ptr;
        let _ = rt.spawn(f);
        yield_thread();
    }
}

#[naked]
#[inline(never)]
unsafe fn switch(old: *mut ThreadContext, new: *const ThreadContext) {
    asm!("
        mov     %rsp, 0x00($0)
        mov     %r15, 0x08($0)
        mov     %r14, 0x10($0)
        mov     %r13, 0x18($0)
        mov     %r12, 0x20($0)
        mov     %rbx, 0x28($0)
        mov     %rbp, 0x30($0)
        mov     0x00($1), %rsp
        mov     0x08($1), %r15
        mov     0x10($1), %r14
        mov     0x18($1), %r13
        mov     0x20($1), %r12
        mov     0x28($1), %rbx
        mov     0x30($1), %rbp
        ret
        "
    : "=*m"(old)
    : "r"(new)
    :
    : "alignstack" // needed to work on windows
    );
}

fn get_rt() -> &'static mut Runtime {
    unsafe {
        let rt_ptr = RUNTIME as *mut Runtime;
        &mut *rt_ptr
    }
}
/// The result of a generator resumption.
/// This enum is returned from the Generator::resume method and indicates the possible return values of a generator.
///  Currently this corresponds to either a suspension point (Yielded) or a termination point (Complete).
pub enum GeneratorState {
    /// Generator is ready to work.
    Ready,
    /// The generator suspended with a value.
    /// This state indicates that a generator has been suspended. The value provided in this variant corresponds to the expression passed to yield and allows generators to provide a value each time they yield.
    Yielded(Box<dyn std::any::Any>),
    /// The generator completed with a return value.

    /// This state indicates that a generator has finished execution with the provided value. Once a generator has returned Complete it is considered a programmer error to call resume again.
    Complete(Box<dyn std::any::Any>),
}

impl Default for GeneratorState {
    fn default() -> Self {
        Self::Ready
    }
}
pub struct Generator {
    state: std::cell::Cell<GeneratorState>,
    complete: std::cell::Cell<bool>,
    thread_id: usize,
    to: usize,
    is_join: bool,
}
use std::rc::Rc;
/// Suspend generator with value
pub fn generator_yield<T: 'static>(val: T) -> Result<(), &'static str> {
    let rt = get_rt();
    rt.t_yield_generator(val)
}
/// Complete generator with value
pub fn generator_return<T: 'static>(val: T) -> Result<(), &'static str> {
    let rt = get_rt();
    rt.t_return_generator(val)
}
/// Generators, also commonly referred to as coroutines
impl Generator {
    /// Spawn generator
    pub fn spawn(closure: fn()) -> Rc<Self> {
        let rt = get_rt();

        let to = rt.current;
        let thread_id = rt.spawn(closure);
        rt.threads[thread_id].state = State::Suspended;
        let generator = Rc::new(Generator {
            state: std::cell::Cell::new(GeneratorState::Ready),
            thread_id,
            to,
            complete: std::cell::Cell::new(false),
            is_join: false,
        });
        rt.threads[thread_id].generator = Some(generator.clone());
        generator
    }

    fn spawn_joinable(id: usize) -> Rc<Self> {
        let rt = get_rt();

        let to = rt.current;
        let thread_id = id;
        rt.threads[thread_id].state = State::Suspended;
        let generator = Rc::new(Generator {
            state: std::cell::Cell::new(GeneratorState::Ready),
            thread_id,
            to,
            complete: std::cell::Cell::new(false),
            is_join: true,
        });
        rt.threads[thread_id].generator = Some(generator.clone());
        generator
    }
    /// Resumes the execution of this generator.
    /// This function will resume execution of the generator or start execution if it hasn't already. This call will return back into the
    /// generator's last suspension point, resuming execution from the latest yield. The generator will continue executing until it
    /// either yields or returns, at which point this function will return.
    pub fn resume(&self) -> Result<GeneratorState, &'static str> {
        if self.complete.get() {
            return Err("Generator already complete");
        }
        let rt = get_rt();
        rt.threads[rt.current].state = State::Suspended;
        rt.threads[self.thread_id].state = State::Running;
        let old = rt.current;
        rt.current = self.thread_id;
        assert!(rt.threads[self.thread_id].generator.is_some());
        unsafe {
            switch(&mut rt.threads[old].ctx, &rt.threads[self.thread_id].ctx);
        }
        let state = self.state.take();
        if let GeneratorState::Complete(_) = &state {
            self.complete.set(true);
        }
        Ok(state)
    }
}

//// Iterate through generator:
/// ```rust
/// use greenie::*;
///
/// fn green_main() {
///     let generator = Generator::spawn(|| {
///         generator_yield(1);
///         generator_yield(2);
///         generator_yield(2.5);
///         generator_return("Hello!");
///     });
///
///     let result = iterate_generator! {
///         for (x in generator) {
///             // do something
///         }
///     };
///     println!("{}",result.downcast_ref::<&'static str>().unwrap());
///
/// }
///
///
/// ```
#[macro_export]
macro_rules! iterate_generator {
    (for ($x: ident in $generator: expr) $b: block) => {
        loop {
            match $generator.resume() {
                Ok(GeneratorState::Yielded($x)) => $b,
                Ok(GeneratorState::Complete($x)) => {
                    break $x
                },
                Err(e) => {
                    eprintln!("{}",e);
                    std::process::exit(1);
                }
                _ => unreachable!()
            }
        }
    };
    (for ($x: ident in $generator: expr) $b: block and $b2: block) => {
        loop {
            match $generator.resume() {
                Ok(GeneratorState::Yielded($x)) => $b,
                Ok(GeneratorState::Complete($x)) => {
                    $b2
                    break;
                },
                Ok(GeneratorState::Ready) => panic!("impossible"),
                Err(e) => {
                    eprintln!("{}",e);
                    std::process::exit(1);
                }
                _ => unreachable!()
            }
        }
    };

    (enumerate for ($c: ident, $x: ident in $generator: expr) $b: block) => {
        let mut $c = 0;
        loop {
            match $generator.resume() {
                Ok(GeneratorState::Yielded($x)) => $b,
                Ok(GeneratorState::Complete($x)) => {
                    break $x
                },
                Err(e) => {
                    eprintln!("{}",e);
                    std::process::exit(1);
                }
                _ => unreachable!()
            }
            $c += 1;
        }
    };

    (enumerate for ($c: ident, $x: ident in $generator: expr) $b: block and $b2: block) => {
        let mut $c = 0;
        loop {
            match $generator.resume() {
                Ok(GeneratorState::Yielded($x)) => $b,
                Ok(GeneratorState::Complete($x)) => {
                    $b2
                    break;
                },
                Ok(GeneratorState::Ready) => panic!("impossible"),
                Err(e) => {
                    eprintln!("{}",e);
                    std::process::exit(1);
                }
                _ => unreachable!()
            }
            $c += 1;
        }
    };
}