# HG changeset patch # User Adam Chlipala # Date 1241304081 14400 # Node ID eac1974924bbc331dc5faa42e3d7ec8b15987341 # Parent c125df6fabfc267f5fdfa2258a90547a20bfdb7b FFI transactionals diff -r c125df6fabfc -r eac1974924bb include/types.h --- a/include/types.h Sat May 02 18:20:15 2009 -0400 +++ b/include/types.h Sat May 02 18:41:21 2009 -0400 @@ -45,4 +45,6 @@ #define FLOATS_MAX 100 #define TIMES_MAX 100 +typedef void (*uw_callback)(void *); + #endif diff -r c125df6fabfc -r eac1974924bb include/urweb.h --- a/include/urweb.h Sat May 02 18:20:15 2009 -0400 +++ b/include/urweb.h Sat May 02 18:41:21 2009 -0400 @@ -183,4 +183,6 @@ __attribute__((noreturn)) void uw_return_blob(uw_context, uw_Basis_blob, uw_Basis_string mimeType); +void uw_register_transactional(uw_context, void *data, uw_callback commit, uw_callback rollback, uw_callback free); + #endif diff -r c125df6fabfc -r eac1974924bb src/c/urweb.c --- 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" diff -r c125df6fabfc -r eac1974924bb tests/cffi.ur --- a/tests/cffi.ur Sat May 02 18:20:15 2009 -0400 +++ b/tests/cffi.ur Sat May 02 18:41:21 2009 -0400 @@ -8,7 +8,22 @@