Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Add lock_count to struct pkgdb and only (un)lock database when needed.
David Naylor committed 13 years ago
commit 9e0f54852b472258ccd0c5f0f1d0999b17e64cbf
parent 414ad00
2 files changed +25 -6
modified libpkg/pkgdb.c
@@ -586,6 +586,11 @@ pkgdb_open(struct pkgdb **db_p, pkgdb_t type)
	if (*db_p != NULL) {
		reopen = true;
		db = *db_p;
+
		if (db->lock_count) {
+
			db->lock_count = 1;
+
			if (pkgdb_unlock(db) != EPKG_OK)
+
				return (EPKG_FATAL);
+
		}
		if (db->type == type)
			return (EPKG_OK);
	}
@@ -599,6 +604,7 @@ pkgdb_open(struct pkgdb **db_p, pkgdb_t type)
	}

	db->type = type;
+
	db->lock_count = 0;

	if (!reopen) {
		snprintf(localpath, sizeof(localpath), "%s/local.sqlite", dbdir);
@@ -730,6 +736,10 @@ pkgdb_close(struct pkgdb *db)
		return;

	if (db->sqlite != NULL) {
+
		if (db->lock_count) {
+
			db->lock_count = 1;
+
			pkgdb_unlock(db);  /* Unlock failure is not terminal */
+
		}
		if (db->type == PKGDB_REMOTE) {
			pkgdb_detach_remotes(db->sqlite);
		}
@@ -3218,12 +3228,12 @@ pkgdb_query_fetch(struct pkgdb *db, match_t match, int nbpkgs, char **pkgs, cons
		sbuf_reset(sql);
		sbuf_printf(sql, deps_sql, reponame, reponame);
		sbuf_finish(sql);
-
		
+

		do {
			sql_exec(db->sqlite, sbuf_get(sql));
		} while (sqlite3_changes(db->sqlite) != 0);
	}
-
		
+

	sbuf_reset(sql);
	sbuf_printf(sql, weight_sql, reponame);
	sbuf_finish(sql);
@@ -3295,11 +3305,19 @@ pkgshell_open(const char **reponame)
int
pkgdb_lock(struct pkgdb *db)
{
-
	return sql_exec(db->sqlite, "PRAGMA main.locking_mode=EXCLUSIVE;BEGIN IMMEDIATE;COMMIT;");
+
	assert(db->lock_count >= 0);
+
	if (!(db->lock_count++))
+
		return sql_exec(db->sqlite, "PRAGMA main.locking_mode=EXCLUSIVE;BEGIN IMMEDIATE;COMMIT;");
+
	else
+
		return (EPKG_OK);
}

int
pkgdb_unlock(struct pkgdb *db)
{
-
	return sql_exec(db->sqlite, "PRAGMA main.locking_mode=NORMAL;BEGIN IMMEDIATE;COMMIT;");
+
	assert(db->lock_count >= 1);
+
	if (!(--db->lock_count))
+
		return sql_exec(db->sqlite, "PRAGMA main.locking_mode=NORMAL;BEGIN IMMEDIATE;COMMIT;");
+
	else
+
		return (EPKG_OK);
}
modified libpkg/private/pkgdb.h
@@ -2,7 +2,7 @@
 * Copyright (c) 2011-2012 Baptiste Daroussin <bapt@FreeBSD.org>
 * Copyright (c) 2011-2012 Julien Laffaye <jlaffaye@FreeBSD.org>
 * All rights reserved.
-
 * 
+
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
@@ -12,7 +12,7 @@
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
-
 * 
+
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
@@ -35,6 +35,7 @@
struct pkgdb {
	sqlite3 *sqlite;
	pkgdb_t type;
+
	int lock_count;
};

struct pkgdb_it {