2 Copyright © 1995-2011, The AROS Development Team. All rights reserved.
5 Desc: Create a new task, improved version
9 #include <proto/arossupport.h>
10 #include <exec/memory.h>
11 #include <exec/execbase.h>
15 #include "exec_debug.h"
21 struct MemEntry nml_ME
[3];
24 static const struct newMemList MemTemplate
=
29 {{MEMF_CLEAR
|MEMF_PUBLIC
}, sizeof(struct Task
)}, /* Task descriptor itself */
30 {{MEMF_CLEAR
}, AROS_STACKSIZE
}, /* Task's stack */
31 {{MEMF_CLEAR
|MEMF_PUBLIC
}, 0 } /* Task name */
36 /*****************************************************************************
39 #include <exec/tasks.h>
40 #include <proto/exec.h>
42 AROS_LH1(struct Task
*, NewCreateTaskA
,
45 AROS_LHA(struct TagItem
*, tags
, A0
),
48 struct ExecBase
*, SysBase
, 153, Exec
)
54 tags - TagList which may contain the following tags:
56 TASKTAG_ERROR (ULONG *) - a pointer to an optional location for secondary
57 return code. The code itself will be set to
58 TASKERROR_OK on success or TASKERROR_NOMEMORY on
60 TASKTAG_PC (APTR) - Start address of the task's code.
61 TAGKTAG_FINALPC (APTR) - Address of the finalization routine. Defaults to
62 SysBase->TaskExitCode.
63 TASKTAG_STACKSIZE (ULONG) - Size of task's stack. Defaults to CPU-dependent
65 TASKTAG_NAME (STRPTR) - A pointer to task name. The name will be copied.
66 TASKTAG_USERDATA (APTR) - Anything. Will be written into tc_UserData.
67 TASKTAG_PRI (BYTE) - Task's priority. Defaults to 0.
69 TASKTAG_ARG8 (IPTR) - Arguments (up to 8) which will be passed to task's
70 entry function. The arguments are supplied in
72 TASKTAG_FLAGS (ULONG) - Initial value for tc_Flags.
73 TASKTAG_TCBEXTRASIZE (ULONG) - Value which will be added to sizeof(struct Task)
74 in order to determine final size of task structure.
75 Can be used for appending user data to task structure.
78 A pointer to the new task or NULL on failure.
85 Value of TASKTAG_FLAGS is actually ignored.
86 There are some more tags which are currently not implemented.
94 ******************************************************************************/
98 struct Task
* newtask
,
100 struct newMemList nml
= MemTemplate
;
102 const struct TagItem
*tstate
= tags
;
104 ULONG
*errPtr
= NULL
;
106 APTR finalpc
= SysBase
->TaskExitCode
;
107 char *taskname
= NULL
;
108 APTR userdata
= NULL
;
112 while ((tag
= LibNextTagItem(&tstate
)))
117 errPtr
= (ULONG
*)tag
->ti_Data
;
121 initpc
= (APTR
)tag
->ti_Data
;
124 case TASKTAG_FINALPC
:
125 finalpc
= (APTR
)tag
->ti_Data
;
128 case TASKTAG_STACKSIZE
:
129 nml
.nml_ME
[1].me_Length
= AROS_ALIGN(tag
->ti_Data
);
133 taskname
= (char *)tag
->ti_Data
;
134 nml
.nml_ME
[2].me_Length
= strlen(taskname
) + 1;
137 case TASKTAG_USERDATA
:
138 userdata
= (APTR
)tag
->ti_Data
;
146 flags
= tag
->ti_Data
;
149 case TASKTAG_TCBEXTRASIZE
:
150 nml
.nml_ME
[0].me_Length
+= tag
->ti_Data
;
155 DADDTASK("NewCreateTaskA: name %s\n", taskname
? taskname
: "<NULL>");
157 if (NewAllocEntry((struct MemList
*)&nml
, &ml
, NULL
))
159 APTR name
= ml
->ml_ME
[2].me_Addr
;
162 strcpy(name
, taskname
);
164 newtask
= ml
->ml_ME
[0].me_Addr
;
166 newtask
->tc_Node
.ln_Type
= NT_TASK
;
167 newtask
->tc_Node
.ln_Pri
= pri
;
168 newtask
->tc_Node
.ln_Name
= name
;
170 /* FIXME: NewAddTask() will reset flags to 0 */
171 newtask
->tc_Flags
= flags
;
172 newtask
->tc_UserData
= userdata
;
174 newtask
->tc_SPReg
= (APTR
)((IPTR
)ml
->ml_ME
[1].me_Addr
+ nml
.nml_ME
[1].me_Length
);
175 newtask
->tc_SPLower
= ml
->ml_ME
[1].me_Addr
;
176 newtask
->tc_SPUpper
= newtask
->tc_SPReg
;
178 NEWLIST(&newtask
->tc_MemEntry
);
179 AddHead(&newtask
->tc_MemEntry
, (struct Node
*)ml
);
181 /* TASKTAG_ARGx will be processed by PrepareContext() */
182 task2
= NewAddTask (newtask
, initpc
, finalpc
, tags
);
193 /* Set secondary error code if requested */
195 *errPtr
= newtask
? TASKERROR_OK
: TASKERROR_NOMEMORY
;