r4684@vps: verhaegs | 2007-05-04 21:04:10 -0400
[AROS.git] / rom / exec / obtainsemaphore.c
blob7f92c7b86723479543b0e2768f03589290a9a5bb
1 /*
2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Lock a semaphore.
6 Lang: english
7 */
8 #include "exec_intern.h"
9 #include "semaphores.h"
10 #include <exec/semaphores.h>
11 #include <aros/atomic.h>
13 #include <proto/exec.h>
15 #define CHECK_INITSEM 1
17 /*****************************************************************************/
18 #undef Exec
19 #ifdef UseExecstubs
20 # define Exec _Exec
21 #endif
23 /* NAME */
24 #include <proto/exec.h>
26 AROS_LH1(void, ObtainSemaphore,
28 /* SYNOPSIS */
29 AROS_LHA(struct SignalSemaphore *, sigSem, A0),
31 /* LOCATION */
32 struct ExecBase *, SysBase, 94, Exec)
34 /* FUNCTION
35 Obtain an exclusive lock on a semaphore. If the semaphore is already
36 in use by another task this function will wait until the semaphore
37 becomes free.
39 INPUTS
40 sigSem - Pointer to semaphore structure
42 RESULT
44 NOTES
45 This function preserves all registers.
47 EXAMPLE
49 BUGS
51 SEE ALSO
52 ReleaseSemaphore()
54 INTERNALS
56 *****************************************************************************/
58 #undef Exec
60 AROS_LIBFUNC_INIT
61 AROS_LIBBASE_EXT_DECL(struct ExecBase *,SysBase)
63 struct Task *me;
65 /* Get pointer to current task */
66 me=SysBase->ThisTask;
68 #if CHECK_INITSEM
69 if (sigSem->ss_Link.ln_Type != NT_SIGNALSEM)
71 kprintf("\n\nObtainSemaphore called on a not intialized semaphore!!! "
72 "sem = %x task = %x (%s)\n\n", sigSem, me, me->tc_Node.ln_Name);
73 Alert(AN_SemCorrupt);
75 #endif
77 /* Arbitrate for the semaphore structure */
78 Forbid();
81 ss_QueueCount == -1 indicates that the semaphore is
82 free, so we increment this straight away. If it then
83 equals 0, then we are the first to allocate this semaphore.
85 Note: This will need protection for SMP machines.
87 sigSem->ss_QueueCount++;
88 if( sigSem->ss_QueueCount == 0 )
90 /* We now own the semaphore. This is quick. */
91 sigSem->ss_Owner = me;
92 sigSem->ss_NestCount++;
95 /* The semaphore was in use, but was it by us? */
96 else if( sigSem->ss_Owner == me )
98 /* Yes, just increase the nesting count */
99 sigSem->ss_NestCount++;
103 Else, some other task must own it. We have
104 to set a waiting request here.
106 else
109 We need a node to mark our semaphore request. Lets use some
110 stack memory.
112 struct SemaphoreRequest sr;
113 sr.sr_Waiter = me;
116 Have to clear the signal to make sure that we don't
117 return immediately. We then add the SemReq to the
118 waiters list of the semaphore. We were the last to
119 request, so we must be the last to get the semaphore.
122 #warning This must be atomic!
123 AROS_ATOMIC_AND(me->tc_SigRecvd, ~SIGF_SINGLE);
125 AddTail((struct List *)&sigSem->ss_WaitQueue, (struct Node *)&sr);
128 Finally, we simply wait, ReleaseSemaphore() will fill in
129 who owns the semaphore.
131 Wait(SIGF_SINGLE);
134 /* All Done! */
135 Permit();
137 AROS_LIBFUNC_EXIT
138 } /* ObtainSemaphore */