#pragma once #include template class RustMutexProxy { public: RustMutexProxy(T *data, std::mutex &mutex) : data_(data), mutex_(mutex) {} ~RustMutexProxy() { mutex_.unlock(); } T &operator*() { return *data_; } T *operator->() { return data_; } private: T *data_; std::mutex &mutex_; }; template class RustMutex { public: RustMutex(T && t) : t_(t) {} RustMutexProxy lock() { mutex_.lock(); return RustMutexProxy(&t_, mutex_); } private: T t_; std::mutex mutex_; }; // Note that name must be a global string that lives until the end of the process, // for AssertCurrentThreadName to work. void SetCurrentThreadName(const char *threadName); void AssertCurrentThreadName(const char *threadName);