/alps/fastwriter

To get this branch, use:
bzr branch http://darksoft.org/webbzr/alps/fastwriter

« back to all changes in this revision

Viewing changes to fastwriter.c

  • Committer: Suren A. Chilingaryan
  • Date: 2011-12-13 16:54:10 UTC
  • Revision ID: csa@dside.dyndns.org-20111213165410-b36e4989rogac4fg
Few synchronization and alignment related fixes

Show diffs side-by-side

added added

removed removed

Lines of Context:
68
68
     default:
69
69
        ctx->size = ctx->params.buffer_size;
70
70
    }
71
 
    
72
 
    ctx->buffer = malloc(ctx->size);
73
 
    if (!ctx->buffer) {
 
71
 
 
72
    if (ctx->size%FASTWRITER_SYNCIO_ALIGN)
 
73
        ctx->size += FASTWRITER_SYNCIO_ALIGN - (ctx->size%FASTWRITER_SYNCIO_ALIGN);
 
74
 
 
75
    err = posix_memalign(&ctx->buffer, FASTWRITER_SYNCIO_ALIGN, ctx->size);
 
76
    if ((err)||(!ctx->buffer)) {
74
77
        fastwriter_close(ctx);
75
78
        return ENOMEM;
76
79
    }
149
152
        ctx->buffer = NULL;
150
153
    }
151
154
    
152
 
    return 0;
 
155
    return ctx->err;
153
156
    
154
157
}
155
158
 
177
180
 
178
181
    fastwriter_t *ctx = (fastwriter_t*)user;
179
182
 
 
183
 
180
184
    while ((ctx->run_flag)||(ctx->head != ctx->tail)) {
181
185
        if (ctx->head != ctx->tail) {
182
186
            head = ctx->head;
219
223
                while ((ctx->run_flag)&&(ctx->head == head)) {
220
224
                    pthread_cond_wait(&ctx->data_cond, &ctx->data_cond_mutex);
221
225
                }
 
226
                pthread_mutex_unlock(&ctx->data_cond_mutex);
222
227
            }
223
228
        } else {
224
229
            pthread_mutex_lock(&ctx->data_cond_mutex);
225
230
            while ((ctx->run_flag)&&(ctx->head == ctx->tail)) {
226
231
                pthread_cond_wait(&ctx->data_cond, &ctx->data_cond_mutex);
227
232
            }
 
233
            pthread_mutex_unlock(&ctx->data_cond_mutex);
228
234
        }
229
235
    }
230
236
    
251
257
        end = ctx->size - (free - size);
252
258
        if (end > ctx->max_usage) ctx->max_usage = end;
253
259
    }
254
 
    
 
260
 
255
261
    if (!ctx->run_flag) {
256
262
        if (ctx->err) return ctx->err;
257
263
        return EBADFD;
258
264
    }
259
 
    
 
265
 
260
266
    if (ctx->pos < ctx->tail) end = ctx->tail;
261
267
    else end = ctx->size;
262
 
    
 
268
 
263
269
 
264
270
    part1 = end - ctx->pos;
265
 
    
266
 
    if (part1 > size) {
 
271
 
 
272
    if (part1 < size) {
267
273
            // tail < pos (we have checked for free space)
268
274
        end = size - part1;
269
275
        memcpy(ctx->buffer + ctx->pos, data, part1);
307
313
 
308
314
int fastwriter_push_data(fastwriter_t *ctx, size_t size, const void *buf) {
309
315
    int err;
 
316
 
310
317
    err = fastwriter_push(ctx, size, buf);
311
318
    if (err) return err;
312
 
    
 
319
 
313
320
    err = fastwriter_commit(ctx);
314
321
    if (err) fastwriter_cancel(ctx);
315
322