Welcome, Guest. Please login or register.

Author Topic: No need for Semaphore when data size is <= 32 bit ?  (Read 1922 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2869
    • Show only replies by Jose
No need for Semaphore when data size is <= 32 bit ?
« on: May 01, 2006, 01:25:49 AM »
Would I be correct in assuming that all data that is smaller than 32 bits doesn't actually need a Semaphore when shared between tasks and/or processes cause it's allways valid (the CPU reads/writes to it at once so there's no way a task/process will be interrupted in the middle). ?
\\"We made Amiga, they {bleep}ed it up\\"
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show only replies by Piru
    • http://www.iki.fi/sintonen/
Re: No need for Semaphore when data size is <= 32 bit ?
« Reply #1 on: May 01, 2006, 01:40:26 AM »
You wouldn't.

Hint: How to read the value and modify it in arbitrary way and have that in single, atomic operation? Just reading or writing the value doesn't give anything useful, or at least I can't think of any use for that.

Even if you would only read or modify it, even the modify might not be atomic operation (it isn't with PPC).

If the code is m68k only, and if the code only does atomic read or update operation, then it would work. But like I said, such conditions aren't met often.
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16867
  • Country: gb
  • Thanked: 4 times
    • Show only replies by Karlos
Re: No need for Semaphore when data size is <= 32 bit ?
« Reply #2 on: May 01, 2006, 10:17:49 AM »
Quote

Jose wrote:
Would I be correct in assuming that all data that is smaller than 32 bits doesn't actually need a Semaphore when shared between tasks and/or processes cause it's allways valid (the CPU reads/writes to it at once so there's no way a task/process will be interrupted in the middle). ?


How can you guarentee the read write operation will not be interrupted? It could be preempted to switch out before it gets the chance to write the modified data back. Your operation would need to be atomic, as Piru says. I am not sure off the top of my head but IIRC, only instructions like TAS etc have an atomic read/write cycle.
int p; // A
 

Offline Doppie1200

  • Sr. Member
  • ****
  • Join Date: May 2004
  • Posts: 497
    • Show only replies by Doppie1200
Re: No need for Semaphore when data size is <= 32 bit ?
« Reply #3 on: May 01, 2006, 11:31:27 AM »
Thread safety is not about data size.
It is about to guarantee resources are not used by another process.




Regards,
Erno

(O\\\\_|_/O) <- this is supposed to look like the front of my beetle
(entire front not possible in signature)
 

Offline Steady

Re: No need for Semaphore when data size is <= 32 bit ?
« Reply #4 on: May 01, 2006, 01:12:44 PM »
And, of course, the TAS instruction is a no-no on Amiga anyway as it can screw up chipset timings.

As everyone else is saying, if something is public and could potentially be changed by another process, protect it with a semaphore.

It's not the size that matters.
 

Offline itix

  • Hero Member
  • *****
  • Join Date: Oct 2002
  • Posts: 2380
    • Show only replies by itix
Re: No need for Semaphore when data size is <= 32 bit ?
« Reply #5 on: May 01, 2006, 02:39:34 PM »
@Karlos

Certain operations are atomic. For example if you wish to set flags
atomically you can have:

or.l #1, 4(a4)

This operation is never interrupted and there is nobody else who would modify data in between. Of course this works in assembler only because C compiler might not work this way.
My Amigas: A500, Mac Mini and PowerBook
 

Offline itix

  • Hero Member
  • *****
  • Join Date: Oct 2002
  • Posts: 2380
    • Show only replies by itix
Re: No need for Semaphore when data size is <= 32 bit ?
« Reply #6 on: May 01, 2006, 02:43:56 PM »
That is, normally in C you would use something like

Forbid()
globalflags |= 1;
Permit()

when in 68k assembler coder can use single or.l instruction without Forbid()/Permit() arbitration.

This is sometimes used when dealing with Intuition windows.
My Amigas: A500, Mac Mini and PowerBook
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show only replies by Piru
    • http://www.iki.fi/sintonen/
Re: No need for Semaphore when data size is <= 32 bit ?
« Reply #7 on: May 01, 2006, 02:51:34 PM »
It should also be pointed out that Forbid/Permit arbitration should be used when there is no long, blocking operation done using the locked object. If there is some such operation (such as scanning a list for example), semaphore should be used.
 

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2869
    • Show only replies by Jose
Re: No need for Semaphore when data size is <= 32 bit ?
« Reply #8 on: May 01, 2006, 05:25:14 PM »
I actually forgot to mention that the thread/process involved only does a very tiny instruction with the variable, that most likely corresponds to one instruction in assembler.

@Piru
"Just reading or writing the value doesn't give anything useful, or at least I can't think of any use for that."

Raising a counter;) But maybe this wouln't be atomic in PPC ?
\\"We made Amiga, they {bleep}ed it up\\"
 

Offline SamuraiCrow

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2280
  • Country: us
  • Gender: Male
    • Show only replies by SamuraiCrow
Re: No need for Semaphore when data size is <= 32 bit ?
« Reply #9 on: May 01, 2006, 05:39:54 PM »
No.  On a PowerPC you'd have to load into a register, add 1, and store the register back to memory.  3 opcodes=Non-atomic.
 

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2869
    • Show only replies by Jose
Re: No need for Semaphore when data size is <= 32 bit ?
« Reply #10 on: May 01, 2006, 06:05:42 PM »
Pah, 68k rules:)
Ok I'll use a damn Semaphore for the thing... :-x
\\"We made Amiga, they {bleep}ed it up\\"
 

Offline itix

  • Hero Member
  • *****
  • Join Date: Oct 2002
  • Posts: 2380
    • Show only replies by itix
Re: No need for Semaphore when data size is <= 32 bit ?
« Reply #11 on: May 01, 2006, 09:40:27 PM »
@Jose & SamuraiCrow

It is possible implement atomically incrementing counter in PPC asm without using Forbid()/Permit() or Semaphore arbitration. But better use other more portable methods than asm.
My Amigas: A500, Mac Mini and PowerBook
 

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2869
    • Show only replies by Jose
Re: No need for Semaphore when data size is <= 32 bit ?
« Reply #12 on: May 05, 2006, 01:29:48 AM »
I'm now really getting into a part of my code that uses this. I had some more thought into it. There are 2 reasons I can remember to use a semaphore: protect access from another task and, more serious (cause it can lead to a crash or a program malfunction), data integrity. Even if raising a counter is not atomic in PPC, I'm sure the write cannot be interrupted (you don't get a 32 bit counter's read with half the current and half the previous value), so data integrity is not a problem here.
So to avoid using data that is not uptodate I decided to not use a semaphore, but a software interrupt in the task that's using it. The timer.device is about the only one that can be used with an interrupt  from what I've read on the RKM's libraries, and that's the one I'm receiving messages from, after wich some actions are take and counter is raised, so it's all good.

Any caution/problems/advice with this solution, or it's just brilliant ? 8-)
\\"We made Amiga, they {bleep}ed it up\\"