transactions.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Simple transactions API
  3. *
  4. * Copyright (c) 2021 Virtuozzo International GmbH.
  5. *
  6. * Author:
  7. * Sementsov-Ogievskiy Vladimir <vsementsov@virtuozzo.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. #include "qemu/osdep.h"
  23. #include "qemu/transactions.h"
  24. #include "qemu/queue.h"
  25. typedef struct TransactionAction {
  26. TransactionActionDrv *drv;
  27. void *opaque;
  28. QSLIST_ENTRY(TransactionAction) entry;
  29. } TransactionAction;
  30. struct Transaction {
  31. QSLIST_HEAD(, TransactionAction) actions;
  32. };
  33. Transaction *tran_new(void)
  34. {
  35. Transaction *tran = g_new(Transaction, 1);
  36. QSLIST_INIT(&tran->actions);
  37. return tran;
  38. }
  39. void tran_add(Transaction *tran, TransactionActionDrv *drv, void *opaque)
  40. {
  41. TransactionAction *act;
  42. act = g_new(TransactionAction, 1);
  43. *act = (TransactionAction) {
  44. .drv = drv,
  45. .opaque = opaque
  46. };
  47. QSLIST_INSERT_HEAD(&tran->actions, act, entry);
  48. }
  49. void tran_abort(Transaction *tran)
  50. {
  51. TransactionAction *act, *next;
  52. QSLIST_FOREACH(act, &tran->actions, entry) {
  53. if (act->drv->abort) {
  54. act->drv->abort(act->opaque);
  55. }
  56. }
  57. QSLIST_FOREACH_SAFE(act, &tran->actions, entry, next) {
  58. if (act->drv->clean) {
  59. act->drv->clean(act->opaque);
  60. }
  61. g_free(act);
  62. }
  63. g_free(tran);
  64. }
  65. void tran_commit(Transaction *tran)
  66. {
  67. TransactionAction *act, *next;
  68. QSLIST_FOREACH(act, &tran->actions, entry) {
  69. if (act->drv->commit) {
  70. act->drv->commit(act->opaque);
  71. }
  72. }
  73. QSLIST_FOREACH_SAFE(act, &tran->actions, entry, next) {
  74. if (act->drv->clean) {
  75. act->drv->clean(act->opaque);
  76. }
  77. g_free(act);
  78. }
  79. g_free(tran);
  80. }