we aren't in forbid state on the smp build..
[AROS.git] / rom / exec / findtask.c
blob7459f6016a5e918535cde4b6791e81df7913d1f2
1 /*
2 Copyright © 1995-2017, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Search a task by name.
6 Lang: english
7 */
9 #define DEBUG 0
11 #include <exec/execbase.h>
12 #include <aros/libcall.h>
13 #include <proto/exec.h>
15 #include "exec_intern.h"
17 /*****************************************************************************
19 NAME */
21 AROS_LH1(struct Task *, FindTask,
23 /* SYNOPSIS */
24 AROS_LHA(CONST_STRPTR, name, A1),
26 /* LOCATION */
27 struct ExecBase *, SysBase, 49, Exec)
29 /* FUNCTION
30 Find a task with a given name or get the address of the current task.
31 Finding the address of the current task is a very quick function
32 call, but finding a special task is a very CPU intensive instruction.
33 Note that generally a task may already be gone when this function
34 returns.
36 INPUTS
37 name - Pointer to name or NULL for current task.
39 RESULT
40 Address of task structure found.
42 NOTES
44 EXAMPLE
46 BUGS
48 SEE ALSO
50 INTERNALS
52 ******************************************************************************/
54 AROS_LIBFUNC_INIT
56 #if defined(__AROSEXEC_SMP__)
57 spinlock_t *listLock;
58 #endif
59 struct Task *ret, *thisTask = GET_THIS_TASK;
61 /* Quick return for a quick argument */
62 if (name == NULL)
63 return thisTask;
65 /* Always protect task lists */
66 #if defined(__AROSEXEC_SMP__)
67 listLock = EXECTASK_SPINLOCK_LOCKDISABLE(&PrivExecBase(SysBase)->TaskReadySpinLock, SPINLOCK_MODE_READ);
68 #else
69 Disable();
70 #endif
72 /* First look into the ready list. */
73 ret = (struct Task *)FindName(&SysBase->TaskReady, name);
74 if (ret == NULL)
76 #if defined(__AROSEXEC_SMP__)
77 EXECTASK_SPINLOCK_UNLOCK(listLock);
78 Enable();
79 listLock = EXECTASK_SPINLOCK_LOCKDISABLE(&PrivExecBase(SysBase)->TaskWaitSpinLock, SPINLOCK_MODE_READ);
80 #endif
81 /* Then into the waiting list. */
82 ret = (struct Task *)FindName(&SysBase->TaskWait, name);
83 if (ret == NULL)
86 Finally test the running task(s). This is mainly of importance on smp systems.
88 #if defined(__AROSEXEC_SMP__)
89 EXECTASK_SPINLOCK_UNLOCK(listLock);
90 Enable();
91 listLock = EXECTASK_SPINLOCK_LOCKDISABLE(&PrivExecBase(SysBase)->TaskRunningSpinLock, SPINLOCK_MODE_READ);
92 ret = (struct Task *)FindName(&PrivExecBase(SysBase)->TaskRunning, name);
93 if (ret == NULL)
95 EXECTASK_SPINLOCK_UNLOCK(listLock);
96 Enable();
97 listLock = EXECTASK_SPINLOCK_LOCKDISABLE(&PrivExecBase(SysBase)->TaskSpinningLock, SPINLOCK_MODE_READ);
98 ret = (struct Task *)FindName(&PrivExecBase(SysBase)->TaskSpinning, name);
100 #else
102 char *s1;
103 const char *s2 = name;
104 s1 = thisTask->tc_Node.ln_Name;
105 /* Check as long as the names are identical. */
106 while (*s1++ == *s2)
107 /* Terminator found? */
108 if (!*s2++)
110 /* Got it. */
111 ret = thisTask;
112 break;
114 #endif
118 #if defined(__AROSEXEC_SMP__)
119 EXECTASK_SPINLOCK_UNLOCK(listLock);
120 #endif
121 Enable();
123 /* Return whatever was found. */
124 return ret;
126 AROS_LIBFUNC_EXIT
127 } /* FindTask */