34
Clocks & Asynchronous Events

Clocks & Asynchronous Events. Overview Clocks API Implementation Asynchronous Events API Single Threaded Model Multi-Threaded Model Code Walkthrough

  • View
    217

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Clocks & Asynchronous Events. Overview Clocks  API  Implementation Asynchronous Events  API  Single Threaded Model  Multi-Threaded Model  Code Walkthrough

Clocks & Asynchronous Events

Page 2: Clocks & Asynchronous Events. Overview Clocks  API  Implementation Asynchronous Events  API  Single Threaded Model  Multi-Threaded Model  Code Walkthrough

Overview

Clocks API Implementation

Asynchronous Events API Single Threaded Model Multi-Threaded Model Code Walkthrough Demo

Page 3: Clocks & Asynchronous Events. Overview Clocks  API  Implementation Asynchronous Events  API  Single Threaded Model  Multi-Threaded Model  Code Walkthrough

Clocks

Real-Time Specification Classes: Clock

getRealtimeClock() getResolution() getTime() setResolution()

Page 4: Clocks & Asynchronous Events. Overview Clocks  API  Implementation Asynchronous Events  API  Single Threaded Model  Multi-Threaded Model  Code Walkthrough

Implementation

o Clocks are represented by a c struct:

struct clockStruct {htime time;htime resolution;

/* int threadID; /* ID of the thread instantiating this clock */struct clockStruct *nextClock; /* linked list */THREAD TimerQueue; /* queue of threads waiting on this clock */

};

typedef struct clockStruct CLOCK;

Page 5: Clocks & Asynchronous Events. Overview Clocks  API  Implementation Asynchronous Events  API  Single Threaded Model  Multi-Threaded Model  Code Walkthrough

Implementation

Multiple clocks allow: Events to be checked at different frequencies More efficiency

Page 6: Clocks & Asynchronous Events. Overview Clocks  API  Implementation Asynchronous Events  API  Single Threaded Model  Multi-Threaded Model  Code Walkthrough

Asynchronous Events

An asynchronous event represents something that can happen.

It can have a set of handlers associated with it.

When the event occurs, the handler is scheduled by the scheduler.

Page 7: Clocks & Asynchronous Events. Overview Clocks  API  Implementation Asynchronous Events  API  Single Threaded Model  Multi-Threaded Model  Code Walkthrough

Real-Time Specification

The RTSJ includes a number of classes for dealing with Asynchronous Events. AsyncEvent AsyncEventHandler Interruptable

Page 8: Clocks & Asynchronous Events. Overview Clocks  API  Implementation Asynchronous Events  API  Single Threaded Model  Multi-Threaded Model  Code Walkthrough

AsyncEvent

Methods addHandler(asyncEventHandler handler) bindTo(java.lang.String happening) Fire()

Page 9: Clocks & Asynchronous Events. Overview Clocks  API  Implementation Asynchronous Events  API  Single Threaded Model  Multi-Threaded Model  Code Walkthrough

AsyncEventHandler

Code which is run in response to an eventA java.lang.Runnable with a set of

parametersSimilar to a RealTimeThread

Page 10: Clocks & Asynchronous Events. Overview Clocks  API  Implementation Asynchronous Events  API  Single Threaded Model  Multi-Threaded Model  Code Walkthrough

Basic Problem with a Single Thread

Time

scheduler scheduler

Thread 1 Thread 2

Page 11: Clocks & Asynchronous Events. Overview Clocks  API  Implementation Asynchronous Events  API  Single Threaded Model  Multi-Threaded Model  Code Walkthrough

Basic Problem with a Single Thread

Time

scheduler scheduler

Thread 1

Event Occurs

Thread 2

Page 12: Clocks & Asynchronous Events. Overview Clocks  API  Implementation Asynchronous Events  API  Single Threaded Model  Multi-Threaded Model  Code Walkthrough

Basic Problem with a Single Thread

Time

scheduler scheduler

Thread 1

Event OccursEvent Handled

Thread 2

Page 13: Clocks & Asynchronous Events. Overview Clocks  API  Implementation Asynchronous Events  API  Single Threaded Model  Multi-Threaded Model  Code Walkthrough

This shows how the KVM currently works.Events are put into a queue.Queue is polled periodically at

rescheduling.Allows context switching to be

implementation independent.

Basic Problem with a Single Thread

Page 14: Clocks & Asynchronous Events. Overview Clocks  API  Implementation Asynchronous Events  API  Single Threaded Model  Multi-Threaded Model  Code Walkthrough

Problems with this model

Asynchronous events should be handled very quickly.

If the current thread does not yield, the VM hangs.

Page 15: Clocks & Asynchronous Events. Overview Clocks  API  Implementation Asynchronous Events  API  Single Threaded Model  Multi-Threaded Model  Code Walkthrough

Solution

When an event happens, the currently executing thread should be switched out of context and the event handler switched in.

Page 16: Clocks & Asynchronous Events. Overview Clocks  API  Implementation Asynchronous Events  API  Single Threaded Model  Multi-Threaded Model  Code Walkthrough

Asynchronous Events

Thread 1

Scheduler

Event Handler

Time

Page 17: Clocks & Asynchronous Events. Overview Clocks  API  Implementation Asynchronous Events  API  Single Threaded Model  Multi-Threaded Model  Code Walkthrough

Asynchronous Events

Thread 1

Scheduler

Event Handler

Time

Page 18: Clocks & Asynchronous Events. Overview Clocks  API  Implementation Asynchronous Events  API  Single Threaded Model  Multi-Threaded Model  Code Walkthrough

Asynchronous Events

Thread 1

Scheduler

Event Handler

Time

Page 19: Clocks & Asynchronous Events. Overview Clocks  API  Implementation Asynchronous Events  API  Single Threaded Model  Multi-Threaded Model  Code Walkthrough

Asynchronous Events

Thread 1

Scheduler

Event Handler

Time

Page 20: Clocks & Asynchronous Events. Overview Clocks  API  Implementation Asynchronous Events  API  Single Threaded Model  Multi-Threaded Model  Code Walkthrough

Asynchronous Events

Thread 1

Scheduler

Event Handler

Time

Page 21: Clocks & Asynchronous Events. Overview Clocks  API  Implementation Asynchronous Events  API  Single Threaded Model  Multi-Threaded Model  Code Walkthrough

Asynchronous Events

Thread 1

Scheduler

Event Handler

Time

Page 22: Clocks & Asynchronous Events. Overview Clocks  API  Implementation Asynchronous Events  API  Single Threaded Model  Multi-Threaded Model  Code Walkthrough

Asynchronous Events using Signals

This method for handling asynchronous events uses multiple threads.

An external thread sends a signal to the KVM where it is caught by a signal handler.

ExternalThread

KVM Thread

Signal Handler

Signal HandlerSignal

Page 23: Clocks & Asynchronous Events. Overview Clocks  API  Implementation Asynchronous Events  API  Single Threaded Model  Multi-Threaded Model  Code Walkthrough

Demo Overview

TwoThreadsTest.java

SimpleThread.java

Java.lang.threadsleep();

nativeCore.cJava_lang_Thread_sleep();

Java

C

Page 24: Clocks & Asynchronous Events. Overview Clocks  API  Implementation Asynchronous Events  API  Single Threaded Model  Multi-Threaded Model  Code Walkthrough

TwoThreadsTest.java

class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji").start(); new SimpleThread("Taiti").start(); new SimpleThread("Hawaii").start(); }}

Page 25: Clocks & Asynchronous Events. Overview Clocks  API  Implementation Asynchronous Events  API  Single Threaded Model  Multi-Threaded Model  Code Walkthrough

SimpleThread.java

class SimpleThread extends Thread { String name; public SimpleThread(String str) { super();

name = new String(str); } public void run() { for (int i = 0; i < 10; i++) { System.out.println(i + " " + name); try { sleep((int)(1000)); } catch (InterruptedException e) {} } System.out.println("DONE! " + name); }}

Page 26: Clocks & Asynchronous Events. Overview Clocks  API  Implementation Asynchronous Events  API  Single Threaded Model  Multi-Threaded Model  Code Walkthrough

Java/lang/thread.java

public static native void sleep(long millis) throws InterruptedException;

Page 27: Clocks & Asynchronous Events. Overview Clocks  API  Implementation Asynchronous Events  API  Single Threaded Model  Multi-Threaded Model  Code Walkthrough

Kvm/VmCommon/src/nativeCore.c

void Java_java_lang_Thread_sleep(void){ long64 period; popLong(period);

/* only block if the time period is not zero */ if (ll_zero_ge(period)) { /* Copy CurrentThread because suspendThread clears it */ THREAD thisThread = CurrentThread;

/* Suspend the current thread before we add the timer */ suspendThread();

/* Now add the timer (this is the safe way) */ registerAlarm(thisThread, period, resumeThread);

} else { raiseException("java/lang/IllegalArgumentException"); }}

Page 28: Clocks & Asynchronous Events. Overview Clocks  API  Implementation Asynchronous Events  API  Single Threaded Model  Multi-Threaded Model  Code Walkthrough

Signal Handlers:

• registerAlarm() adds a thread to TimerQueue• TimerQueue is a Queue of threads sorted by

wakeupTime• To handle timer events, we start a new thread

clockThread which checks the TimerQueue and sends a signal to the JVM when a timer has expired.

• When a signal is received by the VM, execution is interrupted and the signal handler clock_signal_handler() is called.

• clock_signal_handler() switches the waiting thread back into context.

Page 29: Clocks & Asynchronous Events. Overview Clocks  API  Implementation Asynchronous Events  API  Single Threaded Model  Multi-Threaded Model  Code Walkthrough

Demo Overview (2)

RTClock_md();

KVM Thread

Signal Handler

Signal HandlerSIGUSR1

Clock_signal_handler();

Page 30: Clocks & Asynchronous Events. Overview Clocks  API  Implementation Asynchronous Events  API  Single Threaded Model  Multi-Threaded Model  Code Walkthrough

Kvm/VmUnix/src/runtime_md.c

/*===================================================== * FUNCTION: InitializeRTClock * TYPE: initialization * OVERVIEW: called from StartJVM.c to start a seperate clock * thread for RT clock interrupt * INTERFACE: * parameters: none * returns: none *===================================================*/ void InitializeRTClock() {

pthread_t clockThread; pthread_create(&clockThread, NULL, RTClock_md, NULL); printf("* Initialize RTClock done\n");

}

Page 31: Clocks & Asynchronous Events. Overview Clocks  API  Implementation Asynchronous Events  API  Single Threaded Model  Multi-Threaded Model  Code Walkthrough

Kvm/VmUnix/src/runtime_md.c

/*================================================================= * FUNCTION: RTClock_md * TYPE: Machine dependent function * OVERVIEW: runs in a seperate pthread to send interrupts for timer events *================================================================*/ void *RTClock_md(void *p) {

ulong64 now, nextTimer; for(;;) { if (TimerQueue != NULL) {

now = CurrentTime_md(); enterSystemCriticalSection();

nextTimer = GET_ULONG(TimerQueue->wakeupTime); exitSystemCriticalSection(); if (ll_compare_le(nextTimer, now)) {

kill(0, 10); /* send signal SIGUSR1 */ }

} }

} /* RTClock_md */

Page 32: Clocks & Asynchronous Events. Overview Clocks  API  Implementation Asynchronous Events  API  Single Threaded Model  Multi-Threaded Model  Code Walkthrough

Kvm/VmUnix/src/runtime_md.c

/*================================================ * FUNCTION: clock_signal_handler * TYPE: RT clock * OVERVIEW: called when we receive a signal from the * clock thread * INTERFACE: * parameters: signal * returns: none *=============================================*/

static void clock_signal_handler(int sig) { reschedule(); }

Page 33: Clocks & Asynchronous Events. Overview Clocks  API  Implementation Asynchronous Events  API  Single Threaded Model  Multi-Threaded Model  Code Walkthrough

Kvm/VmCommon/h/events.h

#define reschedule() \ printf("Rescheduling...\n"); \

do { \ ulong64 wakeupTime; \ if (!areAliveThreads()) { \ return; /* end of program */ \ } \ checkTimerQueue(&wakeupTime); \

BetterHandleEvent(wakeupTime); \ __ProcessDebugCmds(0); \

} while (!SwitchThread());

Page 34: Clocks & Asynchronous Events. Overview Clocks  API  Implementation Asynchronous Events  API  Single Threaded Model  Multi-Threaded Model  Code Walkthrough

OS Limitations

Linux does not support some threading functions: Suspend Resume

These are supported by: OpenBSD RT-Linux Solaris Timesys Linux