-
Notifications
You must be signed in to change notification settings - Fork 25
/
thd_iin.c
268 lines (224 loc) · 7.54 KB
/
thd_iin.c
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
/*
* thd_iin.c
* $Id: thd_iin.c,v 1.1 2006/09/01 17:37:58 bobi Exp $
*
* Copyright 2004 Bobi B., w1zard0f07@yahoo.com
*
* This file is part of hdl_dump.
*
* hdl_dump is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* hdl_dump is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with hdl_dump; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#if defined(_BUILD_WIN32)
#include <windows.h>
#include "sema.h" /* POSIX semaphores for win32 */
#elif defined(__MACH__)
#include "sema.h" /* POSIX semaphores for macOS */
#else
#include <pthread.h>
#include <semaphore.h>
#endif
#include <stdio.h>
#include <string.h>
#include "iin.h"
#include "osal.h"
#include "retcodes.h"
typedef struct buffer_type
{
/* input */
u_int32_t start_sector, num_sectors;
/* output */
int result;
char buf[IIN_SECTOR_SIZE * IIN_NUM_SECTORS];
u_int32_t bytes;
} buffer_t;
typedef struct job_type
{
u_int32_t start_sector, num_sectors;
buffer_t *dest;
} job_t;
typedef struct threaded_decorator_type
{
iin_t iin;
iin_t *worker;
u_int32_t total_sectors;
job_t job;
/* both are exclusive => at any moment only one is acquirable */
sem_t worker_lock, master_lock;
buffer_t buf[2];
size_t active_buf; /* <= % 2 == index of the current buffer */
volatile int done;
} threaded_decorator_t;
/**************************************************************/
#if defined(_BUILD_WIN32)
static DWORD WINAPI
thd_loop(void *p)
#else
static void *
thd_loop(void *p)
#endif
{
threaded_decorator_t *thd = (threaded_decorator_t *)p;
do {
int result = sem_wait(&thd->worker_lock);
if (result == 0 && !thd->done) {
job_t *job = &thd->job;
buffer_t *dest = job->dest;
const char *data = NULL;
/* execute queued read and copy data */
dest->result = thd->worker->read(thd->worker, job->start_sector,
job->num_sectors,
&data, &dest->bytes);
if (dest->result == RET_OK)
memcpy(dest->buf, data, dest->bytes);
/* complete */
dest->start_sector = job->start_sector;
dest->num_sectors = job->num_sectors;
/* unlock master */
sem_post(&thd->master_lock);
}
} while (!thd->done);
/* notify master */
sem_post(&thd->master_lock);
return (0);
}
/**************************************************************/
static int
thd_read(iin_t *iin,
u_int32_t start_sector,
u_int32_t num_sectors,
/*@out@*/ const char **data,
/*@out@*/ u_int32_t *length)
{
threaded_decorator_t *thd = (threaded_decorator_t *)iin;
int result;
do {
result = sem_wait(&thd->master_lock);
if (result == 0) { /* worker is idle */
const buffer_t *buf = thd->buf + (thd->active_buf % 2);
if (start_sector == buf->start_sector &&
num_sectors == buf->num_sectors) { /* data is currently available */
*data = buf->buf;
*length = buf->bytes;
result = buf->result;
break;
} else { /* data not available; schedule and wait untill its ready */
thd->job.start_sector = start_sector;
thd->job.num_sectors = num_sectors;
thd->job.dest = thd->buf + (thd->active_buf % 2);
sem_post(&thd->worker_lock);
}
}
} while (1);
if (result == RET_OK &&
num_sectors == IIN_NUM_SECTORS &&
start_sector + num_sectors < thd->total_sectors) { /* schedule pre-fetch for the next group of sectors */
thd->job.start_sector = start_sector + num_sectors;
thd->job.num_sectors = IIN_NUM_SECTORS;
thd->job.dest = thd->buf + (++thd->active_buf % 2);
sem_post(&thd->worker_lock);
} else
/* since there is no pre-fetch scheduled,
unlock master or the next read call would block forever */
sem_post(&thd->master_lock);
return (result);
}
/**************************************************************/
static int
thd_stat(iin_t *iin,
/*@out@*/ u_int32_t *sector_size,
/*@out@*/ u_int32_t *num_sectors)
{
threaded_decorator_t *thd = (threaded_decorator_t *)iin;
*sector_size = IIN_SECTOR_SIZE;
*num_sectors = thd->total_sectors;
return (RET_OK);
}
/**************************************************************/
static int
thd_close(iin_t *iin)
{
threaded_decorator_t *thd = (threaded_decorator_t *)iin;
int result;
/* wait for worker to finish... */
sem_wait(&thd->master_lock);
thd->done = 1;
sem_post(&thd->worker_lock);
sem_wait(&thd->master_lock);
/* clean-up */
sem_destroy(&thd->worker_lock);
sem_destroy(&thd->master_lock);
result = thd->worker->close(thd->worker);
thd->worker = NULL;
osal_free(thd), thd = NULL;
return (result);
}
/**************************************************************/
static char *
thd_last_error(iin_t *iin)
{
threaded_decorator_t *thd = (threaded_decorator_t *)iin;
return (thd->worker->last_error(thd->worker));
}
/**************************************************************/
static void
thd_dispose_error(iin_t *iin,
/*@only@*/ char *error)
{
threaded_decorator_t *thd = (threaded_decorator_t *)iin;
thd->worker->dispose_error(thd->worker, error);
}
/**************************************************************/
iin_t *
thd_create(iin_t *worker)
{
u_int32_t total_sectors, sector_size;
threaded_decorator_t *thd;
if (worker->stat(worker, §or_size, &total_sectors) != RET_OK)
return (NULL);
thd = (threaded_decorator_t *)osal_alloc(sizeof(threaded_decorator_t));
if (thd != NULL) {
memset(thd, 0, sizeof(threaded_decorator_t));
thd->iin.stat = &thd_stat;
thd->iin.read = &thd_read;
thd->iin.close = &thd_close;
/* TODO: preserve errors? */
thd->iin.last_error = &thd_last_error;
thd->iin.dispose_error = &thd_dispose_error;
strcpy(thd->iin.source_type, "Threaded decorator");
thd->worker = worker;
thd->total_sectors = total_sectors;
thd->active_buf = 0;
thd->done = 0;
/* master is unlocked, worker is locked; would run on 1st need */
if (sem_init(&thd->worker_lock, 0, 0) == 0) {
if (sem_init(&thd->master_lock, 0, 1) == 0) {
#if defined(_BUILD_WIN32)
DWORD thread_id = 0;
HANDLE h = CreateThread(NULL, 0, &thd_loop, thd, 0, &thread_id);
if (h != NULL)
return ((iin_t *)thd); /* win32: success */
#else
pthread_t thread_id = 0;
if (pthread_create(&thread_id, NULL, &thd_loop, thd) == 0)
return ((iin_t *)thd); /* unix: success */
#endif
(void)sem_destroy(&thd->master_lock);
}
(void)sem_destroy(&thd->worker_lock);
}
osal_free(thd), thd = NULL;
}
return (NULL);
}