2 Copyright © 1995-2007, The AROS Development Team. All rights reserved.
5 Desc: Lock a semaphore.
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 /*****************************************************************************/
24 #include <proto/exec.h>
26 AROS_LH1(void, ObtainSemaphore
,
29 AROS_LHA(struct SignalSemaphore
*, sigSem
, A0
),
32 struct ExecBase
*, SysBase
, 94, Exec
)
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
40 sigSem - Pointer to semaphore structure
45 This function preserves all registers.
56 *****************************************************************************/
64 /* Get pointer to current task */
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
);
76 /* Arbitrate for the semaphore structure */
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.
108 We need a node to mark our semaphore request. Lets use some
111 struct SemaphoreRequest sr
;
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.
137 } /* ObtainSemaphore */