]> rtime.felk.cvut.cz Git - arc.git/blob - system/kernel/resource.c
Initial commit.
[arc.git] / system / kernel / resource.c
1 /* -------------------------------- Arctic Core ------------------------------
2  * Arctic Core - the open source AUTOSAR platform http://arccore.com
3  *
4  * Copyright (C) 2009  ArcCore AB <contact@arccore.com>
5  *
6  * This source code is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 as published by the
8  * Free Software Foundation; See <http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt>.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13  * for more details.
14  * -------------------------------- Arctic Core ------------------------------*/
15
16
17
18
19
20
21
22
23 \r
24 #include "types.h"\r
25 #include "Os.h"\r
26 #include "assert.h"\r
27 #include "sys.h"\r
28 #include "stdlib.h"\r
29 #include "kernel.h"\r
30 #include "internal.h"\r
31 #include "hooks.h"\r
32 #include "task_i.h"\r
33 #include "ext_config.h"\r
34 \r
35 \r
36 #define valid_standard_id() (rid->nr < Oil_GetResourceCnt()) //&& !(rid->type == RESOURCE_TYPE_INTERNAL) )\r
37 #define valid_internal_id() (rid->nr < Oil_GetResourceCnt()) //&& (rid->type == RESOURCE_TYPE_INTERNAL) )\r
38 \r
39 \r
40 static StatusType GetResource_( resource_obj_t * );\r
41 StatusType ReleaseResource_( resource_obj_t * );\r
42 \r
43 StatusType GetResource( ResourceType ResID ) {\r
44         resource_obj_t *rid = Oil_GetResource(ResID);\r
45         StatusType rv = GetResource_(rid);\r
46 \r
47         if (rv != E_OK)\r
48             goto err;\r
49 \r
50         OS_STD_END_1(OSServiceId_GetResource,ResID);\r
51 }\r
52 \r
53 #if 0\r
54 StatusType GetResourceInternal( ResourceType ResID ) {\r
55         return GetResource_(ResID,1);\r
56 }\r
57 #endif\r
58 \r
59 static StatusType GetResource_( resource_obj_t * rid ) {\r
60         StatusType rv = E_OK;\r
61 \r
62         if( rid->nr == RES_SCHEDULER ) {\r
63                 // Lock the sheduler\r
64 #warning Check this\r
65                 os_sys.scheduler_lock = 1;\r
66                 //simple_printf("RES_SCHEDULER, NOT supported yet\n");\r
67                 //while(1);\r
68         }\r
69         // Check if valid resource\r
70         if( !valid_standard_id() ) {\r
71                 rv = E_OS_ID;\r
72                 goto err;\r
73         }\r
74         // Check that the resource does not belong to another application or task\r
75         if(     ( (os_task_nr_to_mask(get_curr_pid()) & rid->task_mask ) == 0 )\r
76                 || ( get_curr_application_id() !=  rid->application_owner_id)\r
77                 || ( rid->owner != (TaskType)(-1)))\r
78         {\r
79                 rv = E_OS_ACCESS;\r
80                 goto err;\r
81         }\r
82 \r
83         rid->owner = get_curr_pid();\r
84         rid->old_task_prio = os_pcb_set_prio(os_get_curr_pcb() ,rid->ceiling_priority);\r
85         goto ok;\r
86 err:\r
87         ERRORHOOK(rv);\r
88 ok:\r
89         return rv;\r
90 }\r
91 \r
92 StatusType ReleaseResource( ResourceType ResID) {\r
93     StatusType rv = E_OK;\r
94         if( ResID == RES_SCHEDULER ) {\r
95         #warning check this\r
96                 os_sys.scheduler_lock=0;\r
97         } else {\r
98             resource_obj_t *rid = Oil_GetResource(ResID);\r
99             rv = ReleaseResource_(rid);\r
100         }\r
101 \r
102         if (rv != E_OK)\r
103             goto err;\r
104 \r
105         OS_STD_END_1(OSServiceId_ReleaseResource,ResID);\r
106 }\r
107 \r
108 StatusType ReleaseResource_( resource_obj_t * rid ) {\r
109         if (!valid_standard_id()) {\r
110                 return E_OS_ID;\r
111         } else {\r
112 \r
113         // Release it...\r
114         rid->owner = (TaskType) (-1);\r
115         os_pcb_set_prio(os_get_curr_pcb(), rid->old_task_prio);\r
116 \r
117         return E_OK;\r
118         }\r
119 }\r
120 \r
121 // TODO: Remove this function later.. this is done in oil generator\r
122 //       instead.\r
123 void os_resource_calc_attributes( void ) {\r
124         // Calc ceiling\r
125 //      ResourceType *rsrc;\r
126         for( int i=0;i<Oil_GetResourceCnt();i++) {\r
127 //              rsrc = Oil_GetResource();\r
128                 /* TODO: Do this when there's more time */\r
129 //              rsrc\r
130         }\r
131 }\r
132 \r
133 //\r
134 void os_resource_get_internal( void ) {\r
135         resource_obj_t *rt = os_get_resource_int_p();\r
136 \r
137         if( rt != NULL ) {\r
138                 //simple_printf("Get IR proc:%s prio:%d old_task_prio:%d\n",get_curr_pcb()->name, rt->ceiling_priority,rt->old_task_prio);\r
139                 GetResource_(rt);\r
140         }\r
141         //GetResourceInternal(os_get_curr_pcb()->resource_internal);\r
142 }\r
143 \r
144 void os_resource_release_internal( void ) {\r
145         resource_obj_t *rt = os_get_resource_int_p();\r
146 \r
147         if(  rt != NULL ) {\r
148                 //simple_printf("Rel IR proc:%s prio:%d old_task_prio:%d\n",get_curr_pcb()->name,rt->ceiling_priority,rt->old_task_prio);\r
149                 ReleaseResource_(rt);\r
150         }\r
151         //ReleaseResource(os_get_curr_pcb()->resource_internal);\r
152 }\r
153 \r
154 \r
155 \r