Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/08/11 00:04:16 (13 years ago)
Author:
cneumuel
Message:

#1233

  • locking for childHiveJobs in OptimizerHiveJob avoid multi threaded access issues
  • added IsPrivileged to gui
  • minor changes
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.4/HiveJobs/HiveJob.cs

    r6373 r6381  
    2525using System.Drawing;
    2626using System.Linq;
     27using System.Threading;
    2728using HeuristicLab.Collections;
    2829using HeuristicLab.Common;
     
    3738  public class HiveJob : NamedItem, IItemTree<HiveJob> {
    3839    protected static object locker = new object();
     40    protected ReaderWriterLockSlim childHiveJobsLock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
    3941
    4042    public override Image ItemImage {
     
    109111    protected ItemList<HiveJob> childHiveJobs;
    110112    public virtual ReadOnlyItemList<HiveJob> ChildHiveJobs {
    111       get { return childHiveJobs.AsReadOnly(); }
     113      get {
     114        childHiveJobsLock.EnterReadLock();
     115        try {
     116          return childHiveJobs.AsReadOnly();
     117        }
     118        finally { childHiveJobsLock.ExitReadLock(); }
     119      }
    112120    }
    113121
     
    117125    #region Constructors and Cloning
    118126    public HiveJob() {
    119       this.Job = new Job() {
    120         CoresNeeded = 1,
    121         MemoryNeeded = 0
    122       };
     127      this.Job = new Job() { CoresNeeded = 1, MemoryNeeded = 0 };
    123128      job.State = JobState.Offline;
    124129      this.childHiveJobs = new ItemList<HiveJob>();
     
    152157      this.Job = cloner.Clone(original.job);
    153158      this.ItemJob = cloner.Clone(original.ItemJob);
    154       this.childHiveJobs = cloner.Clone(original.childHiveJobs);
     159      original.childHiveJobsLock.EnterReadLock();
     160      try {
     161        this.childHiveJobs = cloner.Clone(original.childHiveJobs);
     162      }
     163      finally { original.childHiveJobsLock.ExitReadLock(); }
    155164      this.syncJobsWithOptimizers = original.syncJobsWithOptimizers;
    156165      this.isFinishedJobDownloaded = original.isFinishedJobDownloaded;
     
    198207
    199208    public virtual void AddChildHiveJob(HiveJob hiveJob) {
    200       this.childHiveJobs.Add(hiveJob);
     209      childHiveJobsLock.EnterWriteLock();
     210      try {
     211        this.childHiveJobs.Add(hiveJob);
     212      }
     213      finally { childHiveJobsLock.ExitWriteLock(); }
    201214    }
    202215
     
    237250      IEnumerable<Type> usedTypes;
    238251      byte[] jobByteArray = PersistenceUtil.Serialize(this.ItemJob, out usedTypes);
    239 
    240       JobData jobData = new JobData() {
    241         JobId = job.Id,
    242         Data = jobByteArray
    243       };
    244 
     252      JobData jobData = new JobData() { JobId = job.Id, Data = jobByteArray };
    245253      PluginUtil.CollectDeclaringPlugins(plugins, usedTypes);
    246 
    247254      return jobData;
    248255    }
     
    295302    /// </summary>
    296303    public IEnumerable<HiveJob> GetAllHiveJobs() {
    297       var jobs = new List<HiveJob>();
    298       jobs.Add(this);
    299       foreach (HiveJob child in this.ChildHiveJobs) {
    300         jobs.AddRange(child.GetAllHiveJobs());
    301       }
    302       return jobs;
     304      childHiveJobsLock.EnterReadLock();
     305      try {
     306        var jobs = new List<HiveJob>();
     307        jobs.Add(this);
     308        foreach (HiveJob child in this.childHiveJobs) {
     309          jobs.AddRange(child.GetAllHiveJobs());
     310        }
     311        return jobs;
     312      }
     313      finally { childHiveJobsLock.ExitReadLock(); }
    303314    }
    304315
    305316    public HiveJob GetParentByJobId(Guid jobId) {
    306       if (this.ChildHiveJobs.SingleOrDefault(j => j.job.Id == jobId) != null)
    307         return this;
    308       foreach (HiveJob child in this.childHiveJobs) {
    309         HiveJob result = child.GetParentByJobId(jobId);
    310         if (result != null)
    311           return result;
    312       }
    313       return null;
     317      childHiveJobsLock.EnterReadLock();
     318      try {
     319        if (this.ChildHiveJobs.SingleOrDefault(j => j.job.Id == jobId) != null)
     320          return this;
     321        foreach (HiveJob child in this.childHiveJobs) {
     322          HiveJob result = child.GetParentByJobId(jobId);
     323          if (result != null)
     324            return result;
     325        }
     326        return null;
     327      }
     328      finally { childHiveJobsLock.ExitWriteLock(); }
    314329    }
    315330
     
    321336        return this;
    322337      } else {
    323         foreach (HiveJob child in this.ChildHiveJobs) {
    324           HiveJob result = child.GetHiveJobByJobId(jobId);
    325           if (result != null)
    326             return result;
    327         }
     338        childHiveJobsLock.EnterReadLock();
     339        try {
     340          foreach (HiveJob child in this.childHiveJobs) {
     341            HiveJob result = child.GetHiveJobByJobId(jobId);
     342            if (result != null)
     343              return result;
     344          }
     345        }
     346        finally { childHiveJobsLock.ExitReadLock(); }
    328347      }
    329348      return null;
     
    331350
    332351    public void RemoveByJobId(Guid jobId) {
    333       IEnumerable<HiveJob> jobs = ChildHiveJobs.Where(j => j.Job.Id == jobId).ToList(); // if Guid.Empty needs to be removed, there could be more than one with this jobId
    334       foreach (HiveJob j in jobs) {
    335         this.childHiveJobs.Remove(j);
    336       }
    337       foreach (HiveJob child in ChildHiveJobs) {
    338         child.RemoveByJobId(jobId);
    339       }
     352      childHiveJobsLock.EnterWriteLock();
     353      try {
     354        IEnumerable<HiveJob> jobs = childHiveJobs.Where(j => j.Job.Id == jobId).ToList();
     355        foreach (HiveJob j in jobs) {
     356          this.childHiveJobs.Remove(j);
     357        }
     358        foreach (HiveJob child in childHiveJobs) {
     359          child.RemoveByJobId(jobId);
     360        }
     361      }
     362      finally { childHiveJobsLock.ExitWriteLock(); }
    340363    }
    341364
    342365    public IEnumerable<IItemTree<HiveJob>> GetChildItems() {
    343       return this.childHiveJobs;
     366      return this.ChildHiveJobs;
    344367    }
    345368
     
    371394    public void Pause() {
    372395      if (this.Job.IsParentJob) {
    373         foreach (var child in ChildHiveJobs) {
    374           ServiceLocator.Instance.CallHiveService(s => s.PauseJob(child.job.Id));
    375         }
     396        childHiveJobsLock.EnterReadLock();
     397        try {
     398          foreach (var child in childHiveJobs) {
     399            ServiceLocator.Instance.CallHiveService(s => s.PauseJob(child.job.Id));
     400          }
     401        }
     402        finally { childHiveJobsLock.ExitReadLock(); }
    376403      } else {
    377404        ServiceLocator.Instance.CallHiveService(s => s.PauseJob(this.job.Id));
     
    381408    public void Stop() {
    382409      if (this.Job.IsParentJob) {
    383         foreach (var child in ChildHiveJobs) {
    384           ServiceLocator.Instance.CallHiveService(s => s.StopJob(child.job.Id));
    385         }
     410        childHiveJobsLock.EnterReadLock();
     411        try {
     412          foreach (var child in childHiveJobs) {
     413            ServiceLocator.Instance.CallHiveService(s => s.StopJob(child.job.Id));
     414          }
     415        }
     416        finally { childHiveJobsLock.ExitReadLock(); }
    386417      } else {
    387418        ServiceLocator.Instance.CallHiveService(s => s.StopJob(this.job.Id));
Note: See TracChangeset for help on using the changeset viewer.