Files @ f4d1c8c04de6
Branch filter:

Location: CSY/reowolf/src/runtime2/mod.rs

f4d1c8c04de6 17.0 KiB application/rls-services+xml Show Annotation Show as Raw Download as Raw
MH
modified scheduler to use new ExecTree and Consensus
  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
// Structure of module

mod runtime;
mod messages;
mod connector;
mod branch;
mod native;
mod port;
mod scheduler;
mod inbox;
mod consensus;
mod inbox2;

#[cfg(test)] mod tests;
mod connector2;

// Imports

use std::collections::VecDeque;
use std::sync::{Arc, Condvar, Mutex, RwLock};
use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
use std::thread::{self, JoinHandle};

use crate::collections::RawVec;
use crate::ProtocolDescription;

use inbox::Message;
use connector2::{ConnectorPDL, ConnectorPublic, ConnectorScheduling};
use scheduler::{Scheduler, ControlMessageHandler};
use native::{Connector, ConnectorApplication, ApplicationInterface};
use crate::runtime2::inbox2::MessageFancy;
use crate::runtime2::port::{Port, PortState};
use crate::runtime2::scheduler::{ComponentCtxFancy, SchedulerCtx};

/// A kind of token that, once obtained, allows mutable access to a connector.
/// We're trying to use move semantics as much as possible: the owner of this
/// key is the only one that may execute the connector's code.
pub(crate) struct ConnectorKey {
    pub index: u32, // of connector
}

impl ConnectorKey {
    /// Downcasts the `ConnectorKey` type, which can be used to obtain mutable
    /// access, to a "regular ID" which can be used to obtain immutable access.
    #[inline]
    pub fn downcast(&self) -> ConnectorId {
        return ConnectorId(self.index);
    }

    /// Turns the `ConnectorId` into a `ConnectorKey`, marked as unsafe as it
    /// bypasses the type-enforced `ConnectorKey`/`ConnectorId` system
    #[inline]
    pub unsafe fn from_id(id: ConnectorId) -> ConnectorKey {
        return ConnectorKey{ index: id.0 };
    }
}

/// A kind of token that allows shared access to a connector. Multiple threads
/// may hold this
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct ConnectorId(pub u32);

impl ConnectorId {
    // TODO: Like the other `new_invalid`, maybe remove
    #[inline]
    pub fn new_invalid() -> ConnectorId {
        return ConnectorId(u32::MAX);
    }

    #[inline]
    pub(crate) fn is_valid(&self) -> bool {
        return self.0 != u32::MAX;
    }
}

// TODO: Change this, I hate this. But I also don't want to put `public` and
//  `router` of `ScheduledConnector` back into `Connector`. The reason I don't
//  want `Box<dyn Connector>` everywhere is because of the v-table overhead. But
//  to truly design this properly I need some benchmarks.
pub(crate) enum ConnectorVariant {
    UserDefined(ConnectorPDL),
    Native(Box<dyn Connector>),
}

impl Connector for ConnectorVariant {
    fn run(&mut self, scheduler_ctx: SchedulerCtx, comp_ctx: &mut ComponentCtxFancy) -> ConnectorScheduling {
        match self {
            ConnectorVariant::UserDefined(c) => c.run(scheduler_ctx, comp_ctx),
            ConnectorVariant::Native(c) => c.run(scheduler_ctx, comp_ctx),
        }
    }
}

pub(crate) struct ScheduledConnector {
    pub connector: ConnectorVariant, // access by connector
    pub ctx_fancy: ComponentCtxFancy,
    pub public: ConnectorPublic, // accessible by all schedulers and connectors
    pub router: ControlMessageHandler,
    pub shutting_down: bool,
}

// -----------------------------------------------------------------------------
// Runtime
// -----------------------------------------------------------------------------

/// Externally facing runtime.
pub struct Runtime {
    inner: Arc<RuntimeInner>,
}

impl Runtime {
    pub fn new(num_threads: u32, protocol_description: ProtocolDescription) -> Runtime {
        // Setup global state
        assert!(num_threads > 0, "need a thread to run connectors");
        let runtime_inner = Arc::new(RuntimeInner{
            protocol_description,
            port_counter: AtomicU32::new(0),
            connectors: RwLock::new(ConnectorStore::with_capacity(32)),
            connector_queue: Mutex::new(VecDeque::with_capacity(32)),
            schedulers: Mutex::new(Vec::new()),
            scheduler_notifier: Condvar::new(),
            active_connectors: AtomicU32::new(0),
            active_interfaces: AtomicU32::new(1), // this `Runtime` instance
            should_exit: AtomicBool::new(false),
        });

        // Launch threads
        {
            let mut schedulers = Vec::with_capacity(num_threads as usize);
            for thread_index in 0..num_threads {
                let cloned_runtime_inner = runtime_inner.clone();
                let thread = thread::Builder::new()
                    .name(format!("thread-{}", thread_index))
                    .spawn(move || {
                        let mut scheduler = Scheduler::new(cloned_runtime_inner, thread_index);
                        scheduler.run();
                    })
                    .unwrap();

                schedulers.push(thread);
            }

            let mut lock = runtime_inner.schedulers.lock().unwrap();
            *lock = schedulers;
        }

        // Return runtime
        return Runtime{ inner: runtime_inner };
    }

    /// Returns a new interface through which channels and connectors can be
    /// created.
    pub fn create_interface(&self) -> ApplicationInterface {
        self.inner.increment_active_interfaces();
        let (connector, mut interface) = ConnectorApplication::new(self.inner.clone());
        let connector_key = self.inner.create_interface_component(connector);
        interface.set_connector_id(connector_key.downcast());

        // Note that we're not scheduling. That is done by the interface in case
        // it is actually needed.
        return interface;
    }
}

impl Drop for Runtime {
    fn drop(&mut self) {
        self.inner.decrement_active_interfaces();
        let mut lock = self.inner.schedulers.lock().unwrap();
        for handle in lock.drain(..) {
            handle.join().unwrap();
        }
    }
}

// -----------------------------------------------------------------------------
// RuntimeInner
// -----------------------------------------------------------------------------

pub(crate) struct RuntimeInner {
    // Protocol
    pub(crate) protocol_description: ProtocolDescription,
    // Regular counter for port IDs
    port_counter: AtomicU32,
    // Storage of connectors and the work queue
    connectors: RwLock<ConnectorStore>,
    connector_queue: Mutex<VecDeque<ConnectorKey>>,
    schedulers: Mutex<Vec<JoinHandle<()>>>,
    // Conditions to determine whether the runtime can exit
    scheduler_notifier: Condvar,  // coupled to mutex on `connector_queue`.
    // TODO: Figure out if we can simply merge the counters?
    active_connectors: AtomicU32, // active connectors (if sleeping, then still considered active)
    active_interfaces: AtomicU32, // active API interfaces that can add connectors/channels
    should_exit: AtomicBool,
}

impl RuntimeInner {
    // --- Managing the components queued for execution

    /// Wait until there is a connector to run. If there is one, then `Some`
    /// will be returned. If there is no more work, then `None` will be
    /// returned.
    pub(crate) fn wait_for_work(&self) -> Option<ConnectorKey> {
        let mut lock = self.connector_queue.lock().unwrap();
        while lock.is_empty() && !self.should_exit.load(Ordering::Acquire) {
            lock = self.scheduler_notifier.wait(lock).unwrap();
        }

        return lock.pop_front();
    }

    pub(crate) fn push_work(&self, key: ConnectorKey) {
        let mut lock = self.connector_queue.lock().unwrap();
        lock.push_back(key);
        self.scheduler_notifier.notify_one();
    }

    // --- Creating/using ports

    /// Creates a new port pair. Note that these are stored globally like the
    /// connectors are. Ports stored by components belong to those components.
    pub(crate) fn create_channel(&self, creating_connector: ConnectorId) -> (Port, Port) {
        use port::{PortIdLocal, PortKind};

        let getter_id = self.port_counter.fetch_add(2, Ordering::SeqCst);
        let putter_id = PortIdLocal::new(getter_id + 1);
        let getter_id = PortIdLocal::new(getter_id);

        let getter_port = Port{
            self_id: getter_id,
            peer_id: putter_id,
            kind: PortKind::Getter,
            state: PortState::Open,
            peer_connector: creating_connector,
        };
        let putter_port = Port{
            self_id: putter_id,
            peer_id: getter_id,
            kind: PortKind::Putter,
            state: PortState::Open,
            peer_connector: creating_connector,
        };

        return (getter_port, putter_port);
    }

    /// Sends a message to a particular connector. If the connector happened to
    /// be sleeping then it will be scheduled for execution.
    pub(crate) fn send_message(&self, target_id: ConnectorId, message: MessageFancy) {
        let target = self.get_component_public(target_id);
        target.inbox.insert_message(message);

        let should_wake_up = target.sleeping
            .compare_exchange(true, false, Ordering::SeqCst, Ordering::Acquire)
            .is_ok();

        if should_wake_up {
            let key = unsafe{ ConnectorKey::from_id(target_id) };
            self.push_work(key);
        }
    }

    // --- Creating/retrieving/destroying components

    /// Creates an initially sleeping application connector.
    fn create_interface_component(&self, component: ConnectorApplication) -> ConnectorKey {
        // Initialize as sleeping, as it will be scheduled by the programmer.
        let mut lock = self.connectors.write().unwrap();
        let key = lock.create(ConnectorVariant::Native(Box::new(component)), true);

        self.increment_active_components();
        return key;
    }

    /// Creates a new PDL component. This function just creates the component.
    /// If you create it initially awake, then you must add it to the work
    /// queue. Other aspects of correctness (i.e. setting initial ports) are
    /// relinquished to the caller!
    pub(crate) fn create_pdl_component(&self, connector: ConnectorPDL, initially_sleeping: bool) -> ConnectorKey {
        // Create as not sleeping, as we'll schedule it immediately
        let key = {
            let mut lock = self.connectors.write().unwrap();
            lock.create(ConnectorVariant::UserDefined(connector), initially_sleeping)
        };

        self.increment_active_components();
        return key;
    }

    #[inline]
    pub(crate) fn get_component_private(&self, connector_key: &ConnectorKey) -> &'static mut ScheduledConnector {
        let lock = self.connectors.read().unwrap();
        return lock.get_private(connector_key);
    }

    #[inline]
    pub(crate) fn get_component_public(&self, connector_id: ConnectorId) -> &'static ConnectorPublic {
        let lock = self.connectors.read().unwrap();
        return lock.get_public(connector_id);
    }

    pub(crate) fn destroy_component(&self, connector_key: ConnectorKey) {
        let mut lock = self.connectors.write().unwrap();
        lock.destroy(connector_key);
        self.decrement_active_components();
    }

    // --- Managing exit condition

    #[inline]
    pub(crate) fn increment_active_interfaces(&self) {
        let _old_num = self.active_interfaces.fetch_add(1, Ordering::SeqCst);
        println!("DEBUG: Incremented active interfaces to {}", _old_num + 1);
        debug_assert_ne!(_old_num, 0); // once it hits 0, it stays zero
    }

    pub(crate) fn decrement_active_interfaces(&self) {
        let old_num = self.active_interfaces.fetch_sub(1, Ordering::SeqCst);
        println!("DEBUG: Decremented active interfaces to {}", old_num - 1);
        debug_assert!(old_num > 0);
        if old_num == 1 { // such that active interfaces is now 0
            let num_connectors = self.active_connectors.load(Ordering::Acquire);
            if num_connectors == 0 {
                self.signal_for_shutdown();
            }
        }
    }

    #[inline]
    fn increment_active_components(&self) {
        let _old_num = self.active_connectors.fetch_add(1, Ordering::SeqCst);
        println!("DEBUG: Incremented components to {}", _old_num + 1);
    }

    fn decrement_active_components(&self) {
        let old_num = self.active_connectors.fetch_sub(1, Ordering::SeqCst);
        println!("DEBUG: Decremented components to {}", old_num - 1);
        debug_assert!(old_num > 0);
        if old_num == 1 { // such that we have no more active connectors (for now!)
            let num_interfaces = self.active_interfaces.load(Ordering::Acquire);
            if num_interfaces == 0 {
                self.signal_for_shutdown();
            }
        }
    }

    #[inline]
    fn signal_for_shutdown(&self) {
        debug_assert_eq!(self.active_interfaces.load(Ordering::Acquire), 0);
        debug_assert_eq!(self.active_connectors.load(Ordering::Acquire), 0);

        println!("DEBUG: Signaling for shutdown");
        let _lock = self.connector_queue.lock().unwrap();
        let should_signal = self.should_exit
            .compare_exchange(false, true, Ordering::SeqCst, Ordering::Acquire)
            .is_ok();

        if should_signal {
            println!("DEBUG: Notifying all waiting schedulers");
            self.scheduler_notifier.notify_all();
        }
    }
}

// TODO: Come back to this at some point
unsafe impl Send for RuntimeInner {}
unsafe impl Sync for RuntimeInner {}

// -----------------------------------------------------------------------------
// ConnectorStore
// -----------------------------------------------------------------------------

struct ConnectorStore {
    // Freelist storage of connectors. Storage should be pointer-stable as
    // someone might be mutating the vector while we're executing one of the
    // connectors.
    connectors: RawVec<*mut ScheduledConnector>,
    free: Vec<usize>,
}

impl ConnectorStore {
    fn with_capacity(capacity: usize) -> Self {
        Self {
            connectors: RawVec::with_capacity(capacity),
            free: Vec::with_capacity(capacity),
        }
    }

    /// Retrieves public part of connector - accessible by many threads at once.
    fn get_public(&self, id: ConnectorId) -> &'static ConnectorPublic {
        unsafe {
            let connector = self.connectors.get(id.0 as usize);
            debug_assert!(!connector.is_null());
            return &(**connector).public;
        }
    }

    /// Retrieves private part of connector - accessible by one thread at a
    /// time.
    fn get_private(&self, key: &ConnectorKey) -> &'static mut ScheduledConnector {
        unsafe {
            let connector = self.connectors.get_mut(key.index as usize);
            debug_assert!(!connector.is_null());
            return &mut (**connector);
        }
    }

    /// Creates a new connector. Caller should ensure ports are set up correctly
    /// and the connector is queued for execution if needed.
    fn create(&mut self, connector: ConnectorVariant, initially_sleeping: bool) -> ConnectorKey {
        let mut connector = ScheduledConnector {
            connector,
            ctx_fancy: ComponentCtxFancy::new_empty(),
            public: ConnectorPublic::new(initially_sleeping),
            router: ControlMessageHandler::new(),
            shutting_down: false,
        };

        let index;
        let key;

        if self.free.is_empty() {
            // No free entries, allocate new entry
            index = self.connectors.len();
            key = ConnectorKey{ index: index as u32 };
            connector.ctx_fancy.id = key.downcast();

            let connector = Box::into_raw(Box::new(connector));
            self.connectors.push(connector);
        } else {
            // Free spot available
            index = self.free.pop().unwrap();
            key = ConnectorKey{ index: index as u32 };
            connector.ctx_fancy.id = key.downcast();

            unsafe {
                let target = self.connectors.get_mut(index);
                std::ptr::write(*target, connector);
            }
        }

        return key;
    }

    /// Destroys a connector. Caller should make sure it is not scheduled for
    /// execution. Otherwise one experiences "bad stuff" (tm).
    fn destroy(&mut self, key: ConnectorKey) {
        unsafe {
            let target = self.connectors.get_mut(key.index as usize);
            std::ptr::drop_in_place(*target);
            // Note: but not deallocating!
        }

        self.free.push(key.index as usize);
    }
}

impl Drop for ConnectorStore {
    fn drop(&mut self) {
        // Everything in the freelist already had its destructor called, so only
        // has to be deallocated
        for free_idx in self.free.iter().copied() {
            unsafe {
                let memory = self.connectors.get_mut(free_idx);
                let layout = std::alloc::Layout::for_value(&**memory);
                std::alloc::dealloc(*memory as *mut u8, layout);

                // mark as null for the remainder
                *memory = std::ptr::null_mut();
            }
        }

        // With the deallocated stuff marked as null, clear the remainder that
        // is not null
        for idx in 0..self.connectors.len() {
            unsafe {
                let memory = *self.connectors.get_mut(idx);
                if !memory.is_null() {
                    let _ = Box::from_raw(memory); // take care of deallocation, bit dirty, but meh
                }
            }
        }
    }
}