Subversion Repositories shark

Rev

Rev 1655 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1655 giacomo 1
/*
2
 *
3
 *
4
 *
5
 */
6
 
7
#include <kernel/func.h>
8
#include <kernel/model.h>
9
 
10
#include <sys/types.h>
11
#include <sys/stat.h>
12
#include <sys/mount.h>
13
#include <dirent.h>
14
#include <fcntl.h>
15
#include <unistd.h>
16
#include <errno.h>
17
#include <string.h>
18
#include <stdlib.h>
19
#include <semaphore.h>
20
#include <stdio.h>
21
 
22
#include "common.h"
23
 
24
#define NOSHOW
25
#undef NOSHOW
26
 
27
#define sem_signal sem_post
28
 
29
int noscroll=0;
30
sem_t console;
31
 
32
#define MPROC    ((50<(MAX_PROC-10))?50:MAX_PROC-10)
33
//#define MWCET    1000l
34
//#define MPERIOD  (MWCET*(MPROC+10))
35
 
36
TASK viewdir(void *prof);
37
sem_t actmutex,actsync;
38
char *globpathname;
39
PID globpid;
40
int counter=0,actcounter=0;
41
 
42
void activate_task(int prof, char *pathname)
43
{
44
  char tname[32];
45
  NRT_TASK_MODEL m;
46
  PID pid;
47
 
48
REPEAT:
49
  sem_wait(&actmutex);
50
 
51
  if (actcounter>=MPROC) {
52
    sem_signal(&actmutex);
53
    task_delay(10000);
54
    goto REPEAT;
55
  }
56
 
57
  globpathname=pathname;
58
  counter++;
59
  sprintf(tname,"tsk%i",counter);
60
 
61
  /*
62
  printd("Û");
63
  sem_wait(&console);
64
  printd("Ü");
65
  cprintf("%03i has called for ##%s##\n",prof,pathname);
66
  sem_signal(&console);
67
  */
68
 
69
  nrt_task_default_model(m);
70
  //soft_task_def_met(m,MWCET);
71
  //soft_task_def_wcet(m,MWCET);
72
  //soft_task_def_periodic(m);
73
  nrt_task_def_arg(m,(void*)counter);
74
  //soft_task_def_period(m,MPERIOD);
75
 
76
  globpid=pid=task_create(tname,viewdir,&m,NULL);
77
  if (pid==-1) {
78
    sem_wait(&console);
79
    cprintf("can't create '%s'\n",tname);
80
    perror("can't create task");
81
    sem_signal(&console);
82
    sys_end();
83
    return;
84
  }
85
  task_activate(pid);
86
  sem_wait(&actsync);
87
  actcounter++;
88
 
89
  /*
90
  printd("Û");
91
  sem_wait(&console);
92
  printd("Ü");
93
  if (noscroll) place(0,9);
94
  cprintf("task running: %5i/%5i --- task started: %5i\n",
95
          actcounter,MAX_PROC,counter);
96
  sem_signal(&console);
97
  */
98
 
99
  sem_signal(&actmutex);
100
}
101
 
102
/*
103
 *
104
 */
105
 
106
int filecounter=0;
107
 
108
TASK viewdir(void *pointer)
109
{
110
  struct dirent *den;
111
  struct stat st;
112
  char *str;
113
  DIR *d;
114
  int res;
115
  int x;
116
  char pathname[1024];
117
  PID mypid;
118
  int prof=(int)pointer;
119
 
120
  strcpy(pathname,globpathname);
121
  mypid=globpid;
122
  sem_signal(&actsync);  
123
 
124
  //sem_wait(&console);
125
  //cprintf("%03i on <%s>\n",prof,pathname);
126
  //sem_signal(&console);
127
 
128
  str=pathname+(x=strlen(pathname));
129
  d=opendir(pathname);
130
 
131
  if (d==NULL) {
132
    sem_wait(&console);
133
    cprintf("%03i ERR: can't open dir %s\n",prof,pathname);
134
    cprintf("errno: %i '%s'\n",errno,strerror(errno));
135
    sem_signal(&console);
136
 
137
    sys_end();
138
    l1_exit(0);
139
 
140
    goto END;
141
  }
142
 
143
  while ((den=readdir(d))!=NULL) {
144
 
145
    if (x==1&&*pathname=='/')
146
      strcat(pathname,den->d_name);
147
    else
148
      strcat(strcat(pathname,"/"),den->d_name);
149
 
150
    sem_wait(&console);
151
#ifndef NOSHOW
152
    if (noscroll) {
153
      place(0,10);
154
      cprintf("                                                           ");
155
      place(0,10);
156
    }
157
    //cprintf("%03i(0_%03i) %s\n",prof,(int)mypid,pathname);
158
    cprintf("t%03i %s\n",prof,pathname);
159
#endif
160
    filecounter++;
161
    sem_signal(&console);
162
 
163
    //if (*den->d_name=='.') {
164
      //  sem_wait(console,BLOCK);
165
      //  cprintf("%03i HAS FOUND <%s>\n",prof,den->d_name);
166
      //  sem_signal(console);
167
      //}
168
 
169
    if (!strcmp(den->d_name,".")) {
170
      //sem_wait(console,BLOCK);
171
      //cprintf("%03i skip for .\n",prof);
172
      //sem_signal(console);
173
      goto SKIP;
174
    }
175
    if (!strcmp(den->d_name,"..")) {
176
      //sem_wait(console,BLOCK);
177
      //cprintf("%03i skip for ..\n",prof);
178
      //sem_signal(console);
179
      goto SKIP;
180
    }
181
 
182
    res=stat(pathname,&st);
183
    if (res!=0) {
184
      sem_wait(&console);
185
      cprintf("t%03i can't stat %s\n",prof,pathname);
186
      cprintf("errno: %i '%s'\n",errno,strerror(errno));
187
      sem_signal(&console);
188
 
189
      sys_end();
190
      l1_exit(0);
191
 
192
      closedir(d);
193
      goto END;
194
    } else {          
195
      if (S_ISDIR(st.st_mode)) {
196
        sem_wait(&console);
197
        //if (den->d_name[0]=='.')
198
          //  cprintf("%03i called for <%s>\n",prof,den->d_name);
199
        sem_signal(&console);
200
        activate_task(prof,pathname);
201
      }
202
 
203
    }
204
 
205
  SKIP:
206
    *str='\0';
207
  }
208
 
209
  closedir(d);
210
 
211
END:
212
  sem_wait(&actmutex);
213
  actcounter--;
214
  sem_signal(&actmutex);
215
 
216
  return 0;
217
}
218
 
219
int main(int argc,char *argv[])
220
{
221
  extern void dump_sem_table(void);
222
  TIME stime,etime;
223
  //  int res;
224
 
225
  /*
226
  temp_device=bdev_scan_devices(choose_temp_callback);
227
  if (temp_device<0) {
228
    cprintf("can't find a filesystem to mount on /TEMP!!!\n");
229
    return -1;
230
  }
231
  */
232
 
233
  /* mounting 2nd msdos partition on /TEMP */
234
  /*
235
  res=mount(temp_device,FS_MSDOS,"/TEMP",NULL);
236
  if (res!=0) {
237
    cprintf("can't mount XXX on /TEMP\n");
238
    cprintf("errno: %i '%s'\n",errno,strerror(errno));
239
    return -1;
240
  }
241
  */
242
 
243
  /* clear screen */
244
  //clear();
245
 
246
  showmessage("This test show all filenames of a directory of an hardisk\n"
247
              "recursively using a soft task for every directory.\n");
248
 
249
  stime=sys_gettime(NULL);
250
 
251
  sem_init(&console,0,1);
252
  sem_init(&actmutex,0,1);
253
  sem_init(&actsync,0,0);
254
 
255
  //dump_sem_table();
256
 
257
  //keyb_getchar();
258
 
259
  activate_task(-1,"/TEMP");
260
 
261
  for(;;) {
262
    sem_wait(&actmutex);
263
    if (actcounter==0) break;
264
    sem_signal(&actmutex);
265
  }
266
 
267
  etime=sys_gettime(NULL);
268
 
269
  cprintf("\nfiles: %i\n",filecounter);
270
  cprintf("time   : %li\n",etime-stime);
271
 
272
  waitend();
273
 
274
  return 0;
275
}