2 Copyright © 1995-2011, 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 there's no ThisTask, the function is called from within memory
69 * allocator in exec's pre-init code. We are already single-threaded,
76 if (sigSem
->ss_Link
.ln_Type
!= NT_SIGNALSEM
)
78 kprintf("\n\nObtainSemaphore called on a not intialized semaphore!!! "
79 "sem = %x task = %x (%s)\n\n", sigSem
, me
, me
->tc_Node
.ln_Name
);
84 /* Arbitrate for the semaphore structure */
88 ss_QueueCount == -1 indicates that the semaphore is
89 free, so we increment this straight away. If it then
90 equals 0, then we are the first to allocate this semaphore.
92 Note: This will need protection for SMP machines.
94 sigSem
->ss_QueueCount
++;
95 if( sigSem
->ss_QueueCount
== 0 )
97 /* We now own the semaphore. This is quick. */
98 sigSem
->ss_Owner
= me
;
99 sigSem
->ss_NestCount
++;
102 /* The semaphore was in use, but was it by us? */
103 else if( sigSem
->ss_Owner
== me
)
105 /* Yes, just increase the nesting count */
106 sigSem
->ss_NestCount
++;
110 Else, some other task must own it. We have
111 to set a waiting request here.
116 We need a node to mark our semaphore request. Lets use some
119 struct SemaphoreRequest sr
;
123 Have to clear the signal to make sure that we don't
124 return immediately. We then add the SemReq to the
125 waiters list of the semaphore. We were the last to
126 request, so we must be the last to get the semaphore.
129 /* This must be atomic! */
130 AROS_ATOMIC_AND(me
->tc_SigRecvd
, ~SIGF_SINGLE
);
132 AddTail((struct List
*)&sigSem
->ss_WaitQueue
, (struct Node
*)&sr
);
135 Finally, we simply wait, ReleaseSemaphore() will fill in
136 who owns the semaphore.
145 } /* ObtainSemaphore */