public function get_lock($resource, $timeout, $maxlifetime = 86400) {
        $giveuptime = time() + $timeout;

        $hash = md5($this->type . '_' . $resource);
        $lockdir = $this->lockdirectory . '/' . substr($hash, 0, 2);

        if (!check_dir_exists($lockdir, true, true)) {
            return false;
        }

        $lockfilename = $lockdir . '/' . $hash;

        $filehandle = fopen($lockfilename, "wb");

        // Could not open the lock file.
        if (!$filehandle) {
            return false;
        }

        do {
            // Will block on windows. So sad.
            $wouldblock = false;
            $locked = flock($filehandle, LOCK_EX | LOCK_NB, $wouldblock);
            if (!$locked && $wouldblock && $timeout > 0) {
                usleep(rand(10000, 250000)); // Sleep between 10 and 250 milliseconds.
            }
            // Try until the giveup time.
        } while (!$locked && $wouldblock && time() < $giveuptime);

        if (!$locked) {
            fclose($filehandle);
            return false;
        }
        if ($this->verbose) {
            fwrite($filehandle, $this->get_debug_info());
        }
        return new lock($filehandle, $this);
    }