Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Copyright 2019 The WebRTC project authors. All Rights Reserved.
- *
- * Use of this source code is governed by a BSD-style license
- * that can be found in the LICENSE file in the root of the source
- * tree. An additional intellectual property rights grant can be found
- * in the file PATENTS. All contributing project authors may
- * be found in the AUTHORS file in the root of the source tree.
- */
- #include "rtc_base/synchronization/yield_policy.h"
- #include "absl/base/attributes.h"
- #include <thread>
- #include <mutex>
- #include <map>
- #include <sys/types.h>
- template<typename T>
- class ThreadLocalValue {
- public:
- T get() {
- std::thread::id thread_id = std::this_thread::get_id();
- m.lock();
- T result = tr[thread_id];
- m.unlock();
- return result;
- }
- void set(T t) {
- std::thread::id thread_id = std::this_thread::get_id();
- m.lock();
- tr[thread_id] = t;
- m.unlock();
- }
- private:
- std::map<std::thread::id, T> tr;
- std::mutex m;
- };
- namespace rtc {
- ThreadLocalValue<YieldInterface *>& getInstance() {
- /* Have a static local variable representing the unique instance. Since
- * it's static, there is only one instance of this variable. It's also only
- * initialized when getInstance is called.
- */
- static ThreadLocalValue<YieldInterface *> *theInstance = new ThreadLocalValue<YieldInterface*>();
- return *theInstance;
- }
- ScopedYieldPolicy::ScopedYieldPolicy(YieldInterface* policy)
- : previous_(getInstance().get()) {
- getInstance().set(policy);
- }
- ScopedYieldPolicy::~ScopedYieldPolicy() {
- getInstance().set(previous_);
- }
- void ScopedYieldPolicy::YieldExecution() {
- YieldInterface *inter = getInstance().get();
- if (inter) {
- inter->YieldExecution();
- }
- }
- } // namespace rtc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement