let with_lock shared mutex f = try let res = lock shared mutex; f () in unlock shared mutex; res with e -> unlock shared mutex; raise e