C++ in the kernel

Bakul Shah bakul at bitblocks.com
Tue Oct 30 11:11:25 PDT 2007


> * Bakul Shah <bakul at bitblocks.com> [071030 09:36] wrote:
> > 
> > The structured macro paper referenced on the K wiki page also
> > seems rather interesting.  A powerful macro facility needs to
> > be well integrated with the language (much like Lisp or
> > Scheme's macros) so that you can write for instance
> > 
> >     critical_section(lock) {
> > 	...
> > 	bar:
> > 	...
> > 	if (cond1) break;
> > 	...
> > 	if (cond2) goto foo;
> > 	...
> > 	if (cond3) goto bar;
> > 	...
> > 	if (cond4) return; // from enclosing function
> > 	...
> >     }
> >     ...
> >     foo:
> 
> 
> do you mean like C++:
> 
> do {
> 	critical_object critical_instance();
> 
> 
> 
> 
> }

No idea but I can not see how that will do what I had in mind. A purely
lexical translation of the snippet I gave above would be something like:

  /* struct mtx lock is a non local object */
  {
	mutex_lock(&lock);
	...
	bar:
	...
	if (cond1) goto _break11;
	...
	if (cond2) goto _foo12;
	...
	if (cond3) goto bar;
	...
	if (cond4) goto _return13; // from enclosing function
	...
    _break11:
	mutex_unlock(&lock);
	break;
    _foo12:
	mutex_unlock(&lock);
	goto foo;
    _return13:
	mutex_unlock(&lock);
	return;
    }
    ...
    foo:

This as an example of what *I would want* a macro facility to
do!  Implementing such a facility would not be very hard
(after all it is just tree surgery) but coming up with a
macro language that is non-ugly is very hard and I do realize
that.  One level up, this is *one way* to achieve writing
error free mutex code.  Another is through some sort of
static analysis tool.  A third is through code walkthroughs.
Each has different costs.

> Just wondering how much of this we want to roll on on our own.

So am I! But it is worth pursuing some of these threads at
the discussion level.

The more important point is the underlying "need" which we
can all agree on (I hope!), which is to be able to write such
code in an error free way.  If we have such a list we can
figure out which ones to focus on and how to satisfy them.


More information about the freebsd-arch mailing list