void Lock_Destroy_IMP(Lock *self) { LockIVARS *const ivars = Lock_IVARS(self); DECREF(ivars->folder); DECREF(ivars->name); SUPER_DESTROY(self, LOCK); }
Lock* Lock_init(Lock *self, Folder *folder, String *name, int32_t timeout, int32_t interval) { LockIVARS *const ivars = Lock_IVARS(self); // Validate. if (interval <= 0) { DECREF(self); THROW(ERR, "Invalid value for 'interval': %i32", interval); } StringIterator *iter = Str_Top(name); int32_t code_point; while (STR_OOB != (code_point = StrIter_Next(iter))) { if (isalnum(code_point) || code_point == '.' || code_point == '-' || code_point == '_' ) { continue; } DECREF(self); THROW(ERR, "Lock name contains disallowed characters: '%o'", name); } DECREF(iter); // Assign. ivars->folder = (Folder*)INCREF(folder); ivars->timeout = timeout; ivars->name = Str_Clone(name); ivars->interval = interval; return self; }
bool Lock_Obtain_IMP(Lock *self) { LockIVARS *const ivars = Lock_IVARS(self); int32_t time_left = ivars->interval == 0 ? 0 : ivars->timeout; bool locked = Lock_Request(self); while (!locked) { time_left -= ivars->interval; if (time_left <= 0) { break; } Sleep_millisleep(ivars->interval); locked = Lock_Request(self); } if (!locked) { ERR_ADD_FRAME(Err_get_error()); } return locked; }
String* Lock_Get_Host_IMP(Lock *self) { return Lock_IVARS(self)->host; }
String* Lock_Get_Lock_Path_IMP(Lock *self) { return Lock_IVARS(self)->lock_path; }
String* Lock_Get_Name_IMP(Lock *self) { return Lock_IVARS(self)->name; }