A theory of self-pulsing lasers