Tom Ramcigam (magicmarmot) wrote,
Tom Ramcigam

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.


  • (no subject)

    It finally happened. It had to, really. I was in the bottom two cut from LJ-Idol this week. I made it to the top 50, from some rather larger…

  • Mayville

    "Too many bats in the belfry, eh?" The question came from a small man in the scrubs-and-robe garb of an inmate. He looked a little like a garden…

  • LJ-Idol

    Another batch of entries. Consistently amazed at how good the writing is. Voting is open for…

  • Post a new comment


    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.