/* * Copyright 2010-2014 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. * A copy of the License is located at * * http://aws.amazon.com/apache2.0 * * or in the "license" file accompanying this file. This file is distributed * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * express or implied. See the License for the specific language governing * permissions and limitations under the License. */ using System; using System.Collections.Generic; namespace Amazon.Runtime.Internal { /// /// A singleton which stores the list of pending web requests and pending callbacks. /// A singleton is used to maintain this state globally across multiple Unity scenes. /// public sealed class UnityRequestQueue { private static readonly UnityRequestQueue _instance = new UnityRequestQueue(); private static readonly object _requestsLock = new object(); private static readonly object _callbacksLock = new object(); private static readonly object _mainThreadCallbackLock = new object(); private Queue _requests = new Queue(); private Queue _callbacks = new Queue(); private Queue _mainThreadCallbacks = new Queue(); /// /// The private contructor for the singleton class. /// private UnityRequestQueue() { } /// /// Returns a singleton instance of UnityRequestQueue. /// public static UnityRequestQueue Instance { get { return _instance; } } /// /// Enqueues a request to be processed by the UnityMainThreadDispatcher. /// Unity /// /// An instance of UnityWebRequest. public void EnqueueRequest(IUnityHttpRequest request) { lock (_requestsLock) { _requests.Enqueue(request); } } /// /// Dequeues a request from the queue of pending requests. /// /// An instance of UnityWebRequest. public IUnityHttpRequest DequeueRequest() { IUnityHttpRequest request = null; lock (_requestsLock) { if (_requests.Count > 0) { request = _requests.Dequeue(); } } return request; } /// /// Enqueues a callback method that should be invoked on the /// main thread by the UnityMainThreadDispatcher. /// /// Instance of RuntimeAsyncResult that contains /// all the information about the callback. public void EnqueueCallback(RuntimeAsyncResult asyncResult) { lock (_callbacksLock) { _callbacks.Enqueue(asyncResult); } } /// /// Dequeues an callback from the queue of pending callbacks that /// need to be invoked. /// /// public RuntimeAsyncResult DequeueCallback() { RuntimeAsyncResult asyncResult = null; lock (_callbacksLock) { if (_callbacks.Count > 0) { asyncResult = _callbacks.Dequeue(); } } return asyncResult; } /// /// Enqueue an anonymous method on main thread /// /// public void ExecuteOnMainThread(Action action) { lock(_mainThreadCallbackLock) { _mainThreadCallbacks.Enqueue(action); } } public Action DequeueMainThreadOperation() { Action action = null; lock(_mainThreadCallbackLock) { if (_mainThreadCallbacks.Count > 0) { action = _mainThreadCallbacks.Dequeue(); } } return action; } } }