コード例 #1
0
        public Resource Lock(string resource)
        {
            LockWrapper wrapper = null;
            bool        own     = false;

            lock (instanceLock) {
                if (resourceLocks.ContainsKey(resource))
                {
                    wrapper = resourceLocks[resource];
                    wrapper.counter++;
                }
                else
                {
                    wrapper = new LockWrapper();
                    wrapper.instanceLock = new object();
                    wrapper.counter      = 1;
                    wrapper.taken        = true;
                    own = true;
                    resourceLocks[resource] = wrapper;
                }
            }
            if (!own)
            {
                lock (wrapper.instanceLock) {
                    while (wrapper.taken)
                    {
                        Monitor.Wait(wrapper.instanceLock);
                    }
                }
            }
            return(new Resource(this, resource));
        }
コード例 #2
0
 private static object getLockOn(Guid key, Dictionary <Guid, LockWrapper> dict)
 {
     lock (dict)
     {
         if (!dict.TryGetValue(key, out LockWrapper value))
         {
             value  = new LockWrapper();
             U[key] = value;
         }
         else
         {
             value.Useing++;
         }
         return(value.MyLock);
     }
 }