Add a flag for each gear which tells in which direction it is turning.
Then do a distance check like proposed by Pappenheimer.

If two gears with different rotation direction are in distance of a gear, then you have a lock.

Two connected gears always require to have different rotation direction.