Semaphore question

5 posts / 0 new
Last post
trixie
trixie's picture
Offline
Last seen: 6 months 3 weeks ago
Joined: 2011-02-03 13:58
Semaphore question

In one of my programs I'm accessing two system structures, which I thought I'd better protect using a semaphore. Do I need to InitSemaphore() two different ones, or can one semaphore be used in two different situations?

gazelle
gazelle's picture
Offline
Last seen: 1 year 9 months ago
Joined: 2011-04-13 12:52
Re: Semaphore question

As it is your own semaphore, you can use it however you like.

AFAIK, If you want to protect your access to the system structures against any other task, a semaphore won't help you. If the system structure doesn't provide any sort of protection by itself you'll need a Forbid()/Permit().

trixie
trixie's picture
Offline
Last seen: 6 months 3 weeks ago
Joined: 2011-02-03 13:58
Re: Semaphore question

@gazelle

I see, so a semaphore would only be good for protecting your own data?

AmigaOne X5000-020 / 2GB RAM / Sapphire Pulse Radeon RX 560 / AmigaOS 4.1 Final Edition Update 2

thomas
thomas's picture
Offline
Last seen: 3 months 1 week ago
Joined: 2011-05-16 14:23
Re: Semaphore question

A semaphore does not protect anything. A semaphore only affects ObtainSemaphore and ReleaseSemaphore. Once you've called ObtainSemaphore, all other calls to ObtainSemaphore are blocked until you call ReleaseSemaphore. That's all it does, nothing else.

Protection only arises implicitly if you combine the semaphore with a piece of data and everybody who wants to access the data has the discipline to call ObtainSemaphore before and ReleaseSemaphore after the access. Really everybody. If only one program accesses the data without obtaining the semaphore, protection is lost.

The best way to protect your data is to keep the structure private and provide some read_data() and write_data() functions which handle the semaphore internally.

Yes, only if a semaphore already exists for the data, it provides some protection. You cannot make your own semaphore to protect foreign data. Only if all programs know about the semphore and use it, it provides protection.

trixie
trixie's picture
Offline
Last seen: 6 months 3 weeks ago
Joined: 2011-02-03 13:58
Re: Semaphore question

Thank you for a very in-depth explanation, Thomas!

AmigaOne X5000-020 / 2GB RAM / Sapphire Pulse Radeon RX 560 / AmigaOS 4.1 Final Edition Update 2

Log in or register to post comments