core: implement rise-fall counting for overlapping pulses to be handled correctly.

Consider this sequence (done e. g. by two independent threads):
raise--raise--put--put
Previous handling:
raise---------put-----
New handling:
raise--------------put

The idea is similar to recursive mutexes or ref-counting.