sqlite: Use our locking mechanism also when sqlite3_threadsafe() returns 0

We previously checked for older library versions without locking support at
all. But newer libraries can be built in single-threading mode as well, where
we have to care about the locking.
This commit is contained in:
Martin Willi
2015-04-13 15:31:58 +02:00
parent 4e621ada96
commit 6a84a4049d
@@ -68,6 +68,18 @@ typedef struct {
} transaction_t; } transaction_t;
/**
* Check if the SQLite library is thread safe
*/
static bool is_threadsave()
{
#if SQLITE_VERSION_NUMBER >= 3005000
return sqlite3_threadsafe() > 0;
#endif
/* sqlite connections prior to 3.5 may be used by a single thread only */
return FALSE;
}
/** /**
* Create and run a sqlite stmt using a sql string and args * Create and run a sqlite stmt using a sql string and args
*/ */
@@ -168,9 +180,10 @@ typedef struct {
static void sqlite_enumerator_destroy(sqlite_enumerator_t *this) static void sqlite_enumerator_destroy(sqlite_enumerator_t *this)
{ {
sqlite3_finalize(this->stmt); sqlite3_finalize(this->stmt);
#if SQLITE_VERSION_NUMBER < 3005000 if (!is_threadsave())
this->database->mutex->unlock(this->database->mutex); {
#endif this->database->mutex->unlock(this->database->mutex);
}
free(this->columns); free(this->columns);
free(this); free(this);
} }
@@ -248,10 +261,10 @@ METHOD(database_t, query, enumerator_t*,
sqlite_enumerator_t *enumerator = NULL; sqlite_enumerator_t *enumerator = NULL;
int i; int i;
#if SQLITE_VERSION_NUMBER < 3005000 if (!is_threadsave())
/* sqlite connections prior to 3.5 may be used by a single thread only, */ {
this->mutex->lock(this->mutex); this->mutex->lock(this->mutex);
#endif }
va_start(args, sql); va_start(args, sql);
stmt = run(this, sql, &args); stmt = run(this, sql, &args);