Include <X11/Xlib.h> even if USE_VIDMODE is
[tangerine.git] / rom / exec / attemptsemaphore.c
blobd1f9ff3bae5d222b17f2ea2f81b119f65d49a423
1 /*
2 Copyright © 1995-2001, 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 return 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
52 AROS_LIBBASE_EXT_DECL(struct ExecBase *,SysBase)
54 struct Task *me = FindTask(NULL);
56 #if CHECK_INITSEM
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);
62 #endif
64 /* Arbitrate for semaphore nesting count and owner fields */
65 Forbid();
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
70 wrong.
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. */
87 else
89 sigSem->ss_QueueCount--;
92 Permit();
95 We own the semaphore if it is owned by me
97 return (sigSem->ss_Owner == me ? TRUE : FALSE);
99 AROS_LIBFUNC_EXIT
100 } /* AttemptSemaphore */