您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ -->
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  6. <title>Reentrancy (The Red Hat newlib C Library)</title>
  7. <meta name="description" content="Reentrancy (The Red Hat newlib C Library)">
  8. <meta name="keywords" content="Reentrancy (The Red Hat newlib C Library)">
  9. <meta name="resource-type" content="document">
  10. <meta name="distribution" content="global">
  11. <meta name="Generator" content="makeinfo">
  12. <link href="index.html#Top" rel="start" title="Top">
  13. <link href="Document-Index.html#Document-Index" rel="index" title="Document Index">
  14. <link href="Document-Index.html#SEC_Contents" rel="contents" title="Table of Contents">
  15. <link href="index.html#Top" rel="up" title="Top">
  16. <link href="Misc.html#Misc" rel="next" title="Misc">
  17. <link href="setlocale.html#setlocale" rel="prev" title="setlocale">
  18. <style type="text/css">
  19. <!--
  20. a.summary-letter {text-decoration: none}
  21. blockquote.indentedblock {margin-right: 0em}
  22. blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
  23. blockquote.smallquotation {font-size: smaller}
  24. div.display {margin-left: 3.2em}
  25. div.example {margin-left: 3.2em}
  26. div.lisp {margin-left: 3.2em}
  27. div.smalldisplay {margin-left: 3.2em}
  28. div.smallexample {margin-left: 3.2em}
  29. div.smalllisp {margin-left: 3.2em}
  30. kbd {font-style: oblique}
  31. pre.display {font-family: inherit}
  32. pre.format {font-family: inherit}
  33. pre.menu-comment {font-family: serif}
  34. pre.menu-preformatted {font-family: serif}
  35. pre.smalldisplay {font-family: inherit; font-size: smaller}
  36. pre.smallexample {font-size: smaller}
  37. pre.smallformat {font-family: inherit; font-size: smaller}
  38. pre.smalllisp {font-size: smaller}
  39. span.nolinebreak {white-space: nowrap}
  40. span.roman {font-family: initial; font-weight: normal}
  41. span.sansserif {font-family: sans-serif; font-weight: normal}
  42. ul.no-bullet {list-style: none}
  43. -->
  44. </style>
  45. </head>
  46. <body lang="en">
  47. <a name="Reentrancy"></a>
  48. <div class="header">
  49. <p>
  50. Next: <a href="Misc.html#Misc" accesskey="n" rel="next">Misc</a>, Previous: <a href="Locale.html#Locale" accesskey="p" rel="prev">Locale</a>, Up: <a href="index.html#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="Document-Index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Document-Index.html#Document-Index" title="Index" rel="index">Index</a>]</p>
  51. </div>
  52. <hr>
  53. <a name="Reentrancy-1"></a>
  54. <h2 class="chapter">10 Reentrancy</h2>
  55. <a name="index-reentrancy"></a>
  56. <p>Reentrancy is a characteristic of library functions which allows multiple
  57. processes to use the same address space with assurance that the values stored
  58. in those spaces will remain constant between calls. The Red Hat
  59. newlib implementation of the library functions ensures that
  60. whenever possible, these library functions are reentrant. However,
  61. there are some functions that can not be trivially made reentrant.
  62. Hooks have been provided to allow you to use these functions in a fully
  63. reentrant fashion.
  64. </p>
  65. <a name="index-_005freent"></a>
  66. <a name="index-reent_002eh"></a>
  67. <a name="index-reentrancy-structure"></a>
  68. <p>These hooks use the structure <code>_reent</code> defined in <samp>reent.h</samp>.
  69. A variable defined as &lsquo;<samp>struct _reent</samp>&rsquo; is called a <em>reentrancy
  70. structure</em>. All functions which must manipulate global information are
  71. available in two versions. The first version has the usual name, and
  72. uses a single global instance of the reentrancy structure. The second
  73. has a different name, normally formed by prepending &lsquo;<samp>_</samp>&rsquo; and
  74. appending &lsquo;<samp>_r</samp>&rsquo;, and takes a pointer to the particular reentrancy
  75. structure to use.
  76. </p>
  77. <p>For example, the function <code>fopen</code> takes two arguments, <var>file</var>
  78. and <var>mode</var>, and uses the global reentrancy structure. The function
  79. <code>_fopen_r</code> takes the arguments, <var>struct_reent</var>, which is a
  80. pointer to an instance of the reentrancy structure, <var>file</var>
  81. and <var>mode</var>.
  82. </p>
  83. <p>There are two versions of &lsquo;<samp>struct _reent</samp>&rsquo;, a normal one and one
  84. for small memory systems, controlled by the <code>_REENT_SMALL</code>
  85. definition from the (automatically included) <samp>&lt;sys/config.h&gt;</samp>.
  86. </p>
  87. <a name="index-global-reentrancy-structure"></a>
  88. <a name="index-_005fimpure_005fptr"></a>
  89. <p>Each function which uses the global reentrancy structure uses the global
  90. variable <code>_impure_ptr</code>, which points to a reentrancy structure.
  91. </p>
  92. <p>This means that you have two ways to achieve reentrancy. Both require
  93. that each thread of execution control initialize a unique global
  94. variable of type &lsquo;<samp>struct _reent</samp>&rsquo;:
  95. </p>
  96. <ol>
  97. <li> <a name="index-extra-argument_002c-reentrant-fns"></a>
  98. Use the reentrant versions of the library functions, after initializing
  99. a global reentrancy structure for each process. Use the pointer to this
  100. structure as the extra argument for all library functions.
  101. </li><li> Ensure that each thread of execution control has a pointer to its own
  102. unique reentrancy structure in the global variable <code>_impure_ptr</code>,
  103. and call the standard library subroutines.
  104. </li></ol>
  105. <a name="index-list-of-reentrant-functions"></a>
  106. <a name="index-reentrant-function-list"></a>
  107. <p>The following functions are provided in both reentrant
  108. and non-reentrant versions.
  109. </p>
  110. <div class="example">
  111. <pre class="example"><em>Equivalent for errno variable:</em>
  112. </pre><pre class="example">_errno_r
  113. </pre><pre class="example"><em>Locale functions:</em>
  114. </pre><pre class="example">_localeconv_r _setlocale_r
  115. </pre><pre class="example"><em>Equivalents for stdio variables:</em>
  116. </pre><pre class="example">_stdin_r _stdout_r _stderr_r
  117. </pre><pre class="example"><em>Stdio functions:</em>
  118. </pre><pre class="example">_fdopen_r _perror_r _tempnam_r
  119. _fopen_r _putchar_r _tmpnam_r
  120. _getchar_r _puts_r _tmpfile_r
  121. _gets_r _remove_r _vfprintf_r
  122. _iprintf_r _rename_r _vsnprintf_r
  123. _mkstemp_r _snprintf_r _vsprintf_r
  124. _mktemp_t _sprintf_r
  125. </pre><pre class="example"><em>Signal functions:</em>
  126. </pre><pre class="example">_init_signal_r _signal_r
  127. _kill_r __sigtramp_r
  128. _raise_r
  129. </pre><pre class="example"><em>Stdlib functions:</em>
  130. </pre><pre class="example">_calloc_r _mblen_r _setenv_r
  131. _dtoa_r _mbstowcs_r _srand_r
  132. _free_r _mbtowc_r _strtod_r
  133. _getenv_r _memalign_r _strtol_r
  134. _mallinfo_r _mstats_r _strtoul_r
  135. _malloc_r _putenv_r _system_r
  136. _malloc_r _rand_r _wcstombs_r
  137. _malloc_stats_r _realloc_r _wctomb_r
  138. </pre><pre class="example"><em>String functions:</em>
  139. </pre><pre class="example">_strdup_r _strtok_r
  140. </pre><pre class="example"><em>System functions:</em>
  141. </pre><pre class="example">_close_r _link_r _unlink_r
  142. _execve_r _lseek_r _wait_r
  143. _fcntl_r _open_r _write_r
  144. _fork_r _read_r
  145. _fstat_r _sbrk_r
  146. _gettimeofday_r _stat_r
  147. _getpid_r _times_r
  148. </pre><pre class="example"><em>Time function:</em>
  149. </pre><pre class="example">_asctime_r
  150. </pre></div>
  151. <hr>
  152. <div class="header">
  153. <p>
  154. Next: <a href="Misc.html#Misc" accesskey="n" rel="next">Misc</a>, Previous: <a href="Locale.html#Locale" accesskey="p" rel="prev">Locale</a>, Up: <a href="index.html#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="Document-Index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Document-Index.html#Document-Index" title="Index" rel="index">Index</a>]</p>
  155. </div>
  156. </body>
  157. </html>