Time constrained pulseIn

In working on a project where some external hardware transmits a pulse coded signal using a transistor to pull a voltage to ground after a resistor.

This is fine, except I needed to use pulseIn() to check for the sync bit which runs high for a specific period (about 11ms). I also want to be able to check when the device is "off", but in that scenario the signal line stays high and the pulseIn() function waits for a full 3 minutes to expire before returning. That obviously would not work, and in this scenario interrupts were not really appropriate, so I added a pulseInMax() function based on what I found in wiring_pulse.c:
(added to wiring_pulse.c)

unsigned long pulseInMax(uint8_t pin, uint8_t state, unsigned long timeout, unsigned long maxlen)
{
      // cache the port and bit of the pin in order to speed up the
      // pulse width measuring loop and achieve finer resolution.  calling
      // digitalRead() instead yields much coarser resolution.
      uint8_t bit = digitalPinToBitMask(pin);
      uint8_t port = digitalPinToPort(pin);
      uint8_t stateMask = (state ? bit : 0);

      
      // convert the timeout from microseconds to a number of times through
      // the initial loop; it takes 16 clock cycles per iteration.
      unsigned long numloops = 0;
      unsigned long pulseloops = 0;
      unsigned long maxloops = microsecondsToClockCycles(timeout) / 16;
      unsigned long maxtimeloops = microsecondsToClockCycles(maxlen) /16;
      
      // wait for any previous pulse to end
      while ((*portInputRegister(port) & bit) == stateMask)
            if (numloops++ == maxloops)
                  return 0;
      
      // wait for the pulse to start
      while ((*portInputRegister(port) & bit) != stateMask)
            if (numloops++ == maxloops)
                  return 0;
      
      // wait for the pulse to stop
      while ((*portInputRegister(port) & bit) == stateMask)
            if (pulseloops++ == maxtimeloops)
                  return 0;

      // convert the reading to microseconds. The loop has been determined
      // to be 10 clock cycles long and have about 16 clocks between the edge
      // and the start of the loop. There will be some error introduced by
      // the interrupt handlers.
      return clockCyclesToMicroseconds(pulseloops * 16 + 16); 
}

It is also necessary to add to wiring.h:
unsigned long pulseInMax(uint8_t pin, uint8_t state, unsigned long timeout, unsigned long maxlen); //added by nate

Based on the assumption that the addition of a conditional branch added 6 cycles (as seemed to be indicated from the pulseIn() function) I just added a conditional to the final loop to add expiration logic if the loop limit was exceeded.

I realize this may drift the value slightly if my assumptions are off but in practice for the signal I am reading it works excellently and solved the problem completely.

I searched around but did not come across and implementation like this, so I apologize if it is redundant. I'm hoping to further test the accuracy of this against pulseIn in the near future.

----Nate