Implemented lock in buffer to prevent race conditions with interrupts.

Note 1) It locks on write and won't read (read returns -1) while locked.
Note 2) Lock does NOT prevent writing — it's ONLY preventing reading-while-writing.
Note 3) Being "locked" does NOT prevent it from writing!