2 Copyright © 1995-2001, 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 *****************************************************************************/
52 AROS_LIBBASE_EXT_DECL(struct ExecBase
*,SysBase
)
54 struct Task
*me
= FindTask(NULL
);
57 if (sigSem
->ss_Link
.ln_Type
!= NT_SIGNALSEM
)
59 kprintf("\n\nAttemptSemaphore called on an unintialized semaphore!!! "
60 "sem = %x task = %x (%s)\n\n", sigSem
, me
, me
->tc_Node
.ln_Name
);
64 /* Arbitrate for semaphore nesting count and owner fields */
68 We are going to lock or fail, so we increment the
69 ss_QueueCount anyway. We shall fix it up later if it was
72 sigSem
->ss_QueueCount
++;
74 /* If it is now equal to zero, then we have got it */
75 if( sigSem
->ss_QueueCount
== 0 )
77 sigSem
->ss_Owner
= me
;
78 sigSem
->ss_NestCount
++;
80 /* It was already owned by me, so lets just inc the nest count */
81 else if( sigSem
->ss_Owner
== me
)
83 sigSem
->ss_NestCount
++;
86 /* Owned by somebody else, we fix up the owner count. */
89 sigSem
->ss_QueueCount
--;
95 We own the semaphore if it is owned by me
97 return (sigSem
->ss_Owner
== me
? TRUE
: FALSE
);
100 } /* AttemptSemaphore */