home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / modula3 / 1043 < prev    next >
Encoding:
Text File  |  1992-11-23  |  3.7 KB  |  81 lines

  1. Newsgroups: comp.lang.modula3
  2. Path: sparky!uunet!decwrl!pa.dec.com!src.dec.com!src.dec.com!mjordan
  3. From: mjordan@src.dec.com (Mick Jordan)
  4. Subject: Re: Failing system calls and threads
  5. Message-ID: <1992Nov23.173612.16490@src.dec.com>
  6. Sender: news@src.dec.com (News)
  7. Organization: DEC Systems Research Center
  8. References:  <1992Nov20.120734.23671@rdg.dec.com>
  9. Date: Mon, 23 Nov 92 17:36:12 GMT
  10. Lines: 69
  11.  
  12. In article <1992Nov20.120734.23671@rdg.dec.com>, jch@rdg.dec.com (John Haxby) writes:
  13.  
  14. |> Obviously, if some other thread gets to run between the call to write() and
  15. |> the examination of errno then the value of errno may no longer be valid or
  16. |> useful when you come to look at it.
  17. |> 
  18. |> The obvious way around this is to protect the code with some suitable
  19. |> mutex, eg
  20. |> 
  21. |>     LOCK Cerrno.mu DO
  22. |>         write (...)
  23. |>         IF Cerrno.errno ...
  24. |>     END
  25. |> 
  26. |> but this has at least three significant problems: the overhead in acquiring
  27. |> a lock around every system call is non-negligable (I assume); not all uses
  28. |> of errno are guaranteed to be protected (especially in uses within C
  29. |> libraries); only one outstanding system call is can be permitted (in an
  30. |> environement that uses kernel-supported threads).
  31.  
  32. In general, when you are dealing with a library that exports shared global 
  33. variables, written in any language (including Modula-3), multi-threaded clients 
  34. must use locks to synchronise the operations and access to the variables.
  35. In the specific case you mention of errno, one of the standard tricks in
  36. the C world is to treat errno as a macro and define it as a function that
  37. accesses a thread specific value. This is what thread-friendly C libraries
  38. do. But most systems dont have thread-friendly libraries and unless all
  39. C code has been recompiled against the functional errno, the solution
  40. doesnt work.
  41.  
  42. However, the overhead in acquiring the lock is (should be) negligible
  43. in comparision with entering the operating system. So, for example, we
  44. chose your first solution for the Olivetti Modula-3 library.
  45.  
  46. |> The solution comes from knowing that errno is introduced by the C library
  47. |> and not by the kernel: the kernel returns a result and an indication of
  48. |> whether that result is an error code or a returned value.  On a VAX, the
  49. |> indication is the carry flag, on a (DEC) mips machine, the indication
  50. |> is left in a register when the syscall instruction `returns'.  The
  51. |> implementation of the solution involves re-writing all of the system calls
  52. |> so that the are of the form (expressed in C)
  53. |> 
  54. |>     int err = sysfunc (&res, arg1, arg2, ...);
  55. |> 
  56. |> The &res is only written to the if the return result is non-zero; the
  57. |> return result is otherwise what would be written to errno.  The Modula-3
  58. |> implementation of, say, Unix.write would then look something like this
  59. |> (syntax notwithstanding :-):
  60. |> 
  61.  
  62. |> Does this make sense?  Will it avoid the problems I mentioned earlier?
  63. |> And, a question for the other implementations: can this form of solution be
  64. |> used? (specifically, are there any implementations out there where errno is
  65. |> explicitly known by the kernel?)
  66.  
  67. In the days before Modula code and C code intermixed so easily, it was
  68. possible to adopt this solution, since the Modula libraries typically
  69. preempted all the Unix interface and dealt directly with the Unix
  70. kernel entry rather than the C library. In general, of course, it is
  71. neither possible nor desirable to modify the code of the library to
  72. make it more thread friendly. In particular one cant usually find
  73. a layer just below the surface on which you can layer a more suitable
  74. veneer. Therefore if large scale exploitation of C and C++ code from 
  75. Modula-3 is to be possible, we have to lobby for thread-friendly designs
  76. at the C and C++ level.
  77.  
  78. Mick Jordan
  79.  
  80.  
  81.