FreeCalypso > hg > fc-magnetite
comparison src/gpf2/osl/os_sem_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 | 27db9775cbab |
comparison
equal
deleted
inserted
replaced
486:c433cca731a3 | 487:91e8dac34ada |
---|---|
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 "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_SEM_TABLE_ENTRY SemTable[]; | |
18 | |
19 static NU_SEMAPHORE SemSemCB; | |
20 | |
21 static int | |
22 os_GetSemaphoreEntry(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 for (;;) { | |
30 Idx++; | |
31 if (Idx > MaxSemaphores) | |
32 return(OS_ERROR); | |
33 if (SemTable[Idx].Name[0]) | |
34 break; | |
35 } | |
36 } else | |
37 Idx = Index; | |
38 if (Idx > MaxSemaphores) | |
39 return(OS_ERROR); | |
40 if (SemTable[Idx].Name[0]) { | |
41 *Handle = Idx; | |
42 return(OS_OK); | |
43 } else | |
44 return(OS_ERROR); | |
45 } | |
46 | |
47 GLOBAL LONG | |
48 os_SemaphoreInformation(USHORT Index, char *Buffer) | |
49 { | |
50 OS_HANDLE Handle; | |
51 OPTION SuspendType; | |
52 UNSIGNED Current, TasksWaiting; | |
53 NU_TASK *First; | |
54 CHAR Name[NU_MAX_NAME]; | |
55 | |
56 if (os_GetSemaphoreEntry(Index, &Handle) < 0) | |
57 return(OS_ERROR); | |
58 if (NU_Semaphore_Information(&SemTable[Handle].SemCB, Name, &Current, | |
59 &SuspendType, &TasksWaiting, &First) | |
60 != NU_SUCCESS) | |
61 return(OS_ERROR); | |
62 sprintf(Buffer, "Semname:%s Count:%ld Suspend:%d Waiting:%ld", Name, | |
63 Current, SuspendType, TasksWaiting); | |
64 return(OS_OK); | |
65 } | |
66 | |
67 GLOBAL LONG | |
68 os_SemInit(void) | |
69 { | |
70 USHORT i; | |
71 | |
72 if (NU_Create_Semaphore(&SemSemCB, "SEMSEM", 1, NU_PRIORITY) | |
73 != NU_SUCCESS) | |
74 return(OS_ERROR); | |
75 for (i = 1; i <= MaxSemaphores; i++) | |
76 bzero(&SemTable[i], sizeof(T_OS_SEM_TABLE_ENTRY)); | |
77 return(OS_OK); | |
78 } | |
79 | |
80 GLOBAL LONG | |
81 os_ResetSemaphore(OS_HANDLE TaskHandle, OS_HANDLE SemHandle, | |
82 USHORT init_counter) | |
83 { | |
84 STATUS sts; | |
85 | |
86 sts = NU_Obtain_Semaphore(&SemSemCB, NU_SUSPEND); | |
87 if (!SemTable[SemHandle].Name[0]) { | |
88 error_out: if (sts == NU_SUCCESS) | |
89 NU_Release_Semaphore(&SemSemCB); | |
90 return(OS_ERROR); | |
91 } | |
92 if (NU_Reset_Semaphore(&SemTable[SemHandle].SemCB, init_counter) | |
93 != NU_SUCCESS) | |
94 goto error_out; | |
95 if (sts == NU_SUCCESS) | |
96 NU_Release_Semaphore(&SemSemCB); | |
97 return(OS_OK); | |
98 } | |
99 | |
100 GLOBAL LONG | |
101 os_QuerySemaphore(OS_HANDLE TaskHandle, OS_HANDLE SemHandle, USHORT *Count) | |
102 { | |
103 OPTION SuspendType; | |
104 UNSIGNED SemCount, TasksWaiting; | |
105 NU_TASK *First; | |
106 CHAR Name[NU_MAX_NAME]; | |
107 | |
108 if (NU_Semaphore_Information(&SemTable[SemHandle].SemCB, Name, | |
109 &SemCount, &SuspendType, &TasksWaiting, | |
110 &First) != NU_SUCCESS) | |
111 return(OS_ERROR); | |
112 *Count = SemCount; | |
113 return(OS_OK); | |
114 } | |
115 | |
116 GLOBAL LONG | |
117 os_OpenSemaphore(OS_HANDLE TaskHandle, char *Name, OS_HANDLE *SemHandle) | |
118 { | |
119 USHORT i; | |
120 | |
121 for (i = 1; i <= MaxSemaphores; i++) { | |
122 if (!SemTable[i].Name[0]) | |
123 continue; | |
124 if (strncmp(Name, SemTable[i].Name, RESOURCE_NAMELEN-1)) | |
125 continue; | |
126 *SemHandle = i; | |
127 return(OS_OK); | |
128 } | |
129 return(OS_ERROR); | |
130 } | |
131 | |
132 GLOBAL unsigned char * | |
133 os_FindSuspendingSema(unsigned int *tcb) | |
134 { | |
135 USHORT i; | |
136 SM_SUSPEND *susp, *susp_loopchk; | |
137 | |
138 for (i = 1; i <= MaxSemaphores; i++) { | |
139 if (!SemTable[i].Name[0]) | |
140 continue; | |
141 susp = SemTable[i].SemCB.sm_suspension_list; | |
142 if (!susp) | |
143 continue; | |
144 if (susp->sm_suspended_task == (NU_TASK *)tcb) | |
145 return(SemTable[i].SemCB.sm_name); | |
146 susp = (SM_SUSPEND *)susp->sm_suspend_link.cs_next; | |
147 for (susp_loopchk = susp; susp != susp_loopchk; | |
148 susp = (SM_SUSPEND *)susp->sm_suspend_link.cs_next) | |
149 if (susp->sm_suspended_task == (NU_TASK *)tcb) | |
150 return(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 } |