0006-threads-optimize-single-threaded-applications.patch 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. From 5ca03df6978345c297225212cc0ca33d476b0272 Mon Sep 17 00:00:00 2001
  2. From: Waldemar Brodkorb <wbx@openadk.org>
  3. Date: Wed, 7 Dec 2016 07:56:44 +0100
  4. Subject: [PATCH] threads: optimize single threaded applications
  5. Revert the removal of the weak pthread functions and
  6. guarantee a link order so that single threaded applications
  7. doesn't link in all the pthread functions they don't use.
  8. Signed-off-by: Max Filippov <jcmvbkbc@gmail.com>
  9. Tested-by: Waldemar Brodkorb <wbx@uclibc-ng.org>
  10. Signed-off-by: Waldemar Brodkorb <wbx@openadk.org>
  11. ---
  12. libc/misc/internals/Makefile.in | 4 +++-
  13. libc/misc/internals/__uClibc_main.c | 37 +++++++++++++++++++++++++++++++++++++
  14. 2 files changed, 40 insertions(+), 1 deletion(-)
  15. diff --git a/libc/misc/internals/Makefile.in b/libc/misc/internals/Makefile.in
  16. index ae094ee..ce7f75a 100644
  17. --- a/libc/misc/internals/Makefile.in
  18. +++ b/libc/misc/internals/Makefile.in
  19. @@ -25,7 +25,9 @@ libc-shared-y += $(MISC_INTERNALS_OUT)/__uClibc_main.oS
  20. else
  21. libc-shared-y += $(MISC_INTERNALS_OUT)/__uClibc_main.os
  22. endif
  23. -libc-static-y += $(MISC_INTERNALS_OUT)/__uClibc_main.o
  24. +# link order is important to not pull in pthread functions, when
  25. +# a single threaded application is statically linked
  26. +libc-static-y := $(MISC_INTERNALS_OUT)/__uClibc_main.o $(libc-static-y)
  27. libc-static-$(UCLIBC_FORMAT_FLAT_SEP_DATA) += \
  28. $(MISC_INTERNALS_OUT)/shared_flat_initfini.o \
  29. $(MISC_INTERNALS_OUT)/shared_flat_add_library.o
  30. diff --git a/libc/misc/internals/__uClibc_main.c b/libc/misc/internals/__uClibc_main.c
  31. index 46e24d8..d80565e 100644
  32. --- a/libc/misc/internals/__uClibc_main.c
  33. +++ b/libc/misc/internals/__uClibc_main.c
  34. @@ -68,6 +68,43 @@ uintptr_t __stack_chk_guard attribute_relro;
  35. void internal_function _dl_aux_init (ElfW(auxv_t) *av);
  36. +#ifdef __UCLIBC_HAS_THREADS__
  37. +/*
  38. + * uClibc internal locking requires that we have weak aliases
  39. + * for dummy functions in case a single threaded application is linked.
  40. + * This needs to be in compilation unit that is pulled always
  41. + * in or linker will disregard these weaks.
  42. + */
  43. +
  44. +static int __pthread_return_0 (pthread_mutex_t *unused) { return 0; }
  45. +weak_alias (__pthread_return_0, __pthread_mutex_lock)
  46. +weak_alias (__pthread_return_0, __pthread_mutex_trylock)
  47. +weak_alias (__pthread_return_0, __pthread_mutex_unlock)
  48. +
  49. +int weak_function
  50. +__pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
  51. +{
  52. + return 0;
  53. +}
  54. +
  55. +void weak_function
  56. +_pthread_cleanup_push_defer(struct _pthread_cleanup_buffer *__buffer,
  57. + void (*__routine) (void *), void *__arg)
  58. +{
  59. + __buffer->__routine = __routine;
  60. + __buffer->__arg = __arg;
  61. +}
  62. +
  63. +void weak_function
  64. +_pthread_cleanup_pop_restore(struct _pthread_cleanup_buffer *__buffer,
  65. + int __execute)
  66. +{
  67. + if (__execute)
  68. + __buffer->__routine(__buffer->__arg);
  69. +}
  70. +
  71. +#endif /* __UCLIBC_HAS_THREADS__ */
  72. +
  73. #endif /* !SHARED */
  74. /* Defeat compiler optimization which assumes function addresses are never NULL */
  75. --
  76. 2.1.4