Page 1 of 1

Interrupt philosophy

Posted: Sat Apr 23, 2011 6:20 am
by RaffoPazzo
Hi. My OS' code is intended to be strongly typed and fully object oriented, even if in C and even for low-level stuff.

First of all, to better understand the point of view i'm going to propose, i would invite you to have a look at the following blog post
Not all properties are created equal, talking about Plato's "Existential vs Accidental Properties" applied to software.

For interrupt handling, I came out with two possible solutions, with different implications.

In the first one, they exist two entities the "Interrupt" and the "InterruptHandler". Modeling this scenario in C (simpler to post then UML) it is:

Code: Select all

/**
 * The id is an "existential property" of an Interrupt.
 */
struct Interrupt {
  uint32 id;
}

/**
 * The interrupt that an interrupt handler is design to handle is, instead, an
 * "accidental property" for the handler.
 */
struct InterruptHandler {
  void (*handle) (struct InterruptHandler* this, struct Interrupt* interrupt);
}
The above let it possible to invoke an InterruptHandler on an Interrupt it is not able to handle or, oppositely, handle several interrupts with the same handler.
This implies that an user wishing to register his/her own InterruptHandler has to test the Interrupt's id, both to avoid handling unknown interrupts and switching on different interrupts.

Code: Select all

/**
 * This handler is able to handle just one interrupt.
 */
void simpleHandler_handle(struct InterruptHandler* this, struct Interrupt* interrupt) {
  if ( interrupt != NULL ) {
    if ( interrupt->id == HANDLING_INTERRUPT ) {
      // TODO: Handle interrupt
    } else {
      // TODO: Unknown interrupt
    }
  } else {
    // TODO: Panic
  }
}

/**
 * This one, insted, is designed to handle several interrupts.
 */
void complexHandler_handle( struct InterruptHandler* this, struct Interrupt* interrupt ) {
  if ( interrupt != NULL ) {
    swtich( interrupt->id ) {
      // Handle several interrupts
    }
  } else {
    // TODO: Panic
  }
}
A second design, instead, does not define an explicit "Interrupt" entity but leave it implied in the "InterruptHandler" by making the "interrupt id" an existential property for the "InterruptHandler". This implies that the handler has not to test the interrupt id but it is impossible to write an InterruptHandler able to handle several interrupts.

Code: Select all

struct InterruptHandler {
  uint32 (*getId)();
  void (*handle)(struct InterruptHandler* this);
};

uint32 myHandler_id() {
  return HANDLING_INTERRUPT;
}

void myHandler_handle( struct InterruptHandler* this ) {
  /**
   * TODO: Just handle the interrupt, because this handler 
   * is guaranteed to be invoked on the right interrupt.
   * Nevertheless there is no way to handle several interrupts.
   */
}
What do you guys think about that?

Re: Interrupt philosophy

Posted: Sat Apr 23, 2011 8:17 am
by Brendan
Hi,
RaffoPazzo wrote:First of all, to better understand the point of view i'm going to propose, i would invite you to have a look at the following blog post
Not all properties are created equal, talking about Plato's "Existential vs Accidental Properties" applied to software.
Short summary: lots of techno-babble that tries to pretend that an "essential property" is something new (or long forgotten), even though any half-decent database administrator would call it a "key". For OOP languages the difference between "essential properties" and "accidental properties" is largely irrelevant, because objects are kept track of with references (or pointers) rather than keys.
RaffoPazzo wrote:For interrupt handling, I came out with two possible solutions, with different implications.
RaffoPazzo wrote:What do you guys think about that?
You seem to be trying very hard to over-complicate something very simple.


Cheers,

Brendan

Re: Interrupt philosophy

Posted: Sun Apr 24, 2011 8:08 am
by RaffoPazzo
Hi, Brendan. Thank you for your interest.
Brendan wrote:Short summary: lots of techno-babble that tries to pretend that an "essential property" is something new (or long forgotten), even though any half-decent database administrator would call it a "key". For OOP languages the difference between "essential properties" and "accidental properties" is largely irrelevant, because objects are kept track of with references (or pointers) rather than keys.
An existential property is deeply different from a key because an existential property "describe" what an entity is, while a key has the goal to identify this entity. To keep on your DBA example, it could exist the table Person in which, for sure, an entry can be identified by its field "Tax Id" or, alternatively, with a multiple key built up using Name, Surname, Birthdate or whatever the DBA think appropriately. However, none of these properties are existential ones because they are not a person. They are not you, no matter which information i can use to find you in my phone book.

For OOP languages (more properly we should talk about Object Oriented Design), the same apply (no matter of references or keys since they just identify an object) and the difference between existential and accidental properties does matter in good designs. For sure, in your experience, you have faced low quality code ( which i usually name hironically C-- :) ) where a class has an empty constructor and thousands of setter method. This kind of code, as you know, is hardly maintainable and hardly difficult to understand by someone new to the project. As a rule of thumb an existential property is one you wouldn't forget in your constructors and without setters, so read-only, since modify them will mean modify the modeled entity. An accidental property is, instead, generally left out constructors and editable by setters.

However, may be you're right. May be, i am firing mosquitos with artillery but it is sometime the price of strongly typed designs.

Looking forward your opinions.

Have a nice easter day,
Raffaele

Re: Interrupt philosophy

Posted: Sun Apr 24, 2011 2:52 pm
by OSwhatever
I guess my kernel is done in something called C--. Very often when I'm doing what I thing is a superb solution in C+ OO design I usually end up redesigning it to something simpler for various reasons.

System programming in C++ is a little bit different than usual applications in C++. Often you have to take a step down from the strict C++ OO design. I find myself often abusing the type safeness in C++ in system programming for example. To a strict C++ programmer I would probably be accused of blasphemy and be called various names. It works out for me and that is what I care about.

In the case of the interrupt handler I did something similar but removed it since it didn't fit in with my kernel. In the end you often tend to adjust to the API which is usually just ordinary C calls.

Killing my darlings also applies with C++.

Re: Interrupt philosophy

Posted: Sun Apr 24, 2011 9:15 pm
by NickJohnson
It seems like what the essential part of what you call an "essential property" is that there is a 1-to-1 (more precisely, a bijective) relationship between each object and its essential property, so that you can think of the object and the property as being equally representative of some unique concept or thing (like a person, an interrupt, etc.,) and inversely, that if the essential property changes, the object must be different, and vice versa. I don't see how this definition differs from a key or object pointer/reference. Correct me if that's not what you and/or the article meant.

If I am correct, then all the article is saying is that keys are nice, and that you should make sure keys are unique when you use them. :roll:

Re: Interrupt philosophy

Posted: Thu May 05, 2011 12:56 pm
by bluemoon
By the way, why would you need to handle multiple interrupts within same routine, and have a switch() that branch out to different handlers?
CPU already does the switch() for you. you register different handlers with different interrupt, and no need that extra switch().

As others suggested, interrupt handler needed to be fast, and even fast code is not fast, but try not doing is fast.

Re: Interrupt philosophy

Posted: Fri May 06, 2011 5:06 am
by Solar
OSwhatever wrote:System programming in C++ is a little bit different than usual applications in C++. Often you have to take a step down from the strict C++ OO design.
Not requiring a "strict" OO design (or OO design at all) is one of the things that makes C++ suited for system programming in the first place...

Re: Interrupt philosophy

Posted: Fri May 06, 2011 10:38 am
by Owen
If you're doing pure-OO programming in C++ to start with, I would argue that you're not using the majority of the features of C++ - and those are the features which make it so powerful.