Anharmonic phonon lifetimes in semiconductors from density-functional perturbation theory