Solaris 2.3: Which ANSI C functions "safe" to use in MT applications???

Solaris 2.3: Which ANSI C functions "safe" to use in MT applications???

Post by w » Wed, 28 Sep 1994 18:01:43



Hi,

this is probably a question for the Solaris 2.3 gurus...

Even after carefully RTFM I am  having  problems  with  the  way  Sun
describes if a library routine is safe to be used in a multi-threaded
application  or  not. For instance, ANSI C functions like assert(3x),
atexit(3c),  exit(3c),   free(3c),   malloc(3c),   strerror(3c)   and
tmpfile(3s)  are said to be "safe". But I just don't understand if it
really is allowed  to  call,  say,  malloc(3c)  in  a  multi-threaded
application, given the following "definitions" of the term "safe":

    "...
    Safe: Safe is simply an attribute of code that can be  called
    from  a  multithreaded application. It is a generic term used
    to differentiate between code that is unsafe.

    Unsafe: An unsafe library contains  global  and  static  data
    that  is  not  protected.  It  is  not safe to use unless the
    application arranges for only one thread at time  to  execute
    within  the  library.  Unsafe  libraries may contain routines
    that are safe; however, most of the  library's  routines  are
    unsafe to call.

    MT-Safe: An MT-Safe library is fully prepared for multithrea-
    ded access. It protects  its  global  and  static  data  with
    locks,  and  can  provide a reasonable amount of concurrency.
    Note that a library can be safe to use, but not MT-Safe.  For
    example,  surrounding  an entire library with a monitor makes
    the library safe, but it supports no concurrency so it is not
    considered MT-Safe. An MT-Safe library must permit  a  reaso-
    nable amount of concurrency. (This definition's purpose is to
    give  precision  to what is meant when a library is described
    as safe. The definition of a "safe" library does not  specfiy
    if  the  library supports concurrency. The MT-Safe definition
    makes it clear that the library is safe,  and  supports  some
    concurrency.  This  clarifies  the safe definition, which can
    mean anything from being single threaded to being any  degree
    of multithreaded.)

    Async-Safe: Async-Safe refers to particular library  routines
    that  can  be  safely  called from a signal handler. A thread
    that is executing an Async-Safe  routine  will  not  deadlock
    with  itself  if  interrupted by a signal. Signals are only a
    problem for MT-Safe routines that acquire locks.
    ..."
                              [Quoted from the Intro(3) man page]

The following ANSI C functions are _not_ marked as  "MT-Safe".  Which
of  them  can be reliably called in a multithreaded application, wich
are especially critical?

        abort.3c    getenv.3c       realloc.3c     socketpair.3n
        accept.3n   getsockname.3n  recv.3n        srand.3c
        asctime.3c  getsockopt.3n   recvfrom.3n    strcoll.3c
        assert.3x   gmtime.3c       recvmsg.3n     strerror.3c
        atexit.3c   listen.3n       send.3n        strftime.3c
        bind.3n     localeconv.3c   sendmsg.3n     strtok.3c
        bsearch.3c  localtime.3c    sendto.3n      strxfrm.3c
        calloc.3c   longjmp.3c      setjmp.3c      sysconf.3c
        connect.3n  malloc.3c       setlocale.3c   tmpfile.3s
        ctime.3c    mktime.3c       setsockopt.3n  tmpnam.3s
        exit.3c     qsort.3c        shutdown.3n    vfprintf.3s
        free.3c     rand.3c         socket.3n      vprintf.3s

Any help appreciated,

Wolfgang Denk



"If the code and  the  comments  disagree,  then  both  are  probably
wrong."                                                - Norm Schryer

 
 
 

Solaris 2.3: Which ANSI C functions "safe" to use in MT applications???

Post by Bruce Word » Wed, 05 Oct 1994 06:36:46


[Questions about MT-Safe operations.]

Another question:  How does one do MT-Safe I/O?  Clearly the seek/read
method is out.  aioread() and aiowrite() are out (MT-LEVEL: Unsafe).
aio_read() and aio_write() aren't implemented.  I think that leaves
us with pread() and pwrite() which don't tell us if they are MT safe
or not.  Are they?  If not, what is?

                                                        CBW
--------------------------------------------------------------------------

252-21 Seismological Laboratory, Caltech, Pasadena, CA 91125

 
 
 

Solaris 2.3: Which ANSI C functions "safe" to use in MT applications???

Post by Casper H.S. D » Wed, 05 Oct 1994 07:06:00




>[Questions about MT-Safe operations.]
>Another question:  How does one do MT-Safe I/O?  Clearly the seek/read
>method is out.  aioread() and aiowrite() are out (MT-LEVEL: Unsafe).
>aio_read() and aio_write() aren't implemented.  I think that leaves
>us with pread() and pwrite() which don't tell us if they are MT safe
>or not.  Are they?  If not, what is?

I think you can assume that all system calls are MT-Safe.
(They don't keep state outside the kernel, only inside the kernel
where the kernel takes care of all the locking)

Casper

 
 
 

Solaris 2.3: Which ANSI C functions "safe" to use in MT applications???

Post by Bart Smaalde » Fri, 07 Oct 1994 02:20:48


System calls (section 2 in the man pages) are MT safe by definition;
the kernel takes care of this.  They may have side effects, such as
read advancing the file ptr, but they are mt-safe.  

In this case, p{read,write} are the calls to use if you have multiple
threads doing IO via the same file descriptor.  Keep in mind, however,
that unless the file is completely buffered in memory or the file is
part of a finely striped filesystem the random nature of the IO
operations will not help performance.

Aioread and aiowrite in libaio.so are currently for single-threaded
programs only.

- Bart Smaalders        OS Performance          SunSoft

Quote:

>Another question:  How does one do MT-Safe I/O?  Clearly the seek/read
>method is out.  aioread() and aiowrite() are out (MT-LEVEL: Unsafe).
>aio_read() and aio_write() aren't implemented.  I think that leaves
>us with pread() and pwrite() which don't tell us if they are MT safe
>or not.  Are they?  If not, what is?

 
 
 

1. Are "_r" functions MT safe?

Hello,

I have multithreaded applications to be ported on Linux 7.1 Problem is
that I cant find any documentation for Multithread Safe network and
time modules like gethostbyname_r, getservent_r, getprotobyname_r,
ctime_r. The prototypes and functionality is there in header files and
libraries. But there is no documentation available. What are the
return values and what they depict. At now I am checking if they
populate the host/servent/proto/time structs for error check.

Are they safe to use for multithread applications on linux or they are
still in some sort of development without any final release.

Regards,
Vishal Jain

--------------------------------------------------------------------------------
ILX Systems                             Ph : 212-510-4268 ( W )
123 William St                          Ph : 718-392-5159 ( H )
New York, NY 10038
--------------------------------------------------------------------------------

2. VM Management difference 2..4.7 to 2.4.18

3. Solaris 2.3: tar reads EOF mark and no "mt fsf 1" is required

4. Red Hat 4 and Samba

5. Solaris 2.3 "No directory" "No shell"

6. problems with linuxrc

7. "sz" and "rz" for solaris 2.3 ??

8. Porting from OpenBSD/Linux to Solaris : daemon(3)

9. "weof" operation in "mt" command gives "permission denied"

10. GETSERVBYNAME()????????????????????"""""""""""""

11. Is a bash function a "function" or a "subroutine"?

12. Screen doesn't unblank with "screenblank" and "xlock" under 2.3

13. """"""""My SoundBlast 16 pnp isn't up yet""""""""""""