boolean runActionAndCancelBeforeWrite(@NotNull Runnable cancellation, @NotNull Runnable action) {
    if (isWriteActionRunningOrPending(myApplication)) {
      cancellation.run();
      return false;
    }

    myWriteActionCancellations.add(cancellation);
    try {
      if (isWriteActionRunningOrPending(myApplication)) {
        // the listener might not be notified if write action was requested concurrently with the listener addition
        cancellation.run();
        return false;
      }
      action.run();
      return true;
    }
    finally {
      myWriteActionCancellations.remove(cancellation);
    }
  }
boolean runActionAndCancelBeforeWrite(@NotNull Runnable cancellation, @NotNull Runnable action)