Shop OBEX P1 Docs P2 Docs Learn Events
FullDuplexSerial.spin and locks — Parallax Forums

FullDuplexSerial.spin and locks

Bob BellevilleBob Belleville Posts: 5
edited 2007-03-07 21:07 in Propeller 1
FullDuplexSerial seems to work just fine.

It uses no locks even though the FiFos are multiple bytes.

Is there some aspect of the code I am missing the makes
the need to obtain a lock before reading or writing a FiFo
unnecessary?

many thanks -- Bob

Comments

  • Paul BakerPaul Baker Posts: 6,351
    edited 2007-03-07 19:17
    Locks are only needed if there are multiple producers or multiple consumers. In a standard single producer/single consumer, there is no conflict because each modifies the opposite end of the fifo and there is never a situation where data loss or multiplication can occur.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • Mike GreenMike Green Posts: 23,101
    edited 2007-03-07 20:08
    There is a special case of multi-processor access to shared variables known as the single producer / single consumer that doesn't need any locking.· Basically, only one processor can change the variable, but a second processor can read it.· This works nicely with a buffer in that there's an input pointer that's modified only by the processor writing into the buffer, but accessed by the processor reading the buffer (to see if there's anything there).· Similarly, there's an output pointer that's modified only by the processor reading the buffer, but accessed by the processor writing into the buffer (to see if there's room left).· If the writer thinks that the buffer is full even though the reader is in the process of removing a character, there's no problem since the writer will wait.· Similarly, if the reader thinks that the buffer is empty even if the writer is putting a new character into the buffer, there's no problem since the reader will wait.
    ·
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-03-07 21:07
    Another case where you would need to use locks or flags is if there is an array or group of data that needs to be updated before it can be read. In this case you don’t want one processor reading bytes from the array before they’re all written. Take care.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
Sign In or Register to comment.