greenlet.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. typedef struct _greenlet {
  12. PyObject_HEAD
  13. char* stack_start;
  14. char* stack_stop;
  15. char* stack_copy;
  16. intptr_t stack_saved;
  17. struct _greenlet* stack_prev;
  18. struct _greenlet* parent;
  19. PyObject* run_info;
  20. struct _frame* top_frame;
  21. int recursion_depth;
  22. PyObject* weakreflist;
  23. #if PY_VERSION_HEX >= 0x030700A3
  24. _PyErr_StackItem* exc_info;
  25. _PyErr_StackItem exc_state;
  26. #else
  27. PyObject* exc_type;
  28. PyObject* exc_value;
  29. PyObject* exc_traceback;
  30. #endif
  31. PyObject* dict;
  32. #if PY_VERSION_HEX >= 0x030700A3
  33. PyObject* context;
  34. #endif
  35. #if PY_VERSION_HEX >= 0x30A00B1
  36. CFrame* cframe;
  37. #endif
  38. } PyGreenlet;
  39. #define PyGreenlet_Check(op) PyObject_TypeCheck(op, &PyGreenlet_Type)
  40. #define PyGreenlet_MAIN(op) (((PyGreenlet*)(op))->stack_stop == (char*)-1)
  41. #define PyGreenlet_STARTED(op) (((PyGreenlet*)(op))->stack_stop != NULL)
  42. #define PyGreenlet_ACTIVE(op) (((PyGreenlet*)(op))->stack_start != NULL)
  43. #define PyGreenlet_GET_PARENT(op) (((PyGreenlet*)(op))->parent)
  44. /* C API functions */
  45. /* Total number of symbols that are exported */
  46. #define PyGreenlet_API_pointers 8
  47. #define PyGreenlet_Type_NUM 0
  48. #define PyExc_GreenletError_NUM 1
  49. #define PyExc_GreenletExit_NUM 2
  50. #define PyGreenlet_New_NUM 3
  51. #define PyGreenlet_GetCurrent_NUM 4
  52. #define PyGreenlet_Throw_NUM 5
  53. #define PyGreenlet_Switch_NUM 6
  54. #define PyGreenlet_SetParent_NUM 7
  55. #ifndef GREENLET_MODULE
  56. /* This section is used by modules that uses the greenlet C API */
  57. static void** _PyGreenlet_API = NULL;
  58. # define PyGreenlet_Type \
  59. (*(PyTypeObject*)_PyGreenlet_API[PyGreenlet_Type_NUM])
  60. # define PyExc_GreenletError \
  61. ((PyObject*)_PyGreenlet_API[PyExc_GreenletError_NUM])
  62. # define PyExc_GreenletExit \
  63. ((PyObject*)_PyGreenlet_API[PyExc_GreenletExit_NUM])
  64. /*
  65. * PyGreenlet_New(PyObject *args)
  66. *
  67. * greenlet.greenlet(run, parent=None)
  68. */
  69. # define PyGreenlet_New \
  70. (*(PyGreenlet * (*)(PyObject * run, PyGreenlet * parent)) \
  71. _PyGreenlet_API[PyGreenlet_New_NUM])
  72. /*
  73. * PyGreenlet_GetCurrent(void)
  74. *
  75. * greenlet.getcurrent()
  76. */
  77. # define PyGreenlet_GetCurrent \
  78. (*(PyGreenlet * (*)(void)) _PyGreenlet_API[PyGreenlet_GetCurrent_NUM])
  79. /*
  80. * PyGreenlet_Throw(
  81. * PyGreenlet *greenlet,
  82. * PyObject *typ,
  83. * PyObject *val,
  84. * PyObject *tb)
  85. *
  86. * g.throw(...)
  87. */
  88. # define PyGreenlet_Throw \
  89. (*(PyObject * (*)(PyGreenlet * self, \
  90. PyObject * typ, \
  91. PyObject * val, \
  92. PyObject * tb)) \
  93. _PyGreenlet_API[PyGreenlet_Throw_NUM])
  94. /*
  95. * PyGreenlet_Switch(PyGreenlet *greenlet, PyObject *args)
  96. *
  97. * g.switch(*args, **kwargs)
  98. */
  99. # define PyGreenlet_Switch \
  100. (*(PyObject * \
  101. (*)(PyGreenlet * greenlet, PyObject * args, PyObject * kwargs)) \
  102. _PyGreenlet_API[PyGreenlet_Switch_NUM])
  103. /*
  104. * PyGreenlet_SetParent(PyObject *greenlet, PyObject *new_parent)
  105. *
  106. * g.parent = new_parent
  107. */
  108. # define PyGreenlet_SetParent \
  109. (*(int (*)(PyGreenlet * greenlet, PyGreenlet * nparent)) \
  110. _PyGreenlet_API[PyGreenlet_SetParent_NUM])
  111. /* Macro that imports greenlet and initializes C API */
  112. /* NOTE: This has actually moved to ``greenlet._greenlet._C_API``, but we
  113. keep the older definition to be sure older code that might have a copy of
  114. the header still works. */
  115. # define PyGreenlet_Import() \
  116. { \
  117. _PyGreenlet_API = (void**)PyCapsule_Import("greenlet._C_API", 0); \
  118. }
  119. #endif /* GREENLET_MODULE */
  120. #ifdef __cplusplus
  121. }
  122. #endif
  123. #endif /* !Py_GREENLETOBJECT_H */