Port the SB128 code to AROS.
[AROS.git] / rom / exec / attemptsemaphore.c
blob6f6f401f92dce55b2fcbdcd0200fc479fc64c395
1 /*
2 Copyright © 1995-2007, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Try to lock a sempahore.
6 Lang: english
7 */
8 #include "exec_intern.h"
9 #include <exec/semaphores.h>
10 #include <proto/exec.h>
12 #define CHECK_INITSEM 1
14 /*****************************************************************************
16 NAME */
18 AROS_LH1(ULONG, AttemptSemaphore,
20 /* SYNOPSIS */
21 AROS_LHA(struct SignalSemaphore *, sigSem, A0),
23 /* LOCATION */
24 struct ExecBase *, SysBase, 96, Exec)
26 /* FUNCTION
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
29 returns false instead.
31 INPUTS
32 sigSem - Pointer so semaphore structure.
34 RESULT
35 TRUE if the semaphore could be obtained, FALSE otherwise.
37 NOTES
38 The lock must be freed with ReleaseSemaphore().
40 EXAMPLE
42 BUGS
44 SEE ALSO
45 ReleaseSemaphore()
47 INTERNALS
49 *****************************************************************************/
51 AROS_LIBFUNC_INIT
53 struct Task *me = FindTask(NULL);
55 #if CHECK_INITSEM
56 if (sigSem->ss_Link.ln_Type != NT_SIGNALSEM)
58 kprintf("\n\nAttemptSemaphore called on an uninitialized semaphore!!! "
59 "sem = %x task = %x (%s)\n\n", sigSem, me, me->tc_Node.ln_Name);
61 #endif
63 /* Arbitrate for semaphore nesting count and owner fields */
64 Forbid();
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
69 wrong.
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. */
86 else
88 sigSem->ss_QueueCount--;
91 Permit();
94 We own the semaphore if it is owned by me
96 return (sigSem->ss_Owner == me ? TRUE : FALSE);
98 AROS_LIBFUNC_EXIT
99 } /* AttemptSemaphore */