home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mitsch75.zip / scheme-7_5_17-src.zip / scheme-7.5.17 / src / microcode / transact.c < prev    next >
C/C++ Source or Header  |  2000-12-05  |  3KB  |  125 lines

  1. /* -*-C-*-
  2.  
  3. $Id: transact.c,v 1.5 2000/12/05 21:23:48 cph Exp $
  4.  
  5. Copyright (C) 1990-2000 Massachusetts Institute of Technology
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or (at
  10. your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful, but
  13. WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15. General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21.  
  22. #include <stdio.h>
  23. #include "config.h"
  24. #include "outf.h"
  25. #include "dstack.h"
  26.  
  27. static void
  28. DEFUN (error, (procedure_name, message),
  29.        CONST char * procedure_name AND
  30.        CONST char * message)
  31. {
  32.   outf_fatal ("%s: %s\n", procedure_name, message);
  33.   outf_flush_fatal ();
  34.   abort ();
  35. }
  36.  
  37. enum transaction_state { active, aborting, committing };
  38.  
  39. struct transaction
  40. {
  41.   PTR checkpoint;
  42.   enum transaction_state state;
  43. };
  44.  
  45. static struct transaction * current_transaction;
  46.  
  47. static void
  48. DEFUN (guarantee_current_transaction, (proc), CONST char * proc)
  49. {
  50.   if (current_transaction == 0)
  51.     error (proc, "no transaction");
  52.   switch (current_transaction -> state)
  53.     {
  54.     case committing: error (proc, "commit in progress"); break;
  55.     case aborting: error (proc, "abort in progress"); break;
  56.     case active: break;
  57.     }
  58. }
  59.  
  60. void
  61. DEFUN_VOID (transaction_initialize)
  62. {
  63.   current_transaction = 0;
  64. }
  65.  
  66. void
  67. DEFUN_VOID (transaction_begin)
  68. {
  69.   PTR checkpoint = dstack_position;
  70.   struct transaction * transaction =
  71.     (dstack_alloc (sizeof (struct transaction)));
  72.   (transaction -> checkpoint) = checkpoint;
  73.   (transaction -> state) = active;
  74.   dstack_bind ((¤t_transaction), transaction);
  75. }
  76.  
  77. void
  78. DEFUN_VOID (transaction_abort)
  79. {
  80.   guarantee_current_transaction ("transaction_abort");
  81.   (current_transaction -> state) = aborting;
  82.   dstack_set_position (current_transaction -> checkpoint);
  83. }
  84.  
  85. void
  86. DEFUN_VOID (transaction_commit)
  87. {
  88.   guarantee_current_transaction ("transaction_commit");
  89.   (current_transaction -> state) = committing;
  90.   dstack_set_position (current_transaction -> checkpoint);
  91. }
  92.  
  93. struct action
  94. {
  95.   enum transaction_action_type type;
  96.   void EXFUN ((*procedure), (PTR environment));
  97.   PTR environment;
  98. };
  99.  
  100. static void
  101. DEFUN (execute_action, (action), PTR action)
  102. {
  103.   if ((((struct action *) action) -> type) !=
  104.       (((current_transaction -> state) == committing)
  105.        ? tat_abort : tat_commit))
  106.     (* (((struct action *) action) -> procedure))
  107.       (((struct action *) action) -> environment);
  108. }
  109.  
  110. void
  111. DEFUN (transaction_record_action, (type, procedure, environment),
  112.        enum transaction_action_type type AND
  113.        void EXFUN ((*procedure), (PTR environment)) AND
  114.        PTR environment)
  115. {
  116.   guarantee_current_transaction ("transaction_record_action");
  117.   {
  118.     struct action * action = (dstack_alloc (sizeof (struct action)));
  119.     (action -> type) = type;
  120.     (action -> procedure) = procedure;
  121.     (action -> environment) = environment;
  122.     dstack_protect (execute_action, action);
  123.   }
  124. }
  125.