cancel.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. if (SINGLE_THREAD_P) \
  54. return __NC(name) params; \
  55. int oldtype = LIBC_CANCEL_ASYNC(); \
  56. res_type result = __NC(name) params; \
  57. LIBC_CANCEL_RESET(oldtype); \
  58. return result; \
  59. }
  60. # define lt_strong_alias(name)
  61. # define lt_libc_hidden(name) libc_hidden_def(name)
  62. #elif defined __UCLIBC_HAS_LINUXTHREADS__
  63. # define CANCELLABLE_SYSCALL(res_type, name, param_list, params) \
  64. weak_alias(__NC(name),name) \
  65. lt_strong_alias(name)
  66. # define lt_strong_alias(name) \
  67. __NC_OLD_PROTO(name) \
  68. strong_alias(name,__NC_OLD(name))
  69. # define lt_libc_hidden(name) libc_hidden_weak(name)
  70. #else
  71. # define CANCELLABLE_SYSCALL(res_type, name, param_list, params) \
  72. strong_alias(__NC(name),name)
  73. # define lt_strong_alias(name)
  74. # define lt_libc_hidden(name) libc_hidden_def(name)
  75. #endif
  76. /* disable it, useless, glibc uses it only for tests */
  77. # undef LIBC_CANCEL_HANDLED
  78. # define LIBC_CANCEL_HANDLED()
  79. #endif /* NOT_IN_libc */
  80. #endif