Page 2 of 2
Re: Calling an interrupt from the handling of another.
Posted: Sat Jul 24, 2010 2:06 am
by egos
Ring buffer needs only for static implementation FIFO queue. It is not ring queue. And if it has the size defined as power of 2 it can be wraped by "and size-1" operation. Usually in my kernel event queue has the size 16 K and so it can contain queuesize/packagesize-1 event packages. When posting event and queue is full kernel cleans the queue by setting one pointer as other one and posts RESET event.
Re: Calling an interrupt from the handling of another.
Posted: Sat Jul 24, 2010 2:34 am
by eXeCuTeR
Something really bazaar just happened..
This code works:
Code: Select all
char getch()
{
while(1)
{
if(flag) break;
printf("%d", flag); // printing is actually essential!
}
return read_from_pipe();
}
but all of these, DON'T!
Code: Select all
char getch()
{
while(!flag)
printf("%d", flag); // doesn't even show a number!
return read_from_pipe();
}
Code: Select all
char getch()
{
while(!flag);
return read_from_pipe();
}
(p.s: don't worry about indexes, read_from_pipe() and write_to_pipe() handle them already. and also, excuse me from the lousy names, will be changed in future)
(write_to_pipe() is called from the IRQ and sets the flag to 1, read_from_pipe() from the process sets the flag to 1 if there's absolutely no data to be read which I also check with the indexes)
(also, only write_to_pipe() is external to keyboard.c, what i have shown you now is kbdapi.c)
any explanation to this?
Re: Calling an interrupt from the handling of another.
Posted: Sat Jul 24, 2010 2:37 am
by gerryg400
You need to declare flag as volatile.
Re: Calling an interrupt from the handling of another.
Posted: Sat Jul 24, 2010 2:43 am
by eXeCuTeR
gerryg400 wrote:You need to declare flag as volatile.
Oh that's great! Thanks, all good now!
But I don't see why this actually matters? Because if it was kept into a register, and the IRQ actually change 5 registers (which i remember 3 of them ATM) and if one was it, the results would not be very nice. Just not sure about it, but what the heck.
Re: Calling an interrupt from the handling of another.
Posted: Sat Jul 24, 2010 7:30 am
by gerryg400
But I don't see why this actually matters?
If a variable can be modified outside of the normal flow of the program (e.g. by interrupt service routine or by hardware) you need to declare it as volatile.
In this case
gcc might assume that 'flag' will always be zero because it cannot see any code that changes it. It may optimise away the test and replace it with
If you declare flag as volatile it won't do that.