controller: use a lock for accessing Repositories. Fixes rare crashes when multiple threads want access

This commit is contained in:
Hylke Bons 2012-02-18 01:52:17 +01:00
parent aa610c818e
commit 35a7c13422

View file

@ -34,7 +34,7 @@ namespace SparkleShare {
public abstract class SparkleControllerBase {
public List <SparkleRepoBase> Repositories;
public List<SparkleRepoBase> Repositories = new List<SparkleRepoBase> ();
public readonly string SparklePath = SparkleConfig.DefaultConfig.FoldersPath;
public double ProgressPercentage = 0.0;
@ -93,6 +93,7 @@ namespace SparkleShare {
private List<string> failed_avatars = new List<string> ();
private Object avatar_lock = new Object ();
private Object repo_lock = new Object ();
// Short alias for the translations
@ -200,10 +201,12 @@ namespace SparkleShare {
get {
List<string> unsynced_folders = new List<string> ();
lock (this.repo_lock) {
foreach (SparkleRepoBase repo in Repositories) {
if (repo.HasUnsyncedChanges)
unsynced_folders.Add (repo.Name);
}
}
return unsynced_folders;
}
@ -236,6 +239,7 @@ namespace SparkleShare {
{
List<SparkleChangeSet> list = new List<SparkleChangeSet> ();
lock (this.repo_lock) {
foreach (SparkleRepoBase repo in Repositories) {
List<SparkleChangeSet> change_sets = repo.GetChangeSets (30);
@ -244,6 +248,7 @@ namespace SparkleShare {
else
SparkleHelpers.DebugInfo ("Log", "Could not create log for " + repo.Name);
}
}
list.Sort ((x, y) => (x.Timestamp.CompareTo (y.Timestamp)));
list.Reverse ();
@ -263,10 +268,12 @@ namespace SparkleShare {
string path = Path.Combine (SparkleConfig.DefaultConfig.FoldersPath, name);
int log_size = 50;
lock (this.repo_lock) {
foreach (SparkleRepoBase repo in Repositories) {
if (repo.LocalPath.Equals (path))
return repo.GetChangeSets (log_size);
}
}
return null;
}
@ -553,6 +560,7 @@ namespace SparkleShare {
bool has_syncing_repos = false;
bool has_unsynced_repos = false;
lock (this.repo_lock) {
foreach (SparkleRepoBase repo in Repositories) {
if (repo.Status == SyncStatus.SyncDown ||
repo.Status == SyncStatus.SyncUp ||
@ -564,7 +572,7 @@ namespace SparkleShare {
has_unsynced_repos = true;
}
}
}
if (has_syncing_repos) {
if (OnSyncing != null)
@ -646,7 +654,10 @@ namespace SparkleShare {
};
lock (this.repo_lock) {
Repositories.Add (repo);
}
repo.Initialize ();
}
@ -657,6 +668,7 @@ namespace SparkleShare {
{
string folder_name = Path.GetFileName (folder_path);
lock (this.repo_lock) {
for (int i = 0; i < Repositories.Count; i++) {
SparkleRepoBase repo = Repositories [i];
@ -668,14 +680,13 @@ namespace SparkleShare {
}
}
}
}
// Updates the list of repositories with all the
// folders in the SparkleShare folder
private void PopulateRepositories ()
{
Repositories = new List<SparkleRepoBase> ();
foreach (string folder_name in SparkleConfig.DefaultConfig.Folders) {
string folder_path = new SparkleFolder (folder_name).FullPath;
@ -1077,6 +1088,7 @@ namespace SparkleShare {
// quits if safe
public void TryQuit ()
{
lock (this.repo_lock) {
foreach (SparkleRepoBase repo in Repositories) {
if (repo.Status == SyncStatus.SyncUp ||
repo.Status == SyncStatus.SyncDown ||
@ -1085,6 +1097,7 @@ namespace SparkleShare {
return;
}
}
}
Quit ();
}
@ -1092,8 +1105,10 @@ namespace SparkleShare {
public void Quit ()
{
lock (this.repo_lock) {
foreach (SparkleRepoBase repo in Repositories)
repo.Dispose ();
}
Environment.Exit (0);
}
@ -1104,11 +1119,13 @@ namespace SparkleShare {
folder_name = folder_name.Replace ("%20", " ");
note = note.Replace ("%20", " ");
lock (this.repo_lock) {
foreach (SparkleRepoBase repo in Repositories) {
if (repo.Name.Equals (folder_name))
repo.AddNote (revision, note);
}
}
}
private string [] tango_palette = new string [] {"#eaab00", "#e37222",