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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
|
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* BSD 3 Clause License
*
* Copyright (c) 2007, The Storage Networking Industry Association.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* - Neither the name of The Storage Networking Industry Association (SNIA)
* nor the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/errno.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <synch.h>
#include <tlm_buffers.h>
#include <tlm.h>
#include "tlm_proto.h"
/*
* tlm_allocate_buffers
*
* build a set of buffers
*/
tlm_buffers_t *
tlm_allocate_buffers(boolean_t write, long xfer_size)
{
tlm_buffers_t *buffers = ndmp_malloc(sizeof (tlm_buffers_t));
int buf;
if (buffers == 0)
return (0);
for (buf = 0; buf < TLM_TAPE_BUFFERS; buf++) {
buffers->tbs_buffer[buf].tb_buffer_data =
ndmp_malloc(xfer_size);
if (buffers->tbs_buffer[buf].tb_buffer_data == 0) {
int i;
/* Memory allocation failed. Give everything back */
for (i = 0; i < buf; i++)
free(buffers->tbs_buffer[i].tb_buffer_data);
free(buffers);
return (0);
} else {
buffers->tbs_buffer[buf].tb_buffer_size = (write)
? xfer_size : 0;
buffers->tbs_buffer[buf].tb_full = FALSE;
buffers->tbs_buffer[buf].tb_eof = FALSE;
buffers->tbs_buffer[buf].tb_eot = FALSE;
buffers->tbs_buffer[buf].tb_errno = 0;
buffers->tbs_buffer[buf].tb_buffer_spot = 0;
}
}
(void) mutex_init(&buffers->tbs_mtx, 0, NULL);
(void) cond_init(&buffers->tbs_in_cv, 0, NULL);
(void) cond_init(&buffers->tbs_out_cv, 0, NULL);
buffers->tbs_data_transfer_size = xfer_size;
buffers->tbs_ref = 1;
return (buffers);
}
/*
* tlm_release_buffers
*
* give all memory back to the OS
*/
void
tlm_release_buffers(tlm_buffers_t *buffers)
{
int i;
if (buffers != NULL) {
tlm_buffer_release_in_buf(buffers);
tlm_buffer_release_out_buf(buffers);
(void) mutex_lock(&buffers->tbs_mtx);
if (--buffers->tbs_ref <= 0) {
for (i = 0; i < TLM_TAPE_BUFFERS; i++)
free(buffers->tbs_buffer[i].tb_buffer_data);
}
(void) cond_destroy(&buffers->tbs_in_cv);
(void) cond_destroy(&buffers->tbs_out_cv);
(void) mutex_unlock(&buffers->tbs_mtx);
(void) mutex_destroy(&buffers->tbs_mtx);
free(buffers);
}
}
/*
* tlm_buffer_mark_empty
*
* Mark a buffer empty and clear its flags. No lock is take here:
* the buffer should be marked empty before it is released for use
* by another thread.
*/
void
tlm_buffer_mark_empty(tlm_buffer_t *buf)
{
if (buf == NULL)
return;
buf->tb_full = buf->tb_eof = buf->tb_eot = FALSE;
buf->tb_errno = 0;
}
/*
* tlm_buffer_advance_in_idx
*
* Advance the input index of the buffers(round-robin) and return pointer
* to the next buffer in the buffer pool.
*/
tlm_buffer_t *
tlm_buffer_advance_in_idx(tlm_buffers_t *bufs)
{
if (bufs == NULL)
return (NULL);
(void) mutex_lock(&bufs->tbs_mtx);
if (++bufs->tbs_buffer_in >= TLM_TAPE_BUFFERS)
bufs->tbs_buffer_in = 0;
(void) mutex_unlock(&bufs->tbs_mtx);
return (&bufs->tbs_buffer[bufs->tbs_buffer_in]);
}
/*
* tlm_buffer_advance_out_idx
*
* Advance the output index of the buffers(round-robin) and return pointer
* to the next buffer in the buffer pool.
*/
tlm_buffer_t *
tlm_buffer_advance_out_idx(tlm_buffers_t *bufs)
{
if (bufs == NULL)
return (NULL);
(void) mutex_lock(&bufs->tbs_mtx);
if (++bufs->tbs_buffer_out >= TLM_TAPE_BUFFERS)
bufs->tbs_buffer_out = 0;
(void) mutex_unlock(&bufs->tbs_mtx);
return (&bufs->tbs_buffer[bufs->tbs_buffer_out]);
}
/*
* tlm_buffer_in_buf
*
* Return pointer to the next buffer in the buffer pool.
*/
tlm_buffer_t *
tlm_buffer_in_buf(tlm_buffers_t *bufs, int *idx)
{
tlm_buffer_t *ret;
if (bufs == NULL)
return (NULL);
(void) mutex_lock(&bufs->tbs_mtx);
ret = &bufs->tbs_buffer[bufs->tbs_buffer_in];
if (idx)
*idx = bufs->tbs_buffer_in;
(void) mutex_unlock(&bufs->tbs_mtx);
return (ret);
}
/*
* tlm_buffer_out_buf
*
* Return pointer to the next buffer in the buffer pool.
*/
tlm_buffer_t *
tlm_buffer_out_buf(tlm_buffers_t *bufs, int *idx)
{
tlm_buffer_t *ret;
if (bufs == NULL)
return (NULL);
(void) mutex_lock(&bufs->tbs_mtx);
ret = &bufs->tbs_buffer[bufs->tbs_buffer_out];
if (idx)
*idx = bufs->tbs_buffer_out;
(void) mutex_unlock(&bufs->tbs_mtx);
return (ret);
}
/*
* tlm_buffer_release_in_buf
*
* Another buffer is filled. Wake up the consumer if it's waiting for it.
*/
void
tlm_buffer_release_in_buf(tlm_buffers_t *bufs)
{
(void) mutex_lock(&bufs->tbs_mtx);
bufs->tbs_flags |= TLM_BUF_IN_READY;
(void) cond_signal(&bufs->tbs_in_cv);
(void) mutex_unlock(&bufs->tbs_mtx);
}
/*
* tlm_buffer_release_out_buf
*
* A buffer is used. Wake up the producer to re-fill a buffer if it's waiting
* for the buffer to be used.
*/
void
tlm_buffer_release_out_buf(tlm_buffers_t *bufs)
{
(void) mutex_lock(&bufs->tbs_mtx);
bufs->tbs_flags |= TLM_BUF_OUT_READY;
(void) cond_signal(&bufs->tbs_out_cv);
(void) mutex_unlock(&bufs->tbs_mtx);
}
/*
* tlm_buffer_in_buf_wait
*
* Wait for the input buffer to get available.
*/
void
tlm_buffer_in_buf_wait(tlm_buffers_t *bufs)
{
(void) mutex_lock(&bufs->tbs_mtx);
while ((bufs->tbs_flags & TLM_BUF_IN_READY) == 0)
(void) cond_wait(&bufs->tbs_in_cv, &bufs->tbs_mtx);
bufs->tbs_flags &= ~TLM_BUF_IN_READY;
(void) mutex_unlock(&bufs->tbs_mtx);
}
/*
* tlm_buffer_setup_timer
*
* Set up the time out value.
*/
static inline void
tlm_buffer_setup_timer(timestruc_t *timo, unsigned milli_timo)
{
if (milli_timo == 0)
milli_timo = 1;
if (milli_timo / 1000)
timo->tv_sec = (milli_timo / 1000);
else
timo->tv_sec = 0;
timo->tv_nsec = (milli_timo % 1000) * 1000000L;
}
/*
* tlm_buffer_in_buf_timed_wait
*
* Wait for the input buffer to get ready with a time out.
*/
void
tlm_buffer_in_buf_timed_wait(tlm_buffers_t *bufs, unsigned int milli_timo)
{
timestruc_t timo;
tlm_buffer_setup_timer(&timo, milli_timo);
(void) mutex_lock(&bufs->tbs_mtx);
(void) cond_reltimedwait(&bufs->tbs_in_cv, &bufs->tbs_mtx, &timo);
/*
* TLM_BUF_IN_READY doesn't matter for timedwait but clear
* it here so that cond_wait doesn't get the wrong result.
*/
bufs->tbs_flags &= ~TLM_BUF_IN_READY;
(void) mutex_unlock(&bufs->tbs_mtx);
}
/*
* tlm_buffer_out_buf_timed_wait
*
* Wait for the output buffer to get ready with a time out.
*/
void
tlm_buffer_out_buf_timed_wait(tlm_buffers_t *bufs, unsigned int milli_timo)
{
timestruc_t timo;
tlm_buffer_setup_timer(&timo, milli_timo);
(void) mutex_lock(&bufs->tbs_mtx);
(void) cond_reltimedwait(&bufs->tbs_out_cv, &bufs->tbs_mtx, &timo);
/*
* TLM_BUF_OUT_READY doesn't matter for timedwait but clear
* it here so that cond_wait doesn't get the wrong result.
*/
bufs->tbs_flags &= ~TLM_BUF_OUT_READY;
(void) mutex_unlock(&bufs->tbs_mtx);
}
/*
* tlm_cmd_wait
*
* TLM command synchronization typically use by command
* parent threads to wait for launched threads to initialize.
*/
void
tlm_cmd_wait(tlm_cmd_t *cmd, uint32_t event_type)
{
(void) mutex_lock(&cmd->tc_mtx);
while ((cmd->tc_flags & event_type) == 0)
(void) cond_wait(&cmd->tc_cv, &cmd->tc_mtx);
cmd->tc_flags &= ~event_type;
(void) mutex_unlock(&cmd->tc_mtx);
}
/*
* tlm_cmd_signal
*
* TLM command synchronization typically use by launched threads
* to unleash the parent thread.
*/
void
tlm_cmd_signal(tlm_cmd_t *cmd, uint32_t event_type)
{
(void) mutex_lock(&cmd->tc_mtx);
cmd->tc_flags |= event_type;
(void) cond_signal(&cmd->tc_cv);
(void) mutex_unlock(&cmd->tc_mtx);
}
|