1 /* -------------------------------- Arctic Core ------------------------------
\r
2 * Arctic Core - the open source AUTOSAR platform http://arccore.com
\r
4 * Copyright (C) 2009 ArcCore AB <contact@arccore.com>
\r
6 * This source code is free software; you can redistribute it and/or modify it
\r
7 * under the terms of the GNU General Public License version 2 as published by the
\r
8 * Free Software Foundation; See <http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt>.
\r
10 * This program is distributed in the hope that it will be useful, but
\r
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
\r
14 * -------------------------------- Arctic Core ------------------------------*/
\r
17 #include "internal.h"
\r
19 #include "resource_i.h"
\r
26 #define MAX(_x,_y) (((_x) > (_y)) ? (_x) : (_y))
\r
31 Resource management at interrupt level is NOT supported
\r
36 1. Priority ceiling: Call GetResource() from preemtive
\r
37 task and activate a task with higher priority than the ceiling protocol.
\r
38 The higher priority task should be swapped in.
\r
39 2. Verify that you cannot allocate an internal resource with
\r
41 b) ReleaseResource()
\r
42 3. Internal resource. Allocate 1 internal resource to 3 tasks of different
\r
43 priorities. Verify that
\r
44 a) Higher priority tasks than the group can preement
\r
45 b) For tasks which have the same or lower priority as the highest priority within a group,
\r
46 the tasks within the group behave like non preemptable tasks ( OSEK 4.6.3)
\r
47 4. Attempt to release a resource which has a lower ceiling priority
\r
48 than the statically assigned priority of the calling task or
\r
49 interrupt routine, E_OS_ACCESS
\r
50 5. The general restriction on some system calls that they are not to be called with resources
\r
51 occupied (chapter 8.2) does not apply to internal resources, as internal resources are handled
\r
52 within those calls. However, all standard resources have to be released before the internal
\r
53 resource can be released (see chapter 8.2,
\93LIFO principle
\94).
\r
54 6. Check LIFO order. Return E_OS_ACCESS if not in LIFO order..
\r
55 7. Test Os_IsrAddResource().
\r
59 - GetResource(RES_SCHEDULER) will lock the scheduler even for ISR2
\r
62 1. task.resourceAccess is already calculated by BSW builder. This is the bitmask
\r
63 of what resources is accessable by the task.
\r
66 task.rsrcAccessMask & (1 << RES_SCHEDULER)
\r
72 * - If OsTaskSchedule = NON, Task it not preemptable, no internal resource may be assigned to a task
\r
73 * (cause it already have one of prio 32)
\r
74 * FULL, Task is preemptable
\r
75 * - On Schedule() .... This service has no influence on tasks with no internal resource
\r
76 * assigned (preemptable tasks).
\r
78 * OSEK on internal resources:
\r
79 * - Non preemptable tasks are a special group with an internal resource of the
\r
80 * same priority as RES_SCHEDULER assigned
\r
83 * Assign RES_SCHEDULER with prio 32.
\r
84 * Assign internal resources to NON preemptable task.
\r
86 * So that leaves us with:
\r
88 * - Cannot assign internal resource.
\r
89 * It automatically gets internal resource with same prio as RES_SCHEDULER
\r
92 * - Assigned. Used for grouping tasks.
\r
95 * What does that mean?
\r
96 * - It's probably OK to do a GetResource(RES_SCHEDULER) from a NON task (although pointless)
\r
97 * - GetResource(<any>) from a NON task is wrong
\r
99 * Generation/Implementation:
\r
100 * - Resources to 32. Alloc with .resourceAlloc = ((1<<RES_1) |(1<<RES_2));
\r
101 * - Keep allocated resources as stack to comply with LIFO order.
\r
102 * - A linked resource is just another name for an existing resource. See OsResource in Autosar SWS OS.
\r
103 * This means that no resource object should be generated, just the define in Os_Cfg.h
\r
104 * - A task with Scheduling=NON have priority (although it's internal priority is 32)
\r
108 #define valid_standard_id() (rPtr->nr < OS_RESOURCE_CNT) //&& !(rPtr->type == RESOURCE_TYPE_INTERNAL) )
\r
109 #define valid_internal_id() (rPtr->nr < OS_RESOURCE_CNT) //&& (rPtr->type == RESOURCE_TYPE_INTERNAL) )
\r
114 * This call serves to enter critical sections in the code that are
\r
115 * assigned to the resource referenced by <ResID>. A critical
\r
116 * section shall always be left using ReleaseResource.
\r
118 * The OSEK priority ceiling protocol for resource management is described
\r
119 * in chapter 8.5. Nested resource occupation is only allowed if the inner
\r
120 * critical sections are completely executed within the surrounding critical
\r
121 * section (strictly stacked, see chapter 8.2, Restrictions when using
\r
122 * resources). Nested occupation of one and the same resource is also
\r
123 * forbidden! It is recommended that corresponding calls to GetResource and
\r
124 * ReleaseResource appear within the same function.
\r
126 * It is not allowed to use services which are points of rescheduling for
\r
127 * non preemptable tasks (TerminateTask,ChainTask, Schedule and WaitEvent,
\r
128 * see chapter 4.6.2) in critical sections.
\r
129 * Additionally, critical sections are to be left before completion of
\r
130 * an interrupt service routine.
\r
131 * Generally speaking, critical sections should be short.
\r
132 * The service may be called from an ISR and from task level (see Figure 12-1).
\r
139 StatusType GetResource( ResourceType ResID ) {
\r
140 StatusType rv = E_OK;
\r
141 OsResourceType *rPtr;
\r
147 if( Os_Sys.intNestCnt != 0 ) {
\r
149 /* For interrupts to the scheduler resource seems just dumb to get */
\r
150 OsIsrVarType *isrPtr = Os_SysIsrGetCurr();
\r
152 /* Check we can access it */
\r
153 if( ((isrPtr->constPtr->resourceMask & (1<< ResID)) == 0) ||
\r
154 ( ResID == RES_SCHEDULER ) ) {
\r
159 rPtr = Os_ResourceGet(ResID);
\r
161 /* ceiling prio for ISR seems strange...so no */
\r
162 if( rPtr->owner != NO_TASK_OWNER ) {
\r
164 Irq_Restore(flags);
\r
167 /* Add the resource to the list of resources held by this isr */
\r
168 Os_IsrResourceAdd(rPtr,isrPtr);
\r
171 OsTaskVarType *taskPtr = Os_SysTaskGetCurr();
\r
173 if( ResID == RES_SCHEDULER ) {
\r
174 rPtr = &Os_Sys.resScheduler;
\r
176 /* Check we can access it */
\r
177 if( (taskPtr->constPtr->resourceAccess & (1<< ResID)) == 0 ) {
\r
182 rPtr = Os_ResourceGet(ResID);
\r
185 /* Check for invalid configuration */
\r
186 if( (rPtr->owner != NO_TASK_OWNER) ||
\r
187 (taskPtr->activePriority > rPtr->ceiling_priority) )
\r
190 Irq_Restore(flags);
\r
193 /* Add the resource to the list of resources held by this task */
\r
194 Os_TaskResourceAdd(rPtr,taskPtr);
\r
197 Irq_Restore(flags);
\r
202 OS_STD_END_1(OSServiceId_GetResource,ResID);
\r
206 * ReleaseResource is the counterpart of GetResource and
\r
207 * serves to leave critical sections in the code that are assigned to
\r
208 * the resource referenced by <ResID>.
\r
210 * For information on nesting conditions, see particularities of
\r
211 * GetResource. The service may be called from an ISR and from task level (see
\r
218 StatusType ReleaseResource( ResourceType ResID) {
\r
219 StatusType rv = E_OK;
\r
220 OsTaskVarType *pcbPtr = Os_SysTaskGetCurr();
\r
221 OsResourceType *rPtr;
\r
225 if( ResID == RES_SCHEDULER ) {
\r
226 rPtr = &Os_Sys.resScheduler;
\r
228 /* Check we can access it */
\r
229 if( (pcbPtr->constPtr->resourceAccess & (1<< ResID)) == 0 ) {
\r
233 rPtr = Os_ResourceGet(ResID);
\r
236 /* Check for invalid configuration */
\r
237 if( rPtr->owner == NO_TASK_OWNER)
\r
240 Irq_Restore(flags);
\r
244 if( (pcbPtr->activePriority < rPtr->ceiling_priority))
\r
247 Irq_Restore(flags);
\r
251 Os_TaskResourceRemove(rPtr,pcbPtr);
\r
253 /* do a rescheduling (in some cases) (see OSEK OS 4.6.1) */
\r
254 if ( (pcbPtr->constPtr->scheduling == FULL) &&
\r
255 (Os_Sys.intNestCnt == 0) &&
\r
256 (Os_SchedulerResourceIsFree()) ) {
\r
258 OsTaskVarType* top_pcb = Os_TaskGetTop();
\r
260 /* only dispatch if some other ready task has higher prio */
\r
261 if (top_pcb->activePriority > Os_SysTaskGetCurr()->activePriority) {
\r
262 Os_Dispatch(OP_RELEASE_RESOURCE);
\r
265 Irq_Restore(flags);
\r
267 OS_STD_END_1(OSServiceId_ReleaseResource,ResID);
\r
271 void Os_ResourceGetInternal( void ) {
\r
272 OsTaskVarType *pcbPtr = Os_SysTaskGetCurr();
\r
273 OsResourceType *rt = pcbPtr->constPtr->resourceIntPtr;
\r
276 OS_DEBUG(D_RESOURCE,"Get IR proc:%s prio:%u old_task_prio:%u\n",
\r
277 Os_SysTaskGetCurr()->name,
\r
278 (unsigned)rt->ceiling_priority,
\r
279 (unsigned)rt->old_task_prio);
\r
280 Os_TaskResourceAdd(rt,pcbPtr);
\r
284 void Os_ResourceReleaseInternal( void ) {
\r
285 OsTaskVarType *pcbPtr = Os_SysTaskGetCurr();
\r
286 OsResourceType *rt = pcbPtr->constPtr->resourceIntPtr;
\r
289 OS_DEBUG(D_RESOURCE,"Rel IR proc:%s prio:%u old_task_prio:%u\n",
\r
290 Os_SysTaskGetCurr()->name,
\r
291 (unsigned)rt->ceiling_priority,
\r
292 (unsigned)rt->old_task_prio);
\r
293 Os_TaskResourceRemove(rt,pcbPtr);
\r
304 void Os_ResourceInit( void ) {
\r
305 //TAILQ_INIT(&pcb_p->resourceHead);
\r
306 OsTaskVarType *pcb_p;
\r
307 OsResourceType *rsrc_p;
\r
311 /* For now, assign the scheduler resource here */
\r
312 Os_Sys.resScheduler.ceiling_priority = OS_RES_SCHEDULER_PRIO;
\r
313 strcpy(Os_Sys.resScheduler.id,"RES_SCHEDULER");
\r
314 Os_Sys.resScheduler.nr = RES_SCHEDULER;
\r
315 Os_Sys.resScheduler.owner = NO_TASK_OWNER;
\r
317 /* Calculate ceiling priority
\r
318 * We make this as simple as possible. The ceiling priority
\r
319 * is set to the same priority as the highest priority task that
\r
322 * Note that this applies both internal and standard resources.
\r
324 for( int i=0; i < OS_RESOURCE_CNT; i++) {
\r
325 rsrc_p = Os_ResourceGet(i);
\r
328 for( int pi = 0; pi < OS_TASK_CNT; pi++) {
\r
330 pcb_p = Os_TaskGet(pi);
\r
333 if(pcb_p->constPtr->resourceAccess & (1<<i) ) {
\r
334 topPrio = MAX(topPrio,pcb_p->constPtr->prio);
\r
337 /* Generator fix, add RES_SCHEDULER */
\r
338 // pcb_p->constPtr->resourceAccess |= (1 << RES_SCHEDULER) ;
\r
340 rsrc_p->ceiling_priority = topPrio;
\r