/** * Description : * Simulation of mutex lock, it works while little access only. * * History: * 1. 2005/09/07 create by Moky <mokymo@tencent.com> * 2. 2006/05/23 modified by Moky <mokymo@tencent.com> */ if (typeof g_mutex_lock == "undefined") { g_mutex_lock = new Array(); } function mutex_check_init() { if (!g_mutex_lock) return -1; if (!g_mutex_lock.constructor) return -1; if (g_mutex_lock.constructor.toString().toLowerCase().indexOf("array") == -1) return -1; return 0; } function mutex_lock(name) { var max_sig = 1; if (mutex_check_init() < 0) return -1; if (!g_mutex_lock[name]) g_mutex_lock[name] = 0; /* try lock */ g_mutex_lock[name]++; if (g_mutex_lock[name] > max_sig) {/* lock failed, roll back */ g_mutex_lock[name]--; return -1; } return 0; } function mutex_unlock(name) { if (mutex_check_init() < 0) return -1; if (g_mutex_lock[name] <= 0) return -1; g_mutex_lock[name]--; return 0; }