add mutex to make the `function_impl` thread-safe