fix findtask behaviour when checking running tasks. also check the spinning task...
[AROS.git] / rom / exec / findtaskbypid.c
blob2169e867963d0b91b4236a8f313bd80f3b841b1a
1 /*
2 Copyright © 1995-2015, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <exec/execbase.h>
7 #include <exec/tasks.h>
9 #include "exec_intern.h"
11 /*****************************************************************************
13 NAME */
14 #include <proto/exec.h>
16 AROS_LH1(struct Task *, FindTaskByPID,
18 /* SYNOPSIS */
19 AROS_LHA(ULONG, id, D0),
21 /* LOCATION */
22 struct ExecBase *, SysBase, 166, Exec)
24 /* FUNCTION
25 Scan through the task lists searching for the task whose
26 et_UniqueID field matches.
28 INPUTS
29 id - The task ID to match.
31 RESULT
32 Address of the Task control structure that matches, or
33 NULL otherwise.
35 NOTES
36 This function is source-compatible with MorphOS.
38 EXAMPLE
40 BUGS
42 SEE ALSO
44 INTERNALS
46 ******************************************************************************/
48 AROS_LIBFUNC_INIT
50 #if defined(__AROSEXEC_SMP__)
51 spinlock_t *listLock;
52 #endif
53 struct Task *t;
54 struct ETask *et;
56 /* First up, check running task(s) */
57 #if defined(__AROSEXEC_SMP__)
58 listLock = EXEC_SPINLOCK_LOCK(&PrivExecBase(SysBase)->TaskRunningSpinLock, SPINLOCK_MODE_READ);
59 Forbid();
60 ForeachNode(&PrivExecBase(SysBase)->TaskRunning, t)
62 et = GetETask(t);
63 if (et != NULL && et->et_UniqueID == id)
65 EXEC_SPINLOCK_UNLOCK(listLock);
66 Permit();
67 return t;
70 EXEC_SPINLOCK_UNLOCK(listLock);
71 Permit();
72 listLock = EXEC_SPINLOCK_LOCK(&PrivExecBase(SysBase)->TaskSpinningLock, SPINLOCK_MODE_READ);
73 Forbid();
74 ForeachNode(&PrivExecBase(SysBase)->TaskSpinning, t)
76 et = GetETask(t);
77 if (et != NULL && et->et_UniqueID == id)
79 EXEC_SPINLOCK_UNLOCK(listLock);
80 Permit();
81 return t;
84 EXEC_SPINLOCK_UNLOCK(listLock);
85 Permit();
86 listLock = EXEC_SPINLOCK_LOCK(&PrivExecBase(SysBase)->TaskReadySpinLock, SPINLOCK_MODE_READ);
87 Forbid();
88 #else
89 Disable();
90 if (GET_THIS_TASK != NULL)
92 et = GetETask(GET_THIS_TASK);
93 if (et != NULL && et->et_UniqueID == id)
95 Enable();
96 return GET_THIS_TASK;
99 #endif
100 /* Next, go through the ready list */
101 ForeachNode(&SysBase->TaskReady, t)
103 et = GetETask(t);
104 if (et != NULL && et->et_UniqueID == id)
106 #if defined(__AROSEXEC_SMP__)
107 EXEC_SPINLOCK_UNLOCK(listLock);
108 Permit();
109 #else
110 Enable();
111 #endif
112 return t;
115 #if defined(__AROSEXEC_SMP__)
116 EXEC_SPINLOCK_UNLOCK(listLock);
117 Permit();
118 listLock = EXEC_SPINLOCK_LOCK(&PrivExecBase(SysBase)->TaskWaitSpinLock, SPINLOCK_MODE_READ);
119 Forbid();
120 #endif
121 /* Finally, go through the wait list */
122 ForeachNode(&SysBase->TaskWait, t)
124 et = GetETask(t);
125 if (et != NULL && et->et_UniqueID == id)
127 #if defined(__AROSEXEC_SMP__)
128 EXEC_SPINLOCK_UNLOCK(listLock);
129 Permit();
130 #else
131 Enable();
132 #endif
133 return t;
137 #if defined(__AROSEXEC_SMP__)
138 EXEC_SPINLOCK_UNLOCK(listLock);
139 Permit();
140 #else
141 Enable();
142 #endif
144 return NULL;
146 AROS_LIBFUNC_EXIT