FreeCalypso > hg > fc-magnetite
comparison src/gpf2/osl/os_com_fl.c @ 487:91e8dac34ada
src/gpf2/osl: initial import from old freecalypso-sw tree
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Fri, 22 Jun 2018 05:56:16 +0000 |
parents | |
children | ff91db8c9eb8 |
comparison
equal
deleted
inserted
replaced
486:c433cca731a3 | 487:91e8dac34ada |
---|---|
1 /* | |
2 * This C module is a reconstruction based on the disassembly of | |
3 * os_com.obj in frame_na7_db_fl.lib from the Leonardo package. | |
4 */ | |
5 | |
6 /* set of included headers from COFF symtab: */ | |
7 #include <stdio.h> | |
8 #include <string.h> | |
9 #include "gpfconf.h" /* FreeCalypso addition */ | |
10 #include "../../nucleus/nucleus.h" | |
11 #include "typedefs.h" | |
12 #include "os.h" | |
13 #include "gdi.h" | |
14 #include "os_types.h" | |
15 #include "os_glob.h" | |
16 | |
17 extern T_OS_COM_TABLE_ENTRY ComTable[]; | |
18 | |
19 static NU_SEMAPHORE ComSemCB; | |
20 | |
21 static int | |
22 os_GetQueueEntry(USHORT Index, OS_HANDLE *Handle) | |
23 { | |
24 static USHORT Idx; | |
25 | |
26 if (Index == FIRST_ENTRY) | |
27 Idx = 0; | |
28 if (Index == FIRST_ENTRY || Index == NEXT_ENTRY) { | |
29 while (++Idx <= MaxCommunications && !ComTable[Idx].Name[0]) | |
30 ; | |
31 } else | |
32 Idx = Index; | |
33 if (Idx <= MaxCommunications && ComTable[Idx].Name[0]) { | |
34 *Handle = Idx; | |
35 return(0); | |
36 } else | |
37 return(-1); | |
38 } | |
39 | |
40 GLOBAL LONG | |
41 os_QueueInformation(USHORT Index, char *Buffer) | |
42 { | |
43 OS_HANDLE Handle; | |
44 T_OS_COM_TABLE_ENTRY *ent; | |
45 UNSIGNED Used; | |
46 OPTION SuspendType; | |
47 UNSIGNED TasksWaiting; | |
48 NU_TASK *First; | |
49 CHAR Name[NU_MAX_NAME]; | |
50 | |
51 if (os_GetQueueEntry(Index, &Handle) < 0) | |
52 return(OS_ERROR); | |
53 ent = ComTable + Handle; | |
54 if (NU_Semaphore_Information(&ent->UsedSemCB, Name, &Used, &SuspendType, | |
55 &TasksWaiting, &First) != NU_SUCCESS) | |
56 return(OS_ERROR); | |
57 sprintf(Buffer, "Name:%s Startadr:%lx Entries:%d Used:%ld MaxUsed:%d", | |
58 ent->Name, (ULONG)ent->pQueueMemory, ent->Entries, (LONG)Used, | |
59 ent->MaxUsed); | |
60 return(OS_OK); | |
61 } | |
62 | |
63 GLOBAL LONG | |
64 os_OpenQueue(OS_HANDLE TaskHandle, char *Name, OS_HANDLE *ComHandle) | |
65 { | |
66 USHORT i; | |
67 | |
68 if (!Name) | |
69 return(OS_ERROR); | |
70 for (i = 1; i <= MaxCommunications; i++) | |
71 if (ComTable[i].Name[0] && | |
72 !strncmp(ComTable[i].Name, Name, RESOURCE_NAMELEN - 1)) { | |
73 *ComHandle = i; | |
74 return(OS_OK); | |
75 } | |
76 return(OS_ERROR); | |
77 } | |
78 | |
79 GLOBAL LONG | |
80 os_GetQueueState(OS_HANDLE Caller, OS_HANDLE Handle, ULONG *Used, ULONG *Free) | |
81 { | |
82 if (ComTable[Handle].Name[0]) { | |
83 *Used = ComTable[Handle].UsedSemCB.sm_semaphore_count; | |
84 *Free = ComTable[Handle].FreeSemCB.sm_semaphore_count; | |
85 return(OS_OK); | |
86 } else | |
87 return(OS_ERROR); | |
88 } | |
89 | |
90 GLOBAL LONG | |
91 os_GetQueueName(OS_HANDLE Caller, OS_HANDLE ComHandle, char *Name) | |
92 { | |
93 if (ComHandle > MaxCommunications) | |
94 return(OS_ERROR); | |
95 if (!ComTable[ComHandle].Name[0]) | |
96 return(OS_ERROR); | |
97 strcpy(Name, ComTable[ComHandle].Name); | |
98 return(OS_OK); | |
99 } | |
100 | |
101 GLOBAL LONG | |
102 os_GetQueueHandle(OS_HANDLE Caller, char *Name, OS_HANDLE *ComHandle) | |
103 { | |
104 USHORT i; | |
105 | |
106 for (i = 1; i <= MaxCommunications; i++) | |
107 if (ComTable[i].Name[0] && | |
108 !strncmp(Name, ComTable[i].Name, RESOURCE_NAMELEN - 1)) { | |
109 *ComHandle = i; | |
110 return(OS_OK); | |
111 } | |
112 return(OS_ERROR); | |
113 } | |
114 | |
115 GLOBAL LONG | |
116 os_GetQueueData(OS_HANDLE Caller, OS_HANDLE Handle, USHORT Index, USHORT *Type, | |
117 ULONG *opc, ULONG *ptr, ULONG *time) | |
118 { | |
119 static USHORT entry; | |
120 static T_QDATA_ELEMENT *p; | |
121 | |
122 if (!ComTable[Handle].Name[0]) | |
123 return(OS_ERROR); | |
124 if (Index == FIRST_ENTRY) { | |
125 *Type = ComTable[Handle].current_msg.type; | |
126 *opc = ComTable[Handle].current_msg.opc; | |
127 *time = ComTable[Handle].current_msg.time; | |
128 *ptr = (ULONG) ComTable[Handle].current_msg.ptr; | |
129 p = ComTable[Handle].pQueueMemory; | |
130 entry = 0; | |
131 return(OS_OK); | |
132 } | |
133 if (entry >= ComTable[Handle].Entries) | |
134 return(OS_ERROR); | |
135 entry++; | |
136 *Type = p->Data.data16; | |
137 *ptr = (ULONG) p->Data.ptr; | |
138 *opc = p->Data.data32; | |
139 *time = p->Data.time; | |
140 p++; | |
141 return(OS_OK); | |
142 } | |
143 | |
144 GLOBAL unsigned char * | |
145 os_FindSuspendingQueue(unsigned int *tcb) | |
146 { | |
147 USHORT i; | |
148 SM_SUSPEND *susp, *susp2; | |
149 | |
150 for (i = 1; i <= MaxCommunications; i++) { | |
151 if (!ComTable[i].Name[0]) | |
152 continue; | |
153 if (susp = ComTable[i].FreeSemCB.sm_suspension_list) { | |
154 if (susp->sm_suspended_task == (NU_TASK*)tcb) | |
155 return(ComTable[i].FreeSemCB.sm_name + 1); | |
156 susp = (SM_SUSPEND *) susp->sm_suspend_link.cs_next; | |
157 for (susp2 = susp; ; ) { | |
158 if (susp2->sm_suspended_task == (NU_TASK*)tcb) | |
159 return(ComTable[i].FreeSemCB.sm_name+1); | |
160 susp2 = (SM_SUSPEND *) | |
161 susp2->sm_suspend_link.cs_next; | |
162 if (susp2 == susp) | |
163 break; | |
164 } | |
165 } | |
166 if (susp = ComTable[i].UsedSemCB.sm_suspension_list) { | |
167 if (susp->sm_suspended_task == (NU_TASK*)tcb) | |
168 return(ComTable[i].UsedSemCB.sm_name + 1); | |
169 susp = (SM_SUSPEND *) susp->sm_suspend_link.cs_next; | |
170 for (susp2 = susp; ; ) { | |
171 if (susp2->sm_suspended_task == (NU_TASK*)tcb) | |
172 return(ComTable[i].UsedSemCB.sm_name+1); | |
173 susp2 = (SM_SUSPEND *) | |
174 susp2->sm_suspend_link.cs_next; | |
175 if (susp2 == susp) | |
176 break; | |
177 } | |
178 } | |
179 } | |
180 return(0); | |
181 } | |
182 | |
183 GLOBAL LONG | |
184 os_DestroyQueue(OS_HANDLE TaskHandle, OS_HANDLE ComHandle) | |
185 { | |
186 STATUS sts; | |
187 | |
188 sts = NU_Obtain_Semaphore(&ComSemCB, NU_SUSPEND); | |
189 if (NU_Delete_Semaphore(&ComTable[ComHandle].FreeSemCB) != NU_SUCCESS) { | |
190 return_error: if (sts == NU_SUCCESS) | |
191 NU_Release_Semaphore(&ComSemCB); | |
192 return(OS_ERROR); | |
193 } | |
194 if (NU_Delete_Semaphore(&ComTable[ComHandle].UsedSemCB) != NU_SUCCESS) | |
195 goto return_error; | |
196 if (os_DeallocateMemory(TaskHandle, ComTable[ComHandle].QueueData) | |
197 == OS_ERROR) | |
198 goto return_error; | |
199 ComTable[ComHandle].Name[0] = 0; | |
200 if (sts == NU_SUCCESS) | |
201 NU_Release_Semaphore(&ComSemCB); | |
202 return(OS_OK); | |
203 } | |
204 | |
205 static short | |
206 InitQueueMemory(OS_HANDLE TaskHandle, OS_HANDLE ComHandle, USHORT Entries, | |
207 OS_HANDLE MemPoolHandle) | |
208 { | |
209 T_QDATA_ELEMENT *pElem; | |
210 OS_QDATA **ptrs; | |
211 USHORT i; | |
212 | |
213 if (os_AllocateMemory(TaskHandle, &ComTable[ComHandle].QueueData, | |
214 sizeof(T_QDATA_ELEMENT) * Entries + | |
215 sizeof(OS_QDATA *) * (Entries + 1) | |
216 * OS_MAX_PRIORITY, | |
217 0, MemPoolHandle) == OS_TIMEOUT) | |
218 return(OS_ERROR); | |
219 pElem = (T_QDATA_ELEMENT *) ComTable[ComHandle].QueueData; | |
220 ComTable[ComHandle].pQueueMemory = pElem; | |
221 ComTable[ComHandle].pFreeElement = pElem; | |
222 for (i = 0; i < Entries; i++) { | |
223 if (i < Entries - 1) | |
224 pElem->pNext = pElem + 1; | |
225 else | |
226 pElem->pNext = 0; | |
227 pElem++; | |
228 } | |
229 ptrs = (OS_QDATA **) pElem; | |
230 for (i = 0; i < OS_MAX_PRIORITY; i++) { | |
231 ComTable[ComHandle].Queue[i].pStart = ptrs; | |
232 ComTable[ComHandle].Queue[i].pRead = ptrs; | |
233 ComTable[ComHandle].Queue[i].pWrite = ptrs; | |
234 ptrs += Entries + 1; | |
235 } | |
236 return(OS_OK); | |
237 } | |
238 | |
239 GLOBAL LONG | |
240 os_CreateQueue(OS_HANDLE TaskHandle, OS_HANDLE ComHandle, char *Name, | |
241 USHORT Entries, OS_HANDLE *ActHandle, OS_HANDLE MemPoolHandle) | |
242 { | |
243 STATUS sts; | |
244 OS_HANDLE i; | |
245 char Buffer[RESOURCE_NAMELEN + 1]; | |
246 | |
247 if (os_OpenQueue(TaskHandle, Name, ActHandle) == OS_OK) | |
248 return(OS_ERROR); | |
249 if (!Entries) | |
250 return(OS_ERROR); | |
251 sts = NU_Obtain_Semaphore(&ComSemCB, NU_SUSPEND); | |
252 if (!ComHandle) { | |
253 for (i = 1; i <= MaxCommunications; i++) | |
254 if (!ComTable[i].Name[0]) | |
255 goto good_slot; | |
256 release_sem_error: | |
257 if (sts == NU_SUCCESS) | |
258 NU_Release_Semaphore(&ComSemCB); | |
259 return(OS_ERROR); | |
260 } else { | |
261 i = ComHandle; | |
262 if (i > MaxCommunications) | |
263 goto release_sem_error; | |
264 if (ComTable[i].Name[0]) | |
265 goto release_sem_error; | |
266 } | |
267 good_slot: | |
268 if (InitQueueMemory(TaskHandle, i, Entries, MemPoolHandle) == OS_ERROR) | |
269 goto release_sem_error; | |
270 strncpy(Buffer + 1, Name, RESOURCE_NAMELEN - 1); | |
271 Buffer[RESOURCE_NAMELEN] = 0; | |
272 Buffer[0] = 'U'; | |
273 if (NU_Create_Semaphore(&ComTable[i].UsedSemCB, Buffer, 0, NU_PRIORITY) | |
274 != NU_SUCCESS) | |
275 goto release_sem_error; | |
276 Buffer[0] = 'F'; | |
277 if (NU_Create_Semaphore(&ComTable[i].FreeSemCB, Buffer, Entries, | |
278 NU_PRIORITY) != NU_SUCCESS) | |
279 goto release_sem_error; | |
280 strncpy(ComTable[i].Name, Name, RESOURCE_NAMELEN); | |
281 ComTable[i].Name[RESOURCE_NAMELEN-1] = 0; | |
282 *ActHandle = i; | |
283 ComTable[i].Entries = Entries; | |
284 ComTable[i].MaxUsed = 0; | |
285 if (sts == NU_SUCCESS) | |
286 NU_Release_Semaphore(&ComSemCB); | |
287 return(OS_OK); | |
288 } | |
289 | |
290 GLOBAL LONG | |
291 os_ComInit(void) | |
292 { | |
293 USHORT i; | |
294 | |
295 if (NU_Create_Semaphore(&ComSemCB, "COMSEM", 1, NU_PRIORITY) | |
296 != NU_SUCCESS) | |
297 return(OS_ERROR); | |
298 for (i = 1; i <= MaxCommunications; i++) | |
299 bzero(&ComTable[i], sizeof(T_OS_COM_TABLE_ENTRY)); | |
300 return(OS_OK); | |
301 } | |
302 | |
303 GLOBAL LONG | |
304 os_CloseQueue(OS_HANDLE TaskHandle, OS_HANDLE ComHandle) | |
305 { | |
306 return(OS_OK); | |
307 } |