|
| 1 | +/* |
| 2 | + +----------------------------------------------------------------------+ |
| 3 | + | Swoole | |
| 4 | + +----------------------------------------------------------------------+ |
| 5 | + | This source file is subject to version 2.0 of the Apache license, | |
| 6 | + | that is bundled with this package in the file LICENSE, and is | |
| 7 | + | available through the world-wide-web at the following url: | |
| 8 | + | http://www.apache.org/licenses/LICENSE-2.0.html | |
| 9 | + | If you did not receive a copy of the Apache2.0 license and are unable| |
| 10 | + | to obtain it through the world-wide-web, please send a note to | |
| 11 | + | [email protected] so we can mail you a copy immediately. | |
| 12 | + +----------------------------------------------------------------------+ |
| 13 | + | Author: Tianfeng Han <[email protected]> | |
| 14 | + +----------------------------------------------------------------------+ |
| 15 | +*/ |
| 16 | + |
| 17 | +#include "php_swoole_private.h" |
| 18 | +#include "php_swoole_thread.h" |
| 19 | +#include "swoole_memory.h" |
| 20 | +#include "swoole_lock.h" |
| 21 | + |
| 22 | +#ifdef SW_THREAD |
| 23 | + |
| 24 | +BEGIN_EXTERN_C() |
| 25 | +#include "stubs/php_swoole_thread_barrier_arginfo.h" |
| 26 | +END_EXTERN_C() |
| 27 | + |
| 28 | +using swoole::Barrier; |
| 29 | + |
| 30 | +static zend_class_entry *swoole_thread_barrier_ce; |
| 31 | +static zend_object_handlers swoole_thread_barrier_handlers; |
| 32 | + |
| 33 | + |
| 34 | +struct BarrierResource : public ThreadResource { |
| 35 | + Barrier barrier_; |
| 36 | + BarrierResource(int count) : ThreadResource() { |
| 37 | + barrier_.init(false, count); |
| 38 | + } |
| 39 | + void wait() { |
| 40 | + barrier_.wait(); |
| 41 | + } |
| 42 | + ~BarrierResource() { |
| 43 | + barrier_.destroy(); |
| 44 | + } |
| 45 | +}; |
| 46 | + |
| 47 | +struct BarrierObject { |
| 48 | + BarrierResource *barrier; |
| 49 | + zend_object std; |
| 50 | +}; |
| 51 | + |
| 52 | +static sw_inline BarrierObject *php_swoole_thread_barrier_fetch_object(zend_object *obj) { |
| 53 | + return (BarrierObject *) ((char *) obj - swoole_thread_barrier_handlers.offset); |
| 54 | +} |
| 55 | + |
| 56 | +static BarrierResource *php_swoole_thread_barrier_get_ptr(zval *zobject) { |
| 57 | + return php_swoole_thread_barrier_fetch_object(Z_OBJ_P(zobject))->barrier; |
| 58 | +} |
| 59 | + |
| 60 | +static BarrierResource *php_swoole_thread_barrier_get_and_check_ptr(zval *zobject) { |
| 61 | + BarrierResource *barrier = php_swoole_thread_barrier_get_ptr(zobject); |
| 62 | + if (!barrier) { |
| 63 | + php_swoole_fatal_error(E_ERROR, "must call constructor first"); |
| 64 | + } |
| 65 | + return barrier; |
| 66 | +} |
| 67 | + |
| 68 | +static void php_swoole_thread_barrier_free_object(zend_object *object) { |
| 69 | + BarrierObject *bo = php_swoole_thread_barrier_fetch_object(object); |
| 70 | + zend_long resource_id = zend::object_get_long(object, ZEND_STRL("id")); |
| 71 | + if (bo->barrier && php_swoole_thread_resource_free(resource_id, bo->barrier)) { |
| 72 | + delete bo->barrier; |
| 73 | + bo->barrier = nullptr; |
| 74 | + } |
| 75 | + zend_object_std_dtor(object); |
| 76 | +} |
| 77 | + |
| 78 | +static zend_object *php_swoole_thread_barrier_create_object(zend_class_entry *ce) { |
| 79 | + BarrierObject *bo = (BarrierObject *) zend_object_alloc(sizeof(BarrierObject), ce); |
| 80 | + zend_object_std_init(&bo->std, ce); |
| 81 | + object_properties_init(&bo->std, ce); |
| 82 | + bo->std.handlers = &swoole_thread_barrier_handlers; |
| 83 | + return &bo->std; |
| 84 | +} |
| 85 | + |
| 86 | +SW_EXTERN_C_BEGIN |
| 87 | +static PHP_METHOD(swoole_thread_barrier, __construct); |
| 88 | +static PHP_METHOD(swoole_thread_barrier, wait); |
| 89 | +static PHP_METHOD(swoole_thread_barrier, __wakeup); |
| 90 | +SW_EXTERN_C_END |
| 91 | + |
| 92 | +// clang-format off |
| 93 | +static const zend_function_entry swoole_thread_barrier_methods[] = |
| 94 | +{ |
| 95 | + PHP_ME(swoole_thread_barrier, __construct, arginfo_class_Swoole_Thread_Barrier___construct, ZEND_ACC_PUBLIC) |
| 96 | + PHP_ME(swoole_thread_barrier, wait, arginfo_class_Swoole_Thread_Barrier_wait, ZEND_ACC_PUBLIC) |
| 97 | + PHP_ME(swoole_thread_barrier, __wakeup, arginfo_class_Swoole_Thread_Barrier___wakeup, ZEND_ACC_PUBLIC) |
| 98 | + PHP_FE_END |
| 99 | +}; |
| 100 | +// clang-format on |
| 101 | + |
| 102 | +void php_swoole_thread_barrier_minit(int module_number) { |
| 103 | + SW_INIT_CLASS_ENTRY(swoole_thread_barrier, "Swoole\\Thread\\Barrier", nullptr, swoole_thread_barrier_methods); |
| 104 | + zend_declare_property_long(swoole_thread_barrier_ce, ZEND_STRL("id"), 0, ZEND_ACC_PUBLIC); |
| 105 | + SW_SET_CLASS_CLONEABLE(swoole_thread_barrier, sw_zend_class_clone_deny); |
| 106 | + SW_SET_CLASS_UNSET_PROPERTY_HANDLER(swoole_thread_barrier, sw_zend_class_unset_property_deny); |
| 107 | + SW_SET_CLASS_CUSTOM_OBJECT( |
| 108 | + swoole_thread_barrier, php_swoole_thread_barrier_create_object, php_swoole_thread_barrier_free_object, BarrierObject, std); |
| 109 | +} |
| 110 | + |
| 111 | +static PHP_METHOD(swoole_thread_barrier, __construct) { |
| 112 | + auto bo = php_swoole_thread_barrier_fetch_object(Z_OBJ_P(ZEND_THIS)); |
| 113 | + if (bo->barrier != nullptr) { |
| 114 | + zend_throw_error(NULL, "Constructor of %s can only be called once", SW_Z_OBJCE_NAME_VAL_P(ZEND_THIS)); |
| 115 | + RETURN_FALSE; |
| 116 | + } |
| 117 | + |
| 118 | + zend_long count; |
| 119 | + ZEND_PARSE_PARAMETERS_START(1, 1) |
| 120 | + Z_PARAM_LONG(count) |
| 121 | + ZEND_PARSE_PARAMETERS_END(); |
| 122 | + |
| 123 | + bo->barrier = new BarrierResource(count); |
| 124 | + auto resource_id = php_swoole_thread_resource_insert(bo->barrier); |
| 125 | + zend_update_property_long(swoole_thread_barrier_ce, SW_Z8_OBJ_P(ZEND_THIS), ZEND_STRL("id"), resource_id); |
| 126 | + RETURN_TRUE; |
| 127 | +} |
| 128 | + |
| 129 | +static PHP_METHOD(swoole_thread_barrier, wait) { |
| 130 | + BarrierResource *barrier = php_swoole_thread_barrier_get_and_check_ptr(ZEND_THIS); |
| 131 | + if (barrier) { |
| 132 | + barrier->wait(); |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +static PHP_METHOD(swoole_thread_barrier, __wakeup) { |
| 137 | + auto bo = php_swoole_thread_barrier_fetch_object(Z_OBJ_P(ZEND_THIS)); |
| 138 | + zend_long resource_id = zend::object_get_long(ZEND_THIS, ZEND_STRL("id")); |
| 139 | + bo->barrier = static_cast<BarrierResource *>(php_swoole_thread_resource_fetch(resource_id)); |
| 140 | + if (!bo->barrier) { |
| 141 | + zend_throw_exception(swoole_exception_ce, EMSG_NO_RESOURCE, ECODE_NO_RESOURCE); |
| 142 | + return; |
| 143 | + } |
| 144 | +} |
| 145 | +#endif |
0 commit comments