Asynchronous Transfer of Control in the Timesys Reference Implementation.

Slides:



Advertisements
Similar presentations
Operating Systems: Monitors 1 Monitors (C.A.R. Hoare) higher level construct than semaphores a package of grouped procedures, variables and data i.e. object.
Advertisements

Exception Handling – illustrated by Java mMIC-SFT November 2003 Anders P. Ravn Aalborg University.
Ade Azurat, Advanced Programming 2004 (Based on LYS Stefanus’s slides) Advanced Programming 2004, Based on LYS Stefanus’s slides Slide 2.1 Multithreading.
Multi-threaded applications SE SE-2811 Dr. Mark L. Hornick 2 What SE1011 students are told… When the main() method is called, the instructions.
© Andy Wellings, 2004 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  Completing the Java Model  Overview of the.
Understand Error Handling Software Development Fundamentals LESSON 1.4.
CS 5704 Fall 00 1 Monitors in Java Model and Examples.
15-213/ Intro to Computer Systems by btan with reference to Spring 10’s slides.
Programming R-T Abstractions TSW November 2009 Anders P. Ravn Aalborg University.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L19 (Chapter 24) Multithreading.
© Andy Wellings, 2004 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  Completing the Java Model  Overview of the.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
© Andy Wellings, 2004 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  Completing the Java Model  Overview of the.
Java ThreadsGraphics Programming Graphics Programming: Java Threads.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
1 Thread Pools. 2 What’s A Thread Pool? A programming technique which we will use. A collection of threads that are created once (e.g. when server starts).
Clocks & Asynchronous Events. Overview Clocks  API  Implementation Asynchronous Events  API  Single Threaded Model  Multi-Threaded Model  Code Walkthrough.
The Real-Time Java Profile ITV Real-Time Systems Anders P. Ravn Aalborg University February 2006.
Threads II. Review A thread is a single flow of control through a program Java is multithreaded—several threads may be executing “simultaneously” If you.
UNIX Signals Bach 7.2 Operating Systems Course The Hebrew University Spring 2010.
1 CSC241: Object Oriented Programming Lecture No 27.
Locks CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Real-Time Java Martin Schöberl. Real Time Java2 Overview What are real-time systems Real-time specification for Java RTSJ issues, subset Real-time profile.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Question: What causes asynchronous control? Asynchronous control ≈ not determined by normal instruction execution Example int x = 2; // suppose a transfer.
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.
Instructore: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Concurrent and.
1 Web Based Programming Section 8 James King 12 August 2003.
Dynamic Architectures (Component Reconfiguration) with Fractal.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
Synchronizing threads, thread pools, etc.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
Chapter 6 (6.7 & 6.9 only) - Threads & Mutex Threads –A Thread is a basic unit of CPU utilization consisting of a program counter, register set and stack.
EEL Real-time Java part 2. EEL Acknowledgements All the lecture slides were adopted from the slides of Andy Wellings.
In Java processes are called threads. Additional threads are associated with objects. An application is associated with an initial thread via a static.
Multithreading in JAVA
Li Tak Sing COMPS311F. Threads A thread is a single sequential flow of control within a program. Many programming languages only allow you to write programs.
1 Software Construction and Evolution - CSSE 375 Exception Handling – Chaining & Threading Steve Chenoweth Office: Moench Room F220 Phone: (812)
1 Signals (continued) CS 241 April 9, 2012 University of Illinois.
Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science.
SPL/2010 Guarded Methods and Waiting 1. SPL/2010 Reminder! ● Concurrency problem: asynchronous modifications to object states lead to failure of thread.
Threads in Java1 Concurrency Synchronizing threads, thread pools, etc.
Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Processes, Threads, and Process States. Programs and Processes  Program: an executable file (before/after compilation)  Process: an instance of a program.
Multi-Threading in Java
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
Threads. Objectives You must be able to answer the following questions –What code does a thread execute? –What states can a thread be in? –How does a.
1 Java Programming Java Programming II Concurrent Programming: Threads ( I)
Multithreading & Synchronized Algoritma Pemrograman 3 Sistem Komputer – S1 Universitas Gunadarma 1.
Multithreading. Multitasking The multitasking is the ability of single processor to perform more than one operation at the same time Once systems allowed.
Threads b A thread is a flow of control in a program. b The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.
Chapter 13: Multithreading The Thread class The Thread class The Runnable Interface The Runnable Interface Thread States Thread States Thread Priority.
1 Lecture 19: Unix signals and Terminal management n what is a signal n signal handling u kernel u user n signal generation n signal example usage n terminal.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Chapter 11: Threaded Programs Situations where the program is following multiple execution paths (how to stop one?) Thread: a line of execution Thread.
Threads in Java Two ways to start a thread
Multithreading / Concurrency
Java Programming Language
G53SRP: Real Time Threads in RTSJ (part I)
Threads Chate Patanothai.
G53SRP: Asynchronous Events in RTSJ
Condition Variables and Producer/Consumer
Multithreading.
Condition Variables and Producer/Consumer
Java Based Techhnology
Multithreading.
Multithreaded Programming
Threads and Multithreading
Presentation transcript:

Asynchronous Transfer of Control in the Timesys Reference Implementation

Asynchronous Transfer of Control Event Recognizer (OS or Scheduler) Running Thread Signal Handler Signal Handling Routine ATC is handled with signals in the reference implementation

Sync_md.c Most details of signal handling are in this file. Functions: linuxsyncinit initializes signal handler, masks linuxSyncInterruptWait emulates waiting on a semaphore linuxSyncSuspend suspends the thread linuxSyncResume resumes the thread sigfunc0 signal handler for mutex, suspend crash sends SIGSTOP in case of crash

void linuxSyncSuspend(CVMThreadID *t) { if (POSIX_COOKIE(t) == thr_self()) { t->suspended = 1; pthread_kill(POSIX_COOKIE(t), SIGUSR1); } else { mutex_lock(&t->lock); if (!t->suspended) { t->suspended = 1; /* * If the thread is performing a wait, we do special handling, * to avoid the use of signals. */ if (t->in_wait) { t->suspended_in_wait = 1; } else { pthread_kill(POSIX_COOKIE(t), SIGUSR1); } mutex_unlock(&t->lock); }

SyncResume void linuxSyncResume(CVMThreadID *t) { pthread_mutex_lock(&t->lock); if (t->suspended) { t->suspended = 0; if (t->suspended_in_wait) { t->suspended_in_wait = 0; pthread_cond_signal(&t->suspend_cv); } else { pthread_kill(POSIX_COOKIE(t), SIGUSR1); } pthread_mutex_unlock(&t->lock); }

Dispatcher Loop while(1) { /* suspend Dispatcher thread (us) until signal reception */ sigwait(&signal_set, &signum); (*env)->CallStaticVoidMethod(env,AsyncEvent,eventHappened,sig num); if((*env)->ExceptionCheck(env)){ return; }

Scheduling Scheduling in the RI is handled through signals and timers. Most of the scheduling is handled on the Java side. Some low level functions are in: RMScheduler.java / RMScheduler.c dispatcher.java / dispatcher.c

Semantics of ATC ATC semantics are based on two ideas: The RealtimeThread.interrupt() method. How instances of AIE are propogated.

realtimeThread.interrupt() When t.interrupt is called on RealtimeThread t, A 'generic' AIE becomes pending on t. Then there are two possibilities: if t is in ATC-deferred code, it continues execution until it enters an AI section. By returning to an AI method. By calling an AI method. if t is in AI code, the AIE is thrown immediately. Control is transferred to the first enclosing catch clause. The AIE stays pending.

Import javax.realtime.*; class TimeoutExample extends RealtimeThread{ RealtimeThread target; static class AtcTrigger extends AsyncEventHandler{ AtcTrigger(RealtimeThread target, runnable logic){ // target is the thread to be interrupted // logic.run is the handler AsynchEventHandler(logic); this.target=target; } void interruptableCode() throws AIE { //interruptable code chunk int i = 0; while (true) { i = i + 1; System.out.println(i + "\n"); }

public void run(){ OneShotTimer ost = new OneShotTimer( new RelativeTime( 1000, 0 ), //1 second new AtcTrigger( // async event handler this, // target to be interrupted new Runnable(){ public void run(){ this.target.interrupt(); } ) ); try{ interruptableCode(); } catch (AIE aie) { System.out.println("\n AIE Caught\n\n\n"); aie.happened(false); }

AsyncEventHandler() public AsyncEventHandler( args... ) {... HandlerThread = new RealtimeThread(scheduling, release, memory, area, group, this); } The RealtimeThread creates a new thread to run 'this' which is the run method from the previous slide.

OneShotTimer Class milli, nano are the time until the timer expires. if(isEnabled){ try{ waitLock.wait(milli,nano); if(rescheduled){ rescheduled = false; continue; } catch(Exception e){ e.printStackTrace(); } fire(); //AsyncEvent's method return; }

HandleAsyncEvent() When handleAsyncEvent() is called, the run method of the handler is started. public void handleAsyncEvent(){ if(logic != null){ logic.run(); } In this example, the run method calls "this.target.interrupt" which calls RealtimeThread.interrupt on the InturruptableCode method.

RealtimeThread.interrupt() public synchronized void interrupt() { AsynchronouslyInterruptedException genericAIE = AsynchronouslyInterruptedException.getGeneric(); if(!genericAIE.isDoInterruptibleInProcess()){ postAIE(genericAIE); /* ROBBINS SEZ: * we additionally interrupt with the original interrupt to wake * threads from sleep, wait */ super.interrupt(); return; }

PostAIE() JNIEXPORT void JNICALL Java_javax_realtime_RealtimeThread_postAIE (JNIEnv * env, jobject target, jobject genericAIE) { CVMExecEnv* ee = CVMjniEnv2ExecEnv(env); CVMExecEnv* tee = ThreadObj2ExecEnv(env,target); if(tee==NULL) return; CVMD_gcUnsafeExec(ee,{ CVMgcUnsafeSetPendingAIException(ee,tee, CVMID_icellDirect(ee,genericAIE)); }); if(tee->isIOBlocked==CVM_TRUE){ CVMthreadIOInterrupt(&tee->threadInfo); }

interpreter.h /* This is how AI exceptions actually get posted this macro */ /* unconditionally sets targets exc env pending object to obj */ #define CVMgcUnsafeSetPendingAIException(ee, targetEE, obj) { \ CVMsetPendingAIExceptionObj(ee, targetEE, obj); \ CVMsetPendingAIExceptionFlag(targetEE); \ } #define CVMsetPendingAIExceptionFlag(ee) ((ee)- >pendingAIExceptionFlag = CVM_TRUE) #define CVMsetPendingAIExceptionObj(ee, targetEE, obj_){ \ CVMID_icellSetDirect(ee, CVMpendingAIExceptionICell(targetEE), obj_); \ }

Last Step Finally, in the file interpreter.c, we see that in the interpreter loop, if an exception is pending, and if it's an AIE, then this function is called: CVMsetCurrentExceptionObj(ee,CVMgetPendingAIExceptionObj(ee) ); This is the call which actually runs the handler method.