1 | /* |
---|
2 | * Task Create Shell Command Implmentation |
---|
3 | * |
---|
4 | * COPYRIGHT (c) 1989-2013. |
---|
5 | * On-Line Applications Research Corporation (OAR). |
---|
6 | * |
---|
7 | * The license and distribution terms for this file may be |
---|
8 | * found in the file LICENSE in this distribution or at |
---|
9 | * http://www.rtems.com/license/LICENSE. |
---|
10 | */ |
---|
11 | |
---|
12 | #ifdef HAVE_CONFIG_H |
---|
13 | #include "config.h" |
---|
14 | #endif |
---|
15 | |
---|
16 | #if defined(RTEMS_SMP) |
---|
17 | #define _GNU_SOURCE |
---|
18 | #include <sys/cpuset.h> |
---|
19 | #endif |
---|
20 | |
---|
21 | #include <stdio.h> |
---|
22 | |
---|
23 | #define __need_getopt_newlib |
---|
24 | #include <newlib/getopt.h> |
---|
25 | |
---|
26 | #include <rtems.h> |
---|
27 | #include "shell.h" |
---|
28 | #include <rtems/stringto.h> |
---|
29 | #include <schedsim_shell.h> |
---|
30 | #include <rtems/error.h> |
---|
31 | |
---|
32 | static void print_mode( |
---|
33 | const char *prefix, |
---|
34 | rtems_mode mode |
---|
35 | ) |
---|
36 | { |
---|
37 | printf( |
---|
38 | "%sPreemption: %s Timeslicing: %s\n", |
---|
39 | prefix, |
---|
40 | ((mode & RTEMS_NO_PREEMPT) ? "no" : "yes"), |
---|
41 | ((mode & RTEMS_TIMESLICE) ? "yes" : "no") |
---|
42 | ); |
---|
43 | } |
---|
44 | |
---|
45 | rtems_task dummy_task( |
---|
46 | rtems_task_argument arg |
---|
47 | ) |
---|
48 | { |
---|
49 | } |
---|
50 | |
---|
51 | int rtems_shell_main_task_create( |
---|
52 | int argc, |
---|
53 | char *argv[] |
---|
54 | ) |
---|
55 | { |
---|
56 | char *c_p; |
---|
57 | char name[5]; |
---|
58 | rtems_id id; |
---|
59 | rtems_status_code status; |
---|
60 | long priority; |
---|
61 | rtems_mode mode; |
---|
62 | rtems_mode mask; |
---|
63 | struct getopt_data getopt_reent; |
---|
64 | char option; |
---|
65 | int arg; |
---|
66 | unsigned long affinity; |
---|
67 | #if defined(RTEMS_SMP) |
---|
68 | cpu_set_t cpuset; |
---|
69 | bool do_affinity = false; |
---|
70 | #endif |
---|
71 | |
---|
72 | CHECK_RTEMS_IS_UP(); |
---|
73 | |
---|
74 | mode = 0; |
---|
75 | mask = 0; |
---|
76 | memset(&getopt_reent, 0, sizeof(getopt_data)); |
---|
77 | while ( (option = getopt_r( argc, argv, "a:tTpP", &getopt_reent)) != -1 ) { |
---|
78 | switch (option) { |
---|
79 | case 'a': |
---|
80 | #if defined(RTEMS_SMP) |
---|
81 | c_p = getopt_reent.optarg; |
---|
82 | if ( rtems_string_to_unsigned_long( c_p, &affinity, NULL, 0) ) { |
---|
83 | fprintf( stderr, "Affinity (%s) is not a number\n", argv[2] ); |
---|
84 | return 1; |
---|
85 | } |
---|
86 | do_affinity = true; |
---|
87 | |
---|
88 | CPU_ZERO( &cpuset ); |
---|
89 | cpuset.__bits[0] = affinity; |
---|
90 | #else |
---|
91 | printf( "Ignoring affinity request on uniprocessor\n" ); |
---|
92 | #endif |
---|
93 | break; |
---|
94 | |
---|
95 | case 't': |
---|
96 | mask |= RTEMS_TIMESLICE_MASK; |
---|
97 | mode = (mode & ~RTEMS_TIMESLICE_MASK) | RTEMS_NO_TIMESLICE; |
---|
98 | break; |
---|
99 | case 'T': |
---|
100 | mask |= RTEMS_TIMESLICE_MASK; |
---|
101 | mode = (mode & ~RTEMS_TIMESLICE_MASK) | RTEMS_TIMESLICE; |
---|
102 | break; |
---|
103 | case 'p': |
---|
104 | mask |= RTEMS_PREEMPT_MASK; |
---|
105 | mode = (mode & ~RTEMS_PREEMPT_MASK) | RTEMS_NO_PREEMPT; |
---|
106 | break; |
---|
107 | case 'P': |
---|
108 | mask |= RTEMS_PREEMPT_MASK; |
---|
109 | mode = (mode & ~RTEMS_PREEMPT_MASK) | RTEMS_PREEMPT; |
---|
110 | break; |
---|
111 | default: |
---|
112 | fprintf( stderr, "%s: Usage [-a:tTpP]\n", argv[0] ); |
---|
113 | return -1; |
---|
114 | } |
---|
115 | } |
---|
116 | |
---|
117 | print_mode( "Creating task with: ", mode ); |
---|
118 | |
---|
119 | /* |
---|
120 | * Rest of arguments |
---|
121 | */ |
---|
122 | arg = getopt_reent.optind; |
---|
123 | if ( ((argc - arg) != 2) && ((argc - arg) != 4) ){ |
---|
124 | fprintf( stderr, "%s: Usage [args] name priority -a affinity\n", argv[0] ); |
---|
125 | return -1; |
---|
126 | } |
---|
127 | |
---|
128 | if ( rtems_string_to_long(argv[arg+1], &priority, NULL, 0) ) { |
---|
129 | printf( "Priority argument (%s) is not a number\n", argv[1] ); |
---|
130 | return -1; |
---|
131 | } |
---|
132 | |
---|
133 | /* |
---|
134 | * Now create the task |
---|
135 | */ |
---|
136 | memset( name, '\0', sizeof(name) ); |
---|
137 | strncpy( name, argv[arg], 4 ); |
---|
138 | |
---|
139 | status = rtems_task_create( |
---|
140 | rtems_build_name( name[0], name[1], name[2], name[3] ), |
---|
141 | (rtems_task_priority) priority, |
---|
142 | RTEMS_MINIMUM_STACK_SIZE, |
---|
143 | mode, |
---|
144 | RTEMS_DEFAULT_ATTRIBUTES, |
---|
145 | &id |
---|
146 | ); |
---|
147 | if ( status ) { |
---|
148 | fprintf( |
---|
149 | stderr, |
---|
150 | "Task Create(%s) returned %s\n", |
---|
151 | name, |
---|
152 | rtems_status_text( status ) |
---|
153 | ); |
---|
154 | return -1; |
---|
155 | } |
---|
156 | |
---|
157 | printf( |
---|
158 | "Task (%s) created: id=0x%08x, priority=%ld\n", |
---|
159 | name, |
---|
160 | id, |
---|
161 | priority |
---|
162 | ); |
---|
163 | |
---|
164 | #if defined(RTEMS_SMP) |
---|
165 | /* |
---|
166 | * If specified, set the affinity |
---|
167 | */ |
---|
168 | if ( do_affinity ) { |
---|
169 | status = rtems_task_set_affinity( id, sizeof(cpuset), &cpuset ); |
---|
170 | if ( status != RTEMS_SUCCESSFUL ) { |
---|
171 | fprintf( |
---|
172 | stderr, |
---|
173 | "Task Set Affinity(0x%08x) returned %s\n" |
---|
174 | "Deleting task 0x%08x\n", |
---|
175 | affinity, |
---|
176 | rtems_status_text( status ), |
---|
177 | id |
---|
178 | ); |
---|
179 | rtems_task_delete( id ); |
---|
180 | return -1; |
---|
181 | } |
---|
182 | printf("Task (0x%08x) Set affinity=0x%08x\n", id, cpuset.__bits[0] ); |
---|
183 | } |
---|
184 | #endif |
---|
185 | |
---|
186 | /* |
---|
187 | * Starting the task |
---|
188 | */ |
---|
189 | printf( |
---|
190 | "Task (%s) starting: id=0x%08x, priority=%ld\n", |
---|
191 | name, |
---|
192 | id, |
---|
193 | priority |
---|
194 | ); |
---|
195 | |
---|
196 | status = rtems_task_start( id, dummy_task, 1 ); |
---|
197 | if ( status ) { |
---|
198 | fprintf( |
---|
199 | stderr, |
---|
200 | "Task Start(%s) returned %s\n", |
---|
201 | name, |
---|
202 | rtems_status_text( status ) |
---|
203 | ); |
---|
204 | return -1; |
---|
205 | } |
---|
206 | |
---|
207 | return 0; |
---|
208 | } |
---|
209 | |
---|
210 | rtems_shell_cmd_t rtems_shell_TASK_CREATE_Command = { |
---|
211 | "task_create", /* name */ |
---|
212 | "task_create name priority", /* usage */ |
---|
213 | "rtems", /* topic */ |
---|
214 | rtems_shell_main_task_create, /* command */ |
---|
215 | NULL, /* alias */ |
---|
216 | NULL /* next */ |
---|
217 | }; |
---|