cancel.h 3.4 KB

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