comparison src/gpf/osl/os_com_fl.c @ 0:4e78acac3d88

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