Mercurial > projects > ldc
comparison dmd2/root/async.c @ 1452:638d16625da2
LDC 2 compiles again.
author | Robert Clipsham <robert@octarineparrot.com> |
---|---|
date | Sat, 30 May 2009 17:23:32 +0100 |
parents | |
children | 54b3c1394d62 |
comparison
equal
deleted
inserted
replaced
1423:42bd767ec5a4 | 1452:638d16625da2 |
---|---|
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 unsigned threadaddr; | |
65 hThread = (HANDLE) _beginthreadex(NULL, | |
66 0, | |
67 &startthread, | |
68 this, | |
69 0, | |
70 (unsigned *)&threadaddr); | |
71 | |
72 if (hThread) | |
73 { | |
74 SetThreadPriority(hThread, THREAD_PRIORITY_HIGHEST); | |
75 } | |
76 else | |
77 { | |
78 assert(0); | |
79 } | |
80 } | |
81 | |
82 int AsyncRead::read(size_t i) | |
83 { | |
84 FileData *f = &files[i]; | |
85 WaitForSingleObject(f->event, INFINITE); | |
86 Sleep(0); // give up time slice | |
87 return f->result; | |
88 } | |
89 | |
90 void AsyncRead::dispose(AsyncRead *aw) | |
91 { | |
92 delete aw; | |
93 } | |
94 | |
95 | |
96 | |
97 unsigned __stdcall startthread(void *p) | |
98 { | |
99 AsyncRead *aw = (AsyncRead *)p; | |
100 | |
101 for (size_t i = 0; i < aw->filesdim; i++) | |
102 { FileData *f = &aw->files[i]; | |
103 | |
104 f->result = f->file->read(); | |
105 SetEvent(f->event); | |
106 } | |
107 _endthreadex(EXIT_SUCCESS); | |
108 return EXIT_SUCCESS; // if skidding | |
109 } | |
110 | |
111 #else | |
112 | |
113 #include <stdio.h> | |
114 #include <errno.h> | |
115 | |
116 #include "root.h" | |
117 #include "rmem.h" | |
118 | |
119 struct FileData | |
120 { | |
121 File *file; | |
122 int result; | |
123 //HANDLE event; | |
124 }; | |
125 | |
126 struct AsyncRead | |
127 { | |
128 static AsyncRead *create(size_t nfiles); | |
129 void addFile(File *file); | |
130 void start(); | |
131 int read(size_t i); | |
132 static void dispose(AsyncRead *); | |
133 | |
134 //HANDLE hThread; | |
135 | |
136 size_t filesdim; | |
137 size_t filesmax; | |
138 FileData files[1]; | |
139 }; | |
140 | |
141 | |
142 AsyncRead *AsyncRead::create(size_t nfiles) | |
143 { | |
144 AsyncRead *aw = (AsyncRead *)mem.calloc(1, sizeof(AsyncRead) + | |
145 (nfiles - 1) * sizeof(FileData)); | |
146 aw->filesmax = nfiles; | |
147 return aw; | |
148 } | |
149 | |
150 void AsyncRead::addFile(File *file) | |
151 { | |
152 //printf("addFile(file = %p)\n", file); | |
153 //printf("filesdim = %d, filesmax = %d\n", filesdim, filesmax); | |
154 assert(filesdim < filesmax); | |
155 files[filesdim].file = file; | |
156 //files[filesdim].event = CreateEvent(NULL, TRUE, FALSE, NULL); | |
157 //ResetEvent(files[filesdim].event); | |
158 filesdim++; | |
159 } | |
160 | |
161 void AsyncRead::start() | |
162 { | |
163 } | |
164 | |
165 int AsyncRead::read(size_t i) | |
166 { | |
167 FileData *f = &files[i]; | |
168 f->result = f->file->read(); | |
169 return f->result; | |
170 } | |
171 | |
172 void AsyncRead::dispose(AsyncRead *aw) | |
173 { | |
174 delete aw; | |
175 } | |
176 | |
177 #endif |