Skip to content

Refactor thread resource #5393

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions examples/thread/nested_map.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
$map = new Swoole\Thread\Map();

$map['map1'] = new Swoole\Thread\Map();
$map['list1'] = new Swoole\Thread\ArrayList();

$map['map1']['key1'] = 'value1';
$map['list1'][0] = 'value2';
$map['str'] = 'hello world';

$map['map2'] = [
'a' => uniqid(),
'b' => random_int(1000, 9999),
];

var_dump($map['map1']['key1']);
var_dump($map['list1'][0]);

var_dump($map['list1']->toArray());

var_dump($map['map2']);
4 changes: 4 additions & 0 deletions ext-src/php_swoole_cxx.h
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,10 @@ static inline void object_set(zval *obj, const char *name, size_t l_name, const
zend_update_property_string(Z_OBJCE_P(obj), Z_OBJ_P(obj), name, l_name, value);
}

static inline void object_set(zval *obj, const char *name, size_t l_name, zend_long value) {
zend_update_property_long(Z_OBJCE_P(obj), Z_OBJ_P(obj), name, l_name, value);
}

static inline zval *object_get(zval *obj, const char *name, size_t l_name) {
static zval rv;
return zend_read_property(Z_OBJCE_P(obj), Z_OBJ_P(obj), name, l_name, 1, &rv);
Expand Down
98 changes: 81 additions & 17 deletions ext-src/php_swoole_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,44 +25,81 @@

typedef uint32_t ThreadResourceId;
struct ThreadResource;

ThreadResourceId php_swoole_thread_resource_insert(ThreadResource *res);
bool php_swoole_thread_resource_free(ThreadResourceId resource_id, ThreadResource *res);
ThreadResource *php_swoole_thread_resource_fetch(ThreadResourceId resource_id);

void php_swoole_thread_start(zend_string *file, zend_string *argv);
zend_string *php_swoole_thread_argv_serialize(zval *zdata);
bool php_swoole_thread_argv_unserialize(zend_string *data, zval *zv);
struct ZendArray;

extern zend_class_entry *swoole_thread_ce;
extern zend_class_entry *swoole_thread_error_ce;
extern zend_class_entry *swoole_thread_arraylist_ce;
extern zend_class_entry *swoole_thread_atomic_ce;
extern zend_class_entry *swoole_thread_atomic_long_ce;
extern zend_class_entry *swoole_thread_barrier_ce;
extern zend_class_entry *swoole_thread_lock_ce;
extern zend_class_entry *swoole_thread_map_ce;
extern zend_class_entry *swoole_thread_queue_ce;

void php_swoole_thread_start(zend_string *file, ZendArray *argv);
zend_string *php_swoole_serialize(zval *zdata);
bool php_swoole_unserialize(zend_string *data, zval *zv);
void php_swoole_thread_argv_clean(zval *zdata);
void php_swoole_thread_bailout(void);

zval *php_swoole_thread_get_arguments();
ThreadResource *php_swoole_thread_arraylist_cast(zval *zobject);
ThreadResource *php_swoole_thread_map_cast(zval *zobject);
ThreadResource *php_swoole_thread_queue_cast(zval *zobject);
ThreadResource *php_swoole_thread_lock_cast(zval *zobject);
ThreadResource *php_swoole_thread_atomic_cast(zval *zobject);
ThreadResource *php_swoole_thread_atomic_long_cast(zval *zobject);
ThreadResource *php_swoole_thread_barrier_cast(zval *zobject);

void php_swoole_thread_arraylist_create(zval *return_value, ThreadResource *resource);
void php_swoole_thread_map_create(zval *return_value, ThreadResource *resource);
void php_swoole_thread_queue_create(zval *return_value, ThreadResource *resource);
void php_swoole_thread_lock_create(zval *return_value, ThreadResource *resource);
void php_swoole_thread_atomic_create(zval *return_value, ThreadResource *resource);
void php_swoole_thread_atomic_long_create(zval *return_value, ThreadResource *resource);
void php_swoole_thread_barrier_create(zval *return_value, ThreadResource *resource);

int php_swoole_thread_stream_cast(zval *zstream);
void php_swoole_thread_stream_create(zval *return_value, zend_long sockfd);

int php_swoole_thread_co_socket_cast(zval *zstream, swSocketType *type);
void php_swoole_thread_co_socket_create(zval *return_value, zend_long sockfd, swSocketType type);

#define EMSG_NO_RESOURCE "resource not found"
#define ECODE_NO_RESOURCE -2

enum {
IS_ARRAYLIST = 80,
IS_QUEUE = 81,
IS_LOCK = 82,
IS_MAP = 83,
IS_BARRIER = 84,
IS_ATOMIC = 85,
IS_ATOMIC_LONG = 86,
IS_CO_SOCKET = 97,
IS_STREAM_SOCKET = 98,
IS_SERIALIZED_OBJECT = 99,
};

struct ThreadResource {
uint32_t ref_count;
class ThreadResource {
sw_atomic_t ref_count;

public:
ThreadResource() {
ref_count = 1;
}

uint32_t add_ref() {
return ++ref_count;
void add_ref() {
sw_atomic_add_fetch(&ref_count, 1);
}

uint32_t del_ref() {
return --ref_count;
void del_ref() {
if (sw_atomic_sub_fetch(&ref_count, 1) == 0) {
delete this;
}
}

protected:
virtual ~ThreadResource() {}
};

struct ArrayItem {
Expand All @@ -77,6 +114,7 @@ struct ArrayItem {
swSocketType type;
} socket;
zend_string *serialized_object;
ThreadResource *resource;
} value;

ArrayItem(zval *zvalue) {
Expand All @@ -88,6 +126,10 @@ struct ArrayItem {
key = zend_string_init(_key.val(), _key.len(), 1);
}

void setKey(zend_string *_key) {
key = zend_string_init(ZSTR_VAL(_key), ZSTR_LEN(_key), 1);
}

void store(zval *zvalue);
void fetch(zval *return_value);
void release();
Expand Down Expand Up @@ -115,7 +157,7 @@ struct ZendArray : ThreadResource {
zend_hash_init(&ht, 0, NULL, item_dtor, 1);
}

~ZendArray() {
~ZendArray() override {
zend_hash_destroy(&ht);
}

Expand All @@ -125,6 +167,25 @@ struct ZendArray : ThreadResource {
lock_.unlock();
}

void append(zval *zvalue);

void add(zend_string *skey, zval *zvalue) {
auto item = new ArrayItem(zvalue);
item->setKey(skey);
zend_hash_add_ptr(&ht, item->key, item);
}

void add(zend::String &skey, zval *zvalue) {
auto item = new ArrayItem(zvalue);
item->setKey(skey);
zend_hash_add_ptr(&ht, item->key, item);
}

void add(zend_long index, zval *zvalue) {
auto item = new ArrayItem(zvalue);
zend_hash_index_add_ptr(&ht, index, item);
}

bool index_exists(zend_long index) {
return index < (zend_long) zend_hash_num_elements(&ht);
}
Expand Down Expand Up @@ -189,6 +250,8 @@ struct ZendArray : ThreadResource {
}

void keys(zval *return_value);
void values(zval *return_value);
void toArray(zval *return_value);

void intkey_offsetGet(zend_long index, zval *return_value) {
lock_.lock_rd();
Expand Down Expand Up @@ -251,6 +314,7 @@ struct ZendArray : ThreadResource {

static void incr_update(ArrayItem *item, zval *zvalue, zval *return_value);
static ArrayItem *incr_create(zval *zvalue, zval *return_value);
static ZendArray *from(zend_array *ht);
};

#define INIT_ARRAY_INCR_PARAMS \
Expand Down
2 changes: 1 addition & 1 deletion ext-src/stubs/php_swoole_thread.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public function join(): bool {}
public function joinable(): bool {}
public function detach(): bool {}

public static function getArguments(): array {}
public static function getArguments(): ?array {}
public static function getId(): int {}
public static function getTsrmInfo(): array {}
}
Expand Down
7 changes: 4 additions & 3 deletions ext-src/stubs/php_swoole_thread_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 234aeadaab2ab31facf1909f0e3027e433f2a88f */
* Stub hash: 261ac9fd29d4f2f37118ff3b96428a0b2f85223a */

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Swoole_Thread___construct, 0, 0, 1)
ZEND_ARG_TYPE_INFO(0, script_file, IS_STRING, 0)
Expand All @@ -13,10 +13,11 @@ ZEND_END_ARG_INFO()

#define arginfo_class_Swoole_Thread_detach arginfo_class_Swoole_Thread_join

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Thread_getArguments, 0, 0, IS_ARRAY, 0)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Thread_getArguments, 0, 0, IS_ARRAY, 1)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Thread_getId, 0, 0, IS_LONG, 0)
ZEND_END_ARG_INFO()

#define arginfo_class_Swoole_Thread_getTsrmInfo arginfo_class_Swoole_Thread_getArguments
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Thread_getTsrmInfo, 0, 0, IS_ARRAY, 0)
ZEND_END_ARG_INFO()
4 changes: 2 additions & 2 deletions ext-src/stubs/php_swoole_thread_arraylist.stub.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
namespace Swoole\Thread {
class ArrayList implements ArrayAccess, Countable {
public function __construct() {}
public function __construct(?array $array = null) {}
public function offsetGet(mixed $key): mixed {}
public function offsetExists(mixed $key): bool {}
public function offsetSet(mixed $key, mixed $value): void {}
Expand All @@ -10,6 +10,6 @@ public function count(): int {}
public function incr(mixed $key, mixed $value = 1): mixed {}
public function decr(mixed $key, mixed $value = 1): mixed {}
public function clean(): void {}
public function __wakeup(): void {}
public function toArray(): array {}
}
}
6 changes: 4 additions & 2 deletions ext-src/stubs/php_swoole_thread_arraylist_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 366fd5114ee7fab588f8c004cd08f600918d3394 */
* Stub hash: 70c2427e37953ac2ceefe4c972cbd8b9845b43ab */

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Swoole_Thread_ArrayList___construct, 0, 0, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, array, IS_ARRAY, 1, "null")
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Thread_ArrayList_offsetGet, 0, 1, IS_MIXED, 0)
Expand Down Expand Up @@ -34,4 +35,5 @@ ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Thread_ArrayList_clean, 0, 0, IS_VOID, 0)
ZEND_END_ARG_INFO()

#define arginfo_class_Swoole_Thread_ArrayList___wakeup arginfo_class_Swoole_Thread_ArrayList_clean
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Thread_ArrayList_toArray, 0, 0, IS_ARRAY, 0)
ZEND_END_ARG_INFO()
2 changes: 0 additions & 2 deletions ext-src/stubs/php_swoole_thread_atomic.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ public function set(int $value): void {}
public function cmpset(int $cmp_value, int $new_value): bool {}
public function wait(float $timeout = 1.0): bool {}
public function wakeup(int $count = 1): bool {}
public function __wakeup(): void {}
}
}

Expand All @@ -21,6 +20,5 @@ public function sub(int $sub_value = 1): int {}
public function get(): int {}
public function set(int $value): void {}
public function cmpset(int $cmp_value, int $new_value): bool {}
public function __wakeup(): void {}
}
}
7 changes: 1 addition & 6 deletions ext-src/stubs/php_swoole_thread_atomic_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: c27562c58b557e8e211bbf52859e4e9818c35f55 */
* Stub hash: 4b6cbceb50641f6204da9caed8c514a526c57ee8 */

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Swoole_Thread_Atomic___construct, 0, 0, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, value, IS_LONG, 0, "0")
Expand Down Expand Up @@ -33,9 +33,6 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Thread_Atomic_wakeu
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, count, IS_LONG, 0, "1")
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Thread_Atomic___wakeup, 0, 0, IS_VOID, 0)
ZEND_END_ARG_INFO()

#define arginfo_class_Swoole_Thread_Atomic_Long___construct arginfo_class_Swoole_Thread_Atomic___construct

#define arginfo_class_Swoole_Thread_Atomic_Long_add arginfo_class_Swoole_Thread_Atomic_add
Expand All @@ -47,5 +44,3 @@ ZEND_END_ARG_INFO()
#define arginfo_class_Swoole_Thread_Atomic_Long_set arginfo_class_Swoole_Thread_Atomic_set

#define arginfo_class_Swoole_Thread_Atomic_Long_cmpset arginfo_class_Swoole_Thread_Atomic_cmpset

#define arginfo_class_Swoole_Thread_Atomic_Long___wakeup arginfo_class_Swoole_Thread_Atomic___wakeup
1 change: 0 additions & 1 deletion ext-src/stubs/php_swoole_thread_barrier.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@
class Barrier {
public function __construct(int $count) {}
public function wait(): void {}
public function __wakeup(): void {}
}
}
4 changes: 1 addition & 3 deletions ext-src/stubs/php_swoole_thread_barrier_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: eac62993bfb3fbd87587a8e6997c16bca7dc5dbc */
* Stub hash: 1fe9f55b0b9487e9cd469dcd215acee893254a04 */

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Swoole_Thread_Barrier___construct, 0, 0, 1)
ZEND_ARG_TYPE_INFO(0, count, IS_LONG, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Thread_Barrier_wait, 0, 0, IS_VOID, 0)
ZEND_END_ARG_INFO()

#define arginfo_class_Swoole_Thread_Barrier___wakeup arginfo_class_Swoole_Thread_Barrier_wait
1 change: 0 additions & 1 deletion ext-src/stubs/php_swoole_thread_lock.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ public function trylock(): bool {}
public function lock_read(): bool {}
public function trylock_read(): bool {}
public function unlock(): bool {}
public function __wakeup(): void {}
}
}
5 changes: 1 addition & 4 deletions ext-src/stubs/php_swoole_thread_lock_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: e8d4c9f0d13a62fd6a0d67838b26cb4451f89ee9 */
* Stub hash: 3c5cd43d34f7669ed8144417b6b36dbc43fb4392 */

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Swoole_Thread_Lock___construct, 0, 0, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, type, IS_LONG, 0, "SWOOLE_MUTEX")
Expand All @@ -22,6 +22,3 @@ ZEND_END_ARG_INFO()
#define arginfo_class_Swoole_Thread_Lock_trylock_read arginfo_class_Swoole_Thread_Lock_lock

#define arginfo_class_Swoole_Thread_Lock_unlock arginfo_class_Swoole_Thread_Lock_lock

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Thread_Lock___wakeup, 0, 0, IS_VOID, 0)
ZEND_END_ARG_INFO()
5 changes: 3 additions & 2 deletions ext-src/stubs/php_swoole_thread_map.stub.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
<?php
namespace Swoole\Thread {
class Map implements ArrayAccess, Countable {
public function __construct() {}
public function __construct(?array $array = null) {}
public function offsetGet(mixed $key): mixed {}
public function offsetExists(mixed $key): bool {}
public function offsetSet(mixed $key, mixed $value): void {}
public function offsetUnset(mixed $key): void {}
public function count(): int {}
public function keys(): array {}
public function values(): array {}
public function incr(mixed $key, mixed $value = 1): mixed {}
public function decr(mixed $key, mixed $value = 1): mixed {}
public function add(mixed $key, mixed $value): bool {}
public function update(mixed $key, mixed $value): bool {}
public function clean(): void {}
public function __wakeup(): void {}
public function toArray(): array {}
}
}
7 changes: 5 additions & 2 deletions ext-src/stubs/php_swoole_thread_map_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 44e8467dc5e0e3cc91e46840d4a1ae686ee6c0a6 */
* Stub hash: 40e24b68b9e9d7f7192a8f91e4b31b3020e9faca */

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Swoole_Thread_Map___construct, 0, 0, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, array, IS_ARRAY, 1, "null")
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Thread_Map_offsetGet, 0, 1, IS_MIXED, 0)
Expand All @@ -27,6 +28,8 @@ ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Thread_Map_keys, 0, 0, IS_ARRAY, 0)
ZEND_END_ARG_INFO()

#define arginfo_class_Swoole_Thread_Map_values arginfo_class_Swoole_Thread_Map_keys

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Thread_Map_incr, 0, 1, IS_MIXED, 0)
ZEND_ARG_TYPE_INFO(0, key, IS_MIXED, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, value, IS_MIXED, 0, "1")
Expand All @@ -44,4 +47,4 @@ ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Thread_Map_clean, 0, 0, IS_VOID, 0)
ZEND_END_ARG_INFO()

#define arginfo_class_Swoole_Thread_Map___wakeup arginfo_class_Swoole_Thread_Map_clean
#define arginfo_class_Swoole_Thread_Map_toArray arginfo_class_Swoole_Thread_Map_keys
1 change: 0 additions & 1 deletion ext-src/stubs/php_swoole_thread_queue.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ public function push(mixed $value, int $notify_which = 0): void {}
public function pop(float $wait = 0): mixed {}
public function count(): int {}
public function clean(): void {}
public function __wakeup(): void {}
}
}
Loading