XTA: API Reference for C language

xta_transaction_manager.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009-2019, Christian Ferrari <tiian@users.sourceforge.net>
3  * All rights reserved.
4  *
5  * This file is part of LIXA.
6  *
7  * LIXA is free software: you can redistribute this file and/or modify
8  * it under the terms of the GNU Lesser General Public License version 2.1 as
9  * published by the Free Software Foundation.
10  *
11  * LIXA is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with LIXA. If not, see <http://www.gnu.org/licenses/>.
18  */
19 #include "config.h"
20 
21 
22 
23 /* system includes */
24 #ifdef HAVE_GLIB_H
25 # include <glib.h>
26 #endif
27 /* LIXA includes */
28 #include "lixa_errors.h"
29 #include "lixa_trace.h"
30 #include "client_status.h"
31 /* XTA includes */
33 
34 
35 
36 /* set module trace flag */
37 #ifdef LIXA_TRACE_MODULE
38 # undef LIXA_TRACE_MODULE
39 #endif /* LIXA_TRACE_MODULE */
40 #define LIXA_TRACE_MODULE LIXA_TRACE_MOD_XTA
41 
42 
43 
45 {
46  enum Exception { G_TRY_MALLOC_ERROR
47  , CLIENT_CONFIG_ERROR
48  , NONE } excp;
49  int ret_cod = LIXA_RC_INTERNAL_ERROR;
50  xta_transaction_manager_t *this = NULL;
51 
52  LIXA_TRACE(("xta_transaction_manager_new\n"));
53  TRY {
54  /* allocate the object */
55  if (NULL == (this = (xta_transaction_manager_t *)
56  g_try_malloc0(sizeof(xta_transaction_manager_t))))
57  THROW(G_TRY_MALLOC_ERROR);
58  /* initialize the mutex */
59  g_mutex_init(&this->mutex);
60  /* configure the global config for LIXA client (if necessary) */
61  if (LIXA_RC_OK != (ret_cod = client_config(&global_ccc, TRUE)))
62  THROW(CLIENT_CONFIG_ERROR);
63  THROW(NONE);
64  } CATCH {
65  switch (excp) {
66  case G_TRY_MALLOC_ERROR:
68  break;
69  case CLIENT_CONFIG_ERROR:
70  break;
71  case NONE:
72  ret_cod = LIXA_RC_OK;
73  break;
74  default:
75  ret_cod = LIXA_RC_INTERNAL_ERROR;
76  } /* switch (excp) */
77  } /* TRY-CATCH */
78  LIXA_TRACE(("xta_transaction_manager_new/excp=%d/"
79  "ret_cod=%d/errno=%d\n", excp, ret_cod, errno));
80  return this;
81 }
82 
83 
84 
86 {
87  enum Exception { CLIENT_UNCONFIG_ERROR
88  , NONE } excp;
89  int ret_cod = LIXA_RC_INTERNAL_ERROR;
90 
91  LIXA_TRACE(("xta_transaction_manager_delete\n"));
92  TRY {
93  /* configure the global config for LIXA client (if necessary) */
94  if (LIXA_RC_OK != (ret_cod = client_unconfig(&global_ccc, TRUE)))
95  THROW(CLIENT_UNCONFIG_ERROR);
96  /* destroy transaction objects if any */
97  if (NULL != tm->transactions) {
98  g_hash_table_destroy(tm->transactions);
99  tm->transactions = NULL;
100  } /* if (NULL != tm->transactions) */
101 
102  /* clear the synchronization mutex */
103  g_mutex_clear(&tm->mutex);
104 
105  /* destroy the object itself */
106  g_free(tm);
107 
108  THROW(NONE);
109  } CATCH {
110  switch (excp) {
111  case CLIENT_UNCONFIG_ERROR:
112  break;
113  case NONE:
114  ret_cod = LIXA_RC_OK;
115  break;
116  default:
117  ret_cod = LIXA_RC_INTERNAL_ERROR;
118  } /* switch (excp) */
119  } /* TRY-CATCH */
120  LIXA_TRACE(("xta_transaction_manager_delete/excp=%d/"
121  "ret_cod=%d/errno=%d\n", excp, ret_cod, errno));
122  return;
123 }
124 
125 
126 
129  return &global_ccc;
130 }
131 
132 
133 
134 /*
135  * -Wdeprecated-declarations is disabled because this function is allowed to
136  * call xta_transaction_delete()
137  */
138 #ifdef __GNUC__
139 # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
140 #endif
143 {
144  enum Exception { NULL_OBJECT1
145  , G_HASH_TABLE_NEW_ERROR
146  , NON_DISPOSABLE_TX
147  , NULL_OBJECT2
148  , INTERNAL_ERROR
149  , NONE } excp;
150  int ret_cod = LIXA_RC_INTERNAL_ERROR;
151  xta_transaction_t *tx = NULL;
152 
153  LIXA_TRACE(("xta_transaction_manager_create_transaction\n"));
154  TRY {
155  GThread *self = g_thread_self();
156 
157  /* check object reference */
158  if (NULL == tm)
159  THROW(NULL_OBJECT1);
160 
161  LIXA_TRACE(("xta_transaction_manager_create_transaction: "
162  "locking mutex...\n"));
163  g_mutex_lock(&tm->mutex);
164 
165  /* check if the hash table as been already created */
166  if (NULL == tm->transactions) {
167  if (NULL == (tm->transactions = g_hash_table_new_full(
168  NULL, NULL, NULL,
169  (GDestroyNotify)xta_transaction_delete)))
170  THROW(G_HASH_TABLE_NEW_ERROR);
171  } /* if (NULL == tm->transactions) */
172 
173  /* look for transaction in hash table */
174  /*
175  if (NULL == (tx = g_hash_table_lookup(tm->transactions, self))) {
176  LIXA_TRACE(("xta_transaction_manager_create_transaction: there is "
177  "not a started transactions for this thread, "
178  "starting a new one...\n"));
179  */
180  if (NULL != (tx = g_hash_table_lookup(tm->transactions, self))) {
181  /* check if the transaction has been used for multiple branches */
183  LIXA_TRACE(("xta_transaction_manager_create_transaction: "
184  "there is a started transactions for this thread, "
185  "deleting the old one...\n"));
187  THROW(NON_DISPOSABLE_TX);
188  /* xta_transaction_delete is automatically called by glib (see
189  g_hash_table_new_full above */
190  g_hash_table_remove(tm->transactions, self);
191  /* reset tx to force the creation of a new one below */
192  tx = NULL;
193  }
194  }
195 
196  /* allocate a new transaction object */
197  if (NULL == tx) {
198  if (NULL == (tx = xta_transaction_new()))
199  THROW(NULL_OBJECT2);
200 
201  /* insert the transaction object in the hash map */
202 #if GLIB_MAJOR_VERSION == 2
203 # if GLIB_MINOR_VERSION >= 40
204  if (!g_hash_table_insert(tm->transactions, self, tx)) {
205  THROW(INTERNAL_ERROR);
206  }
207 # else
208  g_hash_table_insert(tm->transactions, self, tx);
209 # endif
210 #endif
211  }
212 
213  THROW(NONE);
214  } CATCH {
215  switch (excp) {
216  case NULL_OBJECT1:
217  ret_cod = LIXA_RC_NULL_OBJECT;
218  break;
219  case G_HASH_TABLE_NEW_ERROR:
221  break;
222  case NON_DISPOSABLE_TX:
223  ret_cod = LIXA_RC_NON_DISPOSABLE_TX;
224  break;
225  case NULL_OBJECT2:
226  ret_cod = LIXA_RC_NULL_OBJECT;
227  break;
228  case INTERNAL_ERROR:
229  ret_cod = LIXA_RC_INTERNAL_ERROR;
230  break;
231  case NONE:
232  ret_cod = LIXA_RC_OK;
233  break;
234  default:
235  ret_cod = LIXA_RC_INTERNAL_ERROR;
236  } /* switch (excp) */
237  /* recoverying memory in the event of error */
238  if (NULL_OBJECT2 < excp && NONE > excp)
240  /* clear mutex */
241  if (NULL_OBJECT1 < excp) {
242  g_mutex_unlock(&tm->mutex);
243  LIXA_TRACE(("xta_transaction_manager_create_transaction: "
244  "mutex unlocked\n"));
245  }
246  } /* TRY-CATCH */
247  LIXA_TRACE(("xta_transaction_manager_create_transaction/excp=%d/"
248  "ret_cod=%d/errno=%d\n", excp, ret_cod, errno));
249  return tx;
250 }
251 #ifdef __GNUC__
252 # pragma GCC diagnostic warning "-Wdeprecated-declarations"
253 #endif
#define LIXA_RC_NULL_OBJECT
Definition: lixa_errors.h:139
#define LIXA_RC_INTERNAL_ERROR
Definition: lixa_errors.h:122
xta_transaction_manager_config_t * xta_transaction_manager_get_config(void)
#define LIXA_RC_OK
Definition: lixa_errors.h:115
void xta_transaction_manager_config_t
xta_transaction_manager_t * xta_transaction_manager_new(void)
int xta_transaction_safe_delete(const xta_transaction_t *transact)
xta_transaction_t * xta_transaction_manager_create_transaction(xta_transaction_manager_t *tm)
int xta_transaction_get_multiple_branches(const xta_transaction_t *transact)
#define LIXA_RC_G_TRY_MALLOC_ERROR
Definition: lixa_errors.h:563
void xta_transaction_manager_delete(xta_transaction_manager_t *tm)
#define LIXA_RC_G_HASH_TABLE_NEW_ERROR
Definition: lixa_errors.h:526
#define LIXA_RC_NON_DISPOSABLE_TX
Definition: lixa_errors.h:605
void xta_transaction_delete(xta_transaction_t *transact)
xta_transaction_t * xta_transaction_new(void)

Copyright © 2009-2019, Christian Ferrari tiian@users.sourceforge.net http://www.tiian.org/