[darwinbuild-changes] [781] branches/PR-7748469/darwinup

source_changes at macosforge.org source_changes at macosforge.org
Sat Mar 13 19:30:38 PST 2010


Revision: 781
          http://trac.macosforge.org/projects/darwinbuild/changeset/781
Author:   wsiegrist at apple.com
Date:     2010-03-13 19:30:35 -0800 (Sat, 13 Mar 2010)
Log Message:
-----------
Retry operations a few times when the database is locked.

Modified Paths:
--------------
    branches/PR-7748469/darwinup/Database.cpp
    branches/PR-7748469/darwinup/Database.h

Modified: branches/PR-7748469/darwinup/Database.cpp
===================================================================
--- branches/PR-7748469/darwinup/Database.cpp	2010-03-14 02:40:37 UTC (rev 780)
+++ branches/PR-7748469/darwinup/Database.cpp	2010-03-14 03:30:35 UTC (rev 781)
@@ -123,7 +123,7 @@
 	}
 	
 	int exists = is_regular_file(m_path);
-	res = sqlite3_open(m_path, &m_db);
+	__retry_if_locked(sqlite3_open(m_path, &m_db));
 	if (res) {
 		sqlite3_close(m_db);
 		m_db = NULL;
@@ -452,7 +452,7 @@
 		return res;
 	}
 	this->bind_columns(stmt, count, param, args);
-	res = sqlite3_step(stmt);
+	__retry_if_locked(sqlite3_step(stmt));
 	sqlite3_reset(stmt);
     cache_release_value(m_statement_cache, pps);
 	va_end(args);
@@ -553,7 +553,7 @@
     char* error;
     if (this->m_db) {
         char *query = sqlite3_vmprintf(fmt, args);
-        res = sqlite3_exec(this->m_db, query, NULL, NULL, &error);
+        __retry_if_locked(sqlite3_exec(this->m_db, query, NULL, NULL, &error));
         sqlite3_free(query);
     } else {
         fprintf(stderr, "Error: database not open.\n");
@@ -562,7 +562,7 @@
     va_end(args);
 	if (error) {
 		strlcpy(m_error, error, m_error_size);
-		fprintf(stderr, "Error: sql(): %s \n", m_error);
+		fprintf(stderr, "Error: sql_once(): %s \n", m_error);
 		fprintf(stderr, "Error: fmt: %s \n", fmt);
 		sqlite3_free(error);
 	}
@@ -593,7 +593,8 @@
 }
 
 int Database::execute(sqlite3_stmt* stmt) {
-	int res = sqlite3_step(stmt);
+	int res = SQLITE_OK;
+	__retry_if_locked(sqlite3_step(stmt));
 	if (res == SQLITE_DONE) {
 		res = SQLITE_OK;
 	} else {
@@ -633,7 +634,7 @@
 	int res = SQLITE_OK;
 	char* query;
 	asprintf(&query, "SELECT count(*) FROM %s;", m_tables[0]->name());
-	res = sqlite3_exec(this->m_db, query, NULL, NULL, NULL);
+	__retry_if_locked(sqlite3_exec(this->m_db, query, NULL, NULL, NULL));
 	free(query);
 	return res != SQLITE_OK;
 }
@@ -770,9 +771,10 @@
 }
 
 bool Database::has_information_table() {
-	int res = sqlite3_exec(this->m_db, 
-						   "SELECT count(*) FROM database_information;", 
-						   NULL, NULL, NULL);
+	int res = SQLITE_OK;
+	__retry_if_locked(sqlite3_exec(this->m_db, 
+								   "SELECT count(*) FROM database_information;", 
+								   NULL, NULL, NULL));
 	return res == SQLITE_OK;
 }
 
@@ -849,7 +851,8 @@
  *   were written to output
  */
 int Database::step_once(sqlite3_stmt* stmt, uint8_t* output, uint32_t* used) {
-	int res = sqlite3_step(stmt);
+	int res = SQLITE_OK;
+	__retry_if_locked(sqlite3_step(stmt));
 	uint8_t* current = output;
 	if (used) *used = 0;
 	if (res == SQLITE_ROW) {

Modified: branches/PR-7748469/darwinup/Database.h
===================================================================
--- branches/PR-7748469/darwinup/Database.h	2010-03-14 02:40:37 UTC (rev 780)
+++ branches/PR-7748469/darwinup/Database.h	2010-03-14 03:30:35 UTC (rev 781)
@@ -83,7 +83,29 @@
 #define ADD_BLOB(table, name) \
 	assert(table->add_column(new Column(name, TYPE_BLOB), this->schema_version())==0);
 
+// retry an operation a few times if we hit a lock
+#define __retry_if_locked(operation) \
+    do { \
+    extern uint32_t verbosity; \
+    fprintf(stderr, "retry: verbosity %u \n", verbosity); \
+    res = operation; \
+    fprintf(stderr, "retry: initial res %d \n", res); \
+    if (res == 5 || res == 6) { \
+        fprintf(stderr, "retry: locked \n"); \
+        int _num_tries = 3; \
+        while (_num_tries--) { \
+            fprintf(stderr, "retry: tries left %d \n", _num_tries); \
+            if (verbosity) fprintf(stdout, "Database is locked, " \
+								   "trying again in 1 second...\n"); \
+	        sleep(1); \
+            res = operation; \
+			fprintf(stderr, "retry: res %d \n", res); \
+		} \
+        if (verbosity) fprintf(stdout, "Database is still locked, giving up.\n"); \
+	} \
+    } while (0);
 
+
 /**
  * 
  * Generic sqlite abstraction
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/darwinbuild-changes/attachments/20100313/3d9a6d40/attachment-0001.html>


More information about the darwinbuild-changes mailing list