greenlet.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* -*- indent-tabs-mode: nil; tab-width: 4; -*- */
  2. /* Greenlet object interface */
  3. #ifndef Py_GREENLETOBJECT_H
  4. #define Py_GREENLETOBJECT_H
  5. #include <Python.h>
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. /* This is deprecated and undocumented. It does not change. */
  10. #define GREENLET_VERSION "1.0.0"
  11. #ifndef GREENLET_MODULE
  12. #define implementation_ptr_t void*
  13. #endif
  14. typedef struct _greenlet {
  15. PyObject_HEAD
  16. PyObject* weakreflist;
  17. PyObject* dict;
  18. implementation_ptr_t pimpl;
  19. } PyGreenlet;
  20. #define PyGreenlet_Check(op) (op && PyObject_TypeCheck(op, &PyGreenlet_Type))
  21. /* C API functions */
  22. /* Total number of symbols that are exported */
  23. #define PyGreenlet_API_pointers 12
  24. #define PyGreenlet_Type_NUM 0
  25. #define PyExc_GreenletError_NUM 1
  26. #define PyExc_GreenletExit_NUM 2
  27. #define PyGreenlet_New_NUM 3
  28. #define PyGreenlet_GetCurrent_NUM 4
  29. #define PyGreenlet_Throw_NUM 5
  30. #define PyGreenlet_Switch_NUM 6
  31. #define PyGreenlet_SetParent_NUM 7
  32. #define PyGreenlet_MAIN_NUM 8
  33. #define PyGreenlet_STARTED_NUM 9
  34. #define PyGreenlet_ACTIVE_NUM 10
  35. #define PyGreenlet_GET_PARENT_NUM 11
  36. #ifndef GREENLET_MODULE
  37. /* This section is used by modules that uses the greenlet C API */
  38. static void** _PyGreenlet_API = NULL;
  39. # define PyGreenlet_Type \
  40. (*(PyTypeObject*)_PyGreenlet_API[PyGreenlet_Type_NUM])
  41. # define PyExc_GreenletError \
  42. ((PyObject*)_PyGreenlet_API[PyExc_GreenletError_NUM])
  43. # define PyExc_GreenletExit \
  44. ((PyObject*)_PyGreenlet_API[PyExc_GreenletExit_NUM])
  45. /*
  46. * PyGreenlet_New(PyObject *args)
  47. *
  48. * greenlet.greenlet(run, parent=None)
  49. */
  50. # define PyGreenlet_New \
  51. (*(PyGreenlet * (*)(PyObject * run, PyGreenlet * parent)) \
  52. _PyGreenlet_API[PyGreenlet_New_NUM])
  53. /*
  54. * PyGreenlet_GetCurrent(void)
  55. *
  56. * greenlet.getcurrent()
  57. */
  58. # define PyGreenlet_GetCurrent \
  59. (*(PyGreenlet * (*)(void)) _PyGreenlet_API[PyGreenlet_GetCurrent_NUM])
  60. /*
  61. * PyGreenlet_Throw(
  62. * PyGreenlet *greenlet,
  63. * PyObject *typ,
  64. * PyObject *val,
  65. * PyObject *tb)
  66. *
  67. * g.throw(...)
  68. */
  69. # define PyGreenlet_Throw \
  70. (*(PyObject * (*)(PyGreenlet * self, \
  71. PyObject * typ, \
  72. PyObject * val, \
  73. PyObject * tb)) \
  74. _PyGreenlet_API[PyGreenlet_Throw_NUM])
  75. /*
  76. * PyGreenlet_Switch(PyGreenlet *greenlet, PyObject *args)
  77. *
  78. * g.switch(*args, **kwargs)
  79. */
  80. # define PyGreenlet_Switch \
  81. (*(PyObject * \
  82. (*)(PyGreenlet * greenlet, PyObject * args, PyObject * kwargs)) \
  83. _PyGreenlet_API[PyGreenlet_Switch_NUM])
  84. /*
  85. * PyGreenlet_SetParent(PyObject *greenlet, PyObject *new_parent)
  86. *
  87. * g.parent = new_parent
  88. */
  89. # define PyGreenlet_SetParent \
  90. (*(int (*)(PyGreenlet * greenlet, PyGreenlet * nparent)) \
  91. _PyGreenlet_API[PyGreenlet_SetParent_NUM])
  92. /*
  93. * PyGreenlet_GetParent(PyObject* greenlet)
  94. *
  95. * return greenlet.parent;
  96. *
  97. * This could return NULL even if there is no exception active.
  98. * If it does not return NULL, you are responsible for decrementing the
  99. * reference count.
  100. */
  101. # define PyGreenlet_GetParent \
  102. (*(PyGreenlet* (*)(PyGreenlet*)) \
  103. _PyGreenlet_API[PyGreenlet_GET_PARENT_NUM])
  104. /*
  105. * deprecated, undocumented alias.
  106. */
  107. # define PyGreenlet_GET_PARENT PyGreenlet_GetParent
  108. # define PyGreenlet_MAIN \
  109. (*(int (*)(PyGreenlet*)) \
  110. _PyGreenlet_API[PyGreenlet_MAIN_NUM])
  111. # define PyGreenlet_STARTED \
  112. (*(int (*)(PyGreenlet*)) \
  113. _PyGreenlet_API[PyGreenlet_STARTED_NUM])
  114. # define PyGreenlet_ACTIVE \
  115. (*(int (*)(PyGreenlet*)) \
  116. _PyGreenlet_API[PyGreenlet_ACTIVE_NUM])
  117. /* Macro that imports greenlet and initializes C API */
  118. /* NOTE: This has actually moved to ``greenlet._greenlet._C_API``, but we
  119. keep the older definition to be sure older code that might have a copy of
  120. the header still works. */
  121. # define PyGreenlet_Import() \
  122. { \
  123. _PyGreenlet_API = (void**)PyCapsule_Import("greenlet._C_API", 0); \
  124. }
  125. #endif /* GREENLET_MODULE */
  126. #ifdef __cplusplus
  127. }
  128. #endif
  129. #endif /* !Py_GREENLETOBJECT_H */