Ejemplo n.º 1
0
        //
        // Returns 0 if lock was not acquired because somebody else already holds it
        // Else returns > 0
        // Does NOT BLOCK
        //
        public long AcquireLock(string itemID)
        {
            if (string.IsNullOrEmpty(itemID))
            {
                throw new ArgumentException("itemID");
            }

            lock (m_locks)
            {
                if (this.GetLock(itemID) != null)
                {
                    return(LockNotAcquired);
                }

                long lockID = this.NextLockID();
                Debug.Assert(RecordItemLockTable.IsValidLockID(lockID));

                m_locks[itemID] = LockData.Create(lockID);
                return(lockID);
            }
        }
Ejemplo n.º 2
0
        //
        // Will only write the item to the local store IF:
        //  - Can take the write lock on the item (no pending edits in progress)
        //  - There are no pending changes on the item
        //
        async Task SafePutItemInLocalStoreAsync(RecordItem item)
        {
            long lockID = m_itemLocks.AcquireLock(item.ID);

            if (!RecordItemLockTable.IsValidLockID(lockID))
            {
                return;  // Item is locked for editing. Don't overrwrite yet
            }

            try
            {
                // Make sure there are no pending updates
                if (!await m_changeManager.HasChangesForItemAsync(item.ID))
                {
                    await m_localStore.PutItemAsync(item);
                }
            }
            finally
            {
                m_itemLocks.SafeReleaseLock(item.ID, lockID);
            }
        }