comparison src/gpf/osl/os_sem_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_sem.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_SEM_TABLE_ENTRY SemTable[];
17
18 static NU_SEMAPHORE SemSemCB;
19
20 static int
21 os_GetSemaphoreEntry(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 for (;;) {
29 Idx++;
30 if (Idx > MaxSemaphores)
31 return(OS_ERROR);
32 if (SemTable[Idx].Name[0])
33 break;
34 }
35 } else
36 Idx = Index;
37 if (Idx > MaxSemaphores)
38 return(OS_ERROR);
39 if (SemTable[Idx].Name[0]) {
40 *Handle = Idx;
41 return(OS_OK);
42 } else
43 return(OS_ERROR);
44 }
45
46 GLOBAL LONG
47 os_SemaphoreInformation(USHORT Index, char *Buffer)
48 {
49 OS_HANDLE Handle;
50 OPTION SuspendType;
51 UNSIGNED Current, TasksWaiting;
52 NU_TASK *First;
53 CHAR Name[NU_MAX_NAME];
54
55 if (os_GetSemaphoreEntry(Index, &Handle) < 0)
56 return(OS_ERROR);
57 if (NU_Semaphore_Information(&SemTable[Handle].SemCB, Name, &Current,
58 &SuspendType, &TasksWaiting, &First)
59 != NU_SUCCESS)
60 return(OS_ERROR);
61 sprintf(Buffer, "Semname:%s Count:%ld Suspend:%d Waiting:%ld", Name,
62 Current, SuspendType, TasksWaiting);
63 return(OS_OK);
64 }
65
66 GLOBAL LONG
67 os_SemInit(void)
68 {
69 USHORT i;
70
71 if (NU_Create_Semaphore(&SemSemCB, "SEMSEM", 1, NU_PRIORITY)
72 != NU_SUCCESS)
73 return(OS_ERROR);
74 for (i = 1; i <= MaxSemaphores; i++)
75 memset(&SemTable[i], 0, sizeof(T_OS_SEM_TABLE_ENTRY));
76 return(OS_OK);
77 }
78
79 GLOBAL LONG
80 os_ResetSemaphore(OS_HANDLE TaskHandle, OS_HANDLE SemHandle,
81 USHORT init_counter)
82 {
83 STATUS sts;
84
85 sts = NU_Obtain_Semaphore(&SemSemCB, NU_SUSPEND);
86 if (!SemTable[SemHandle].Name[0]) {
87 error_out: if (sts == NU_SUCCESS)
88 NU_Release_Semaphore(&SemSemCB);
89 return(OS_ERROR);
90 }
91 if (NU_Reset_Semaphore(&SemTable[SemHandle].SemCB, init_counter)
92 != NU_SUCCESS)
93 goto error_out;
94 if (sts == NU_SUCCESS)
95 NU_Release_Semaphore(&SemSemCB);
96 return(OS_OK);
97 }
98
99 GLOBAL LONG
100 os_QuerySemaphore(OS_HANDLE TaskHandle, OS_HANDLE SemHandle, USHORT *Count)
101 {
102 OPTION SuspendType;
103 UNSIGNED SemCount, TasksWaiting;
104 NU_TASK *First;
105 CHAR Name[NU_MAX_NAME];
106
107 if (NU_Semaphore_Information(&SemTable[SemHandle].SemCB, Name,
108 &SemCount, &SuspendType, &TasksWaiting,
109 &First) != NU_SUCCESS)
110 return(OS_ERROR);
111 *Count = SemCount;
112 return(OS_OK);
113 }
114
115 GLOBAL LONG
116 os_OpenSemaphore(OS_HANDLE TaskHandle, char *Name, OS_HANDLE *SemHandle)
117 {
118 USHORT i;
119
120 for (i = 1; i <= MaxSemaphores; i++) {
121 if (!SemTable[i].Name[0])
122 continue;
123 if (strncmp(Name, SemTable[i].Name, RESOURCE_NAMELEN-1))
124 continue;
125 *SemHandle = i;
126 return(OS_OK);
127 }
128 return(OS_ERROR);
129 }
130
131 GLOBAL unsigned char *
132 os_FindSuspendingSema(unsigned int *tcb)
133 {
134 USHORT i;
135 SM_SUSPEND *susp, *susp_loopchk;
136
137 for (i = 1; i <= MaxSemaphores; i++) {
138 if (!SemTable[i].Name[0])
139 continue;
140 susp = SemTable[i].SemCB.sm_suspension_list;
141 if (!susp)
142 continue;
143 if (susp->sm_suspended_task == (NU_TASK *)tcb)
144 return((unsigned char *) SemTable[i].SemCB.sm_name);
145 susp = (SM_SUSPEND *)susp->sm_suspend_link.cs_next;
146 for (susp_loopchk = susp; susp != susp_loopchk;
147 susp = (SM_SUSPEND *)susp->sm_suspend_link.cs_next)
148 if (susp->sm_suspended_task == (NU_TASK *)tcb)
149 return((unsigned char *)
150 SemTable[i].SemCB.sm_name);
151 }
152 return(0);
153 }
154
155 GLOBAL LONG
156 os_DestroySemaphore(OS_HANDLE TaskHandle, OS_HANDLE SemHandle)
157 {
158 STATUS sts;
159
160 sts = NU_Obtain_Semaphore(&SemSemCB, NU_SUSPEND);
161 if (!SemTable[SemHandle].Name[0]) {
162 error_out: if (sts == NU_SUCCESS)
163 NU_Release_Semaphore(&SemSemCB);
164 return(OS_ERROR);
165 }
166 if (NU_Delete_Semaphore(&SemTable[SemHandle].SemCB) != NU_SUCCESS)
167 goto error_out;
168 SemTable[SemHandle].Name[0] = 0;
169 if (sts == NU_SUCCESS)
170 NU_Release_Semaphore(&SemSemCB);
171 return(OS_OK);
172 }
173
174 GLOBAL LONG
175 os_CreateSemaphore(OS_HANDLE TaskHandle, char *Name, USHORT Count,
176 OS_HANDLE *SemHandle, OS_HANDLE MemPoolHandle)
177 {
178 USHORT i;
179 STATUS sts;
180
181 if (os_OpenSemaphore(TaskHandle, Name, SemHandle) == OS_OK)
182 return(OS_ERROR);
183 sts = NU_Obtain_Semaphore(&SemSemCB, NU_SUSPEND);
184 for (i = 1; i <= MaxSemaphores; i++) {
185 if (SemTable[i].Name[0])
186 continue;
187 if (NU_Create_Semaphore(&SemTable[i].SemCB, Name, Count,
188 NU_PRIORITY) != NU_SUCCESS)
189 break;
190 strncpy(SemTable[i].Name, Name, RESOURCE_NAMELEN);
191 SemTable[i].Name[RESOURCE_NAMELEN-1] = 0;
192 *SemHandle = i;
193 if (sts == NU_SUCCESS)
194 NU_Release_Semaphore(&SemSemCB);
195 return(OS_OK);
196 }
197 if (sts == NU_SUCCESS)
198 NU_Release_Semaphore(&SemSemCB);
199 return(OS_ERROR);
200 }
201
202 GLOBAL LONG
203 os_CloseSemaphore(OS_HANDLE TaskHandle, OS_HANDLE SemHandle)
204 {
205 return(OS_OK);
206 }