[asterisk-dev] Re: [asterisk-commits] russell: trunk r48019 - in /trunk/include/asterisk: threadstorage.h utils.h

Russell Bryant russell at digium.com
Tue Nov 28 21:43:44 MST 2006


Kevin P. Fleming wrote:
> I'll be pedantic here... 'reentrant' is not the correct word to use,
> since this function cannot be convinced to call itself (so there is no
> 're-entry' into the same function).
> 
> I don't know that there is a better word, but I'm hesitant to leave it
> this way and have someone look at the code and wonder how anyone could
> have thought it WAS reentrant :-) There are other functions in the
> standard C library that have similar behavior, so we may be able to
> steal some language from their man pages.

Well, the re-entry into the function that I'm referring to here is successive 
calls to the function within the same thread.  Once you call the function a 
second time within the same thread, the previous result is no longer valid.  If 
it were a reentrant function, this would not the be the case, and the function 
would be safe to call again without corrupting a previous result.  The old 
version would be classified as reentrant since it only operates on data provided 
by the caller, and holds no static data across calls to the function (even if 
the static data is thread-local, and not process-local, as is the case here).

Since the following code is not valid,

ast_log(LOG_NOTICE, "Some ip %s and some ip %s\n",
    ast_inet_ntoa(sin1->sin_addr), ast_inet_ntoa(sin2->sin_addr));

I would classify the function as not reentrant, using the definition of 
reentrancy found in this article:

http://www-128.ibm.com/developerworks/linux/library/l-reent.html

"What is reentrancy?

A reentrant function is one that can be used by more than one task concurrently 
without fear of data corruption. Conversely, a non-reentrant function is one 
that cannot be shared by more than one task unless mutual exclusion to the 
function is ensured either by using a semaphore or by disabling interrupts 
during critical sections of code. A reentrant function can be interrupted at any 
time and resumed at a later time without loss of data. Reentrant functions 
either use local variables or protect their data when global variables are used.

A reentrant function:

    - Does not hold static data over successive calls
    - Does not return a pointer to static data; all data is provided by the 
caller of the function
    - Uses local data or ensures protection of global data by making a local 
copy of it
    - Must not call any non-reentrant functions

Don't confuse reentrance with thread-safety. From the programmer perspective, 
these two are separate concepts: a function can be reentrant, thread-safe, both, 
or neither. Non-reentrant functions cannot be used by multiple threads. 
Moreover, it may be impossible to make a non-reentrant function thread-safe.
IEEE Std 1003.1 lists 118 reentrant UNIX® functions, which aren't duplicated 
here. See Resources for a link to the list at unix.org.

The rest of the functions are non-reentrant because of any of the following:
    - They call malloc or free
    - They are known to use static data structures
    - They are part of the standard I/O library"

-- 
Russell Bryant
Software Engineer
Digium, Inc.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: russell.vcf
Type: text/x-vcard
Size: 266 bytes
Desc: not available
Url : http://lists.digium.com/pipermail/asterisk-dev/attachments/20061128/c80c5f1b/russell.vcf


More information about the asterisk-dev mailing list