gonzui


Format: Advanced Search

t2ex/bsd_source/lib/libc/src_bsd/include/thread_private.hbare sourcepermlink (0.01 seconds)

Search this content:

    1: /* $OpenBSD: thread_private.h,v 1.25 2011/10/16 06:29:56 guenther Exp $ */
    2: 
    3: /* PUBLIC DOMAIN: No Rights Reserved. Marco S Hyman <marc@snafu.org> */
    4: 
    5: #ifndef _THREAD_PRIVATE_H_
    6: #define _THREAD_PRIVATE_H_
    7: 
    8: /*
    9:  * This file defines the thread library interface to libc.  Thread
   10:  * libraries must implement the functions described here for proper
   11:  * inter-operation with libc.   libc contains weak versions of the
   12:  * described functions for operation in a non-threaded environment.
   13:  */
   14: 
   15: /*
   16:  * This variable is 0 until a second thread is created.
   17:  */
   18: #if 1
   19: #define _THREAD_PRIVATE_MUTEX(name)
   20: #define _THREAD_PRIVATE_MUTEX_LOCK(t) __libc_lock_findfp()
   21: extern void __libc_lock_findfp(void);
   22: #define _THREAD_PRIVATE_MUTEX_UNLOCK(t) __libc_unlock_findfp()
   23: extern void __libc_unlock_findfp(void);
   24: #else
   25: extern int __isthreaded;
   26: 
   27: /*
   28:  * Weak symbols are used in libc so that the thread library can
   29:  * efficiently wrap libc functions.
   30:  * 
   31:  * Use WEAK_NAME(n) to get a libc-private name for n (_weak_n),
   32:  *     WEAK_ALIAS(n) to generate the weak symbol n pointing to _weak_n,
   33:  *     WEAK_PROTOTYPE(n) to generate a prototype for _weak_n (based on n).
   34:  */
   35: #define WEAK_NAME(name)         __CONCAT(_weak_,name)
   36: #define WEAK_ALIAS(name)        __weak_alias(name, WEAK_NAME(name))
   37: #ifdef __GNUC__
   38: #define WEAK_PROTOTYPE(name)    __typeof__(name) WEAK_NAME(name)
   39: #else
   40: #define WEAK_PROTOTYPE(name)    /* typeof() only in gcc */
   41: #endif
   42: 
   43: /*
   44:  * Ditto for hand-written syscall stubs:
   45:  * 
   46:  * Use STUB_NAME(n) to get the strong name of the stub: _thread_sys_n
   47:  *     STUB_ALIAS(n) to generate the weak symbol n pointing to _thread_sys_n,
   48:  *     STUB_PROTOTYPE(n) to generate a prototype for _thread_sys_n (based on n).
   49:  */
   50: #define STUB_NAME(name)         __CONCAT(_thread_sys_,name)
   51: #define STUB_ALIAS(name)        __weak_alias(name, STUB_NAME(name))
   52: #ifdef __GNUC__
   53: #define STUB_PROTOTYPE(name)    __typeof__(name) STUB_NAME(name)
   54: #else
   55: #define STUB_PROTOTYPE(name)    /* typeof() only in gcc */
   56: #endif
   57: 
   58: /*
   59:  * helper macro to make unique names in the thread namespace
   60:  */
   61: #define __THREAD_NAME(name)     __CONCAT(_thread_tagname_,name)
   62: 
   63: /*
   64:  * helper functions that exist as (weak) null functions in libc and
   65:  * (strong) functions in the thread library.   These functions:
   66:  *
   67:  * _thread_tag_lock:
   68:  *      lock the mutex associated with the given tag.   If the given
   69:  *      tag is NULL a tag is first allocated.
   70:  *
   71:  * _thread_tag_unlock:
   72:  *      unlock the mutex associated with the given tag.   If the given
   73:  *      tag is NULL a tag is first allocated.
   74:  *
   75:  * _thread_tag_storage:
   76:  *      return a pointer to per thread instance of data associated
   77:  *      with the given tag.  If the given tag is NULL a tag is first
   78:  *      allocated.
   79:  *
   80:  * _thread_mutex_lock:
   81:  *      lock the given mutex. If the given mutex is NULL,
   82:  *      rely on rthreads/pthreads implementation to initialize
   83:  *      the mutex before locking.
   84:  *
   85:  * _thread_mutex_unlock:
   86:  *      unlock the given mutex.
   87:  *
   88:  * _thread_mutex_destroy:
   89:  *      destroy the given mutex.
   90:  */
   91: void    _thread_tag_lock(void **);
   92: void    _thread_tag_unlock(void **);
   93: void   *_thread_tag_storage(void **, void *, size_t, void *);
   94: void    _thread_mutex_lock(void **);
   95: void    _thread_mutex_unlock(void **);
   96: void    _thread_mutex_destroy(void **);
   97: 
   98: /*
   99:  * Macros used in libc to access thread mutex, keys, and per thread storage.
  100:  * _THREAD_PRIVATE_KEY and _THREAD_PRIVATE_MUTEX are different macros for
  101:  * historical reasons.   They do the same thing, define a static variable
  102:  * keyed by 'name' that identifies a mutex and a key to identify per thread
  103:  * data.
  104:  */
  105: #define _THREAD_PRIVATE_KEY(name)                                       \
  106:         static void *__THREAD_NAME(name)
  107: #define _THREAD_PRIVATE_MUTEX(name)                                     \
  108:         static void *__THREAD_NAME(name)
  109: #define _THREAD_PRIVATE_MUTEX_LOCK(name)                                \
  110:         _thread_tag_lock(&(__THREAD_NAME(name)))
  111: #define _THREAD_PRIVATE_MUTEX_UNLOCK(name)                              \
  112:         _thread_tag_unlock(&(__THREAD_NAME(name)))
  113: #define _THREAD_PRIVATE(keyname, storage, error)                        \
  114:         _thread_tag_storage(&(__THREAD_NAME(keyname)), &(storage),     \
  115:                             sizeof (storage), error)
  116: 
  117: /*
  118:  * Macros used in libc to access mutexes.
  119:  */
  120: #define _MUTEX_LOCK(mutex)                                              \
  121:         do {                                                           \
  122:                 if (__isthreaded)                                     \
  123:                         _thread_mutex_lock(mutex);                   \
  124:         } while (0)
  125: #define _MUTEX_UNLOCK(mutex)                                            \
  126:         do {                                                           \
  127:                 if (__isthreaded)                                     \
  128:                         _thread_mutex_unlock(mutex);                 \
  129:         } while (0)
  130: #define _MUTEX_DESTROY(mutex)                                           \
  131:         do {                                                           \
  132:                 if (__isthreaded)                                     \
  133:                         _thread_mutex_destroy(mutex);                        \
  134:         } while (0)
  135: 
  136: /*
  137:  * Resolver code is special cased in that it uses global keys.
  138:  */
  139: extern void *__THREAD_NAME(_res);
  140: extern void *__THREAD_NAME(_res_ext);
  141: extern void *__THREAD_NAME(serv_mutex);
  142: 
  143: /*
  144:  * malloc lock/unlock prototypes and definitions
  145:  */
  146: void    _thread_malloc_lock(void);
  147: void    _thread_malloc_unlock(void);
  148: 
  149: #define _MALLOC_LOCK()          do {                                    \
  150:                                         if (__isthreaded)          \
  151:                                                 _thread_malloc_lock();    \
  152:                                 } while (0)
  153: #define _MALLOC_UNLOCK()        do {                                   \
  154:                                         if (__isthreaded)          \
  155:                                                 _thread_malloc_unlock();\
  156:                                 } while (0)
  157: 
  158: void    _thread_atexit_lock(void);
  159: void    _thread_atexit_unlock(void);
  160: 
  161: #define _ATEXIT_LOCK()          do {                                    \
  162:                                         if (__isthreaded)          \
  163:                                                 _thread_atexit_lock();    \
  164:                                 } while (0)
  165: #define _ATEXIT_UNLOCK()        do {                                   \
  166:                                         if (__isthreaded)          \
  167:                                                 _thread_atexit_unlock();\
  168:                                 } while (0)
  169: 
  170: void    _thread_arc4_lock(void);
  171: void    _thread_arc4_unlock(void);
  172: 
  173: #define _ARC4_LOCK()            do {                                      \
  174:                                         if (__isthreaded)          \
  175:                                                 _thread_arc4_lock();      \
  176:                                 } while (0)
  177: #define _ARC4_UNLOCK()          do {                                    \
  178:                                         if (__isthreaded)          \
  179:                                                 _thread_arc4_unlock();\
  180:                                 } while (0)
  181: 
  182: #endif
  183: #endif /* _THREAD_PRIVATE_H_ */