protected static function get_course_cache_lock($courseid) {
        // Get database lock to ensure this doesn't happen multiple times in parallel. Wait a
        // reasonable time for the lock to be released, so we can give a suitable error message.
        // In case the system crashes while building the course cache, the lock will automatically
        // expire after a (slightly longer) period.
        $lockfactory = \core\lock\lock_config::get_lock_factory('core_modinfo');
        $lock = $lockfactory->get_lock('build_course_cache_' . $courseid,
                self::COURSE_CACHE_LOCK_WAIT, self::COURSE_CACHE_LOCK_EXPIRY);
        if (!$lock) {
            throw new moodle_exception('locktimeout', '', '', null,
                    'core_modinfo/build_course_cache_' . $courseid);
        }
        return $lock;
    }