2 Copyright © 1995-2007, The AROS Development Team. All rights reserved.
5 Desc: Try to lock a sempahore.
8 #include "exec_intern.h"
9 #include <exec/semaphores.h>
10 #include <proto/exec.h>
12 #define CHECK_INITSEM 1
14 /*****************************************************************************
18 AROS_LH1(ULONG
, AttemptSemaphore
,
21 AROS_LHA(struct SignalSemaphore
*, sigSem
, A0
),
24 struct ExecBase
*, SysBase
, 96, Exec
)
27 Tries to get an exclusive lock on a signal semaphore. If the semaphore
28 is already in use by another task this function does not wait but
32 sigSem - Pointer so semaphore structure.
35 TRUE if the semaphore could be obtained, FALSE otherwise.
38 The lock must be freed with ReleaseSemaphore().
49 *****************************************************************************/
53 struct Task
*me
= FindTask(NULL
);
56 if (sigSem
->ss_Link
.ln_Type
!= NT_SIGNALSEM
)
58 kprintf("\n\nAttemptSemaphore called on an unintialized semaphore!!! "
59 "sem = %x task = %x (%s)\n\n", sigSem
, me
, me
->tc_Node
.ln_Name
);
63 /* Arbitrate for semaphore nesting count and owner fields */
67 We are going to lock or fail, so we increment the
68 ss_QueueCount anyway. We shall fix it up later if it was
71 sigSem
->ss_QueueCount
++;
73 /* If it is now equal to zero, then we have got it */
74 if( sigSem
->ss_QueueCount
== 0 )
76 sigSem
->ss_Owner
= me
;
77 sigSem
->ss_NestCount
++;
79 /* It was already owned by me, so lets just inc the nest count */
80 else if( sigSem
->ss_Owner
== me
)
82 sigSem
->ss_NestCount
++;
85 /* Owned by somebody else, we fix up the owner count. */
88 sigSem
->ss_QueueCount
--;
94 We own the semaphore if it is owned by me
96 return (sigSem
->ss_Owner
== me
? TRUE
: FALSE
);
99 } /* AttemptSemaphore */