Example #1
0
    public void close() {
      synchronized (JTermiosImpl.this) {
        if (m_FD >= 0) {
          m_OpenPorts.remove(m_FD);
          m_PortFDs[m_FD] = false;
          m_FD = -1;
        }

        if (m_CancelWaitSema4 != null) SetEvent(m_CancelWaitSema4);
        if (m_Comm != null) {
          ResetEvent(m_SelOVL.hEvent);

          if (!CancelIo(m_Comm))
            log =
                log
                    && log(
                        1,
                        "CancelIo() failed, GetLastError()= %d, %s\n",
                        GetLastError(),
                        lineno(1));
          if (!PurgeComm(m_Comm, PURGE_TXABORT + PURGE_TXCLEAR + PURGE_RXABORT + PURGE_RXCLEAR))
            log =
                log
                    && log(
                        1,
                        "PurgeComm() failed, GetLastError()= %d, %s\n",
                        GetLastError(),
                        lineno(1));
        }
        HANDLE h; // / 'hEvent' might never have been 'read' so read it
        // to this var first

        synchronized (m_RdBuffer) {
          h = (HANDLE) m_RdOVL.readField("hEvent");
          m_RdOVL = null;
          if (h != null && !h.equals(NULL) && !h.equals(INVALID_HANDLE_VALUE)) CloseHandle(h);
        }

        synchronized (m_WrBuffer) {
          h = (HANDLE) m_WrOVL.readField("hEvent");
          m_WrOVL = null;

          if (h != null && !h.equals(NULL) && !h.equals(INVALID_HANDLE_VALUE)) CloseHandle(h);
        }

        // Ensure that select() is through before releasing the m_SelOVL
        waitUnlock();

        h = (HANDLE) m_SelOVL.readField("hEvent");
        m_SelOVL = null;
        if (h != null && !h.equals(NULL) && !h.equals(INVALID_HANDLE_VALUE)) CloseHandle(h);

        if (m_Comm != null && m_Comm != NULL && m_Comm != INVALID_HANDLE_VALUE) CloseHandle(m_Comm);
        m_Comm = null;
      }
    }
 @cli.System.Security.SecuritySafeCriticalAttribute.Annotation
 void release(FileDescriptor fd, long pos, long size) throws IOException {
     FileStream fs = (FileStream)fd.getStream();
     if (WINDOWS)
     {
         int ERROR_NOT_LOCKED = 158;
         OVERLAPPED o = new OVERLAPPED();
         o.OffsetLow = (int)pos;
         o.OffsetHigh = (int)(pos >> 32);
         int result = UnlockFileEx(fs.get_SafeFileHandle(), 0, (int)size, (int)(size >> 32), o);
         if (result == 0 && Marshal.GetLastWin32Error() != ERROR_NOT_LOCKED)
         {
             throw new IOException("Release failed");
         }
     }
     else
     {
         try
         {
             if (false) throw new cli.System.ArgumentOutOfRangeException();
             if (false) throw new cli.System.IO.IOException();
             if (false) throw new cli.System.ObjectDisposedException("");
             fs.Unlock(pos, size);
         }
         catch (cli.System.IO.IOException x)
         {
             if (!NotLockedHack.isErrorNotLocked(x))
             {
                 throw new IOException(x.getMessage());
             }
         }
         catch (cli.System.ArgumentOutOfRangeException
             | cli.System.ObjectDisposedException x)
         {
             throw new IOException(x.getMessage());
         }
     }
 }
 @cli.System.Security.SecuritySafeCriticalAttribute.Annotation
 int lock(FileDescriptor fd, boolean blocking, long pos, long size,
          boolean shared) throws IOException
 {
     FileStream fs = (FileStream)fd.getStream();
     if (WINDOWS)
     {
         int LOCKFILE_FAIL_IMMEDIATELY = 1;
         int LOCKFILE_EXCLUSIVE_LOCK = 2;
         int ERROR_LOCK_VIOLATION = 33;
         int flags = 0;
         OVERLAPPED o = new OVERLAPPED();
         o.OffsetLow = (int)pos;
         o.OffsetHigh = (int)(pos >> 32);
         if (!blocking)
         {
             flags |= LOCKFILE_FAIL_IMMEDIATELY;
         }
         if (!shared)
         {
             flags |= LOCKFILE_EXCLUSIVE_LOCK;
         }
         int result = LockFileEx(fs.get_SafeFileHandle(), flags, 0, (int)size, (int)(size >> 32), o);
         if (result == 0)
         {
             int error = Marshal.GetLastWin32Error();
             if (!blocking && error == ERROR_LOCK_VIOLATION)
             {
                 return NO_LOCK;
             }
             throw new IOException("Lock failed");
         }
         return LOCKED;
     }
     else
     {
         try
         {
             if (false) throw new cli.System.ArgumentOutOfRangeException();
             for (;;)
             {
                 try
                 {
                     if (false) throw new cli.System.IO.IOException();
                     if (false) throw new cli.System.ObjectDisposedException("");
                     fs.Lock(pos, size);
                     return shared ? RET_EX_LOCK : LOCKED;
                 }
                 catch (cli.System.IO.IOException x)
                 {
                     if (!blocking)
                     {
                         return NO_LOCK;
                     }
                     cli.System.Threading.Thread.Sleep(100);
                 }
                 catch (cli.System.ObjectDisposedException x)
                 {
                     throw new IOException(x.getMessage());
                 }
             }
         }
         catch (cli.System.ArgumentOutOfRangeException x)
         {
             throw new IOException(x.getMessage());
         }
     }
 }