summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAndrew Bartlett <abartlet@samba.org>2017-04-27 08:34:56 +1200
committerStefan Metzmacher <metze@samba.org>2017-07-02 17:35:19 +0200
commit32702a974579ae1ee2d097240dc2bf55c515ca57 (patch)
tree149aa73c2553481af3a86e428d07c94198fd1a3a /lib
parente08d02d0279f3a69735b7139d9219b603c68b149 (diff)
downloadsamba-32702a974579ae1ee2d097240dc2bf55c515ca57.tar.gz
tdb: Add new function tdb_transaction_active()
This will allow callers to avoid their own reference counting of transactions. Additionally, this will always line up with the acutal transaction state, even in the error cases where tdb can cancel the transaction Signed-off-by: Andrew Bartlett <abartlet@samba.org> Reviewed-by: Stefan Metzmacher <metze@samba.org>
Diffstat (limited to 'lib')
-rw-r--r--lib/tdb/common/transaction.c8
-rw-r--r--lib/tdb/include/tdb.h18
-rw-r--r--lib/tdb/test/run-traverse-in-transaction.c3
3 files changed, 29 insertions, 0 deletions
diff --git a/lib/tdb/common/transaction.c b/lib/tdb/common/transaction.c
index 420e754d9b6..9b975eaa047 100644
--- a/lib/tdb/common/transaction.c
+++ b/lib/tdb/common/transaction.c
@@ -412,6 +412,14 @@ static const struct tdb_methods transaction_methods = {
transaction_expand_file,
};
+/*
+ * Is a transaction currently active on this context?
+ *
+ */
+_PUBLIC_ bool tdb_transaction_active(struct tdb_context *tdb)
+{
+ return (tdb->transaction != NULL);
+}
/*
start a tdb transaction. No token is returned, as only a single
diff --git a/lib/tdb/include/tdb.h b/lib/tdb/include/tdb.h
index 4bfa1a4dd85..535f07ac00a 100644
--- a/lib/tdb/include/tdb.h
+++ b/lib/tdb/include/tdb.h
@@ -651,6 +651,24 @@ tdb_log_func tdb_log_fn(struct tdb_context *tdb);
void *tdb_get_logging_private(struct tdb_context *tdb);
/**
+ * @brief Is a transaction active?
+ *
+ * It is helpful for the application to know if a transaction is
+ * active, rather than needing to maintain an application-level reference
+ * count.
+ *
+ * @param[in] tdb The database to start the transaction.
+ *
+ * @return true if there is a transaction active, false otherwise
+ *
+ * @see tdb_transaction_start()
+ * @see tdb_transaction_prepare_commit()
+ * @see tdb_transaction_commit()
+ * @see tdb_transaction_cancel()
+ */
+bool tdb_transaction_active(struct tdb_context *tdb);
+
+/**
* @brief Start a transaction.
*
* All operations after the transaction start can either be committed with
diff --git a/lib/tdb/test/run-traverse-in-transaction.c b/lib/tdb/test/run-traverse-in-transaction.c
index 17d64129642..d187b9b58b8 100644
--- a/lib/tdb/test/run-traverse-in-transaction.c
+++ b/lib/tdb/test/run-traverse-in-transaction.c
@@ -63,7 +63,9 @@ int main(int argc, char *argv[])
ok1(external_agent_operation(agent, OPEN, tdb_name(tdb)) == SUCCESS);
+ ok1(tdb_transaction_active(tdb) == 0);
ok1(tdb_transaction_start(tdb) == 0);
+ ok1(tdb_transaction_active(tdb) == 1);
ok1(external_agent_operation(agent, TRANSACTION_START, tdb_name(tdb))
== WOULD_HAVE_BLOCKED);
tdb_traverse(tdb, traverse, NULL);
@@ -77,6 +79,7 @@ int main(int argc, char *argv[])
ok1(external_agent_operation(agent, TRANSACTION_START, tdb_name(tdb))
== WOULD_HAVE_BLOCKED);
ok1(tdb_transaction_commit(tdb) == 0);
+ ok1(tdb_transaction_active(tdb) == 0);
/* Now we should be fine. */
ok1(external_agent_operation(agent, TRANSACTION_START, tdb_name(tdb))
== SUCCESS);