Instance Lock() { ELock_lock(&m_interlock); if (!m_tid) { m_tid = pthread_self(); } else { if (m_tid != pthread_self()) { /// 这里先解锁了 ELock_unlock(&m_interlock); /// while (1) { ELock_lock(&m_interlock); if (!m_tid) { /// break以后实际上是锁住的 m_tid = pthread_self(); break; } ELock_unlock(&m_interlock); } } } AtomicIncrement(&m_lock); ELock_unlock(&m_interlock); return Instance(this); }
~Instance() { ELock_lock(&m_prototype->m_interlock); if(!AtomicDecrement(&m_prototype->m_lock)) { m_prototype->m_tid = NULL; } ELock_unlock(&m_prototype->m_interlock); }
void ELog_Init() { ELock_lock(&g_elog_lock); #ifdef USE_LOG_SYSTEM if (!g_elog_file) { #if defined(_WIN32) || defined(_WIN64) g_elog_file = SafeFOpen("log.log", "w+"); #elif defined(__APPLE__) char homeDir[256]; GetHomeDirectory(homeDir, 255); char logFilename[256]; snprintf(logFilename, 255, "%s/Documents/log.log", homeDir); g_elog_file = SafeFOpen(logFilename, "w+"); #endif } #endif ELock_unlock(&g_elog_lock); }
inline Instance Lock() { ELock_lock(&m_lock); return Instance(&m_lock, &m_data); }