Mercurial > urweb
diff src/c/urweb.c @ 771:eac1974924bb
FFI transactionals
author | Adam Chlipala <adamc@hcoop.net> |
---|---|
date | Sat, 02 May 2009 18:41:21 -0400 |
parents | c125df6fabfc |
children | 8ed1261f838c |
line wrap: on
line diff
--- a/src/c/urweb.c Sat May 02 18:20:15 2009 -0400 +++ b/src/c/urweb.c Sat May 02 18:41:21 2009 -0400 @@ -304,6 +304,11 @@ } data; } input; +typedef struct { + void *data; + uw_callback commit, rollback, free; +} transactional; + struct uw_context { char *headers, *headers_end; @@ -332,6 +337,9 @@ client *client; + transactional *transactionals; + size_t n_transactionals, used_transactionals; + char error_message[ERROR_BUF_LEN]; }; @@ -377,6 +385,9 @@ ctx->error_message[0] = 0; + ctx->transactionals = malloc(0); + ctx->n_transactionals = ctx->used_transactionals = 0; + return ctx; } @@ -398,6 +409,7 @@ free(ctx->inputs); free(ctx->subinputs); free(ctx->cleanup); + free(ctx->transactionals); for (i = 0; i < ctx->n_deltas; ++i) buf_free(&ctx->deltas[i].msgs); @@ -419,6 +431,7 @@ ctx->used_deltas = 0; ctx->client = NULL; ctx->cur_container = NULL; + ctx->used_transactionals = 0; } void uw_reset_keep_request(uw_context ctx) { @@ -2339,7 +2352,15 @@ void uw_commit(uw_context ctx) { unsigned i; - if (uw_db_commit(ctx)) + for (i = 0; i < ctx->used_transactionals; ++i) + if (ctx->transactionals[i].rollback != NULL) + ctx->transactionals[i].commit(ctx->transactionals[i].data); + + for (i = 0; i < ctx->used_transactionals; ++i) + if (ctx->transactionals[i].rollback == NULL) + ctx->transactionals[i].commit(ctx->transactionals[i].data); + + if (uw_db_commit(ctx)) uw_error(ctx, FATAL, "Error running SQL COMMIT"); for (i = 0; i < ctx->used_deltas; ++i) { @@ -2353,15 +2374,43 @@ if (ctx->client) release_client(ctx->client); + + for (i = 0; i < ctx->used_transactionals; ++i) + ctx->transactionals[i].free(ctx->transactionals[i].data); } int uw_rollback(uw_context ctx) { + size_t i; + if (ctx->client) release_client(ctx->client); + for (i = 0; i < ctx->used_transactionals; ++i) + if (ctx->transactionals[i].rollback != NULL) + ctx->transactionals[i].rollback(ctx->transactionals[i].data); + + for (i = 0; i < ctx->used_transactionals; ++i) + ctx->transactionals[i].free(ctx->transactionals[i].data); + return uw_db_rollback(ctx); } +void uw_register_transactional(uw_context ctx, void *data, uw_callback commit, uw_callback rollback, + uw_callback free) { + if (commit == NULL) + uw_error(ctx, FATAL, "uw_register_transactional: NULL commit callback"); + + if (ctx->used_transactionals >= ctx->n_transactionals) { + ctx->transactionals = realloc(ctx->transactionals, ctx->used_transactionals+1); + ++ctx->n_transactionals; + } + + ctx->transactionals[ctx->used_transactionals].data = data; + ctx->transactionals[ctx->used_transactionals].commit = commit; + ctx->transactionals[ctx->used_transactionals].rollback = rollback; + ctx->transactionals[ctx->used_transactionals++].free = free; +} + // "Garbage collection"