revert between 56095 -> 55830 in arch
[AROS.git] / rom / exec / settaskpri.c
blob4d0a9cec99ea64284ae586dc7b72863ab40cd34a
1 /*
2 Copyright © 1995-2017, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Change the priority of a task.
6 Lang: english
7 */
9 #define DEBUG 0
10 #include <aros/debug.h>
12 #include <exec/execbase.h>
13 #include <aros/libcall.h>
14 #include <proto/exec.h>
16 #include "exec_intern.h"
17 #if defined(__AROSEXEC_SMP__)
18 #include "etask.h"
19 #include "exec_locks.h"
20 #endif
22 /*****************************************************************************
24 NAME */
26 AROS_LH2(BYTE, SetTaskPri,
28 /* SYNOPSIS */
29 AROS_LHA(struct Task *, task, A1),
30 AROS_LHA(LONG, priority, D0),
32 /* LOCATION */
33 struct ExecBase *, SysBase, 50, Exec)
35 /* FUNCTION
36 Change the priority of a given task. As a general rule the higher
37 the priority the more CPU time a task gets. Useful values are within
38 -127 to 5.
40 INPUTS
41 task - Pointer to task structure.
42 priority - New priority of the task.
44 RESULT
45 Old task priority.
47 NOTES
49 EXAMPLE
51 BUGS
53 SEE ALSO
55 INTERNALS
57 HISTORY
59 ******************************************************************************/
61 AROS_LIBFUNC_INIT
63 struct Task *thisTask = GET_THIS_TASK;
64 #if defined(__AROSEXEC_SMP__)
65 spinlock_t *task_listlock = NULL;
66 int cpunum = KrnGetCPUNumber();
67 #endif
68 BYTE old;
70 D(bug("[Exec] SetTaskPri(0x%p, %d)\n", task, priority);)
72 /* Always Disable() when doing something with task lists. */
73 #if defined(__AROSEXEC_SMP__)
74 switch (task->tc_State)
76 case TS_RUN:
77 task_listlock =&PrivExecBase(SysBase)->TaskRunningSpinLock;
78 break;
79 case TS_WAIT:
80 task_listlock = &PrivExecBase(SysBase)->TaskWaitSpinLock;
81 break;
82 default:
83 task_listlock = &PrivExecBase(SysBase)->TaskReadySpinLock;
84 break;
86 #endif
87 Disable();
88 #if defined(__AROSEXEC_SMP__)
89 if (task->tc_State == TS_READY)
90 EXEC_LOCK_WRITE(task_listlock);
91 else
92 EXEC_LOCK_READ(task_listlock);
93 #endif
95 /* Get returncode */
96 old = task->tc_Node.ln_Pri;
98 /* Set new value. */
99 task->tc_Node.ln_Pri = priority;
101 /* Check if the task is willing to run. */
102 if (task->tc_State != TS_WAIT)
104 /* If it is in the ready list remove and reinsert it. */
105 if (task->tc_State == TS_READY)
107 Remove(&task->tc_Node);
108 Enqueue(&SysBase->TaskReady, &task->tc_Node);
111 #if defined(__AROSEXEC_SMP__)
112 EXEC_UNLOCK(task_listlock);
114 task_listlock = NULL;
115 if (IntETask(task->tc_UnionETask.tc_ETask)->iet_CpuNumber == cpunum) {
116 #endif
117 if ( task->tc_Node.ln_Pri > thisTask->tc_Node.ln_Pri)
119 D(bug("[Exec] SetTaskPri: Task needs reschedule...\n");)
120 Reschedule();
122 #if defined(__AROSEXEC_SMP__)
124 else if (task->tc_State == TS_RUN)
126 D(bug("[Exec] SetTaskPri: changing priority of task running on another cpu (%03u)\n", IntETask(task->tc_UnionETask.tc_ETask)->iet_CpuNumber);)
127 KrnScheduleCPU(IntETask(task->tc_UnionETask.tc_ETask)->iet_CpuAffinity);
129 #endif
132 /* All done. */
133 #if defined(__AROSEXEC_SMP__)
134 if (task_listlock)
136 EXEC_UNLOCK(task_listlock);
138 #endif
139 Enable();
141 return old;
143 AROS_LIBFUNC_EXIT
144 } /* SetTaskPri */