OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Native Client Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "native_client/src/shared/ppapi_proxy/proxy_var_cache.h" |
| 6 |
| 7 #include <limits> |
| 8 #include <map> |
| 9 |
| 10 #include "native_client/src/include/ref_counted.h" |
| 11 #include "native_client/src/untrusted/pthread/pthread.h" |
| 12 |
| 13 namespace ppapi_proxy { |
| 14 |
| 15 namespace { |
| 16 |
| 17 pthread_mutex_t mu = PTHREAD_MUTEX_INITIALIZER; |
| 18 |
| 19 } // namespace |
| 20 |
| 21 ProxyVarCache* ProxyVarCache::cache_singleton = NULL; |
| 22 |
| 23 ProxyVarCache& ProxyVarCache::GetInstance() { |
| 24 // When the deprecated scripting is removed, this crash should disappear. |
| 25 // static ProxyVarCache cache_singleton; |
| 26 // return cache_singleton; |
| 27 pthread_mutex_lock(&mu); |
| 28 if (cache_singleton == NULL) |
| 29 cache_singleton = new ProxyVarCache(); |
| 30 pthread_mutex_unlock(&mu); |
| 31 return *cache_singleton; |
| 32 } |
| 33 |
| 34 void ProxyVarCache::RetainSharedProxyVar(const SharedProxyVar& proxy_var) { |
| 35 pthread_mutex_lock(&mu); |
| 36 // This implements "insert if absent, retain if present". |
| 37 std::pair<ProxyVarDictionary::iterator, bool> insert_result = |
| 38 proxy_var_cache_.insert( |
| 39 std::pair<int64_t, SharedProxyVar>(proxy_var->id(), proxy_var)); |
| 40 if (!insert_result.second) { |
| 41 // Object already exists, bump the retain count. |
| 42 insert_result.first->second->AddRef(); |
| 43 } |
| 44 pthread_mutex_unlock(&mu); |
| 45 } |
| 46 |
| 47 void ProxyVarCache::RetainProxyVar(const PP_Var& var) { |
| 48 if (!IsCachedType(var)) |
| 49 return; |
| 50 pthread_mutex_lock(&mu); |
| 51 ProxyVarDictionary::iterator iter = proxy_var_cache_.find(var.value.as_id); |
| 52 if (iter != proxy_var_cache_.end()) { |
| 53 iter->second->AddRef(); |
| 54 } |
| 55 pthread_mutex_unlock(&mu); |
| 56 } |
| 57 |
| 58 void ProxyVarCache::ReleaseProxyVar(const PP_Var& var) { |
| 59 if (!IsCachedType(var)) |
| 60 return; |
| 61 pthread_mutex_lock(&mu); |
| 62 ProxyVarDictionary::iterator iter = proxy_var_cache_.find(var.value.as_id); |
| 63 if (iter != proxy_var_cache_.end()) { |
| 64 // Decrement the reference count by one, as requested. |
| 65 iter->second->Release(); |
| 66 // All var reference count updating happens while mu is held, so the |
| 67 // potential race between HasOneRef and AddRef/Release cannot materialize. |
| 68 if (iter->second->HasOneRef()) { |
| 69 // If there is only one reference, it must be from the dictionary. |
| 70 // Delete the dictionary entry. |
| 71 proxy_var_cache_.erase(iter); |
| 72 } |
| 73 } |
| 74 pthread_mutex_unlock(&mu); |
| 75 } |
| 76 |
| 77 SharedProxyVar ProxyVarCache::SharedProxyVarForVar(PP_Var pp_var) const { |
| 78 SharedProxyVar proxy_var; |
| 79 pthread_mutex_lock(&mu); |
| 80 ProxyVarDictionary::const_iterator iter = |
| 81 proxy_var_cache_.find(pp_var.value.as_id); |
| 82 if (iter != proxy_var_cache_.end()) { |
| 83 proxy_var = iter->second; |
| 84 } |
| 85 pthread_mutex_unlock(&mu); |
| 86 return proxy_var; |
| 87 } |
| 88 |
| 89 } // namespace ppapi_proxy |
OLD | NEW |