Tom Ramcigam (magicmarmot) wrote,
Tom Ramcigam
magicmarmot

Sometimes I hate C code.

I currently have two circular buffers, one for each COM port.

I have a parent thread and an "interrupt" thread. Currently I just have the interrupt thread set on a timer so that it runs periodically. This bypasses the whole normal interrupt processing and moves everything to a polling routine.

Everything is fine, except that both threads are attempting to access the circular buffer. When I receive a character in the serial port, it's supposed to grab it from the chip's buffer and push it into the circular buffer. This then increments the head pointer.
It then tests to see if the head pointer has run back into the tail pointer, which would mean a buffer-full error is happening.

The problem is that the parent thread is reading the character out before the test, and bumping the tail pointer. This makes it look like the buffer is full when it's not, so I lose my characters in the receive buffer.

To solve this, I'd like to use a semaphore so I can mutex the circular buffer operations. The problem is that the parent thread doesn't know anything about semaphores.

AAAAAUUUGH!
Subscribe
  • Post a new comment

    Error

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 0 comments