]> git.ipfire.org Git - thirdparty/glibc.git/blobdiff - nptl/pthreadP.h
Add per-thread cache to malloc
[thirdparty/glibc.git] / nptl / pthreadP.h
index f9634ab0ffac97210688164b7e6acab347d66c96..6e7d6ff09edfeecfcd6c636f79b515a1053233de 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
+/* Copyright (C) 2002-2017 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
 
@@ -13,9 +13,8 @@
    Lesser General Public License for more details.
 
    You should have received a copy of the GNU Lesser General Public
-   License along with the GNU C Library; if not, write to the Free
-   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
-   02111-1307 USA.  */
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
 
 #ifndef _PTHREADP_H
 #define _PTHREADP_H    1
@@ -32,6 +31,8 @@
 #include <pthread-functions.h>
 #include <atomic.h>
 #include <kernel-features.h>
+#include <errno.h>
+#include <nptl-signals.h>
 
 
 /* Atomic operations on TLS memory.  */
 enum
 {
   PTHREAD_MUTEX_KIND_MASK_NP = 3,
+
+  PTHREAD_MUTEX_ELISION_NP    = 256,
+  PTHREAD_MUTEX_NO_ELISION_NP = 512,
+
   PTHREAD_MUTEX_ROBUST_NORMAL_NP = 16,
   PTHREAD_MUTEX_ROBUST_RECURSIVE_NP
   = PTHREAD_MUTEX_ROBUST_NORMAL_NP | PTHREAD_MUTEX_RECURSIVE_NP,
@@ -94,8 +99,35 @@ enum
   PTHREAD_MUTEX_PP_ERRORCHECK_NP
   = PTHREAD_MUTEX_PRIO_PROTECT_NP | PTHREAD_MUTEX_ERRORCHECK_NP,
   PTHREAD_MUTEX_PP_ADAPTIVE_NP
-  = PTHREAD_MUTEX_PRIO_PROTECT_NP | PTHREAD_MUTEX_ADAPTIVE_NP
+  = PTHREAD_MUTEX_PRIO_PROTECT_NP | PTHREAD_MUTEX_ADAPTIVE_NP,
+  PTHREAD_MUTEX_ELISION_FLAGS_NP
+  = PTHREAD_MUTEX_ELISION_NP | PTHREAD_MUTEX_NO_ELISION_NP,
+
+  PTHREAD_MUTEX_TIMED_ELISION_NP =
+         PTHREAD_MUTEX_TIMED_NP | PTHREAD_MUTEX_ELISION_NP,
+  PTHREAD_MUTEX_TIMED_NO_ELISION_NP =
+         PTHREAD_MUTEX_TIMED_NP | PTHREAD_MUTEX_NO_ELISION_NP,
 };
+#define PTHREAD_MUTEX_PSHARED_BIT 128
+
+#define PTHREAD_MUTEX_TYPE(m) \
+  ((m)->__data.__kind & 127)
+/* Don't include NO_ELISION, as that type is always the same
+   as the underlying lock type.  */
+#define PTHREAD_MUTEX_TYPE_ELISION(m) \
+  ((m)->__data.__kind & (127|PTHREAD_MUTEX_ELISION_NP))
+
+#if LLL_PRIVATE == 0 && LLL_SHARED == 128
+# define PTHREAD_MUTEX_PSHARED(m) \
+  ((m)->__data.__kind & 128)
+#else
+# define PTHREAD_MUTEX_PSHARED(m) \
+  (((m)->__data.__kind & 128) ? LLL_SHARED : LLL_PRIVATE)
+#endif
+
+/* The kernel when waking robust mutexes on exit never uses
+   FUTEX_PRIVATE_FLAG FUTEX_WAKE.  */
+#define PTHREAD_ROBUST_MUTEX_PSHARED(m) LLL_SHARED
 
 /* Ceiling in __data.__lock.  __data.__lock is signed, so don't
    use the MSB bit in there, but in the mask also include that bit,
@@ -118,17 +150,43 @@ enum
    | PTHREAD_MUTEXATTR_PROTOCOL_MASK | PTHREAD_MUTEXATTR_PRIO_CEILING_MASK)
 
 
+/* For the following, see pthread_rwlock_common.c.  */
+#define PTHREAD_RWLOCK_WRPHASE         1
+#define PTHREAD_RWLOCK_WRLOCKED                2
+#define PTHREAD_RWLOCK_RWAITING                4
+#define PTHREAD_RWLOCK_READER_SHIFT    3
+#define PTHREAD_RWLOCK_READER_OVERFLOW ((unsigned int) 1 \
+                                        << (sizeof (unsigned int) * 8 - 1))
+#define PTHREAD_RWLOCK_WRHANDOVER      ((unsigned int) 1 \
+                                        << (sizeof (unsigned int) * 8 - 1))
+#define PTHREAD_RWLOCK_FUTEX_USED      2
+
+
 /* Bits used in robust mutex implementation.  */
 #define FUTEX_WAITERS          0x80000000
 #define FUTEX_OWNER_DIED       0x40000000
 #define FUTEX_TID_MASK         0x3fffffff
 
 
+/* pthread_once definitions.  See __pthread_once for how these are used.  */
+#define __PTHREAD_ONCE_INPROGRESS      1
+#define __PTHREAD_ONCE_DONE            2
+#define __PTHREAD_ONCE_FORK_GEN_INCR   4
+
+
+/* Condition variable definitions.  See __pthread_cond_wait_common.
+   Need to be defined here so there is one place from which
+   nptl_lock_constants can grab them.  */
+#define __PTHREAD_COND_CLOCK_MONOTONIC_MASK 2
+#define __PTHREAD_COND_SHARED_MASK 1
+
+
 /* Internal variables.  */
 
 
-/* Default stack size.  */
-extern size_t __default_stacksize attribute_hidden;
+/* Default pthread attributes.  */
+extern struct pthread_attr __default_pthread_attr attribute_hidden;
+extern int __default_pthread_attr_lock attribute_hidden;
 
 /* Size and alignment of static TLS block.  */
 extern size_t __static_tls_size attribute_hidden;
@@ -143,12 +201,7 @@ hidden_proto (__stack_user)
 
 /* Attribute handling.  */
 extern struct pthread_attr *__attr_list attribute_hidden;
-extern lll_lock_t __attr_list_lock attribute_hidden;
-
-/* First available RT signal.  */
-extern int __current_sigrtmin attribute_hidden;
-/* Last available RT signal.  */
-extern int __current_sigrtmax attribute_hidden;
+extern int __attr_list_lock attribute_hidden;
 
 /* Concurrency handling.  */
 extern int __concurrency_level attribute_hidden;
@@ -205,7 +258,7 @@ extern int __pthread_debug attribute_hidden;
 
 extern void __pthread_unwind (__pthread_unwind_buf_t *__buf)
      __cleanup_fct_attribute __attribute ((__noreturn__))
-#if !defined SHARED && !defined IS_IN_libpthread
+#if !defined SHARED && !IS_IN (libpthread)
      weak_function
 #endif
      ;
@@ -219,13 +272,14 @@ extern void __pthread_register_cancel (__pthread_unwind_buf_t *__buf)
      __cleanup_fct_attribute;
 extern void __pthread_unregister_cancel (__pthread_unwind_buf_t *__buf)
      __cleanup_fct_attribute;
-#if defined NOT_IN_libc && defined IS_IN_libpthread
+#if IS_IN (libpthread)
 hidden_proto (__pthread_unwind)
 hidden_proto (__pthread_unwind_next)
 hidden_proto (__pthread_register_cancel)
 hidden_proto (__pthread_unregister_cancel)
 # ifdef SHARED
 extern void attribute_hidden pthread_cancel_init (void);
+extern void __unwind_freeres (void);
 # endif
 #endif
 
@@ -252,7 +306,7 @@ __do_cancel (void)
 #define CANCEL_RESET(oldtype) \
   __pthread_disable_asynccancel (oldtype)
 
-#if !defined NOT_IN_libc
+#if IS_IN (libc)
 /* Same as CANCEL_ASYNC, but for use in libc.so.  */
 # define LIBC_CANCEL_ASYNC() \
   __libc_enable_asynccancel ()
@@ -262,13 +316,13 @@ __do_cancel (void)
 # define LIBC_CANCEL_HANDLED() \
   __asm (".globl " __SYMBOL_PREFIX "__libc_enable_asynccancel"); \
   __asm (".globl " __SYMBOL_PREFIX "__libc_disable_asynccancel")
-#elif defined NOT_IN_libc && defined IS_IN_libpthread
+#elif IS_IN (libpthread)
 # define LIBC_CANCEL_ASYNC() CANCEL_ASYNC ()
 # define LIBC_CANCEL_RESET(val) CANCEL_RESET (val)
 # define LIBC_CANCEL_HANDLED() \
   __asm (".globl " __SYMBOL_PREFIX "__pthread_enable_asynccancel"); \
   __asm (".globl " __SYMBOL_PREFIX "__pthread_disable_asynccancel")
-#elif defined NOT_IN_libc && defined IS_IN_librt
+#elif IS_IN (librt)
 # define LIBC_CANCEL_ASYNC() \
   __librt_enable_asynccancel ()
 # define LIBC_CANCEL_RESET(val) \
@@ -282,22 +336,6 @@ __do_cancel (void)
 # define LIBC_CANCEL_HANDLED() /* Nothing.  */
 #endif
 
-/* The signal used for asynchronous cancelation.  */
-#define SIGCANCEL      __SIGRTMIN
-
-
-/* Signal needed for the kernel-supported POSIX timer implementation.
-   We can reuse the cancellation signal since we can distinguish
-   cancellation from timer expirations.  */
-#define SIGTIMER       SIGCANCEL
-
-
-/* Signal used to implement the setuid et.al. functions.  */
-#define SIGSETXID      (__SIGRTMIN + 1)
-
-/* Used to communicate with signal handler.  */
-extern struct xid_command *__xidcmd attribute_hidden;
-
 
 /* Internal prototypes.  */
 
@@ -323,7 +361,7 @@ extern int __make_stacks_executable (void **stack_endp)
 
 /* longjmp handling.  */
 extern void __pthread_cleanup_upto (__jmp_buf target, char *targetframe);
-#if defined NOT_IN_libc && defined IS_IN_libpthread
+#if IS_IN (libpthread)
 hidden_proto (__pthread_cleanup_upto)
 #endif
 
@@ -357,7 +395,9 @@ extern int *__libc_pthread_init (unsigned long int *ptr,
                                 const struct pthread_functions *functions)
      internal_function;
 
-/* Variable set to a nonzero value if more than one thread runs or ran.  */
+/* Variable set to a nonzero value either if more than one thread runs or ran,
+   or if a single-threaded process is trying to cancel itself.  See
+   nptl/descr.h for more context on the single-threaded process case.  */
 extern int __pthread_multiple_threads attribute_hidden;
 /* Pointer to the corresponding variable in libc.  */
 extern int *__libc_multiple_threads_ptr attribute_hidden;
@@ -375,6 +415,7 @@ weak_function;
 
 extern void __pthread_init_static_tls (struct link_map *) attribute_hidden;
 
+extern size_t __pthread_get_minstack (const pthread_attr_t *attr);
 
 /* Namespace save aliases.  */
 extern int __pthread_getschedparam (pthread_t thread_id, int *policy,
@@ -383,22 +424,17 @@ extern int __pthread_setschedparam (pthread_t thread_id, int policy,
                                    const struct sched_param *param);
 extern int __pthread_setcancelstate (int state, int *oldstate);
 extern int __pthread_mutex_init (pthread_mutex_t *__mutex,
-                                __const pthread_mutexattr_t *__mutexattr);
-extern int __pthread_mutex_init_internal (pthread_mutex_t *__mutex,
-                                         __const pthread_mutexattr_t *__mutexattr)
-     attribute_hidden;
+                                const pthread_mutexattr_t *__mutexattr);
 extern int __pthread_mutex_destroy (pthread_mutex_t *__mutex);
-extern int __pthread_mutex_destroy_internal (pthread_mutex_t *__mutex)
-     attribute_hidden;
 extern int __pthread_mutex_trylock (pthread_mutex_t *_mutex);
 extern int __pthread_mutex_lock (pthread_mutex_t *__mutex);
-extern int __pthread_mutex_lock_internal (pthread_mutex_t *__mutex)
-     attribute_hidden;
+extern int __pthread_mutex_timedlock (pthread_mutex_t *__mutex,
+     const struct timespec *__abstime);
 extern int __pthread_mutex_cond_lock (pthread_mutex_t *__mutex)
      attribute_hidden internal_function;
+extern void __pthread_mutex_cond_lock_adjust (pthread_mutex_t *__mutex)
+     attribute_hidden internal_function;
 extern int __pthread_mutex_unlock (pthread_mutex_t *__mutex);
-extern int __pthread_mutex_unlock_internal (pthread_mutex_t *__mutex)
-     attribute_hidden;
 extern int __pthread_mutex_unlock_usercnt (pthread_mutex_t *__mutex,
                                           int __decr)
      attribute_hidden internal_function;
@@ -422,32 +458,29 @@ extern int __pthread_attr_getschedpolicy (const pthread_attr_t *attr,
 extern int __pthread_attr_setschedpolicy (pthread_attr_t *attr, int policy);
 extern int __pthread_attr_getscope (const pthread_attr_t *attr, int *scope);
 extern int __pthread_attr_setscope (pthread_attr_t *attr, int scope);
-extern int __pthread_attr_getstackaddr (__const pthread_attr_t *__restrict
+extern int __pthread_attr_getstackaddr (const pthread_attr_t *__restrict
                                        __attr, void **__restrict __stackaddr);
 extern int __pthread_attr_setstackaddr (pthread_attr_t *__attr,
                                        void *__stackaddr);
-extern int __pthread_attr_getstacksize (__const pthread_attr_t *__restrict
+extern int __pthread_attr_getstacksize (const pthread_attr_t *__restrict
                                        __attr,
                                        size_t *__restrict __stacksize);
 extern int __pthread_attr_setstacksize (pthread_attr_t *__attr,
                                        size_t __stacksize);
-extern int __pthread_attr_getstack (__const pthread_attr_t *__restrict __attr,
+extern int __pthread_attr_getstack (const pthread_attr_t *__restrict __attr,
                                    void **__restrict __stackaddr,
                                    size_t *__restrict __stacksize);
 extern int __pthread_attr_setstack (pthread_attr_t *__attr, void *__stackaddr,
                                    size_t __stacksize);
 extern int __pthread_rwlock_init (pthread_rwlock_t *__restrict __rwlock,
-                                 __const pthread_rwlockattr_t *__restrict
+                                 const pthread_rwlockattr_t *__restrict
                                  __attr);
 extern int __pthread_rwlock_destroy (pthread_rwlock_t *__rwlock);
 extern int __pthread_rwlock_rdlock (pthread_rwlock_t *__rwlock);
-extern int __pthread_rwlock_rdlock_internal (pthread_rwlock_t *__rwlock);
 extern int __pthread_rwlock_tryrdlock (pthread_rwlock_t *__rwlock);
 extern int __pthread_rwlock_wrlock (pthread_rwlock_t *__rwlock);
-extern int __pthread_rwlock_wrlock_internal (pthread_rwlock_t *__rwlock);
 extern int __pthread_rwlock_trywrlock (pthread_rwlock_t *__rwlock);
 extern int __pthread_rwlock_unlock (pthread_rwlock_t *__rwlock);
-extern int __pthread_rwlock_unlock_internal (pthread_rwlock_t *__rwlock);
 extern int __pthread_cond_broadcast (pthread_cond_t *cond);
 extern int __pthread_cond_destroy (pthread_cond_t *cond);
 extern int __pthread_cond_init (pthread_cond_t *cond,
@@ -460,27 +493,44 @@ extern int __pthread_cond_timedwait (pthread_cond_t *cond,
 extern int __pthread_condattr_destroy (pthread_condattr_t *attr);
 extern int __pthread_condattr_init (pthread_condattr_t *attr);
 extern int __pthread_key_create (pthread_key_t *key, void (*destr) (void *));
-extern int __pthread_key_create_internal (pthread_key_t *key,
-                                         void (*destr) (void *));
+extern int __pthread_key_delete (pthread_key_t key);
 extern void *__pthread_getspecific (pthread_key_t key);
-extern void *__pthread_getspecific_internal (pthread_key_t key);
 extern int __pthread_setspecific (pthread_key_t key, const void *value);
-extern int __pthread_setspecific_internal (pthread_key_t key,
-                                          const void *value);
 extern int __pthread_once (pthread_once_t *once_control,
                           void (*init_routine) (void));
-extern int __pthread_once_internal (pthread_once_t *once_control,
-                                   void (*init_routine) (void));
 extern int __pthread_atfork (void (*prepare) (void), void (*parent) (void),
                             void (*child) (void));
 extern pthread_t __pthread_self (void);
 extern int __pthread_equal (pthread_t thread1, pthread_t thread2);
+extern int __pthread_detach (pthread_t th);
+extern int __pthread_cancel (pthread_t th);
 extern int __pthread_kill (pthread_t threadid, int signo);
-extern void __pthread_exit (void *value);
+extern void __pthread_exit (void *value) __attribute__ ((__noreturn__));
+extern int __pthread_join (pthread_t threadid, void **thread_return);
 extern int __pthread_setcanceltype (int type, int *oldtype);
 extern int __pthread_enable_asynccancel (void) attribute_hidden;
 extern void __pthread_disable_asynccancel (int oldtype)
      internal_function attribute_hidden;
+extern void __pthread_testcancel (void);
+
+#if IS_IN (libpthread)
+hidden_proto (__pthread_mutex_init)
+hidden_proto (__pthread_mutex_destroy)
+hidden_proto (__pthread_mutex_lock)
+hidden_proto (__pthread_mutex_trylock)
+hidden_proto (__pthread_mutex_unlock)
+hidden_proto (__pthread_rwlock_rdlock)
+hidden_proto (__pthread_rwlock_wrlock)
+hidden_proto (__pthread_rwlock_unlock)
+hidden_proto (__pthread_key_create)
+hidden_proto (__pthread_getspecific)
+hidden_proto (__pthread_setspecific)
+hidden_proto (__pthread_once)
+hidden_proto (__pthread_setcancelstate)
+hidden_proto (__pthread_testcancel)
+hidden_proto (__pthread_mutexattr_init)
+hidden_proto (__pthread_mutexattr_settype)
+#endif
 
 extern int __pthread_cond_broadcast_2_0 (pthread_cond_2_0_t *cond);
 extern int __pthread_cond_destroy_2_0 (pthread_cond_2_0_t *cond);
@@ -507,21 +557,25 @@ extern int __librt_enable_asynccancel (void) attribute_hidden;
 extern void __librt_disable_asynccancel (int oldtype)
      internal_function attribute_hidden;
 
-#ifdef IS_IN_libpthread
+#if IS_IN (libpthread)
 /* Special versions which use non-exported functions.  */
 extern void __pthread_cleanup_push (struct _pthread_cleanup_buffer *buffer,
                                    void (*routine) (void *), void *arg)
      attribute_hidden;
+
+/* Replace cleanup macros defined in <pthread.h> with internal
+   versions that don't depend on unwind info and better support
+   cancellation.  */
 # undef pthread_cleanup_push
-# define pthread_cleanup_push(routine,arg) \
-  { struct _pthread_cleanup_buffer _buffer;                                  \
-    __pthread_cleanup_push (&_buffer, (routine), (arg));
+# define pthread_cleanup_push(routine,arg)              \
+  { struct _pthread_cleanup_buffer _buffer;             \
+  __pthread_cleanup_push (&_buffer, (routine), (arg));
 
 extern void __pthread_cleanup_pop (struct _pthread_cleanup_buffer *buffer,
                                   int execute) attribute_hidden;
 # undef pthread_cleanup_pop
-# define pthread_cleanup_pop(execute) \
-    __pthread_cleanup_pop (&_buffer, (execute)); }
+# define pthread_cleanup_pop(execute)                   \
+  __pthread_cleanup_pop (&_buffer, (execute)); }
 #endif
 
 extern void __pthread_cleanup_push_defer (struct _pthread_cleanup_buffer *buffer,
@@ -531,19 +585,26 @@ extern void __pthread_cleanup_pop_restore (struct _pthread_cleanup_buffer *buffe
 
 /* Old cleanup interfaces, still used in libc.so.  */
 extern void _pthread_cleanup_push (struct _pthread_cleanup_buffer *buffer,
-                                   void (*routine) (void *), void *arg);
+                                  void (*routine) (void *), void *arg);
 extern void _pthread_cleanup_pop (struct _pthread_cleanup_buffer *buffer,
-                                  int execute);
+                                 int execute);
 extern void _pthread_cleanup_push_defer (struct _pthread_cleanup_buffer *buffer,
-                                         void (*routine) (void *), void *arg);
+                                        void (*routine) (void *), void *arg);
 extern void _pthread_cleanup_pop_restore (struct _pthread_cleanup_buffer *buffer,
-                                          int execute);
+                                         int execute);
 
 extern void __nptl_deallocate_tsd (void) attribute_hidden;
 
+extern void __nptl_setxid_error (struct xid_command *cmdp, int error)
+  attribute_hidden;
 extern int __nptl_setxid (struct xid_command *cmdp) attribute_hidden;
+#ifndef SHARED
+extern void __nptl_set_robust (struct pthread *self);
+#endif
+
+extern void __free_stacks (size_t limit) attribute_hidden;
 
-extern void __free_stack_cache (void) attribute_hidden;
+extern void __wait_lookup_done (void) attribute_hidden;
 
 #ifdef SHARED
 # define PTHREAD_STATIC_FN_REQUIRE(name)
@@ -551,15 +612,39 @@ extern void __free_stack_cache (void) attribute_hidden;
 # define PTHREAD_STATIC_FN_REQUIRE(name) __asm (".globl " #name);
 #endif
 
+/* Returns 0 if POL is a valid scheduling policy.  */
+static inline int
+check_sched_policy_attr (int pol)
+{
+  if (pol == SCHED_OTHER || pol == SCHED_FIFO || pol == SCHED_RR)
+    return 0;
 
-#ifndef __NR_set_robust_list
-/* XXX For the time being...  Once we can rely on the kernel headers
-   having the definition remove these lines.  */
-# if defined __i386__
-#  define __NR_set_robust_list  311
-# elif defined __x86_64__
-#  define __NR_set_robust_list  273
-# endif
-#endif
+  return EINVAL;
+}
+
+/* Returns 0 if PR is within the accepted range of priority values for
+   the scheduling policy POL or EINVAL otherwise.  */
+static inline int
+check_sched_priority_attr (int pr, int pol)
+{
+  int min = __sched_get_priority_min (pol);
+  int max = __sched_get_priority_max (pol);
+
+  if (min >= 0 && max >= 0 && pr >= min && pr <= max)
+    return 0;
+
+  return EINVAL;
+}
+
+/* Returns 0 if ST is a valid stack size for a thread stack and EINVAL
+   otherwise.  */
+static inline int
+check_stacksize_attr (size_t st)
+{
+  if (st >= PTHREAD_STACK_MIN)
+    return 0;
+
+  return EINVAL;
+}
 
 #endif /* pthreadP.h */