cancel.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Copyright (C) 2000-2011 Erik Andersen <andersen@uclibc.org>
  4. *
  5. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  6. */
  7. #ifndef _CANCEL_H
  8. #define _CANCEL_H
  9. /*
  10. * Usage of this header:
  11. * 1. define a static or hidden function __NC(NAME) - expands to __NAME_nocancel
  12. * 2. if it is hidden, add the prototype to the appropiate header where NAME has
  13. * it's prototype (guarded by _LIBC)
  14. * 3. add a CANCELLABLE_SYSCALL(...) line at the end, this will create the function
  15. * NAME (as weak) with enabled cancellation for NPTL (and later for new LT), for
  16. * LT_OLD it will also create a strong_alias to __libc_NAME to be used in libpthread
  17. * 4. if you need libc_hidden_(weak|def) line, use instead lt_libc_hidden, this will
  18. * take care of the correct type, weak or strong depending on the THREADS type
  19. * 5. If the implementation can't be done using CANCELLABLE_SYSCALL (like for fcntl)
  20. * you need to manually add lt_strong_alias() line too, to optionally create the
  21. * __libc_NAME alias
  22. * 6. if functions are needed to implement __NC(NAME), that themselves are cancellable,
  23. * decide how the cancellation should be solved, two variants are possible:
  24. * a. use the other function as __NC(FUNC), this way you access the non-cancellable
  25. * variant and provide by CANCELLABLE_SYSCALL(...) the dedicated cancellation for NAME.
  26. * be aware, that for this case __NC(FUNC) has to be hidden (not static)
  27. * b. use the other function with it's name (FUNC) and add LIBC_CANCEL_HANDLED(); at
  28. * the end of file with a comment telling us which function took care of the cancellation
  29. * Note: LIBC_CANCEL_HANDLED() is noop on uClibc, glibc uses it only for tests, we use
  30. * it only for "documentation".
  31. *
  32. * For now the use of this file is limited to libc, will expand later to support libpthread
  33. * and librt as well.
  34. */
  35. #include <features.h>
  36. #ifndef NOT_IN_libc
  37. #define __NC(name) _NC(name)
  38. #define _NC(name) __##name##_nocancel
  39. #define __NC_OLD(name) _NC_OLD(name)
  40. #define _NC_OLD(name) __libc_##name
  41. #define __NC_PROTO(name) extern __typeof(name) __NC(name) attribute_hidden;
  42. #define __NC_OLD_PROTO(name) extern __typeof(name) __NC_OLD(name);
  43. #if defined __UCLIBC_HAS_THREADS__ && !defined __UCLIBC_HAS_LINUXTHREADS__
  44. # define __NEW_THREADS 1
  45. #else
  46. # define SINGLE_THREAD_P 1
  47. #endif
  48. #ifdef __NEW_THREADS
  49. # include <sysdep-cancel.h>
  50. # define CANCELLABLE_SYSCALL(res_type, name, param_list, params) \
  51. res_type weak_function name param_list \
  52. { \
  53. int oldtype; \
  54. res_type result; \
  55. if (SINGLE_THREAD_P) \
  56. return __NC(name) params; \
  57. oldtype = LIBC_CANCEL_ASYNC(); \
  58. result = __NC(name) params; \
  59. LIBC_CANCEL_RESET(oldtype); \
  60. return result; \
  61. }
  62. # define lt_strong_alias(name)
  63. # define lt_libc_hidden(name) libc_hidden_def(name)
  64. #elif defined __UCLIBC_HAS_LINUXTHREADS__
  65. # define CANCELLABLE_SYSCALL(res_type, name, param_list, params) \
  66. weak_alias(__NC(name),name) \
  67. lt_strong_alias(name)
  68. # define lt_strong_alias(name) \
  69. __NC_OLD_PROTO(name) \
  70. strong_alias(name,__NC_OLD(name))
  71. # define lt_libc_hidden(name) libc_hidden_weak(name)
  72. #else
  73. # define CANCELLABLE_SYSCALL(res_type, name, param_list, params) \
  74. strong_alias(__NC(name),name)
  75. # define lt_strong_alias(name)
  76. # define lt_libc_hidden(name) libc_hidden_def(name)
  77. #endif
  78. /* disable it, useless, glibc uses it only for tests */
  79. # undef LIBC_CANCEL_HANDLED
  80. # define LIBC_CANCEL_HANDLED()
  81. #endif /* NOT_IN_libc */
  82. #endif