Exemplo n.º 1
0
        internal static SessionSemaphore ObtainCrossServerLock(HttpSessionState session, string sessionKey)
        {
            SessionSemaphore semaphore = null;
            var    identity            = Guid.NewGuid().ToString("N");   //compute identity before locking
            string semaphoreKey        = SEMAPHORE_PREFIX + sessionKey;  //compute key before locking

            lock (_singleServerLocks.GetOrAdd(sessionKey, new object())) //one thread per server per session key will pass beyond this point
            {
                do
                {
                    while (session[semaphoreKey] != null)
                    {
                        Thread.Sleep(1);
                    }
                    session[semaphoreKey] = identity;
                    Thread.Sleep(1);
                    if (session[semaphoreKey].ToString() == identity)
                    {
                        semaphore = new SessionSemaphore(identity, semaphoreKey, session);
                    }
                } while (semaphore == null);
            }
            return(semaphore);
        }
Exemplo n.º 2
0
 public static SessionSemaphore ObtainCrossServerLock(this HttpSessionState session, string sessionKey)
 {
     return(SessionSemaphore.ObtainCrossServerLock(session, sessionKey));
 }