Commit of the code which was suggested by Georg as a fix for:
[cake.git] / rom / exec / obtainsemaphore.c
blobe15b9d266b4d3e59f25fd7c4232519f543a4e149
1 /*
2 Copyright © 1995-2007, 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
62 struct Task *me;
64 /* Get pointer to current task */
65 me=SysBase->ThisTask;
67 #if CHECK_INITSEM
68 if (sigSem->ss_Link.ln_Type != NT_SIGNALSEM)
70 kprintf("\n\nObtainSemaphore called on a not intialized semaphore!!! "
71 "sem = %x task = %x (%s)\n\n", sigSem, me, me->tc_Node.ln_Name);
72 Alert(AN_SemCorrupt);
74 #endif
76 /* Arbitrate for the semaphore structure */
77 Forbid();
80 ss_QueueCount == -1 indicates that the semaphore is
81 free, so we increment this straight away. If it then
82 equals 0, then we are the first to allocate this semaphore.
84 Note: This will need protection for SMP machines.
86 sigSem->ss_QueueCount++;
87 if( sigSem->ss_QueueCount == 0 )
89 /* We now own the semaphore. This is quick. */
90 sigSem->ss_Owner = me;
91 sigSem->ss_NestCount++;
94 /* The semaphore was in use, but was it by us? */
95 else if( sigSem->ss_Owner == me )
97 /* Yes, just increase the nesting count */
98 sigSem->ss_NestCount++;
102 Else, some other task must own it. We have
103 to set a waiting request here.
105 else
108 We need a node to mark our semaphore request. Lets use some
109 stack memory.
111 struct SemaphoreRequest sr;
112 sr.sr_Waiter = me;
115 Have to clear the signal to make sure that we don't
116 return immediately. We then add the SemReq to the
117 waiters list of the semaphore. We were the last to
118 request, so we must be the last to get the semaphore.
121 #warning This must be atomic!
122 AROS_ATOMIC_AND(me->tc_SigRecvd, ~SIGF_SINGLE);
124 AddTail((struct List *)&sigSem->ss_WaitQueue, (struct Node *)&sr);
127 Finally, we simply wait, ReleaseSemaphore() will fill in
128 who owns the semaphore.
130 Wait(SIGF_SINGLE);
133 /* All Done! */
134 Permit();
136 AROS_LIBFUNC_EXIT
137 } /* ObtainSemaphore */