zend_fibers.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) Zend Technologies Ltd. (http://www.zend.com) |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.00 of the Zend license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.zend.com/license/2_00.txt. |
  11. | If you did not receive a copy of the Zend license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@zend.com so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Aaron Piotrowski <aaron@trowski.com> |
  16. | Martin Schröder <m.schroeder2007@gmail.com> |
  17. +----------------------------------------------------------------------+
  18. */
  19. #ifndef ZEND_FIBERS_H
  20. #define ZEND_FIBERS_H
  21. #include "zend_API.h"
  22. #include "zend_types.h"
  23. #define ZEND_FIBER_GUARD_PAGES 1
  24. #define ZEND_FIBER_DEFAULT_C_STACK_SIZE (4096 * (((sizeof(void *)) < 8) ? 256 : 512))
  25. #define ZEND_FIBER_VM_STACK_SIZE (1024 * sizeof(zval))
  26. BEGIN_EXTERN_C()
  27. typedef enum {
  28. ZEND_FIBER_STATUS_INIT,
  29. ZEND_FIBER_STATUS_RUNNING,
  30. ZEND_FIBER_STATUS_SUSPENDED,
  31. ZEND_FIBER_STATUS_DEAD,
  32. } zend_fiber_status;
  33. typedef enum {
  34. ZEND_FIBER_FLAG_THREW = 1 << 0,
  35. ZEND_FIBER_FLAG_BAILOUT = 1 << 1,
  36. ZEND_FIBER_FLAG_DESTROYED = 1 << 2,
  37. } zend_fiber_flag;
  38. typedef enum {
  39. ZEND_FIBER_TRANSFER_FLAG_ERROR = 1 << 0,
  40. ZEND_FIBER_TRANSFER_FLAG_BAILOUT = 1 << 1
  41. } zend_fiber_transfer_flag;
  42. void zend_register_fiber_ce(void);
  43. void zend_fiber_init(void);
  44. void zend_fiber_shutdown(void);
  45. extern ZEND_API zend_class_entry *zend_ce_fiber;
  46. typedef struct _zend_fiber_stack zend_fiber_stack;
  47. /* Encapsulates data needed for a context switch. */
  48. typedef struct _zend_fiber_transfer {
  49. /* Fiber that will be switched to / has resumed us. */
  50. zend_fiber_context *context;
  51. /* Value to that should be send to (or was received from) a fiber. */
  52. zval value;
  53. /* Bitmask of flags defined in enum zend_fiber_transfer_flag. */
  54. uint8_t flags;
  55. } zend_fiber_transfer;
  56. /* Coroutine functions must populate the given transfer with a new context
  57. * and (optional) data before they return. */
  58. typedef void (*zend_fiber_coroutine)(zend_fiber_transfer *transfer);
  59. struct _zend_fiber_context {
  60. /* Pointer to boost.context or ucontext_t data. */
  61. void *handle;
  62. /* Pointer that identifies the fiber type. */
  63. void *kind;
  64. /* Entrypoint function of the fiber. */
  65. zend_fiber_coroutine function;
  66. /* Assigned C stack. */
  67. zend_fiber_stack *stack;
  68. /* Fiber status. */
  69. zend_fiber_status status;
  70. /* Reserved for extensions */
  71. void *reserved[ZEND_MAX_RESERVED_RESOURCES];
  72. };
  73. struct _zend_fiber {
  74. /* PHP object handle. */
  75. zend_object std;
  76. /* Flags are defined in enum zend_fiber_flag. */
  77. uint8_t flags;
  78. /* Native C fiber context. */
  79. zend_fiber_context context;
  80. /* Fiber that resumed us. */
  81. zend_fiber_context *caller;
  82. /* Fiber that suspended us. */
  83. zend_fiber_context *previous;
  84. /* Callback and info / cache to be used when fiber is started. */
  85. zend_fcall_info fci;
  86. zend_fcall_info_cache fci_cache;
  87. /* Current Zend VM execute data being run by the fiber. */
  88. zend_execute_data *execute_data;
  89. /* Frame on the bottom of the fiber vm stack. */
  90. zend_execute_data *stack_bottom;
  91. /* Storage for fiber return value. */
  92. zval result;
  93. };
  94. /* These functions may be used to create custom fiber objects using the bundled fiber switching context. */
  95. ZEND_API bool zend_fiber_init_context(zend_fiber_context *context, void *kind, zend_fiber_coroutine coroutine, size_t stack_size);
  96. ZEND_API void zend_fiber_destroy_context(zend_fiber_context *context);
  97. ZEND_API void zend_fiber_switch_context(zend_fiber_transfer *transfer);
  98. ZEND_API void zend_fiber_switch_block(void);
  99. ZEND_API void zend_fiber_switch_unblock(void);
  100. ZEND_API bool zend_fiber_switch_blocked(void);
  101. END_EXTERN_C()
  102. static zend_always_inline zend_fiber *zend_fiber_from_context(zend_fiber_context *context)
  103. {
  104. ZEND_ASSERT(context->kind == zend_ce_fiber && "Fiber context does not belong to a Zend fiber");
  105. return (zend_fiber *)(((char *) context) - XtOffsetOf(zend_fiber, context));
  106. }
  107. static zend_always_inline zend_fiber_context *zend_fiber_get_context(zend_fiber *fiber)
  108. {
  109. return &fiber->context;
  110. }
  111. #endif