1587
|
1
|
|
2 #define _MT 1
|
|
3
|
|
4 #include <stdio.h>
|
|
5 #include <stdlib.h>
|
|
6 #include <assert.h>
|
|
7
|
|
8 #if _WIN32
|
|
9
|
|
10 #include <windows.h>
|
|
11 #include <stdio.h>
|
|
12 #include <errno.h>
|
|
13 #include <process.h>
|
|
14
|
|
15 #include "root.h"
|
|
16 #include "rmem.h"
|
|
17
|
|
18 static unsigned __stdcall startthread(void *p);
|
|
19
|
|
20 struct FileData
|
|
21 {
|
|
22 File *file;
|
|
23 int result;
|
|
24 HANDLE event;
|
|
25 };
|
|
26
|
|
27 struct AsyncRead
|
|
28 {
|
|
29 static AsyncRead *create(size_t nfiles);
|
|
30 void addFile(File *file);
|
|
31 void start();
|
|
32 int read(size_t i);
|
|
33 static void dispose(AsyncRead *);
|
|
34
|
|
35 HANDLE hThread;
|
|
36
|
|
37 size_t filesdim;
|
|
38 size_t filesmax;
|
|
39 FileData files[1];
|
|
40 };
|
|
41
|
|
42
|
|
43 AsyncRead *AsyncRead::create(size_t nfiles)
|
|
44 {
|
|
45 AsyncRead *aw = (AsyncRead *)mem.calloc(1, sizeof(AsyncRead) +
|
|
46 (nfiles - 1) * sizeof(FileData));
|
|
47 aw->filesmax = nfiles;
|
|
48 return aw;
|
|
49 }
|
|
50
|
|
51 void AsyncRead::addFile(File *file)
|
|
52 {
|
|
53 //printf("addFile(file = %p)\n", file);
|
|
54 //printf("filesdim = %d, filesmax = %d\n", filesdim, filesmax);
|
|
55 assert(filesdim < filesmax);
|
|
56 files[filesdim].file = file;
|
|
57 files[filesdim].event = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|
58 ResetEvent(files[filesdim].event);
|
|
59 filesdim++;
|
|
60 }
|
|
61
|
|
62 void AsyncRead::start()
|
|
63 {
|
|
64 //printf("aw->filesdim = %p %d\n", this, filesdim);
|
|
65 if (filesdim)
|
|
66 {
|
|
67 unsigned threadaddr;
|
|
68 hThread = (HANDLE) _beginthreadex(NULL,
|
|
69 0,
|
|
70 &startthread,
|
|
71 this,
|
|
72 0,
|
|
73 (unsigned *)&threadaddr);
|
|
74
|
|
75 if (hThread)
|
|
76 {
|
|
77 SetThreadPriority(hThread, THREAD_PRIORITY_HIGHEST);
|
|
78 }
|
|
79 else
|
|
80 {
|
|
81 assert(0);
|
|
82 }
|
|
83 }
|
|
84 }
|
|
85
|
|
86 int AsyncRead::read(size_t i)
|
|
87 {
|
|
88 FileData *f = &files[i];
|
|
89 WaitForSingleObject(f->event, INFINITE);
|
|
90 Sleep(0); // give up time slice
|
|
91 return f->result;
|
|
92 }
|
|
93
|
|
94 void AsyncRead::dispose(AsyncRead *aw)
|
|
95 {
|
|
96 delete aw;
|
|
97 }
|
|
98
|
|
99
|
|
100
|
|
101 unsigned __stdcall startthread(void *p)
|
|
102 {
|
|
103 AsyncRead *aw = (AsyncRead *)p;
|
|
104
|
|
105 //printf("aw->filesdim = %p %d\n", aw, aw->filesdim);
|
|
106 for (size_t i = 0; i < aw->filesdim; i++)
|
|
107 { FileData *f = &aw->files[i];
|
|
108
|
|
109 f->result = f->file->read();
|
|
110 SetEvent(f->event);
|
|
111 }
|
|
112 _endthreadex(EXIT_SUCCESS);
|
|
113 return EXIT_SUCCESS; // if skidding
|
|
114 }
|
|
115
|
|
116 #else
|
|
117
|
|
118 #include <stdio.h>
|
|
119 #include <errno.h>
|
|
120
|
|
121 #include "root.h"
|
|
122 #include "rmem.h"
|
|
123
|
|
124 struct FileData
|
|
125 {
|
|
126 File *file;
|
|
127 int result;
|
|
128 //HANDLE event;
|
|
129 };
|
|
130
|
|
131 struct AsyncRead
|
|
132 {
|
|
133 static AsyncRead *create(size_t nfiles);
|
|
134 void addFile(File *file);
|
|
135 void start();
|
|
136 int read(size_t i);
|
|
137 static void dispose(AsyncRead *);
|
|
138
|
|
139 //HANDLE hThread;
|
|
140
|
|
141 size_t filesdim;
|
|
142 size_t filesmax;
|
|
143 FileData files[1];
|
|
144 };
|
|
145
|
|
146
|
|
147 AsyncRead *AsyncRead::create(size_t nfiles)
|
|
148 {
|
|
149 AsyncRead *aw = (AsyncRead *)mem.calloc(1, sizeof(AsyncRead) +
|
|
150 (nfiles - 1) * sizeof(FileData));
|
|
151 aw->filesmax = nfiles;
|
|
152 return aw;
|
|
153 }
|
|
154
|
|
155 void AsyncRead::addFile(File *file)
|
|
156 {
|
|
157 //printf("addFile(file = %p)\n", file);
|
|
158 //printf("filesdim = %d, filesmax = %d\n", filesdim, filesmax);
|
|
159 assert(filesdim < filesmax);
|
|
160 files[filesdim].file = file;
|
|
161 //files[filesdim].event = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|
162 //ResetEvent(files[filesdim].event);
|
|
163 filesdim++;
|
|
164 }
|
|
165
|
|
166 void AsyncRead::start()
|
|
167 {
|
|
168 }
|
|
169
|
|
170 int AsyncRead::read(size_t i)
|
|
171 {
|
|
172 FileData *f = &files[i];
|
|
173 f->result = f->file->read();
|
|
174 return f->result;
|
|
175 }
|
|
176
|
|
177 void AsyncRead::dispose(AsyncRead *aw)
|
|
178 {
|
|
179 delete aw;
|
|
180 }
|
|
181
|
|
182 #endif
|