Shop OBEX P1 Docs P2 Docs Learn Events
mutually exclusive memory — Parallax Forums

mutually exclusive memory

MuncherMuncher Posts: 38
edited 2006-06-23 05:28 in Propeller 1
if i wanted to write a program that would have one cog act as a keyboard entry buffer, and i wanted to relay the info from the keyboard to two other cogs, how would i go abotu doing it? (my uestion is on the relaying part)

Thanks, Muncher

Comments

  • cgraceycgracey Posts: 14,133
    edited 2006-06-18 05:05
    Make a circular buffer for bytes received along with a 'head' variable. Every time a byte is received, write it into the buffer at the head position and increment the head. The other two COGs can read this at their leisure by comparing their own 'tails' against the head. When head <> tail, get the byte from the circular buffer pointed to by the tail, and increment the tail. Of course, the head and tails must be all initialized to 0's.

    Many sharing needs can be worked out by simple protocols like this. There· are eight semaphore bits available on the chip for sharing more complicated structures or resources which won't lend themselves to simpler solutions. Personally, I have never had to resort to using these.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    Chip Gracey
    Parallax, Inc.
  • Cliff L. BiffleCliff L. Biffle Posts: 206
    edited 2006-06-18 23:39
    This sort of arrangement benefits from the COG's atomic memory access. Since only one COG is accessing shared RAM at a time, you can be assured that any reads or writes of up to 32 bits are atomic. Which opens up all sorts of lockless data structure possibilities. (You can steal a lot of ideas from the new Java concurrent library in Java5; Java makes the same atomicity guarantee, so the algorithms transfer. I recommend the book Java Concurrency in Practice, but take my recommendation with a grain of salt.)

    Chip, judging from the instruction set documentation, aren't those bits better referred to as locks than semaphores? There's no associated count, and a COG can't await a particular value of the semaphore -- only the availability of the lock bit itself.

    One could certainly implement traditional Dijkstra-style semaphores using the locks, but I think it's a bit of a misnomer.
  • cgraceycgracey Posts: 14,133
    edited 2006-06-19 00:13
    Yeah, you're right! I should have called them LOCKs,·like I did in the documentation. Duh? I must have gotten excited about that bigger word.
    Cliff L. Biffle said...

    Chip, judging from the instruction set documentation, aren't those bits better referred to as locks than semaphores? There's no associated count, and a COG can't await a particular value of the semaphore -- only the availability of the lock bit itself.
    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    Chip Gracey
    Parallax, Inc.
  • Cliff L. BiffleCliff L. Biffle Posts: 206
    edited 2006-06-19 01:57
    Chip Gracey said...
    Yeah, you're right! I should have called them LOCKs, like I did in the documentation. Duh?

    The Propeller block diagram also uses the word semaphore, so I gather both terms are in use here.
  • acantostegaacantostega Posts: 105
    edited 2006-06-23 05:28
    I like the term Locks better -- at least to me it suggests more clearly what they're about.
Sign In or Register to comment.