Professional Documents
Culture Documents
Verónica Gaspes
www2.hh.se/staff/vero
Application
Sensor
Port Sensor A
Port Actuator
Sensor B
Sensor Port
Reactive objects Encoding state layout Encoding methods Programming with TinyTimber
Application
driver
Sensor
Port Sensor A
app
Port Actuator
Sensor B
Sensor Port
driver
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
Provided interface
Class
The set of pins on a circuit that
The kind or type or model of a recognize signals.
circuit.
Required interface
Instance
The set of pins on a circuit that
A particular circuit on a particular
generate signals.
board.
Hardware objects
Provided interface
Class
The set of pins on a circuit that
The kind or type or model of a recognize signals.
circuit.
Required interface
Instance
The set of pins on a circuit that
A particular circuit on a particular
generate signals.
board.
Hardware objects
Provided interface
Class
The set of pins on a circuit that
The kind or type or model of a recognize signals.
circuit.
Required interface
Instance
The set of pins on a circuit that
A particular circuit on a particular
generate signals.
board.
Hardware objects
Provided interface
Class
The set of pins on a circuit that
The kind or type or model of a recognize signals.
circuit.
Required interface
Instance
The set of pins on a circuit that
A particular circuit on a particular
generate signals.
board.
Hardware objects
Provided interface
Class
The set of pins on a circuit that
The kind or type or model of a recognize signals.
circuit.
Required interface
Instance
The set of pins on a circuit that
A particular circuit on a particular
generate signals.
board.
Hardware objects
Provided interface
Class
The set of pins on a circuit that
The kind or type or model of a recognize signals.
circuit.
Required interface
Instance
The set of pins on a circuit that
A particular circuit on a particular
generate signals.
board.
Hardware objects
Provided interface
Class
The set of pins on a circuit that
The kind or type or model of a recognize signals.
circuit.
Required interface
Instance
The set of pins on a circuit that
A particular circuit on a particular
generate signals.
board.
A serial port
State
Internal registers
Provided interface
Signal change
Bit pattern received
Clock pulse
Required interface
Signal change
Interrupt signal
Reactive objects Encoding state layout Encoding methods Programming with TinyTimber
A serial port
State
Internal registers
Provided interface
Signal change
Bit pattern received
Clock pulse
Required interface
Signal change
Interrupt signal
Reactive objects Encoding state layout Encoding methods Programming with TinyTimber
A serial port
State
Internal registers
Provided interface
Signal change
Bit pattern received
Clock pulse
Required interface
Signal change
Interrupt signal
Reactive objects Encoding state layout Encoding methods Programming with TinyTimber
A serial port
State
Internal registers
Provided interface
Signal change
Bit pattern received
Clock pulse
Required interface
Signal change
Interrupt signal
Reactive objects Encoding state layout Encoding methods Programming with TinyTimber
Software Objects
Class
Provided interface
Program behaviour expressed as
The set of methods a class
state variable layout and method
exports.
code.
Required interface
Instance
A record of state variables at at a Method calls issued to other
particular address (the object’s objects.
identity).
Method call
State Call to a function with the
designated object address as the
Current state variable contents of
first argument.
a particular object.
Reactive objects Encoding state layout Encoding methods Programming with TinyTimber
Software Objects
Class
Provided interface
Program behaviour expressed as
The set of methods a class
state variable layout and method
exports.
code.
Required interface
Instance
A record of state variables at at a Method calls issued to other
particular address (the object’s objects.
identity).
Method call
State Call to a function with the
designated object address as the
Current state variable contents of
first argument.
a particular object.
Reactive objects Encoding state layout Encoding methods Programming with TinyTimber
Software Objects
Class
Provided interface
Program behaviour expressed as
The set of methods a class
state variable layout and method
exports.
code.
Required interface
Instance
A record of state variables at at a Method calls issued to other
particular address (the object’s objects.
identity).
Method call
State Call to a function with the
designated object address as the
Current state variable contents of
first argument.
a particular object.
Reactive objects Encoding state layout Encoding methods Programming with TinyTimber
Software Objects
Class
Provided interface
Program behaviour expressed as
The set of methods a class
state variable layout and method
exports.
code.
Required interface
Instance
A record of state variables at at a Method calls issued to other
particular address (the object’s objects.
identity).
Method call
State Call to a function with the
designated object address as the
Current state variable contents of
first argument.
a particular object.
Reactive objects Encoding state layout Encoding methods Programming with TinyTimber
Software Objects
Class
Provided interface
Program behaviour expressed as
The set of methods a class
state variable layout and method
exports.
code.
Required interface
Instance
A record of state variables at at a Method calls issued to other
particular address (the object’s objects.
identity).
Method call
State Call to a function with the
designated object address as the
Current state variable contents of
first argument.
a particular object.
Reactive objects Encoding state layout Encoding methods Programming with TinyTimber
Software Objects
Class
Provided interface
Program behaviour expressed as
The set of methods a class
state variable layout and method
exports.
code.
Required interface
Instance
A record of state variables at at a Method calls issued to other
particular address (the object’s objects.
identity).
Method call
State Call to a function with the
designated object address as the
Current state variable contents of
first argument.
a particular object.
Reactive objects Encoding state layout Encoding methods Programming with TinyTimber
Software Objects
Class
Provided interface
Program behaviour expressed as
The set of methods a class
state variable layout and method
exports.
code.
Required interface
Instance
A record of state variables at at a Method calls issued to other
particular address (the object’s objects.
identity).
Method call
State Call to a function with the
designated object address as the
Current state variable contents of
first argument.
a particular object.
Reactive objects Encoding state layout Encoding methods Programming with TinyTimber
State
class Counter{
x
private int x;
public Counter(){x=0;}
Provided interface
public void inc(){x++;}
inc()
public int read(){return x;}
read() public void reset(){x=0;}
reset() public void show(){
show() System.out.print(x);
}
Required interface }
print from the
System.out object. In Java, not reactive objects!
Reactive objects Encoding state layout Encoding methods Programming with TinyTimber
State
class Counter{
x
private int x;
public Counter(){x=0;}
Provided interface
public void inc(){x++;}
inc()
public int read(){return x;}
read() public void reset(){x=0;}
reset() public void show(){
show() System.out.print(x);
}
Required interface }
print from the
System.out object. In Java, not reactive objects!
Reactive objects Encoding state layout Encoding methods Programming with TinyTimber
State
class Counter{
x
private int x;
public Counter(){x=0;}
Provided interface
public void inc(){x++;}
inc()
public int read(){return x;}
read() public void reset(){x=0;}
reset() public void show(){
show() System.out.print(x);
}
Required interface }
print from the
System.out object. In Java, not reactive objects!
Reactive objects Encoding state layout Encoding methods Programming with TinyTimber
State
class Counter{
x
private int x;
public Counter(){x=0;}
Provided interface
public void inc(){x++;}
inc()
public int read(){return x;}
read() public void reset(){x=0;}
reset() public void show(){
show() System.out.print(x);
}
Required interface }
print from the
System.out object. In Java, not reactive objects!
Reactive objects Encoding state layout Encoding methods Programming with TinyTimber
class MyClass{
int x;
char y; In our programs we do
MyClass(int z){ not allocate objects in
x=0; the heap (as Java
y=z; does!).
}
}
Our constructors are just
preprocessor macros!
MyClass a = new MyClass(13);
Reactive objects Encoding state layout Encoding methods Programming with TinyTimber
class MyClass{
int x;
char y; In our programs we do
MyClass(int z){ not allocate objects in
x=0; the heap (as Java
y=z; does!).
}
}
Our constructors are just
preprocessor macros!
MyClass a = new MyClass(13);
Reactive objects Encoding state layout Encoding methods Programming with TinyTimber
class MyClass{
int x;
char y; In our programs we do
MyClass(int z){ not allocate objects in
x=0; the heap (as Java
y=z; does!).
}
}
Our constructors are just
preprocessor macros!
MyClass a = new MyClass(13);
Reactive objects Encoding state layout Encoding methods Programming with TinyTimber
class MyClass{
int x;
char y; In our programs we do
MyClass(int z){ not allocate objects in
x=0; the heap (as Java
y=z; does!).
}
}
Our constructors are just
preprocessor macros!
MyClass a = new MyClass(13);
Reactive objects Encoding state layout Encoding methods Programming with TinyTimber
class MyClass{
int x;
char y; In our programs we do
MyClass(int z){ not allocate objects in
x=0; the heap (as Java
y=z; does!).
}
}
Our constructors are just
preprocessor macros!
MyClass a = new MyClass(13);
Reactive objects Encoding state layout Encoding methods Programming with TinyTimber
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
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
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
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
In Java
...
MyClass a = new MyClass(13);
a.myMethod(44);
In our C programs
...
MyClass a = initMyClass(13);
myMethod( &a ,44);
In Java
...
MyClass a = new MyClass(13);
a.myMethod(44);
In our C programs
...
MyClass a = initMyClass(13);
myMethod( &a ,44);
In Java
...
MyClass a = new MyClass(13);
a.myMethod(44);
In our C programs
...
MyClass a = initMyClass(13);
myMethod( &a ,44);
In Java
...
MyClass a = new MyClass(13);
a.myMethod(44);
In our C programs
...
MyClass a = initMyClass(13);
myMethod( &a ,44);
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
Asynchronous calls
Time
ASYNC(B,meth,73)
A
Asynchronous calls
Time
ASYNC(B,meth,73)
A
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
Synchronous calls
Time
SYNC(B,meth,73)
A
Synchronous calls
Time
SYNC(B,meth,73)
A
Observations
Observations
Observations
Observations
Observations
Observations
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 Programming with TinyTimber
Implementing SYNC
In TinyTimber.h
typedef struct{
mutex mutex;
} Object;
#define SYNC(obj,meth,arg) = \
sync((Object*)obj,(Meth)meth, arg)
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 Programming with TinyTimber
Implementing ASYNC
In TinyTimber.h
#define ASYNC(obj,meth,arg) = \
async((Object *)obj, (Meth)meth, arg)
Reactive objects Encoding state layout Encoding methods Programming with TinyTimber
Summary
Summary
Summary
Summary
Summary
ASYNC to self?
Time
ASYNC(A,meth,73)
A current A method meth(A,73)
Strictly sequential
execution!
Reactive objects Encoding state layout Encoding methods Programming with TinyTimber
ASYNC to self?
Time
ASYNC(A,meth,73)
A current A method meth(A,73)
Strictly sequential
execution!
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
T1 m1
T1 holds m1
T1 wants m2
T2 holds m2
T2 m2
T2 wants m3
T3 holds m3
T3 wants m1
T3 m3
Reactive objects Encoding state layout Encoding methods Programming with TinyTimber
Deadlock
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
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
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
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
interrupt
write to port
write to port
interrupt
interrupt
write to port
write to port
interrupt
write to port
Connecting interrupts
In TinyTimber.c
#define INTERRUPT(vector, stmt) \
ISR(vector) {stmt; schedule();}
Connecting interrupts
In TinyTimber.c
#define INTERRUPT(vector, stmt) \
ISR(vector) {stmt; schedule();}
Connecting interrupts
In TinyTimber.c
#define INTERRUPT(vector, stmt) \
ISR(vector) {stmt; schedule();}
Example
Example
Example
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
Complication
Pin1 change The reset routine cannot
return as it has not
Timer comp
really interrupted
anything!
.
.
.
In the active system view
this is interpreted as
Reset
compute until someone
turns off the power!
Reactive objects Encoding state layout Encoding methods Programming with TinyTimber
Reset
Complication
Pin1 change The reset routine cannot
return as it has not
Timer comp
really interrupted
anything!
.
.
.
In the active system view
this is interpreted as
Reset
compute until someone
turns off the power!
Reactive objects Encoding state layout Encoding methods Programming with TinyTimber
Reset
Complication
Pin1 change The reset routine cannot
return as it has not
Timer comp
really interrupted
anything!
.
.
.
In the active system view
this is interpreted as
Reset
compute until someone
turns off the power!
Reactive objects Encoding state layout Encoding methods Programming with TinyTimber
main()
main()
main()
main()
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
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
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
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
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
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
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
Sanity rules
Sanity rules
Sanity rules
Sanity rules