make fbuf thread safe

in the meaning that it safely accesses the two global static variables it uses
(global_max_len and fbuf_id) so that it can be safely used in multiple threads without
risking race conditions when accessing those globals