1<html lang="en"> 2<head> 3<title>Reentrancy - Untitled</title> 4<meta http-equiv="Content-Type" content="text/html"> 5<meta name="description" content="Untitled"> 6<meta name="generator" content="makeinfo 4.13"> 7<link title="Top" rel="start" href="index.html#Top"> 8<link rel="prev" href="Locale.html#Locale" title="Locale"> 9<link rel="next" href="Misc.html#Misc" title="Misc"> 10<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> 11<meta http-equiv="Content-Style-Type" content="text/css"> 12<style type="text/css"><!-- 13 pre.display { font-family:inherit } 14 pre.format { font-family:inherit } 15 pre.smalldisplay { font-family:inherit; font-size:smaller } 16 pre.smallformat { font-family:inherit; font-size:smaller } 17 pre.smallexample { font-size:smaller } 18 pre.smalllisp { font-size:smaller } 19 span.sc { font-variant:small-caps } 20 span.roman { font-family:serif; font-weight:normal; } 21 span.sansserif { font-family:sans-serif; font-weight:normal; } 22--></style> 23<link rel="stylesheet" type="text/css" href="../cs.css"> 24</head> 25<body> 26<div class="node"> 27<a name="Reentrancy"></a> 28<p> 29Next: <a rel="next" accesskey="n" href="Misc.html#Misc">Misc</a>, 30Previous: <a rel="previous" accesskey="p" href="Locale.html#Locale">Locale</a>, 31Up: <a rel="up" accesskey="u" href="index.html#Top">Top</a> 32<hr> 33</div> 34 35<h2 class="chapter">10 Reentrancy</h2> 36 37<p><a name="index-reentrancy-447"></a>Reentrancy is a characteristic of library functions which allows multiple 38processes to use the same address space with assurance that the values stored 39in those spaces will remain constant between calls. The Red Hat 40newlib implementation of the library functions ensures that 41whenever possible, these library functions are reentrant. However, 42there are some functions that can not be trivially made reentrant. 43Hooks have been provided to allow you to use these functions in a fully 44reentrant fashion. 45 46 <p><a name="index-g_t_005freent-448"></a><a name="index-reent_002eh-449"></a><a name="index-reentrancy-structure-450"></a>These hooks use the structure <code>_reent</code> defined in <samp><span class="file">reent.h</span></samp>. 47A variable defined as ‘<samp><span class="samp">struct _reent</span></samp>’ is called a <dfn>reentrancy 48structure</dfn>. All functions which must manipulate global information are 49available in two versions. The first version has the usual name, and 50uses a single global instance of the reentrancy structure. The second 51has a different name, normally formed by prepending ‘<samp><span class="samp">_</span></samp>’ and 52appending ‘<samp><span class="samp">_r</span></samp>’, and takes a pointer to the particular reentrancy 53structure to use. 54 55 <p>For example, the function <code>fopen</code> takes two arguments, <var>file</var> 56and <var>mode</var>, and uses the global reentrancy structure. The function 57<code>_fopen_r</code> takes the arguments, <var>struct_reent</var>, which is a 58pointer to an instance of the reentrancy structure, <var>file</var> 59and <var>mode</var>. 60 61 <p>There are two versions of ‘<samp><span class="samp">struct _reent</span></samp>’, a normal one and one 62for small memory systems, controlled by the <code>_REENT_SMALL</code> 63definition from the (automatically included) <samp><span class="file"><sys/config.h></span></samp>. 64 65 <p><a name="index-global-reentrancy-structure-451"></a><a name="index-g_t_005fimpure_005fptr-452"></a>Each function which uses the global reentrancy structure uses the global 66variable <code>_impure_ptr</code>, which points to a reentrancy structure. 67 68 <p>This means that you have two ways to achieve reentrancy. Both require 69that each thread of execution control initialize a unique global 70variable of type ‘<samp><span class="samp">struct _reent</span></samp>’: 71 72 <ol type=1 start=1> 73<li><a name="index-extra-argument_002c-reentrant-fns-453"></a>Use the reentrant versions of the library functions, after initializing 74a global reentrancy structure for each process. Use the pointer to this 75structure as the extra argument for all library functions. 76 77 <li>Ensure that each thread of execution control has a pointer to its own 78unique reentrancy structure in the global variable <code>_impure_ptr</code>, 79and call the standard library subroutines. 80 </ol> 81 82 <p><a name="index-list-of-reentrant-functions-454"></a><a name="index-reentrant-function-list-455"></a>The following functions are provided in both reentrant 83and non-reentrant versions. 84 85<pre class="example"><br><em>Equivalent for errno variable:</em><br> 86 _errno_r 87 88<br><em>Locale functions:</em><br> 89 _localeconv_r _setlocale_r 90 91<br><em>Equivalents for stdio variables:</em><br> 92 _stdin_r _stdout_r _stderr_r 93 94 95<br><em>Stdio functions:</em><br> 96 _fdopen_r _perror_r _tempnam_r 97 _fopen_r _putchar_r _tmpnam_r 98 _getchar_r _puts_r _tmpfile_r 99 _gets_r _remove_r _vfprintf_r 100 _iprintf_r _rename_r _vsnprintf_r 101 _mkstemp_r _snprintf_r _vsprintf_r 102 _mktemp_t _sprintf_r 103 104<br><em>Signal functions:</em><br> 105 _init_signal_r _signal_r 106 _kill_r __sigtramp_r 107 _raise_r 108 109<br><em>Stdlib functions:</em><br> 110 _calloc_r _mblen_r _setenv_r 111 _dtoa_r _mbstowcs_r _srand_r 112 _free_r _mbtowc_r _strtod_r 113 _getenv_r _memalign_r _strtol_r 114 _mallinfo_r _mstats_r _strtoul_r 115 _malloc_r _putenv_r _system_r 116 _malloc_r _rand_r _wcstombs_r 117 _malloc_stats_r _realloc_r _wctomb_r 118 119<br><em>String functions:</em><br> 120 _strdup_r _strtok_r 121 122<br><em>System functions:</em><br> 123 _close_r _link_r _unlink_r 124 _execve_r _lseek_r _wait_r 125 _fcntl_r _open_r _write_r 126 _fork_r _read_r 127 _fstat_r _sbrk_r 128 _gettimeofday_r _stat_r 129 _getpid_r _times_r 130 131 132<br><em>Time function:</em><br> 133 _asctime_r 134</pre> 135 </body></html> 136 137