Lecture6

  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Lecture6 as PDF for free.

More details

  • Words: 7,487
  • Pages: 126
Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Embedded Systems Programming Lecture 6 Ver´ onica Gaspes www2.hh.se/staff/vero

Center for Research on Embedded Systems School of Information Science, Computer and Electrical Engineering

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

The reactive embedded system

Application Sensor

Port

Sensor A Sensor B

Sensor

Port

Port

Actuator

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

The reactive embedded system

Application driver

Sensor

Port

Sensor A app Sensor B

Sensor

Port

driver

Port

Actuator

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Reactive Objects

Boxes Represent software or hardware reactive objects that: Maintain an internal state (variables, registers, etc) Provide a set of methods as reactions to external events (ISRs, etc) Simply rest between reactions! Arrows Represent event or signal or message flow between objects that can be either synchronous asynchronous

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Reactive Objects

Boxes Represent software or hardware reactive objects that: Maintain an internal state (variables, registers, etc) Provide a set of methods as reactions to external events (ISRs, etc) Simply rest between reactions! Arrows Represent event or signal or message flow between objects that can be either synchronous asynchronous

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Reactive Objects

Boxes Represent software or hardware reactive objects that: Maintain an internal state (variables, registers, etc) Provide a set of methods as reactions to external events (ISRs, etc) Simply rest between reactions! Arrows Represent event or signal or message flow between objects that can be either synchronous asynchronous

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Reactive Objects Boxes Represent software or hardware reactive objects that: Maintain an internal state (variables, registers, etc) Provide a set of methods as reactions to external events (ISRs, etc) Simply rest between reactions! Arrows Represent event or signal or message flow between objects that can be either asynchronous synchronous

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Hardware objects Black boxes that do nothing unless stimulated by external events.

Class The kind or type or model of a circuit. Instance A particular circuit on a particular board. State Internal register status or logic status of an object instance.

Provided interface The set of pins on a circuit that recognize signals. Required interface The set of pins on a circuit that generate signals. Method call To raise an input signal and wait for a response (synchronous) or just continue (asynchronous).

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Hardware objects Black boxes that do nothing unless stimulated by external events.

Class The kind or type or model of a circuit. Instance A particular circuit on a particular board. State Internal register status or logic status of an object instance.

Provided interface The set of pins on a circuit that recognize signals. Required interface The set of pins on a circuit that generate signals. Method call To raise an input signal and wait for a response (synchronous) or just continue (asynchronous).

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Hardware objects Black boxes that do nothing unless stimulated by external events.

Class The kind or type or model of a circuit. Instance A particular circuit on a particular board. State Internal register status or logic status of an object instance.

Provided interface The set of pins on a circuit that recognize signals. Required interface The set of pins on a circuit that generate signals. Method call To raise an input signal and wait for a response (synchronous) or just continue (asynchronous).

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Hardware objects Black boxes that do nothing unless stimulated by external events.

Class The kind or type or model of a circuit. Instance A particular circuit on a particular board. State Internal register status or logic status of an object instance.

Provided interface The set of pins on a circuit that recognize signals. Required interface The set of pins on a circuit that generate signals. Method call To raise an input signal and wait for a response (synchronous) or just continue (asynchronous).

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Hardware objects Black boxes that do nothing unless stimulated by external events.

Class The kind or type or model of a circuit. Instance A particular circuit on a particular board. State Internal register status or logic status of an object instance.

Provided interface The set of pins on a circuit that recognize signals. Required interface The set of pins on a circuit that generate signals. Method call To raise an input signal and wait for a response (synchronous) or just continue (asynchronous).

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Hardware objects Black boxes that do nothing unless stimulated by external events.

Class The kind or type or model of a circuit. Instance A particular circuit on a particular board. State Internal register status or logic status of an object instance.

Provided interface The set of pins on a circuit that recognize signals. Required interface The set of pins on a circuit that generate signals. Method call To raise an input signal and wait for a response (synchronous) or just continue (asynchronous).

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Hardware objects Black boxes that do nothing unless stimulated by external events.

Class The kind or type or model of a circuit. Instance A particular circuit on a particular board. State Internal register status or logic status of an object instance.

Provided interface The set of pins on a circuit that recognize signals. Required interface The set of pins on a circuit that generate signals. Method call To raise an input signal and wait for a response (synchronous) or just continue (asynchronous).

Reactive objects

Encoding state layout

A serial port

State Internal registers Provided interface Signal change Bit pattern received Clock pulse Required interface Signal change Interrupt signal

Encoding methods

Programming with TinyTimber

Reactive objects

Encoding state layout

A serial port

State Internal registers Provided interface Signal change Bit pattern received Clock pulse Required interface Signal change Interrupt signal

Encoding methods

Programming with TinyTimber

Reactive objects

Encoding state layout

A serial port

State Internal registers Provided interface Signal change Bit pattern received Clock pulse Required interface Signal change Interrupt signal

Encoding methods

Programming with TinyTimber

Reactive objects

Encoding state layout

A serial port

State Internal registers Provided interface Signal change Bit pattern received Clock pulse Required interface Signal change Interrupt signal

Encoding methods

Programming with TinyTimber

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Software Objects Black boxes that do nothing unless stimulated by external events. Class Program behaviour expressed as state variable layout and method code. Instance A record of state variables at at a particular address (the object’s identity). State Current state variable contents of a particular object.

Provided interface The set of methods a class exports. Required interface Method calls issued to other objects. Method call Call to a function with the designated object address as the first argument.

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Software Objects Black boxes that do nothing unless stimulated by external events. Class Program behaviour expressed as state variable layout and method code. Instance A record of state variables at at a particular address (the object’s identity). State Current state variable contents of a particular object.

Provided interface The set of methods a class exports. Required interface Method calls issued to other objects. Method call Call to a function with the designated object address as the first argument.

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Software Objects Black boxes that do nothing unless stimulated by external events. Class Program behaviour expressed as state variable layout and method code. Instance A record of state variables at at a particular address (the object’s identity). State Current state variable contents of a particular object.

Provided interface The set of methods a class exports. Required interface Method calls issued to other objects. Method call Call to a function with the designated object address as the first argument.

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Software Objects Black boxes that do nothing unless stimulated by external events. Class Program behaviour expressed as state variable layout and method code. Instance A record of state variables at at a particular address (the object’s identity). State Current state variable contents of a particular object.

Provided interface The set of methods a class exports. Required interface Method calls issued to other objects. Method call Call to a function with the designated object address as the first argument.

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Software Objects Black boxes that do nothing unless stimulated by external events. Class Program behaviour expressed as state variable layout and method code. Instance A record of state variables at at a particular address (the object’s identity). State Current state variable contents of a particular object.

Provided interface The set of methods a class exports. Required interface Method calls issued to other objects. Method call Call to a function with the designated object address as the first argument.

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Software Objects Black boxes that do nothing unless stimulated by external events. Class Program behaviour expressed as state variable layout and method code. Instance A record of state variables at at a particular address (the object’s identity). State Current state variable contents of a particular object.

Provided interface The set of methods a class exports. Required interface Method calls issued to other objects. Method call Call to a function with the designated object address as the first argument.

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Software Objects Black boxes that do nothing unless stimulated by external events. Class Program behaviour expressed as state variable layout and method code. Instance A record of state variables at at a particular address (the object’s identity). State Current state variable contents of a particular object.

Provided interface The set of methods a class exports. Required interface Method calls issued to other objects. Method call Call to a function with the designated object address as the first argument.

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

The Counter example State x Provided interface inc() read() reset() show() Required interface print from the System.out object.

class Counter{ private int x; public Counter(){x=0;} public void inc(){x++;} public int read(){return x;} public void reset(){x=0;} public void show(){ System.out.print(x); } } In Java, not reactive objects!

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

The Counter example State x Provided interface inc() read() reset() show() Required interface print from the System.out object.

class Counter{ private int x; public Counter(){x=0;} public void inc(){x++;} public int read(){return x;} public void reset(){x=0;} public void show(){ System.out.print(x); } } In Java, not reactive objects!

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

The Counter example State x Provided interface inc() read() reset() show() Required interface print from the System.out object.

class Counter{ private int x; public Counter(){x=0;} public void inc(){x++;} public int read(){return x;} public void reset(){x=0;} public void show(){ System.out.print(x); } } In Java, not reactive objects!

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

The Counter example State x Provided interface inc() read() reset() show() Required interface print from the System.out object.

class Counter{ private int x; public Counter(){x=0;} public void inc(){x++;} public int read(){return x;} public void reset(){x=0;} public void show(){ System.out.print(x); } } In Java, not reactive objects!

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Encoding state layout We will use a little kernel called TinyTimber. We will use files as modules in C. In MyClass.h #include "TinyTimber.h"

Mandatory! Specified and used by the kernel! Unconstrained!

typedef struct{ Object super; int x; char y; } MyClass; #define initMyClass(z) \ { initObject ,0,z}

initMyClass corresponds to a constructor, it includes programmer defined intialization. Using it #include "MyClass.h" MyClass a = initMyClass(13);

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Encoding state layout We will use a little kernel called TinyTimber. We will use files as modules in C. In MyClass.h #include "TinyTimber.h"

Mandatory! Specified and used by the kernel! Unconstrained!

typedef struct{ Object super; int x; char y; } MyClass; #define initMyClass(z) \ { initObject ,0,z}

initMyClass corresponds to a constructor, it includes programmer defined intialization. Using it #include "MyClass.h" MyClass a = initMyClass(13);

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Encoding state layout We will use a little kernel called TinyTimber. We will use files as modules in C. In MyClass.h #include "TinyTimber.h"

Mandatory! Specified and used by the kernel! Unconstrained!

typedef struct{ Object super; int x; char y; } MyClass; #define initMyClass(z) \ { initObject ,0,z}

initMyClass corresponds to a constructor, it includes programmer defined intialization. Using it #include "MyClass.h" MyClass a = initMyClass(13);

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Encoding state layout We will use a little kernel called TinyTimber. We will use files as modules in C. In MyClass.h #include "TinyTimber.h"

Mandatory! Specified and used by the kernel! Unconstrained!

typedef struct{ Object super; int x; char y; } MyClass; #define initMyClass(z) \ { initObject ,0,z}

initMyClass corresponds to a constructor, it includes programmer defined intialization. Using it #include "MyClass.h" MyClass a = initMyClass(13);

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Encoding state layout We will use a little kernel called TinyTimber. We will use files as modules in C. In MyClass.h #include "TinyTimber.h"

Mandatory! Specified and used by the kernel! Unconstrained!

typedef struct{ Object super; int x; char y; } MyClass; #define initMyClass(z) \ { initObject ,0,z}

initMyClass corresponds to a constructor, it includes programmer defined intialization. Using it #include "MyClass.h" MyClass a = initMyClass(13);

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Encoding state layout We will use a little kernel called TinyTimber. We will use files as modules in C. In MyClass.h #include "TinyTimber.h"

Mandatory! Specified and used by the kernel! Unconstrained!

typedef struct{ Object super; int x; char y; } MyClass; #define initMyClass(z) \ { initObject ,0,z}

initMyClass corresponds to a constructor, it includes programmer defined intialization. Using it #include "MyClass.h" MyClass a = initMyClass(13);

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Comparing with Java

class MyClass{ int x; char y; MyClass(int z){ x=0; y=z; } }

MyClass a = new MyClass(13);

In our programs we do not allocate objects in the heap (as Java does!). Our constructors are just preprocessor macros!

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Comparing with Java

class MyClass{ int x; char y; MyClass(int z){ x=0; y=z; } }

MyClass a = new MyClass(13);

In our programs we do not allocate objects in the heap (as Java does!). Our constructors are just preprocessor macros!

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Comparing with Java

class MyClass{ int x; char y; MyClass(int z){ x=0; y=z; } }

MyClass a = new MyClass(13);

In our programs we do not allocate objects in the heap (as Java does!). Our constructors are just preprocessor macros!

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Comparing with Java

class MyClass{ int x; char y; MyClass(int z){ x=0; y=z; } }

MyClass a = new MyClass(13);

In our programs we do not allocate objects in the heap (as Java does!). Our constructors are just preprocessor macros!

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Comparing with Java

class MyClass{ int x; char y; MyClass(int z){ x=0; y=z; } }

MyClass a = new MyClass(13);

In our programs we do not allocate objects in the heap (as Java does!). Our constructors are just preprocessor macros!

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Encoding methods declarations In MyClass.h typedef struct{ Object super; In Java int x; class MyClass{ char y; int x; } MyClass; char y; ... ... int myMethod( MyClass *self , int q); int myMethod(int q){ x=y+q; In MyClass.c } } int myMethod( MyClass *self , int q){ self -> x = self -> y + q; }

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Encoding methods declarations In MyClass.h typedef struct{ Object super; In Java int x; class MyClass{ char y; int x; } MyClass; char y; ... ... int myMethod( MyClass *self , int q); int myMethod(int q){ x=y+q; In MyClass.c } } int myMethod( MyClass *self , int q){ self -> x = self -> y + q; }

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Encoding methods declarations In MyClass.h typedef struct{ Object super; In Java int x; class MyClass{ char y; int x; } MyClass; char y; ... ... int myMethod( MyClass *self , int q); int myMethod(int q){ x=y+q; In MyClass.c } } int myMethod( MyClass *self , int q){ self -> x = self -> y + q; }

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Encoding methods declarations In MyClass.h typedef struct{ Object super; In Java int x; class MyClass{ char y; int x; } MyClass; char y; ... ... int myMethod( MyClass *self , int q); int myMethod(int q){ x=y+q; In MyClass.c } } int myMethod( MyClass *self , int q){ self -> x = self -> y + q; }

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Encoding function calls In Java ... MyClass a = new MyClass(13); a.myMethod(44); In our C programs ... MyClass a = initMyClass(13); myMethod( &a ,44); But, we are doing all this to do something different than just function calls! We want to have the possibility of introducing the distinction between synchronous and asynchronous messages!

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Encoding function calls In Java ... MyClass a = new MyClass(13); a.myMethod(44); In our C programs ... MyClass a = initMyClass(13); myMethod( &a ,44); But, we are doing all this to do something different than just function calls! We want to have the possibility of introducing the distinction between synchronous and asynchronous messages!

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Encoding function calls In Java ... MyClass a = new MyClass(13); a.myMethod(44); In our C programs ... MyClass a = initMyClass(13); myMethod( &a ,44); But, we are doing all this to do something different than just function calls! We want to have the possibility of introducing the distinction between synchronous and asynchronous messages!

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Encoding function calls In Java ... MyClass a = new MyClass(13); a.myMethod(44); In our C programs ... MyClass a = initMyClass(13); myMethod( &a ,44); But, we are doing all this to do something different than just function calls! We want to have the possibility of introducing the distinction between synchronous and asynchronous messages!

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Asynchronous calls Time

ASYNC(B,meth,73) A

B

(resting)

meth(B,73)

(Pseudo-) parallel execution!

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Asynchronous calls Time

ASYNC(B,meth,73) A

B

some other B method

(Pseudo-) parallel execution between A and B.

meth(B,73)

Strictly sequential execution between B’s methods!

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Asynchronous calls Time

ASYNC(B,meth,73) A

B

some other B method

(Pseudo-) parallel execution between A and B.

meth(B,73)

Strictly sequential execution between B’s methods!

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Asynchronous calls Time

ASYNC(B,meth,73) A

B

some other B method

(Pseudo-) parallel execution between A and B.

meth(B,73)

Strictly sequential execution between B’s methods!

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Synchronous calls Time

SYNC(B,meth,73) A

B

(resting)

meth(B,73) Strictly sequential execution between A and B!

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Synchronous calls Time

SYNC(B,meth,73) A

B

some other B method

(Pseudo-) parallel execution between A and B’s other method.

meth(B,73)

Strictly sequential execution between B’s methods and between A and the method called synchronously.

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Synchronous calls Time

SYNC(B,meth,73) A

B

some other B method

(Pseudo-) parallel execution between A and B’s other method.

meth(B,73)

Strictly sequential execution between B’s methods and between A and the method called synchronously.

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Synchronous calls Time

SYNC(B,meth,73) A

B

some other B method

(Pseudo-) parallel execution between A and B’s other method.

meth(B,73)

Strictly sequential execution between B’s methods and between A and the method called synchronously.

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Observations

Serialization of object methods looks just like standard mutual exclusion. A synchronous call is just like a mutex-protected function call. It is the asynchronous calls that introduce concurrency. Asynchronous calls further more need additional temporary storage (if a call cannot execute immediately) Suggestion: let an asynchronous call be equivalent to a synchronous call executed by a fresh thread!

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Observations

Serialization of object methods looks just like standard mutual exclusion. A synchronous call is just like a mutex-protected function call. It is the asynchronous calls that introduce concurrency. Asynchronous calls further more need additional temporary storage (if a call cannot execute immediately) Suggestion: let an asynchronous call be equivalent to a synchronous call executed by a fresh thread!

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Observations

Serialization of object methods looks just like standard mutual exclusion. A synchronous call is just like a mutex-protected function call. It is the asynchronous calls that introduce concurrency. Asynchronous calls further more need additional temporary storage (if a call cannot execute immediately) Suggestion: let an asynchronous call be equivalent to a synchronous call executed by a fresh thread!

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Observations

Serialization of object methods looks just like standard mutual exclusion. A synchronous call is just like a mutex-protected function call. It is the asynchronous calls that introduce concurrency. Asynchronous calls further more need additional temporary storage (if a call cannot execute immediately) Suggestion: let an asynchronous call be equivalent to a synchronous call executed by a fresh thread!

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Observations

Serialization of object methods looks just like standard mutual exclusion. A synchronous call is just like a mutex-protected function call. It is the asynchronous calls that introduce concurrency. Asynchronous calls further more need additional temporary storage (if a call cannot execute immediately) Suggestion: let an asynchronous call be equivalent to a synchronous call executed by a fresh thread!

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Observations

Serialization of object methods looks just like standard mutual exclusion. A synchronous call is just like a mutex-protected function call. It is the asynchronous calls that introduce concurrency. Asynchronous calls further more need additional temporary storage (if a call cannot execute immediately) Suggestion: let an asynchronous call be equivalent to a synchronous call executed by a fresh thread!

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Implementing SYNC

In TinyTimber.c int synch(Object *to, Meth meth, int arg){ int result; lock(&to->mutex); result = method(to,arg); unlock(&to->mutex); return result; }

Every object has to have its own mutex and we need a way to force every instance to be an object!

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Implementing SYNC

In TinyTimber.c int synch(Object *to, Meth meth, int arg){ int result; lock(&to->mutex); result = method(to,arg); unlock(&to->mutex); return result; }

Every object has to have its own mutex and we need a way to force every instance to be an object!

Reactive objects

Encoding state layout

Encoding methods

Implementing SYNC

In TinyTimber.h typedef struct{ mutex mutex; } Object; typedef int (*Meth)(Object*,int); #define SYNC(obj,meth,arg) = \ sync((Object*)obj,(Meth)meth, arg)

Programming with TinyTimber

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Implementing ASYNC In TinyTimber.c void async(Object* to, Method meth, int arg){ Msg msg = dequeue(&freeQ); msg->function = meth; msg->arg = arg; msg->to = to; if(setjmp(msg->context)!=0){ sync(current->to,current->function,current->arg); enqueue(current,&freeQ); dispatch(dequeue(&readyQ)); } STACKPTR(msg->context)=&msg->stack; enqueue(msg,&readyQ); }

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Implementing ASYNC In TinyTimber.c void async(Object* to, Method meth, int arg){ Msg msg = dequeue(&freeQ); msg->function = meth; msg->arg = arg; msg->to = to; if(setjmp(msg->context)!=0){ sync(current->to,current->function,current->arg); enqueue(current,&freeQ); dispatch(dequeue(&readyQ)); } STACKPTR(msg->context)=&msg->stack; enqueue(msg,&readyQ); }

Reactive objects

Encoding state layout

Encoding methods

Implementing ASYNC

In TinyTimber.h #define ASYNC(obj,meth,arg) = \ async((Object *)obj, (Meth)meth, arg)

Programming with TinyTimber

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Summary

Threads are replaced by asynchronous messages Old operation spawn superceeded by async Old oprations lock and unlock are only used inside sync The new kernel interface: void async(Object *to, Meth meth, int arg) int sync(Object *to, Meth meth, int arg) typedefs for Object and Meth defines for ASYNC and SYNC

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Summary

Threads are replaced by asynchronous messages Old operation spawn superceeded by async Old oprations lock and unlock are only used inside sync The new kernel interface: void async(Object *to, Meth meth, int arg) int sync(Object *to, Meth meth, int arg) typedefs for Object and Meth defines for ASYNC and SYNC

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Summary

Threads are replaced by asynchronous messages Old operation spawn superceeded by async Old oprations lock and unlock are only used inside sync The new kernel interface: void async(Object *to, Meth meth, int arg) int sync(Object *to, Meth meth, int arg) typedefs for Object and Meth defines for ASYNC and SYNC

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Summary

Threads are replaced by asynchronous messages Old operation spawn superceeded by async Old oprations lock and unlock are only used inside sync The new kernel interface: void async(Object *to, Meth meth, int arg) int sync(Object *to, Meth meth, int arg) typedefs for Object and Meth defines for ASYNC and SYNC

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Summary

Threads are replaced by asynchronous messages Old operation spawn superceeded by async Old oprations lock and unlock are only used inside sync The new kernel interface: void async(Object *to, Meth meth, int arg) int sync(Object *to, Meth meth, int arg) typedefs for Object and Meth defines for ASYNC and SYNC

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

ASYNC to self? Time

ASYNC(A,meth,73) A

current A method

Strictly sequential execution!

meth(A,73)

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

ASYNC to self? Time

ASYNC(A,meth,73) A

current A method

Strictly sequential execution!

meth(A,73)

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

SYNC to self? Time

SYNC(A,meth,73) A

? ?

DEADLOCK!

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

SYNC to self? Time

SYNC(A,meth,73) A

? ?

DEADLOCK!

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Deadlock Deadlock arises when requesting new exclusive access to something you already have. In general, a chain of tasks may be involved:

T1

m1

T2

m2

T3

m3

T1 holds m1 T1 wants m2 T2 holds m2 T2 wants m3 T3 holds m3 T3 wants m1

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Deadlock

A system in deadlock will remain stuck, unless a thread chooses to back off from its current claim . . .

Reactive objects

Encoding state layout

Deadlock in the real world

Encoding methods

Programming with TinyTimber

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Deadlock via SYNC

A cycle of possible simultaneus calls to SYNC

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Deadlock via SYNC

Sufficient deadlock protection: insert at least one ASYNC.

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Programming idiom 1. Classes All objects must inherit Object: typedef MyClass{ Object super; // extra fileds } 2. Objects Object instantiation is done declaratively on the top level (static object structure): ClassA a = initClassA(ival); ClassB b1 = initClassB(); ClassB b2 = initClassB();

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Programming idiom 1. Classes All objects must inherit Object: typedef MyClass{ Object super; // extra fileds } 2. Objects Object instantiation is done declaratively on the top level (static object structure): ClassA a = initClassA(ival); ClassB b1 = initClassB(); ClassB b2 = initClassB();

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Programming idiom 1. Classes All objects must inherit Object: typedef MyClass{ Object super; // extra fileds } 2. Objects Object instantiation is done declaratively on the top level (static object structure): ClassA a = initClassA(ival); ClassB b1 = initClassB(); ClassB b2 = initClassB();

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Programming idiom (ctd.)

3. Method calls Whenever a method call goes to another object, either SYNC or ASYNC must be used. (Tiny) Limitation All methods must take arguments self and an int!

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Programming idiom (ctd.)

3. Method calls Whenever a method call goes to another object, either SYNC or ASYNC must be used. (Tiny) Limitation All methods must take arguments self and an int!

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Programming idiom (ctd.)

3. Method calls Whenever a method call goes to another object, either SYNC or ASYNC must be used. (Tiny) Limitation All methods must take arguments self and an int!

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Connecting the external world

interrupt

write to port

write to port interrupt

read from port

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Making the methods explicit

interrupt

write to port

write to port interrupt

read from port

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

The top-level object interrupt write to port

write to port

read from port interrupt

Notice the interrupt handlers.

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

The top-level object

The microprocessor itself! It is just like any other reactive object! it is implicitly instantiated when power is turned on its state is all global variables, of which many will be reactive objects in their own right its methods are the installed interrupt handlers its self is only conceptual (there is no concrete pointer . . . )

The top-level object methods are scheduled by the CPU hardware, not by the TinyTimber kernel!

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

The top-level object

The microprocessor itself! It is just like any other reactive object! it is implicitly instantiated when power is turned on its state is all global variables, of which many will be reactive objects in their own right its methods are the installed interrupt handlers its self is only conceptual (there is no concrete pointer . . . )

The top-level object methods are scheduled by the CPU hardware, not by the TinyTimber kernel!

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

The top-level object

The microprocessor itself! It is just like any other reactive object! it is implicitly instantiated when power is turned on its state is all global variables, of which many will be reactive objects in their own right its methods are the installed interrupt handlers its self is only conceptual (there is no concrete pointer . . . )

The top-level object methods are scheduled by the CPU hardware, not by the TinyTimber kernel!

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

The top-level object

The microprocessor itself! It is just like any other reactive object! it is implicitly instantiated when power is turned on its state is all global variables, of which many will be reactive objects in their own right its methods are the installed interrupt handlers its self is only conceptual (there is no concrete pointer . . . )

The top-level object methods are scheduled by the CPU hardware, not by the TinyTimber kernel!

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

The top-level object

The microprocessor itself! It is just like any other reactive object! it is implicitly instantiated when power is turned on its state is all global variables, of which many will be reactive objects in their own right its methods are the installed interrupt handlers its self is only conceptual (there is no concrete pointer . . . )

The top-level object methods are scheduled by the CPU hardware, not by the TinyTimber kernel!

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

The top-level object

The microprocessor itself! It is just like any other reactive object! it is implicitly instantiated when power is turned on its state is all global variables, of which many will be reactive objects in their own right its methods are the installed interrupt handlers its self is only conceptual (there is no concrete pointer . . . )

The top-level object methods are scheduled by the CPU hardware, not by the TinyTimber kernel!

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

The top-level object

The microprocessor itself! It is just like any other reactive object! it is implicitly instantiated when power is turned on its state is all global variables, of which many will be reactive objects in their own right its methods are the installed interrupt handlers its self is only conceptual (there is no concrete pointer . . . )

The top-level object methods are scheduled by the CPU hardware, not by the TinyTimber kernel!

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Connecting interrupts

In TinyTimber.c #define INTERRUPT(vector, stmt) \ ISR(vector) {stmt; schedule();}

stmt can be any C statement that manipulates global state and/or calls methods of internal objects. Avoid SYNC calls from interrupts, they will report deadlock if the receiver object is busy. schedule(); is a refined version of yield().

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Connecting interrupts

In TinyTimber.c #define INTERRUPT(vector, stmt) \ ISR(vector) {stmt; schedule();}

stmt can be any C statement that manipulates global state and/or calls methods of internal objects. Avoid SYNC calls from interrupts, they will report deadlock if the receiver object is busy. schedule(); is a refined version of yield().

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Connecting interrupts

In TinyTimber.c #define INTERRUPT(vector, stmt) \ ISR(vector) {stmt; schedule();}

stmt can be any C statement that manipulates global state and/or calls methods of internal objects. Avoid SYNC calls from interrupts, they will report deadlock if the receiver object is busy. schedule(); is a refined version of yield().

Reactive objects

Encoding state layout

Encoding methods

Example A Counter example (counter.h) #include "TinyTimber.h" typedef struct{ Object super; int val; } Counter; #define initCounter(n) {initObject(),n} A Counter example (counter.c) int inc(Counter *self, int arg){ self->val = self->val + arg; } int reset(Counter *self, int arg){ self->val = arg; }

Programming with TinyTimber

Reactive objects

Encoding state layout

Encoding methods

Example A Counter example (counter.h) #include "TinyTimber.h" typedef struct{ Object super; int val; } Counter; #define initCounter(n) {initObject(),n} A Counter example (counter.c) int inc(Counter *self, int arg){ self->val = self->val + arg; } int reset(Counter *self, int arg){ self->val = arg; }

Programming with TinyTimber

Reactive objects

Encoding state layout

Encoding methods

Example A Counter example (counter.h) #include "TinyTimber.h" typedef struct{ Object super; int val; } Counter; #define initCounter(n) {initObject(),n} A Counter example (counter.c) int inc(Counter *self, int arg){ self->val = self->val + arg; } int reset(Counter *self, int arg){ self->val = arg; }

Programming with TinyTimber

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Example client

In main.c Counter counter = initCounter(0); INTERRUPT(SIG_PIN_CHANGE1,ASYNC(&counter,inc,1));

Notice Calls to ASYNC(counter,reset,0); will always be inside the body of some method. That method will ultimately be called from an interrupt!

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Example client

In main.c Counter counter = initCounter(0); INTERRUPT(SIG_PIN_CHANGE1,ASYNC(&counter,inc,1));

Notice Calls to ASYNC(counter,reset,0); will always be inside the body of some method. That method will ultimately be called from an interrupt!

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Example client

In main.c Counter counter = initCounter(0); INTERRUPT(SIG_PIN_CHANGE1,ASYNC(&counter,inc,1));

Notice Calls to ASYNC(counter,reset,0); will always be inside the body of some method. That method will ultimately be called from an interrupt!

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Reset When system starts up, a reset signal is generated by the hardware. There will be an interrupt routine like any other one . . .

Pin1 change

Timer comp . . .

Reset

Complication The reset routine cannot return as it has not really interrupted anything! In the active system view this is interpreted as compute until someone turns off the power!

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Reset When system starts up, a reset signal is generated by the hardware. There will be an interrupt routine like any other one . . .

Pin1 change

Timer comp . . .

Reset

Complication The reset routine cannot return as it has not really interrupted anything! In the active system view this is interpreted as compute until someone turns off the power!

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Reset When system starts up, a reset signal is generated by the hardware. There will be an interrupt routine like any other one . . .

Pin1 change

Timer comp . . .

Reset

Complication The reset routine cannot return as it has not really interrupted anything! In the active system view this is interpreted as compute until someone turns off the power!

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

main()

The main() function in C is an abstraction of the reset handler . . . . . . just as a program is an abstraction of the notion of running a computer until it stops In traditional programs main() does indeed return, which can be understood as a request to the OD to turn off the power to the virtual computer that was set up to run the program! In a reactive system we do not want power to be turned off at all, but we also do not want to let main() compute forever just to keep it from returning . . . a reactive system rests when it is not reacting

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

main()

The main() function in C is an abstraction of the reset handler . . . . . . just as a program is an abstraction of the notion of running a computer until it stops In traditional programs main() does indeed return, which can be understood as a request to the OD to turn off the power to the virtual computer that was set up to run the program! In a reactive system we do not want power to be turned off at all, but we also do not want to let main() compute forever just to keep it from returning . . . a reactive system rests when it is not reacting

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

main()

The main() function in C is an abstraction of the reset handler . . . . . . just as a program is an abstraction of the notion of running a computer until it stops In traditional programs main() does indeed return, which can be understood as a request to the OD to turn off the power to the virtual computer that was set up to run the program! In a reactive system we do not want power to be turned off at all, but we also do not want to let main() compute forever just to keep it from returning . . . a reactive system rests when it is not reacting

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

main()

The main() function in C is an abstraction of the reset handler . . . . . . just as a program is an abstraction of the notion of running a computer until it stops In traditional programs main() does indeed return, which can be understood as a request to the OD to turn off the power to the virtual computer that was set up to run the program! In a reactive system we do not want power to be turned off at all, but we also do not want to let main() compute forever just to keep it from returning . . . a reactive system rests when it is not reacting

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

The idle task Solution Let main() finish by literally putting the CPU to sleep until the next interrupt! (Most architectures have a special machine instruction that does so!) We want main() to finish by calling this instruction: void idle(){ ENABLE(); while(1)SLEEP(); } Implemented in TinyTimber #define STARTUP(stmt) \ int main(){initialize();stm;idle();}

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

The idle task Solution Let main() finish by literally putting the CPU to sleep until the next interrupt! (Most architectures have a special machine instruction that does so!) We want main() to finish by calling this instruction: void idle(){ ENABLE(); while(1)SLEEP(); } Implemented in TinyTimber #define STARTUP(stmt) \ int main(){initialize();stm;idle();}

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

The idle task Solution Let main() finish by literally putting the CPU to sleep until the next interrupt! (Most architectures have a special machine instruction that does so!) We want main() to finish by calling this instruction: void idle(){ ENABLE(); while(1)SLEEP(); } Implemented in TinyTimber #define STARTUP(stmt) \ int main(){initialize();stm;idle();}

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

The idle task Solution Let main() finish by literally putting the CPU to sleep until the next interrupt! (Most architectures have a special machine instruction that does so!) We want main() to finish by calling this instruction: void idle(){ ENABLE(); while(1)SLEEP(); } Implemented in TinyTimber #define STARTUP(stmt) \ int main(){initialize();stm;idle();}

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

The idle task Solution Let main() finish by literally putting the CPU to sleep until the next interrupt! (Most architectures have a special machine instruction that does so!) We want main() to finish by calling this instruction: void idle(){ ENABLE(); while(1)SLEEP(); } Implemented in TinyTimber #define STARTUP(stmt) \ int main(){initialize();stm;idle();}

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Example client + reset

In main.c Counter counter = initCounter(0); INTERRUPT(SIG_PIN_CHANGE1, ASYNC(&counter,inc,1)); STARTUP(ASYNC(&counter,reset,0));

Notice! No main() function

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Example client + reset

In main.c Counter counter = initCounter(0); INTERRUPT(SIG_PIN_CHANGE1, ASYNC(&counter,inc,1)); STARTUP(ASYNC(&counter,reset,0));

Notice! No main() function

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Sanity rules

In a system of reactive objects Methods only access variables that belong to self. Global variables that are not objects, are considered local to the top-level object. method calls between objects that are wrapped within a SYNC or ASYNC shield. Properly upheld, these rules guarantee a system that is free from deadlock (provided the absence of cyclic SYNC) free from critical section race conditions

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Sanity rules

In a system of reactive objects Methods only access variables that belong to self. Global variables that are not objects, are considered local to the top-level object. method calls between objects that are wrapped within a SYNC or ASYNC shield. Properly upheld, these rules guarantee a system that is free from deadlock (provided the absence of cyclic SYNC) free from critical section race conditions

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Sanity rules

In a system of reactive objects Methods only access variables that belong to self. Global variables that are not objects, are considered local to the top-level object. method calls between objects that are wrapped within a SYNC or ASYNC shield. Properly upheld, these rules guarantee a system that is free from deadlock (provided the absence of cyclic SYNC) free from critical section race conditions

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Sanity rules

In a system of reactive objects Methods only access variables that belong to self. Global variables that are not objects, are considered local to the top-level object. method calls between objects that are wrapped within a SYNC or ASYNC shield. Properly upheld, these rules guarantee a system that is free from deadlock (provided the absence of cyclic SYNC) free from critical section race conditions

Reactive objects

Encoding state layout

Encoding methods

Programming with TinyTimber

Sanity rules

In a system of reactive objects Methods only access variables that belong to self. Global variables that are not objects, are considered local to the top-level object. method calls between objects that are wrapped within a SYNC or ASYNC shield. Properly upheld, these rules guarantee a system that is free from deadlock (provided the absence of cyclic SYNC) free from critical section race conditions

Related Documents

Lecture6
June 2020 10
Lecture6
November 2019 10
Lecture6-ieee802
November 2019 15
Avr Lecture6
November 2019 13
Lecture6-lans
May 2020 8