<html>
  <head>
    <meta content="text/html; charset=windows-1252"
      http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    Hello Miguel,<br>
    <br>
    You can see in the code, the last timed element is fclose(f), which
    is just closing the file used for diagnostics writing in the timing
    code, which when removed, still leaves 6900ms (it varies between
    5000ms and > 10000ms).<br>
    <br>
    But I will look at if there is something else I can do based on your
    hint.<br>
    <br>
    Regards,<br>
    Jason.<br>
    <br>
    NSERIAL_EXPORT int WINAPI serial_close(struct serialhandle *handle)<br>
    {<br>
      clock_t start, end;<br>
    <br>
      FILE *f;<br>
      f =
    fopen("/home/jcurl/Programming/SerialPortStream/dll/serialunix/build/out.txt",
    "a");<br>
      fprintf(f, "c: serial_close(handle=%p)\n", handle);<br>
      start = clock();<br>
    <br>
      if (handle == NULL) {<br>
        errno = EINVAL;<br>
        return -1;<br>
      }<br>
    <br>
      fprintf(f, "c: serial_close(handle=%p) fd=%d\n", handle,
    handle->fd);<br>
      if (handle->fd == -1) return 0;<br>
      flushbuffer(handle);<br>
      end = clock();<br>
      fprintf(f, "c: serial_close(handle=%p) flushbuffer elapsed=%f\n",<br>
         handle,<br>
         ((double)1000.0*(end-start))/CLOCKS_PER_SEC);<br>
    <br>
      if (handle->prfd != -1) {<br>
        close(handle->prfd);<br>
        handle->prfd = -1;<br>
      }<br>
      end = clock();<br>
      fprintf(f, "c: serial_close(handle=%p) close(prfd) elapsed=%f\n",<br>
         handle,<br>
         ((double)1000.0*(end-start))/CLOCKS_PER_SEC);<br>
      if (handle->pwfd != -1) {<br>
        close(handle->pwfd);<br>
        handle->pwfd = -1;<br>
      }<br>
      end = clock();<br>
      fprintf(f, "c: serial_close(handle=%p) close(pwfd) elapsed=%f\n",<br>
         handle,<br>
         ((double)1000.0*(end-start))/CLOCKS_PER_SEC);<br>
    <br>
      close(handle->fd);<br>
      handle->fd = -1;<br>
    <br>
      end = clock();<br>
      fprintf(f, "c: serial_close(handle=%p) close(fd) elapsed=%f\n",<br>
         handle,<br>
         ((double)1000.0*(end-start))/CLOCKS_PER_SEC);<br>
      fclose(f);<br>
    <br>
      return 0;<br>
    }<br>
    <br>
    <br>
    <div class="moz-cite-prefix">On 23/04/2016 17:40, Jason Curl wrote:<br>
    </div>
    <blockquote cite="mid:571B9786.9070001@arcor.de" type="cite">
      <meta content="text/html; charset=windows-1252"
        http-equiv="Content-Type">
      Hello Miguel,<br>
      <br>
      > c: serial_close(handle=0x7fe044008d80) close(fd)
      elapsed=21,530000<br>
      <br>
      The time is from when the function entered (using the clock()
      function) and the end.<br>
      <br>
      There seems to be about 6900ms between calling the P/Invoke method
      and when the function actually gets called. You'll see a second
      call later works as expected (the C method takes 0,066000ms and
      the .NET says 0ms using).<br>
      <br>
      <div class="moz-cite-prefix">On 23/04/2016 17:15, Miguel de Icaza
        wrote:<br>
      </div>
      <blockquote
cite="mid:CANqeOFoqp2+ofCwkC1kPg-_60sOJC_A6QwHNAH_9qTOXGGQ6dg@mail.gmail.com"
        type="cite">Time the C calls, it is possible that close is
        flushing the data and is not returning until that happebs<br>
        <div class="gmail_quote">
          <div dir="ltr">On Sat, Apr 23, 2016 at 9:33 AM Jason Curl <<a
              moz-do-not-send="true" class="moz-txt-link-abbreviated"
              href="mailto:jcurlnews@arcor.de"><a class="moz-txt-link-abbreviated" href="mailto:jcurlnews@arcor.de">jcurlnews@arcor.de</a></a>>

            wrote:<br>
          </div>
          <blockquote class="gmail_quote" style="margin:0 0 0
            .8ex;border-left:1px #ccc solid;padding-left:1ex">I've
            tested the latest snapshot of Mono and the problem remains:<br>
            <br>
            Mono JIT compiler version 4.5.0 (Nightly 4.5.0.421/c5789eb
            Thu Mar 24<br>
            18:42:59 UTC 2016)<br>
            Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and
            Contributors.<br>
            <a moz-do-not-send="true" href="http://www.mono-project.com"
              rel="noreferrer" target="_blank">www.mono-project.com</a><br>
                 TLS:           __thread<br>
                 SIGSEGV:       altstack<br>
                 Notifications: epoll<br>
                 Architecture:  amd64<br>
                 Disabled:      none<br>
                 Misc:          softdebug<br>
                 LLVM:          supported, not enabled.<br>
                 GC:            sgen<br>
            <br>
            nUnit-gui now works properly (4.2.3.4 would hang when trying
            to close)<br>
            and using MONO debug variables (MONO_LOG_MASK="dll";<br>
            MONO_LOG_LEVEL="debug", I can see that the method
            "serial_close" is<br>
            called at the correct time, but I don't see any other
            activity while<br>
            blocked.<br>
            <br>
            Mono: DllImport searching in: 'nserial' ('libnserial.so').<br>
            Mono: Searching for 'serial_close'.<br>
            Mono: Probing 'serial_close'.<br>
            Mono: Found as 'serial_close'.<br>
            <br>
            PAUSE FOR 6 SECONDS<br>
            <br>
            Mono: DllImport searching in: 'nserial' ('libnserial.so').<br>
            Mono: Searching for 'serial_read'.<br>
            Mono: Probing 'serial_read'.<br>
            Mono: Found as 'serial_read'.<br>
            Mono: DllImport searching in: 'nserial' ('libnserial.so').<br>
            Mono: Searching for 'serial_terminate'.<br>
            Mono: Probing 'serial_terminate'.<br>
            Mono: Found as 'serial_terminate'.<br>
            <br>
            Any help is appreciated.<br>
            <br>
            On 22.04.2016 23:37, Jason Curl wrote:<br>
            > I've generally got my library working<br>
            > (<a moz-do-not-send="true"
              href="http://github.com/jcurl/serialportstream"
              rel="noreferrer" target="_blank">github.com/jcurl/serialportstream</a>)
            on Linux with Mono 4.2.3.4 from<br>
            > the Xamarin repositories on Ubuntu 16.04, with one
            small problem.<br>
            ><br>
            > One new test case (ClosedWhenBlocked) takes 6923ms for
            a simple<br>
            > close() function using marshalling, where the function
            in C only needs<br>
            > 22ms. It's called twice (there's two instances), the
            second is<br>
            > effectively instantaneous. On my machine, the issue is
            100%<br>
            > reproducible. My C code and .NET is OK (as far as I can
            tell) and<br>
            > appears to be some blocking behaviour in the
            Marshalling code of the<br>
            > .NET framework. How can I debug further if it's me or
            .NET?<br>
            ><br>
            > Note, I can't compare on Windows as it's Linux specific
            code.<br>
            ><br>
            ><br>
            > Closing serialSource<br>
            > ...<br>
            > --> serial_close(140601190288768)   =>
            0x7FE044008D80<br>
            > ...<br>
            > -->  serial_close(140601190288768) took 6916 ms<br>
            > IO.Ports.SerialPortStream Verbose: 0 : --><br>
            > serial_close(140601190288768) took 6916 ms<br>
            > ...<br>
            > Closed serialSource after 6923 ms<br>
            > ...<br>
            > --> serial_close(140601190278688)  =>
            0x7FE044006620<br>
            > -->  serial_close(140601190278688) took 0 ms<br>
            > -->  serial_close(140601190278688) took 0 ms<br>
            ><br>
            ><br>
            ><br>
            > The code in question is<br>
            >         [DllImport("nserial",
            EntryPoint="serial_close",<br>
            > SetLastError=true)]<br>
            >         private static extern int nserial_close(IntPtr
            handle);<br>
            >         public int serial_close(IntPtr handle)<br>
            >         {<br>
            >             System.Diagnostics.Stopwatch sw = new<br>
            > System.Diagnostics.Stopwatch();<br>
            >             sw.Start();<br>
            >             Console.WriteLine("-->
            serial_close({0})", handle);<br>
            >             int result = nserial_close(handle);<br>
            >             Console.WriteLine("--> 
            serial_close({0}) took {1} ms",<br>
            > handle, sw.ElapsedMilliseconds);<br>
            >             errno = Marshal.GetLastWin32Error();<br>
            >             Console.WriteLine("--> 
            serial_close({0}) took {1} ms",<br>
            > handle, sw.ElapsedMilliseconds);<br>
            >             return result;<br>
            >         }<br>
            ><br>
            > I added diagnostics to my C code to write performance
            data to disk and<br>
            > it is:<br>
            > c: serial_close(handle=0x7fe044008d80)<br>
            > c: serial_close(handle=0x7fe044008d80) fd=4<br>
            > c: serial_close(handle=0x7fe044008d80) flushbuffer
            elapsed=0,005000<br>
            > c: serial_close(handle=0x7fe044008d80) close(prfd)
            elapsed=0,028000<br>
            > c: serial_close(handle=0x7fe044008d80) close(pwfd)
            elapsed=0,046000<br>
            > c: serial_close(handle=0x7fe044008d80) close(fd)
            elapsed=21,530000<br>
            > c: serial_close(handle=0x7fe044006620)<br>
            > c: serial_close(handle=0x7fe044006620) fd=14<br>
            > c: serial_close(handle=0x7fe044006620) flushbuffer
            elapsed=0,004000<br>
            > c: serial_close(handle=0x7fe044006620) close(prfd)
            elapsed=0,048000<br>
            > c: serial_close(handle=0x7fe044006620) close(pwfd)
            elapsed=0,062000<br>
            > c: serial_close(handle=0x7fe044006620) close(fd)
            elapsed=0,066000<br>
            ><br>
            > _______________________________________________<br>
            > Mono-devel-list mailing list<br>
            > <a moz-do-not-send="true"
              href="mailto:Mono-devel-list@lists.ximian.com"
              target="_blank">Mono-devel-list@lists.ximian.com</a><br>
            > <a moz-do-not-send="true"
              href="http://lists.ximian.com/mailman/listinfo/mono-devel-list"
              rel="noreferrer" target="_blank">http://lists.ximian.com/mailman/listinfo/mono-devel-list</a><br>
            ><br>
            <br>
            _______________________________________________<br>
            Mono-devel-list mailing list<br>
            <a moz-do-not-send="true"
              href="mailto:Mono-devel-list@lists.ximian.com"
              target="_blank">Mono-devel-list@lists.ximian.com</a><br>
            <a moz-do-not-send="true"
              href="http://lists.ximian.com/mailman/listinfo/mono-devel-list"
              rel="noreferrer" target="_blank">http://lists.ximian.com/mailman/listinfo/mono-devel-list</a><br>
          </blockquote>
        </div>
      </blockquote>
      <br>
      <br>
      <fieldset class="mimeAttachmentHeader"></fieldset>
      <br>
      <pre wrap="">_______________________________________________
Mono-devel-list mailing list
<a class="moz-txt-link-abbreviated" href="mailto:Mono-devel-list@lists.ximian.com">Mono-devel-list@lists.ximian.com</a>
<a class="moz-txt-link-freetext" href="http://lists.ximian.com/mailman/listinfo/mono-devel-list">http://lists.ximian.com/mailman/listinfo/mono-devel-list</a>
</pre>
    </blockquote>
    <br>
  </body>
</html>