Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Merge pull request #265 from DragonSA/recursive-lock
Baptiste Daroussin committed 13 years ago
commit 058a878e7a0391951fb5771b2b03eebb49ddb538
parent 0c156ff
2 files changed +18 -8
modified libpkg/pkgdb.c
@@ -619,6 +619,7 @@ pkgdb_open(struct pkgdb **db_p, pkgdb_t type)
	struct pkg_config_kv *repokv = NULL;

	if (*db_p != NULL) {
+
		assert((*db_p)->lock_count == 0);
		reopen = true;
		db = *db_p;
		if (db->type == type)
@@ -634,6 +635,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);
@@ -765,6 +767,7 @@ pkgdb_close(struct pkgdb *db)
		return;

	if (db->sqlite != NULL) {
+
		assert(db->lock_count == 0);
		if (db->type == PKGDB_REMOTE) {
			pkgdb_detach_remotes(db->sqlite);
		}
@@ -3352,17 +3355,23 @@ pkgshell_open(const char **reponame)
int
pkgdb_lock(struct pkgdb *db)
{
-
	assert(db != NULL);
-

-
	return sql_exec(db->sqlite, "PRAGMA main.locking_mode=EXCLUSIVE;BEGIN IMMEDIATE;COMMIT;");
+
        assert(db != NULL);
+
	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)
{
-
	assert(db != NULL);
-

-
	return sql_exec(db->sqlite, "PRAGMA main.locking_mode=NORMAL;BEGIN IMMEDIATE;COMMIT;");
+
        assert(db != NULL);
+
	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);
}

int64_t
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 {