-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsfs.c
More file actions
312 lines (263 loc) · 6.3 KB
/
sfs.c
File metadata and controls
312 lines (263 loc) · 6.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/************************************************
Simple Function Scheduler
$Id$
*************************************************/
/*
#include <stdio.h>
#define dbg_printf(s) printf(s);
*/
#define dbg_printf(s) /**/
#include "sfs.h"
#define HEAD 0
#define TAIL 7
#define BODY (TAIL+1)
#define SFS_NULL ((struct SFS_tg *)0)
/*-------------------- public function --------------------*/
short SFS_initialize(void);
short SFS_dispatch(void);
short SFS_fork(char *,short,void (*)());
void *SFS_work(void);
void *SFS_otherWork(char *);
short SFS_kill(void);
short SFS_change(char *,short,void (*)());
/*-------------------- static function & variable --------------------*/
static struct SFS_tg SFS[BODY];
static struct SFS_tg *pTask;
static struct SFS_tg *pPool;
static struct SFS_tg *exe;
static void none(void){ return; }
static struct SFS_tg * SFS_obtain(void);
static void SFS_regist(struct SFS_tg *);
static void SFS_giveup(void);
static struct SFS_tg * SFS_find(char *);
/*------------------------------*/
static char *strncpy(char *,char *,unsigned int);
static int strcnt(char *);
static int strcmp(char *,char *);
/*-------------------- public function define --------------------*/
short SFS_initialize(void)
{
short iLoop = 1;
SFS[HEAD].name[0] = 0x00;
SFS[HEAD].order = 0x0000;
SFS[HEAD].pFront = SFS_NULL;
SFS[HEAD].pBack = &SFS[iLoop];
SFS[HEAD].pFunction = none;
while(iLoop<TAIL){
SFS[iLoop].name[0] = 0x00;
SFS[iLoop].order = 0x0000;
SFS[iLoop].pFront = &SFS[iLoop-1];
SFS[iLoop].pBack = &SFS[iLoop+1];
SFS[iLoop].pFunction = none;
iLoop++;
}
SFS[iLoop].name[0] = 0x00;
SFS[iLoop].order = 0x0000;
SFS[iLoop].pFront = &SFS[iLoop-1];
SFS[iLoop].pBack = SFS_NULL;
SFS[iLoop].pFunction = none;
pPool = &SFS[HEAD];
pTask = SFS_NULL;
return 0;
}
short SFS_dispatch(void)
{
short tcnt=0;
static struct SFS_tg * next;
exe = pTask;
while(exe!=SFS_NULL){
next = exe->pBack;
(*exe->pFunction)();
exe = next;
tcnt++;
}
exe = SFS_NULL;
return tcnt;
}
short SFS_fork(char *name,short order,void (*func)())
{
struct SFS_tg * sfs;
short retf = 0;
sfs = SFS_obtain();
if(sfs!=SFS_NULL){
strncpy(sfs->name,name,SFS_NAME_SIZE);
sfs->order = order;
sfs->pFunction = func;
SFS_regist(sfs);
retf = -1;
}
dbg_printf(name);
dbg_printf(" fork !\n");
return retf;
}
void *SFS_work(void)
{
return (void *)exe->work;
}
void *SFS_otherWork(char *name)
{
struct SFS_tg * sfs;
sfs = SFS_find(name);
if(sfs==SFS_NULL)
return (void *)0;
return (void *)sfs->work;
}
short SFS_kill(void)
{
if(exe!=SFS_NULL){
exe->pFunction = SFS_giveup;
}
dbg_printf("kill !\n");
return 0;
}
short SFS_change(char *name,short order,void (*func)())
{
if(exe!=SFS_NULL){
strncpy(exe->name,name,SFS_NAME_SIZE);
exe->order = order;
exe->pFunction = func;
}
dbg_printf("change !!\n");
return 0;
}
/*-------------------- static functions --------------------*/
static struct SFS_tg * SFS_obtain(void)
{
struct SFS_tg * sfs;
if(pPool==SFS_NULL)
return SFS_NULL;
sfs = pPool;
pPool = pPool->pBack;
return sfs;
}
static void SFS_regist(struct SFS_tg *sfs)
{
struct SFS_tg * entry;
short order = sfs->order;
/* short flag = 1; */
if(pTask==SFS_NULL){
pTask = sfs;
sfs->pBack = SFS_NULL;
}else{
entry = pTask;
while(entry->pBack!=SFS_NULL){
if(order < entry->order){
break;
}else if(order >= entry->order){
if(order < entry->pBack->order){
break;
}
}
entry = entry->pBack;
}
if(order < entry->order){ /* befor.. */
if(entry->pFront == SFS_NULL){ /* inserts in the head. */
pTask = sfs;
sfs->pFront = SFS_NULL;
sfs->pBack = entry;
entry->pFront = sfs;
}else{ /* inserts in the entry point front of a main part. */
sfs->pFront = entry->pFront;
sfs->pBack = entry;
entry->pFront = sfs;
}
}else{ /* to back.. */
if(entry->pBack == SFS_NULL){ /* inserts in a tail. */
entry->pBack = sfs;
sfs->pBack = SFS_NULL;
}else{ /* inserts behind the entry point of a main part. */
sfs->pBack = entry->pBack;
sfs->pFront = entry;
entry->pBack = sfs;
}
}
}
}
static void SFS_release(struct SFS_tg *sfs)
{
struct SFS_tg * entry;
if(pPool==SFS_NULL){
pPool = sfs;
sfs->pFront = SFS_NULL;
sfs->pBack = SFS_NULL;
}else{
entry = pPool;
while(entry->pBack!=SFS_NULL){
entry = entry->pBack;
}
entry->pBack = sfs;
sfs->pBack = SFS_NULL;
}
}
static void SFS_giveup(void)
{
struct SFS_tg *front_sfs = exe->pFront;
struct SFS_tg *back_sfs = exe->pBack;
if(front_sfs==SFS_NULL){
if(back_sfs==SFS_NULL){
pTask = SFS_NULL;
}else{
back_sfs->pFront = SFS_NULL;
pTask = back_sfs;
}
SFS_release(exe);
}else if(back_sfs==SFS_NULL){
front_sfs->pBack = SFS_NULL;
SFS_release(exe);
}else{
front_sfs->pBack = back_sfs;
back_sfs->pFront = front_sfs;
SFS_release(exe);
}
dbg_printf("give up !\n");
}
static struct SFS_tg * SFS_find(char *name)
{
struct SFS_tg * sfs;
if(pTask==SFS_NULL)
return SFS_NULL;
sfs = pTask;
while(sfs!=SFS_NULL){
if(!strcmp(sfs->name,name))
break;
sfs = sfs->pBack;
}
return sfs;
}
/*------------------------------*/
static char *strncpy(char *s1,char *s2,unsigned int n)
{
char *_s1=s1;
unsigned int _n;
if( s1 < s2 ){
while( n-- && *s2 )
*s1++ = *s2++;
*s1 = 0;
}else{
_n = strcnt(s2);
n = _n < n ? _n:n;
s2 += n;
s1 += n;
*(s1) = 0;
while( n-- )
*--s1 = *--s2;
}
return _s1;
}
static int strcnt(char *s)
{
int cnt;
for(cnt=0;*(s+cnt);cnt++);
return cnt;
}
static int strcmp(char *s1,char *s2)
{
int retf;
while( *s1 && *s1 == *s2 ){
s1++;
s2++;
}
retf = *s1 - *s2;
return retf;
}
/* ----[EOF]---- */