1 | /* |
---|
2 | * This file is part of libtrace |
---|
3 | * |
---|
4 | * Copyright (c) 2007,2008,2009,2010 The University of Waikato, Hamilton, |
---|
5 | * New Zealand. |
---|
6 | * |
---|
7 | * Authors: Perry Lorier |
---|
8 | * Shane Alcock |
---|
9 | * |
---|
10 | * All rights reserved. |
---|
11 | * |
---|
12 | * This code has been developed by the University of Waikato WAND |
---|
13 | * research group. For further information please see http://www.wand.net.nz/ |
---|
14 | * |
---|
15 | * libtrace is free software; you can redistribute it and/or modify |
---|
16 | * it under the terms of the GNU General Public License as published by |
---|
17 | * the Free Software Foundation; either version 2 of the License, or |
---|
18 | * (at your option) any later version. |
---|
19 | * |
---|
20 | * libtrace is distributed in the hope that it will be useful, |
---|
21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
23 | * GNU General Public License for more details. |
---|
24 | * |
---|
25 | * You should have received a copy of the GNU General Public License |
---|
26 | * along with libtrace; if not, write to the Free Software |
---|
27 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
28 | * |
---|
29 | * $Id: iow-lzo.c 1521 2010-02-08 22:21:16Z salcock $ |
---|
30 | * |
---|
31 | */ |
---|
32 | |
---|
33 | /* This writes out lzo files in the same format as lzop does. It's not as |
---|
34 | * flexible as lzop in an attempt to try and create a very fast method for |
---|
35 | * writing data out. |
---|
36 | * |
---|
37 | * Data is written out in blocks, and the blocks are all compressed in seperate |
---|
38 | * independant threads (if possible), thus letting you use multicore cpu's to |
---|
39 | * get compression for the absolute least amount of walltime while capturing. |
---|
40 | */ |
---|
41 | |
---|
42 | #include "config.h" |
---|
43 | #include <lzo/lzo1x.h> |
---|
44 | #include "wandio_internal.h" |
---|
45 | #include "wandio.h" |
---|
46 | #include <sys/types.h> |
---|
47 | #include <sys/stat.h> |
---|
48 | #include <fcntl.h> |
---|
49 | #include <stdlib.h> |
---|
50 | #include <string.h> |
---|
51 | #include <time.h> /* for mtime */ |
---|
52 | #include <errno.h> |
---|
53 | #include <assert.h> |
---|
54 | #include <inttypes.h> |
---|
55 | #include <arpa/inet.h> |
---|
56 | #include <pthread.h> |
---|
57 | #include <unistd.h> /* for sysconf */ |
---|
58 | #include <stdbool.h> |
---|
59 | #ifdef HAVE_SYS_PRCTL_H |
---|
60 | #include <sys/prctl.h> |
---|
61 | #endif |
---|
62 | |
---|
63 | |
---|
64 | enum { |
---|
65 | M_LZO1X_1 = 1, |
---|
66 | M_LZO1X_1_15 = 2, |
---|
67 | M_LZO1X_999 = 3, |
---|
68 | M_NRV1A = 0x1a, |
---|
69 | M_NRV1B = 0x1b, |
---|
70 | M_NRV2A = 0x2a, |
---|
71 | M_NRV2B = 0x2b, |
---|
72 | M_NRV2D = 0x2d, |
---|
73 | M_ZLIB = 128, |
---|
74 | }; |
---|
75 | |
---|
76 | static const int F_OS_UNIX = 0x03000000L; |
---|
77 | static const int F_OS_MASK = 0xff000000L; |
---|
78 | |
---|
79 | static const int F_CS_NATIVE = 0x00000000L; |
---|
80 | static const int F_CS_MASK = 0x00f00000L; |
---|
81 | |
---|
82 | static const int F_H_CRC32 = 0x00001000L; |
---|
83 | static const int F_ADLER32_D = 0x00000001L; |
---|
84 | static const int F_ADLER32_C = 0x00000002L; |
---|
85 | |
---|
86 | /* popquiz! You throught "static const int" would be well constant didn't you? |
---|
87 | * You'd be wrong, you can't use them in places where the compiler needs a |
---|
88 | * constant, so you need to use an enum, since enums /are/ constant the compiler |
---|
89 | * will let you use them as such. Sigh. |
---|
90 | */ |
---|
91 | enum { MAX_BLOCK_SIZE = 128*1024 }; /* lzop can only decompress blocks |
---|
92 | this large */ |
---|
93 | |
---|
94 | /* According to lzop lzo can increase the data to this size, so save this |
---|
95 | * much space in our buffers |
---|
96 | */ |
---|
97 | enum { MAX_BUFFER_SIZE = MAX_BLOCK_SIZE+MAX_BLOCK_SIZE/16+64+3 }; |
---|
98 | |
---|
99 | static const unsigned char lzop_magic[9] = |
---|
100 | { 0x89, 0x4c, 0x5a, 0x4f, 0x00, 0x0d, 0x0a, 0x1a, 0x0a }; |
---|
101 | |
---|
102 | |
---|
103 | /* Libtrace IO module implementing a lzo writer */ |
---|
104 | |
---|
105 | enum err_t { |
---|
106 | ERR_OK = 1, |
---|
107 | ERR_EOF = 0, |
---|
108 | ERR_ERROR = -1 |
---|
109 | }; |
---|
110 | |
---|
111 | const int ADLER32_INIT_VALUE = 1; |
---|
112 | const int CRC32_INIT_VALUE = 0; |
---|
113 | |
---|
114 | struct buffer_t { |
---|
115 | unsigned int offset; |
---|
116 | char buffer[MAX_BUFFER_SIZE]; |
---|
117 | }; |
---|
118 | |
---|
119 | struct lzothread_t { |
---|
120 | pthread_t thread; |
---|
121 | pthread_cond_t in_ready; |
---|
122 | pthread_cond_t out_ready; |
---|
123 | pthread_mutex_t mutex; |
---|
124 | bool closing; |
---|
125 | enum { EMPTY, WAITING, FULL } state; |
---|
126 | int num; |
---|
127 | struct buffer_t inbuf; |
---|
128 | struct buffer_t outbuf; |
---|
129 | }; |
---|
130 | |
---|
131 | struct lzow_t { |
---|
132 | iow_t *child; |
---|
133 | enum err_t err; |
---|
134 | int threads; |
---|
135 | int next_thread; |
---|
136 | struct lzothread_t *thread; |
---|
137 | }; |
---|
138 | |
---|
139 | extern iow_source_t lzo_wsource; |
---|
140 | |
---|
141 | #define DATA(iow) ((struct lzow_t *)((iow)->data)) |
---|
142 | #define min(a,b) ((a)<(b) ? (a) : (b)) |
---|
143 | |
---|
144 | static void write_buf(struct buffer_t *buffer,const void *data, size_t len) |
---|
145 | { |
---|
146 | assert(buffer->offset + len < sizeof(buffer->buffer) && "Exceeded output buffer size in lzo compressor"); |
---|
147 | memcpy(&buffer->buffer[buffer->offset], data, len); |
---|
148 | buffer->offset += len; |
---|
149 | } |
---|
150 | |
---|
151 | static void write32(struct buffer_t *buffer, uint32_t value) |
---|
152 | { |
---|
153 | value = htonl(value); |
---|
154 | write_buf(buffer, &value, sizeof(value)); |
---|
155 | } |
---|
156 | |
---|
157 | static void write16(struct buffer_t *buffer, uint16_t value) |
---|
158 | { |
---|
159 | value = htons(value); |
---|
160 | write_buf(buffer, &value, sizeof(value)); |
---|
161 | } |
---|
162 | |
---|
163 | static void write8(struct buffer_t *buffer, uint8_t value) |
---|
164 | { |
---|
165 | write_buf(buffer, &value, sizeof(value)); |
---|
166 | } |
---|
167 | |
---|
168 | static int lzo_wwrite_block(const char *buffer, off_t len, struct buffer_t *outbuf) |
---|
169 | { |
---|
170 | char b2[MAX_BUFFER_SIZE]; |
---|
171 | int err; |
---|
172 | lzo_uint dst_len; |
---|
173 | char scratch[LZO1X_1_MEM_COMPRESS]; |
---|
174 | |
---|
175 | outbuf->offset=0; |
---|
176 | |
---|
177 | memset(scratch,0,sizeof(scratch)); |
---|
178 | err=lzo1x_1_compress((void*)buffer, len, |
---|
179 | (void*)b2, &dst_len, |
---|
180 | scratch); |
---|
181 | |
---|
182 | switch(err) { |
---|
183 | case LZO_E_OK: |
---|
184 | break; |
---|
185 | case LZO_E_ERROR: |
---|
186 | return -EINVAL; /* WTF? */ |
---|
187 | case LZO_E_OUT_OF_MEMORY: |
---|
188 | return -ENOMEM; /* Uh oh */ |
---|
189 | case LZO_E_NOT_COMPRESSIBLE: |
---|
190 | return -EINVAL; /* Claimed not to be used, dunno what we'll do */ |
---|
191 | case LZO_E_INPUT_OVERRUN: |
---|
192 | return -EINVAL; /* Can't happen on compress? */ |
---|
193 | case LZO_E_OUTPUT_OVERRUN: |
---|
194 | return -ENOMEM; |
---|
195 | case LZO_E_LOOKBEHIND_OVERRUN: |
---|
196 | return -EINVAL; |
---|
197 | case LZO_E_EOF_NOT_FOUND: |
---|
198 | return -ENOENT; /* Can't happen on compress? */ |
---|
199 | case LZO_E_INPUT_NOT_CONSUMED: |
---|
200 | return -EINVAL; |
---|
201 | case LZO_E_NOT_YET_IMPLEMENTED: |
---|
202 | return -ENOSYS; |
---|
203 | default: |
---|
204 | fprintf(stderr,"Unknown lzo error %d\n",err); |
---|
205 | return -EINVAL; |
---|
206 | } |
---|
207 | |
---|
208 | write32(outbuf, len); /* Original length */ |
---|
209 | write32(outbuf, min((uint32_t)len,(uint32_t)dst_len)); |
---|
210 | /* CRC32 of the uncompressed buffer */ |
---|
211 | #if 0 |
---|
212 | write32(outbuf, lzo_crc32(CRC32_INIT_VALUE, (void*)buffer, len)); |
---|
213 | #endif |
---|
214 | write32(outbuf, |
---|
215 | lzo_adler32(ADLER32_INIT_VALUE, (const void*)buffer, len)); |
---|
216 | write_buf(outbuf, b2, dst_len); |
---|
217 | |
---|
218 | /* Return the number of bytes compressed */ |
---|
219 | return len; |
---|
220 | } |
---|
221 | |
---|
222 | /* There is one of these threads per core in a machine. This compresses |
---|
223 | * a block of data and returns it, the main thread tehn is responsible to |
---|
224 | * write these back out in the right order. |
---|
225 | */ |
---|
226 | static void *lzo_compress_thread(void *data) |
---|
227 | { |
---|
228 | struct lzothread_t *me = (struct lzothread_t *)data; |
---|
229 | int err; |
---|
230 | char namebuf[17]; |
---|
231 | |
---|
232 | #ifdef PR_SET_NAME |
---|
233 | if (prctl(PR_GET_NAME, namebuf, 0,0,0) == 0) { |
---|
234 | char label[16]; |
---|
235 | namebuf[16] = '\0'; /* Make sure it's NUL terminated */ |
---|
236 | sprintf(label,"[lzo%d]",me->num); |
---|
237 | /* If the filename is too long, overwrite the last few bytes */ |
---|
238 | if (strlen(namebuf)>=16-strlen(label)) { |
---|
239 | strcpy(namebuf+15-strlen(label),label); |
---|
240 | } |
---|
241 | else { |
---|
242 | strncat(namebuf," ",16); |
---|
243 | strncat(namebuf,label,16); |
---|
244 | } |
---|
245 | prctl(PR_SET_NAME, namebuf, 0,0,0); |
---|
246 | } |
---|
247 | #endif |
---|
248 | |
---|
249 | pthread_mutex_lock(&me->mutex); |
---|
250 | while (!me->closing) { |
---|
251 | while (me->state != WAITING) { |
---|
252 | if (me->closing) |
---|
253 | break; |
---|
254 | pthread_cond_wait(&me->in_ready, &me->mutex); |
---|
255 | } |
---|
256 | if (me->closing) |
---|
257 | break; |
---|
258 | |
---|
259 | err=lzo_wwrite_block( |
---|
260 | me->inbuf.buffer, |
---|
261 | me->inbuf.offset, |
---|
262 | &me->outbuf); |
---|
263 | |
---|
264 | if (err < 0) |
---|
265 | break; |
---|
266 | /* Make sure someone else hasn't clobbered us!*/ |
---|
267 | assert(me->state == WAITING); |
---|
268 | me->state = FULL; |
---|
269 | pthread_cond_signal(&me->out_ready); |
---|
270 | } |
---|
271 | pthread_mutex_unlock(&me->mutex); |
---|
272 | |
---|
273 | return NULL; |
---|
274 | } |
---|
275 | |
---|
276 | iow_t *lzo_wopen(iow_t *child, int compress_level) |
---|
277 | { |
---|
278 | const int opt_filter = 0; |
---|
279 | int flags; |
---|
280 | iow_t *iow; |
---|
281 | struct buffer_t buffer; |
---|
282 | buffer.offset=0; |
---|
283 | int i; |
---|
284 | |
---|
285 | if (!child) |
---|
286 | return NULL; |
---|
287 | |
---|
288 | if (lzo_init() != LZO_E_OK) { |
---|
289 | /* Fail */ |
---|
290 | return NULL; |
---|
291 | } |
---|
292 | |
---|
293 | /* Compress level is useless for LZO, but getting UNUSED into here |
---|
294 | * is more trouble than it is worth so this check will at least |
---|
295 | * stop us from getting warnings about it. |
---|
296 | */ |
---|
297 | if (compress_level < 0) |
---|
298 | return NULL; |
---|
299 | |
---|
300 | iow = malloc(sizeof(iow_t)); |
---|
301 | iow->source = &lzo_wsource; |
---|
302 | iow->data = malloc(sizeof(struct lzow_t)); |
---|
303 | |
---|
304 | DATA(iow)->child = child; |
---|
305 | DATA(iow)->err = ERR_OK; |
---|
306 | |
---|
307 | flags = 0; |
---|
308 | flags |= F_OS_UNIX & F_OS_MASK; /* Operating System */ |
---|
309 | flags |= F_CS_NATIVE & F_CS_MASK; /* Character Set */ |
---|
310 | flags |= F_ADLER32_D; /* We adler32 the uncompressed data */ |
---|
311 | /* flags |= F_STDIN; */ |
---|
312 | /* flags |= F_STDOUT */ |
---|
313 | /* flags |= F_MULTIPART; */ |
---|
314 | /* flags |= F_H_CRC32; */ |
---|
315 | |
---|
316 | write_buf(&buffer, lzop_magic, sizeof(lzop_magic)); |
---|
317 | write16(&buffer, 0x1010 &0xFFFF); /* version: pretend to be LZOP version 0x1010 from lzop's version.h */ |
---|
318 | write16(&buffer, lzo_version() & 0xFFFF); /* libversion */ |
---|
319 | write16(&buffer, opt_filter ? 0x0950 : 0x0940); /* version needed to extract */ |
---|
320 | write8(&buffer, M_LZO1X_1); /* method */ |
---|
321 | write8(&buffer, 5); /* level */ |
---|
322 | write32(&buffer, flags); /* flags */ |
---|
323 | /* if (flags & F_H_FILTER) |
---|
324 | write32(iow, opt_filter); |
---|
325 | */ |
---|
326 | write32(&buffer, 0x600); /* mode: We assume traces may be sensitive */ |
---|
327 | write32(&buffer, time(NULL)); /* mtime */ |
---|
328 | write32(&buffer, 0); /* GMTdiff */ |
---|
329 | |
---|
330 | /* Length, filename */ |
---|
331 | write8(&buffer, strlen("compresseddata")); |
---|
332 | write_buf(&buffer, "compresseddata",strlen("compresseddata")); |
---|
333 | |
---|
334 | if (flags & F_H_CRC32) { |
---|
335 | write32(&buffer, |
---|
336 | lzo_crc32(CRC32_INIT_VALUE, |
---|
337 | (const void*)buffer.buffer+sizeof(lzop_magic), |
---|
338 | buffer.offset-sizeof(lzop_magic))); |
---|
339 | } |
---|
340 | else { |
---|
341 | uint32_t chksum=lzo_adler32( |
---|
342 | ADLER32_INIT_VALUE, |
---|
343 | (const void *)buffer.buffer+sizeof(lzop_magic), |
---|
344 | buffer.offset-sizeof(lzop_magic)); |
---|
345 | write32(&buffer, chksum); |
---|
346 | } |
---|
347 | |
---|
348 | wandio_wwrite(DATA(iow)->child, |
---|
349 | buffer.buffer, |
---|
350 | buffer.offset); |
---|
351 | |
---|
352 | /* Set up the thread pool -- one thread per core */ |
---|
353 | DATA(iow)->threads = min((uint32_t)sysconf(_SC_NPROCESSORS_ONLN), |
---|
354 | use_threads); |
---|
355 | DATA(iow)->thread = malloc( |
---|
356 | sizeof(struct lzothread_t) * DATA(iow)->threads); |
---|
357 | DATA(iow)->next_thread = 0; |
---|
358 | for(i=0; i<DATA(iow)->threads; ++i) { |
---|
359 | pthread_cond_init(&DATA(iow)->thread[i].in_ready, NULL); |
---|
360 | pthread_cond_init(&DATA(iow)->thread[i].out_ready, NULL); |
---|
361 | pthread_mutex_init(&DATA(iow)->thread[i].mutex, NULL); |
---|
362 | DATA(iow)->thread[i].closing = false; |
---|
363 | DATA(iow)->thread[i].num = i; |
---|
364 | DATA(iow)->thread[i].state = EMPTY; |
---|
365 | DATA(iow)->thread[i].inbuf.offset = 0; |
---|
366 | |
---|
367 | pthread_create(&DATA(iow)->thread[i].thread, |
---|
368 | NULL, |
---|
369 | lzo_compress_thread, |
---|
370 | (void*)&DATA(iow)->thread[i]); |
---|
371 | } |
---|
372 | |
---|
373 | return iow; |
---|
374 | } |
---|
375 | |
---|
376 | static struct lzothread_t *get_next_thread(iow_t *iow) |
---|
377 | { |
---|
378 | return &DATA(iow)->thread[DATA(iow)->next_thread]; |
---|
379 | } |
---|
380 | |
---|
381 | static off_t lzo_wwrite(iow_t *iow, const char *buffer, off_t len) |
---|
382 | { |
---|
383 | off_t ret = 0; |
---|
384 | while (len>0) { |
---|
385 | off_t size = len; |
---|
386 | off_t err; |
---|
387 | struct buffer_t outbuf; |
---|
388 | |
---|
389 | if (!DATA(iow)->threads) { |
---|
390 | size = min(len, MAX_BLOCK_SIZE); |
---|
391 | err=lzo_wwrite_block(buffer, size, &outbuf); |
---|
392 | /* Flush the data out */ |
---|
393 | wandio_wwrite(DATA(iow)->child, |
---|
394 | outbuf.buffer, |
---|
395 | outbuf.offset); |
---|
396 | |
---|
397 | if (err < 0) {/* Error */ |
---|
398 | if (ret == 0) |
---|
399 | return err; |
---|
400 | /* If we've written some data, return that fact now, let them call back |
---|
401 | * and try and write more data, fail again then. |
---|
402 | */ |
---|
403 | return ret; |
---|
404 | } |
---|
405 | else { |
---|
406 | assert(err == size); |
---|
407 | buffer += size; |
---|
408 | len -= size; |
---|
409 | } |
---|
410 | } |
---|
411 | else { |
---|
412 | off_t space; |
---|
413 | |
---|
414 | pthread_mutex_lock(&get_next_thread(iow)->mutex); |
---|
415 | /* If this thread is still compressing, wait for it to finish */ |
---|
416 | while (get_next_thread(iow)->state == WAITING) { |
---|
417 | pthread_cond_wait( |
---|
418 | &get_next_thread(iow)->out_ready, |
---|
419 | &get_next_thread(iow)->mutex); |
---|
420 | } |
---|
421 | |
---|
422 | /* Flush any data out thats there */ |
---|
423 | if (get_next_thread(iow)->state == FULL) { |
---|
424 | assert(get_next_thread(iow)->outbuf.offset |
---|
425 | < sizeof(get_next_thread(iow)->outbuf.buffer)); |
---|
426 | wandio_wwrite(DATA(iow)->child, |
---|
427 | get_next_thread(iow)->outbuf.buffer, |
---|
428 | get_next_thread(iow)->outbuf.offset); |
---|
429 | get_next_thread(iow)->state = EMPTY; |
---|
430 | get_next_thread(iow)->inbuf.offset = 0; |
---|
431 | } |
---|
432 | |
---|
433 | assert(get_next_thread(iow)->state == EMPTY); |
---|
434 | |
---|
435 | /* Figure out how much space we can copy into this buffer */ |
---|
436 | assert(MAX_BLOCK_SIZE <= sizeof(get_next_thread(iow)->inbuf.buffer)); |
---|
437 | space = MAX_BLOCK_SIZE-get_next_thread(iow)->inbuf.offset; |
---|
438 | size = min(space, size); |
---|
439 | assert(size>0); |
---|
440 | assert(size <= MAX_BLOCK_SIZE); |
---|
441 | assert(get_next_thread(iow)->inbuf.offset + size <= MAX_BLOCK_SIZE); |
---|
442 | |
---|
443 | /* Move our data in */ |
---|
444 | memcpy(&get_next_thread(iow)->inbuf.buffer[get_next_thread(iow)->inbuf.offset], |
---|
445 | buffer, |
---|
446 | size); |
---|
447 | get_next_thread(iow)->inbuf.offset += size; |
---|
448 | |
---|
449 | /* If the buffer is now full Trigger the thread to start compressing this block, |
---|
450 | * and move onto the next block. |
---|
451 | */ |
---|
452 | if (get_next_thread(iow)->inbuf.offset >= sizeof(get_next_thread(iow)->inbuf.buffer) |
---|
453 | ||get_next_thread(iow)->inbuf.offset >= MAX_BLOCK_SIZE) { |
---|
454 | assert(get_next_thread(iow)->state == EMPTY); |
---|
455 | get_next_thread(iow)->state = WAITING; |
---|
456 | pthread_cond_signal(&get_next_thread(iow)->in_ready); |
---|
457 | |
---|
458 | pthread_mutex_unlock(&get_next_thread(iow)->mutex); |
---|
459 | |
---|
460 | DATA(iow)->next_thread = |
---|
461 | (DATA(iow)->next_thread+1) % DATA(iow)->threads; |
---|
462 | } |
---|
463 | else |
---|
464 | pthread_mutex_unlock(&get_next_thread(iow)->mutex); |
---|
465 | |
---|
466 | /* Update the lengths */ |
---|
467 | buffer += size; |
---|
468 | len -= size; |
---|
469 | } |
---|
470 | } |
---|
471 | return len; |
---|
472 | } |
---|
473 | |
---|
474 | static void shutdown_thread(iow_t *iow, struct lzothread_t *thread) |
---|
475 | { |
---|
476 | pthread_mutex_lock(&thread->mutex); |
---|
477 | |
---|
478 | /* If this buffer is empty it shouldn't have any data in it, we should have taken |
---|
479 | * care of that before. |
---|
480 | */ |
---|
481 | /* thread->state == EMPTY implies thread->inbuf.offset == 0 */ |
---|
482 | assert(!(thread->state == EMPTY) || thread->inbuf.offset == 0); |
---|
483 | |
---|
484 | while (thread->state == WAITING) { |
---|
485 | pthread_cond_wait( |
---|
486 | &thread->out_ready, |
---|
487 | &thread->mutex); |
---|
488 | } |
---|
489 | if (thread->state == FULL) { |
---|
490 | wandio_wwrite(DATA(iow)->child, |
---|
491 | thread->outbuf.buffer, |
---|
492 | thread->outbuf.offset); |
---|
493 | thread->state = EMPTY; |
---|
494 | thread->inbuf.offset = 0; |
---|
495 | } |
---|
496 | /* Now the thread should be empty, so ask it to shut down */ |
---|
497 | assert(thread->state == EMPTY && thread->inbuf.offset == 0); |
---|
498 | thread->closing = true; |
---|
499 | pthread_cond_signal(&thread->in_ready); |
---|
500 | pthread_mutex_unlock(&thread->mutex); |
---|
501 | /* And wait for it to die */ |
---|
502 | pthread_join(thread->thread,NULL); |
---|
503 | } |
---|
504 | |
---|
505 | static void lzo_wclose(iow_t *iow) |
---|
506 | { |
---|
507 | const uint32_t zero = 0; |
---|
508 | int i; |
---|
509 | |
---|
510 | /* Flush the last buffer */ |
---|
511 | pthread_mutex_lock(&get_next_thread(iow)->mutex); |
---|
512 | if (get_next_thread(iow)->state == EMPTY && get_next_thread(iow)->inbuf.offset != 0) { |
---|
513 | get_next_thread(iow)->state = WAITING; |
---|
514 | pthread_cond_signal(&get_next_thread(iow)->in_ready); |
---|
515 | } |
---|
516 | pthread_mutex_unlock(&get_next_thread(iow)->mutex); |
---|
517 | |
---|
518 | DATA(iow)->next_thread = |
---|
519 | (DATA(iow)->next_thread+1) % DATA(iow)->threads; |
---|
520 | |
---|
521 | /* Right, now we have to shutdown all our threads -- in order */ |
---|
522 | for(i=DATA(iow)->next_thread; i<DATA(iow)->threads; ++i) { |
---|
523 | shutdown_thread(iow,&DATA(iow)->thread[i]); |
---|
524 | } |
---|
525 | for(i=0; i<DATA(iow)->next_thread; ++i) { |
---|
526 | shutdown_thread(iow,&DATA(iow)->thread[i]); |
---|
527 | } |
---|
528 | |
---|
529 | /* Write out an end of file marker */ |
---|
530 | wandio_wwrite(DATA(iow)->child, |
---|
531 | &zero, |
---|
532 | sizeof(zero)); |
---|
533 | |
---|
534 | /* And clean everything up */ |
---|
535 | wandio_wdestroy(DATA(iow)->child); |
---|
536 | free(DATA(iow)->thread); |
---|
537 | free(iow->data); |
---|
538 | free(iow); |
---|
539 | } |
---|
540 | |
---|
541 | iow_source_t lzo_wsource = { |
---|
542 | "lzo", |
---|
543 | lzo_wwrite, |
---|
544 | lzo_wclose |
---|
545 | }; |
---|
546 | |
---|