Keyboard Buffer Synchronozation

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
prajwal
Member
Member
Posts: 154
Joined: Sat Oct 23, 2004 11:00 pm
Contact:

Keyboard Buffer Synchronozation

Post by prajwal »

I'v a problem in my OS code.

scenario :

1) There is a Keyboard Queue Buffer and a variable called 'sync' - Global
2) The Keyboard handler will put the read character(after scancode conversion) into this Queue Buffer
3) There is a blocking function getChar() which reads a character from Queue Buffer.
4) This function polls the Queue Buffer in a tight loop until it reads a character and then returns


Problem: The key pressed will echo on screen but with unpredictable output.... means junk chars, previous character
got from previous IRQ9 will also get displayed..... as a whole display is not proper...



a) Queue Read and Write are correct(tested)
b) Display function is Correct(tested)


pseuodo Code:

getChar()
{
while(readFromQueue() == success) ;
}

readFromQueue()
{
sync = 1

..... QUEUE read operation ......

sync = 0
}

keyboard_handler()
{
... All keyboard port related stuff

writeToBuffer() ;

..... IRET etc.....
}

writeToBuffer()
{
if(sync == 1)
return ;

..... QUEUE write operation ......

return ;
}

But if I put a waiting loop in getChar() like:
getchar()
{
while(readFromQueue() == success)
{
for(i=0;i<999;i++) ;

}
}

Then everything works fine.....

NOTE: I know that if sync = 1 and IRQ9 comes... the keyed in character is lost... but thats fine....

Please give a soluation or an alternative approach....
User avatar
proxy
Member
Member
Posts: 108
Joined: Wed Jan 19, 2005 12:00 am
Contact:

Re: Keyboard Buffer Synchronozation

Post by proxy »

better to use semaphores instead of polling/spinlocks. what i do is first i made my getch() function atomic so that no keyboard interrupt may occur while i'm getting a character (in non-smp this means interrupts off for this thread). Then i have a global semaphore initialized to 0 so first p() operation will block (and implicitly yield the thread which is important since we wont get a timer interrupt). Now whenever my kyeboard interrupt occurs i do a v() on the global lock after i add the data to the queue. Now this makes the thread that asked for the char runnable again so when it gets scheduled it will resume with interrupts still off and pop the value out of the queue....everyone is happy :)

proxy
Da_Maestro
Member
Member
Posts: 144
Joined: Tue Oct 26, 2004 11:00 pm
Location: Australia

Re: Keyboard Buffer Synchronozation

Post by Da_Maestro »

writeToBuffer()
{
if(sync == 1)
return ;

..... QUEUE write operation ......



Shouldn't that be a while loop not an if statement.

No wait, that will junk the whole thing.

What should happen in your code is the write method should set sync to 1 and then zero when finished, and the read method should spin ONLY IF SYNC IS ONE
Two things are infinite: The universe and human stupidity. But I'm not quite sure about the universe.
--- Albert Einstein
Post Reply