Uploaded image for project: 'Harmony'
  1. Harmony
  2. HARMONY-6663

Time-consuming operation with lock held in ServerSocketChannelImpl.java

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Won't Fix
    • 6.0M1
    • None
    • Classlib
    • None
    • Windows XP

    Description

      I found a time-consuming operation with lock held in modules/nio/src/main/java/common/org/apache/harmony/nio/internal/ServerSocketChannelImpl.java

      public SocketChannel accept() throws IOException {
      ...
      synchronized (acceptLock) {
      boolean isBlocking = isBlocking();
      ...
      do {
      try

      { ((ServerSocketAdapter) socket).accept(socketGot, (SocketChannelImpl) sockChannel); // select successfully, break out immediately. break; } catch (SocketTimeoutException e) { // continue to accept if the channel is in blocking // mode. }
      } while (isBlocking);
      }
      ...
      }

      Thread A executes accept() method, holds a lock on "acceptLock ", but may block at socket.accept() for a long time.
      Thread B executes accept(), cannot acquire the lock on "acceptLock ".

      Consequence:
      Lock contends, significantly downgrade software performance.

      I suggest to move the do-while block out of the synchronization block as follow:
      public SocketChannel accept() throws IOException {
      ...
      boolean isBlocking = isBlocking();
      synchronized (acceptLock) { ... }
      do {
      try { ((ServerSocketAdapter) socket).accept(socketGot, (SocketChannelImpl) sockChannel); // select successfully, break out immediately. break; }

      catch (SocketTimeoutException e)

      { // continue to accept if the channel is in blocking // mode. }

      } while (isBlocking);

      ...
      }

      Attachments

        Activity

          People

            Unassigned Unassigned
            cashcrop Wendy Feng
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Time Tracking

                Estimated:
                Original Estimate - 7h
                7h
                Remaining:
                Remaining Estimate - 7h
                7h
                Logged:
                Time Spent - Not Specified
                Not Specified