Gallows humor is laughter in the face of the extreme — the joke made when the situation has removed all other options. Soldiers in trenches, patients with diagnoses, people at the edge of what they can bear: they all discover this register independently, because the alternative is a despair that helps no one. Nasreddin told jokes at funerals not because he was callous but because he understood that grief needs air, and laughter is how air gets in.
Each step builds on the last.