Login
Username:

Password:

Remember me



Lost Password?

Register now!

Sections

Who's Online
50 user(s) are online (34 user(s) are browsing Forums)

Members: 1
Guests: 49

Spectre660, more...

Headlines






MutexAttemptWithSignal
Just popping in
Joined:
2006/11/30 11:59
From Lund, Sweden
Posts: 169
I must be missing something very basic here:

APTR mtx AllocSysObjectTags(ASOT_MUTEXASOMUTEX_RecursiveFALSETAG_END);
MutexObtain(mtx);
uint32 sigmask MutexAttemptWithSignal(mtxSIGF_SINGLE);


Like I expect, the MutexAttemptWithSignal call hangs. If I use Scout to signal the task, it continues. That's also what I would expect. But sigmask is 0. If I understand the documentation correctly it should have been SIGF_SINGLE, shouldn't it?

   Report Go to top

Re: MutexAttemptWithSignal
Just can't stay away
Joined:
2006/11/30 11:30
From Finland
Posts: 1804
@sTix

The SIGF_SINGLE signal is used internally by the semaphore/mutex functions to signal when a semaphore/mutex has become free so it can't be used in the way that you are using it.

   Report Go to top

Re: MutexAttemptWithSignal
Just popping in
Joined:
2006/11/30 11:59
From Lund, Sweden
Posts: 169
@salass00

Ok, good to know. So in other words I'm tricking myself to believe that the mutex is obtained by signaling manually?


Edited by sTix on 2021/8/23 8:32:18
   Report Go to top

Re: MutexAttemptWithSignal
Just popping in
Joined:
2006/11/30 11:59
From Lund, Sweden
Posts: 169
@salass00

Thanks for helping out. The snippet below should be safe though? It behaves in the exact same way.

BYTE sigbit AllocSignal(-1);
APTR mtx = AllocSysObjectTags(ASOT_MUTEX, ASOMUTEX_Recursive, FALSE, TAG_END); 
MutexObtain(mtx); 
uint32 sigmask = MutexAttemptWithSignal(mtx, 1 << sigbit);



   Report Go to top

Re: MutexAttemptWithSignal
Just can't stay away
Joined:
2006/11/30 11:30
From Finland
Posts: 1804
@sTix

To be honest I'm not really sure what the purpose of non-recursive mutices is (as they do not save memory or CPU time AFAIK) and what exactly the correct behavior should be for your code.

What happens however for your code is that after returning from Wait(SIGF_SINGLE|sigmask) MutexAttemptWithSignal() checks if it is now the owner of the mutex in question and if it is it always returns zero indicating that the mutex was successfully obtained.

   Report Go to top

Re: MutexAttemptWithSignal
Just popping in
Joined:
2006/11/30 11:59
From Lund, Sweden
Posts: 169
@salass00

I guess it's possible to shave off a few bytes and cpu cycles by not caring about ownership. Not being owned by a thread makes it possible to throw them around, one thread locking, another one unlocking and so on. I don't know if this is applicable on AmigaOS though. I'm just doing some random experiments to learn the Amiga way of doing things. The only use case I care about is testing AttemptWithSignal ;) Didn't turn out the way I expected, but I learnt something. Thanks!

   Report Go to top

Re: MutexAttemptWithSignal
Just popping in
Joined:
2007/6/6 9:12
Posts: 12
@sTix
Your code _should_ work as you expect and return the signal if that is the reason for MutextAttemptWithSignal() returning, or zero if the mutex is obtained.

How do you trigger with the correct signal? It may be you only posted example code and left out the debug output telling you which signal you have to trigger from the outside, but as it is shown here you can't really see the correct signal to send :)

Disclaimer: I've never used the new mutex function, so I don't know more than what the autodocs and examples indicate.

   Report Go to top

Re: MutexAttemptWithSignal
Just popping in
Joined:
2006/11/30 11:59
From Lund, Sweden
Posts: 169
@graff

I don't use any code to signal the task, the code is nothing but the lines you see above. I use the Scout utility which seems to peek into the task struct to see what signals the task is waiting on. And just like salass00 wrote above, it's waiting for SIGF_SINGLE|sigmask, so I signal using sigmask only and it unblocks as expected, but then returns 0. I do get a valid sigbit from AllocSignal also, not -1.

   Report Go to top

Re: MutexAttemptWithSignal
Home away from home
Joined:
2006/12/4 23:15
Posts: 2296
@sTix

If you attempt that mutex on a different process, you will get the result you expect.

As Frederik already said It's returning 0 because the process your attempting the mutex on already owns it.

Do you have real world use case where you need anon recursive mutex to be used in this way?

_________________
Blender For OS4.x : Blues : Walker Broad
   Report Go to top

Re: MutexAttemptWithSignal
Just popping in
Joined:
2006/11/30 11:59
From Lund, Sweden
Posts: 169
@broadblues

No, I don't have any real use case. I was just surprised by this behaviour while experimenting with timers. I thought that I could attempt to lock, hang until a timeout signal arrived and then verify that the timeout occured. This only makes sense when having multiple threads of course, for testing purposes I thought that I could skip the threading part, because I'm lazy. I didn't expect the mutex to keep track of ownership since it non-recursive. I don't understand why it does that, it's unneccesary and leads to unintuitive results IMO. It's just my opinion though, in ISO C11 the outcome of the same operation is undefined, so I guess the behaviour can be considered correct.

   Report Go to top





[Advanced Search]



Powered by XOOPS 2.0 © 2001-2016 The XOOPS Project