25
25
26
26
typedef uint32_t ThreadResourceId;
27
27
struct ThreadResource ;
28
-
29
- ThreadResourceId php_swoole_thread_resource_insert (ThreadResource * res );
30
- bool php_swoole_thread_resource_free (ThreadResourceId resource_id , ThreadResource * res );
31
- ThreadResource * php_swoole_thread_resource_fetch (ThreadResourceId resource_id );
32
-
33
- void php_swoole_thread_start (zend_string * file , zend_string * argv );
34
- zend_string * php_swoole_thread_argv_serialize (zval * zdata );
35
- bool php_swoole_thread_argv_unserialize (zend_string * data , zval * zv );
28
+ struct ZendArray ;
29
+
30
+ extern zend_class_entry *swoole_thread_ce;
31
+ extern zend_class_entry *swoole_thread_error_ce;
32
+ extern zend_class_entry *swoole_thread_arraylist_ce;
33
+ extern zend_class_entry *swoole_thread_atomic_ce;
34
+ extern zend_class_entry *swoole_thread_atomic_long_ce;
35
+ extern zend_class_entry *swoole_thread_barrier_ce;
36
+ extern zend_class_entry *swoole_thread_lock_ce;
37
+ extern zend_class_entry *swoole_thread_map_ce;
38
+ extern zend_class_entry *swoole_thread_queue_ce;
39
+
40
+ void php_swoole_thread_start (zend_string *file, ZendArray *argv);
36
41
zend_string *php_swoole_serialize (zval *zdata);
37
42
bool php_swoole_unserialize (zend_string *data, zval *zv);
38
- void php_swoole_thread_argv_clean (zval * zdata );
39
43
void php_swoole_thread_bailout (void );
40
44
41
- zval * php_swoole_thread_get_arguments ();
45
+ ThreadResource *php_swoole_thread_arraylist_cast (zval *zobject);
46
+ ThreadResource *php_swoole_thread_map_cast (zval *zobject);
47
+ ThreadResource *php_swoole_thread_queue_cast (zval *zobject);
48
+ ThreadResource *php_swoole_thread_lock_cast (zval *zobject);
49
+ ThreadResource *php_swoole_thread_atomic_cast (zval *zobject);
50
+ ThreadResource *php_swoole_thread_atomic_long_cast (zval *zobject);
51
+ ThreadResource *php_swoole_thread_barrier_cast (zval *zobject);
52
+
53
+ void php_swoole_thread_arraylist_create (zval *return_value, ThreadResource *resource);
54
+ void php_swoole_thread_map_create (zval *return_value, ThreadResource *resource);
55
+ void php_swoole_thread_queue_create (zval *return_value, ThreadResource *resource);
56
+ void php_swoole_thread_lock_create (zval *return_value, ThreadResource *resource);
57
+ void php_swoole_thread_atomic_create (zval *return_value, ThreadResource *resource);
58
+ void php_swoole_thread_atomic_long_create (zval *return_value, ThreadResource *resource);
59
+ void php_swoole_thread_barrier_create (zval *return_value, ThreadResource *resource);
60
+
61
+ int php_swoole_thread_stream_cast (zval *zstream);
62
+ void php_swoole_thread_stream_create (zval *return_value, zend_long sockfd);
63
+
64
+ int php_swoole_thread_co_socket_cast (zval *zstream, swSocketType *type);
65
+ void php_swoole_thread_co_socket_create (zval *return_value, zend_long sockfd, swSocketType type);
42
66
43
67
#define EMSG_NO_RESOURCE " resource not found"
44
68
#define ECODE_NO_RESOURCE -2
45
69
46
70
enum {
71
+ IS_ARRAYLIST = 80 ,
72
+ IS_QUEUE = 81 ,
73
+ IS_LOCK = 82 ,
74
+ IS_MAP = 83 ,
75
+ IS_BARRIER = 84 ,
76
+ IS_ATOMIC = 85 ,
77
+ IS_ATOMIC_LONG = 86 ,
47
78
IS_CO_SOCKET = 97 ,
48
79
IS_STREAM_SOCKET = 98 ,
49
80
IS_SERIALIZED_OBJECT = 99 ,
50
81
};
51
82
52
- struct ThreadResource {
53
- uint32_t ref_count ;
83
+ class ThreadResource {
84
+ sw_atomic_t ref_count;
54
85
86
+ public:
55
87
ThreadResource () {
56
88
ref_count = 1 ;
57
89
}
58
90
59
- uint32_t add_ref () {
60
- return ++ ref_count ;
91
+ void add_ref () {
92
+ sw_atomic_add_fetch (& ref_count, 1 ) ;
61
93
}
62
94
63
- uint32_t del_ref () {
64
- return -- ref_count ;
95
+ void del_ref () {
96
+ if (sw_atomic_sub_fetch (&ref_count, 1 ) == 0 ) {
97
+ delete this ;
98
+ }
65
99
}
100
+
101
+ protected:
102
+ virtual ~ThreadResource () {}
66
103
};
67
104
68
105
struct ArrayItem {
@@ -77,6 +114,7 @@ struct ArrayItem {
77
114
swSocketType type;
78
115
} socket;
79
116
zend_string *serialized_object;
117
+ ThreadResource *resource;
80
118
} value;
81
119
82
120
ArrayItem (zval *zvalue) {
@@ -88,6 +126,10 @@ struct ArrayItem {
88
126
key = zend_string_init (_key.val (), _key.len (), 1 );
89
127
}
90
128
129
+ void setKey (zend_string *_key) {
130
+ key = zend_string_init (ZSTR_VAL (_key), ZSTR_LEN (_key), 1 );
131
+ }
132
+
91
133
void store (zval *zvalue);
92
134
void fetch (zval *return_value);
93
135
void release ();
@@ -115,7 +157,7 @@ struct ZendArray : ThreadResource {
115
157
zend_hash_init (&ht, 0 , NULL , item_dtor, 1 );
116
158
}
117
159
118
- ~ZendArray () {
160
+ ~ZendArray () override {
119
161
zend_hash_destroy (&ht);
120
162
}
121
163
@@ -125,6 +167,25 @@ struct ZendArray : ThreadResource {
125
167
lock_.unlock ();
126
168
}
127
169
170
+ void append (zval *zvalue);
171
+
172
+ void add (zend_string *skey, zval *zvalue) {
173
+ auto item = new ArrayItem (zvalue);
174
+ item->setKey (skey);
175
+ zend_hash_add_ptr (&ht, item->key , item);
176
+ }
177
+
178
+ void add (zend::String &skey, zval *zvalue) {
179
+ auto item = new ArrayItem (zvalue);
180
+ item->setKey (skey);
181
+ zend_hash_add_ptr (&ht, item->key , item);
182
+ }
183
+
184
+ void add (zend_long index, zval *zvalue) {
185
+ auto item = new ArrayItem (zvalue);
186
+ zend_hash_index_add_ptr (&ht, index , item);
187
+ }
188
+
128
189
bool index_exists (zend_long index) {
129
190
return index < (zend_long) zend_hash_num_elements (&ht);
130
191
}
@@ -189,6 +250,8 @@ struct ZendArray : ThreadResource {
189
250
}
190
251
191
252
void keys (zval *return_value);
253
+ void values (zval *return_value);
254
+ void toArray (zval *return_value);
192
255
193
256
void intkey_offsetGet (zend_long index, zval *return_value) {
194
257
lock_.lock_rd ();
@@ -251,6 +314,7 @@ struct ZendArray : ThreadResource {
251
314
252
315
static void incr_update (ArrayItem *item, zval *zvalue, zval *return_value);
253
316
static ArrayItem *incr_create (zval *zvalue, zval *return_value);
317
+ static ZendArray *from (zend_array *ht);
254
318
};
255
319
256
320
#define INIT_ARRAY_INCR_PARAMS \
0 commit comments