@@ -28,14 +28,28 @@ static inline ext_object *ext_object_from_obj(zend_object *obj) {
2828}
2929
3030static zend_object * ext_create_object (zend_class_entry * ce ) {
31- ext_object * intern = ecalloc (1 , sizeof (ext_object ) + zend_object_properties_size (ce ));
31+ fprintf (stderr , "[C DEBUG] ext_create_object called!\n" );
32+ fflush (stderr );
33+
34+ /* Always allocate without properties due to corrupted class entry */
35+ ext_object * intern = ecalloc (1 , sizeof (ext_object ));
36+
37+ fprintf (stderr , "[C DEBUG] About to call zend_object_std_init\n" );
38+ fflush (stderr );
3239
3340 zend_object_std_init (& intern -> std , ce );
34- object_properties_init (& intern -> std , ce );
41+
42+ fprintf (stderr , "[C DEBUG] zend_object_std_init completed\n" );
43+ fflush (stderr );
44+
45+ /* Skip object_properties_init due to corrupted class entry */
3546
3647 intern -> std .handlers = & object_handlers_ext ;
3748 intern -> go_handle = 0 ; /* will be set in __construct */
3849
50+ fprintf (stderr , "[C DEBUG] ext_create_object returning\n" );
51+ fflush (stderr );
52+
3953 return & intern -> std ;
4054}
4155
@@ -57,19 +71,43 @@ void init_object_handlers() {
5771}
5872
5973static zend_class_entry * Worker_ce = NULL ;
60- static ext_object * current_worker = NULL ;
74+ static __thread uintptr_t current_go_worker_handle = 0 ; /* Thread-local Go worker handle */
6175
6276PHP_METHOD (Bottledcode_DurablePhp_Ext_Worker , __construct ) {
77+ fprintf (stderr , "[C DEBUG] Constructor called\n" );
78+ fflush (stderr );
79+
6380 ZEND_PARSE_PARAMETERS_NONE ();
6481
82+ fprintf (stderr , "[C DEBUG] Constructor parameters parsed\n" );
83+ fflush (stderr );
84+
6585 ext_object * intern = ext_object_from_obj (Z_OBJ_P (ZEND_THIS ));
6686
87+ fprintf (stderr , "[C DEBUG] Got intern object, go_handle=%lu\n" , intern -> go_handle );
88+ fflush (stderr );
89+
6790 /* Constructor is called more than once, make it no-op */
6891 if (intern -> go_handle != 0 ) {
92+ fprintf (stderr , "[C DEBUG] Constructor already called, returning\n" );
93+ fflush (stderr );
6994 return ;
7095 }
7196
72- intern -> go_handle = create_Worker_object ();
97+ /* Use the thread-local Go worker handle if available */
98+ if (current_go_worker_handle != 0 ) {
99+ fprintf (stderr , "[C DEBUG] Constructor using thread-local handle %lu\n" , current_go_worker_handle );
100+ fflush (stderr );
101+ intern -> go_handle = current_go_worker_handle ;
102+ } else {
103+ /* Fallback: create a new Worker object */
104+ fprintf (stderr , "[C DEBUG] Constructor creating new Worker object\n" );
105+ fflush (stderr );
106+ intern -> go_handle = create_Worker_object ();
107+ }
108+
109+ fprintf (stderr , "[C DEBUG] Constructor completed\n" );
110+ fflush (stderr );
73111}
74112
75113
@@ -220,13 +258,36 @@ PHP_METHOD(Bottledcode_DurablePhp_Ext_Worker, delete) {
220258}
221259
222260PHP_METHOD (Bottledcode_DurablePhp_Ext_Worker , GetCurrent ) {
261+ fprintf (stderr , "[C DEBUG] GetCurrent method entry\n" );
262+ fflush (stderr );
263+
223264 ZEND_PARSE_PARAMETERS_NONE ();
224265
225- if (current_worker == NULL ) {
266+ fprintf (stderr , "[C DEBUG] GetCurrent called, thread-local handle=%lu\n" , current_go_worker_handle );
267+ fflush (stderr );
268+
269+ /* If no thread-local worker handle, return NULL */
270+ if (current_go_worker_handle == 0 ) {
271+ fprintf (stderr , "[C DEBUG] No thread-local handle, returning NULL\n" );
272+ fflush (stderr );
226273 RETURN_NULL ();
227274 }
228275
229- RETURN_OBJ_COPY (& current_worker -> std );
276+ fprintf (stderr , "[C DEBUG] About to create Worker object with object_init_ex\n" );
277+ fflush (stderr );
278+
279+ /* Create a new Worker object - PHP constructor will pick up the thread-local handle */
280+ zval obj ;
281+ if (object_init_ex (& obj , Worker_ce ) != SUCCESS ) {
282+ fprintf (stderr , "[C DEBUG] object_init_ex failed\n" );
283+ fflush (stderr );
284+ RETURN_NULL ();
285+ }
286+
287+ fprintf (stderr , "[C DEBUG] object_init_ex succeeded, returning object\n" );
288+ fflush (stderr );
289+
290+ RETURN_ZVAL (& obj , 0 , 0 );
230291}
231292
232293void register_all_classes () {
@@ -241,37 +302,34 @@ void register_all_classes() {
241302
242303/* Function to set the current worker from Go */
243304void set_current_worker_handle (uintptr_t handle ) {
244- if (current_worker != NULL ) {
245- /* Release previous worker */
246- zend_object_release (& current_worker -> std );
247- current_worker = NULL ;
248- }
249-
250- if (handle != 0 ) {
251- /* Create a new Worker object and set its handle */
252- zend_object * obj = ext_create_object (Worker_ce );
253- current_worker = ext_object_from_obj (obj );
254- current_worker -> go_handle = handle ;
255-
256- /* Add ref to keep it alive */
257- GC_ADDREF (& current_worker -> std );
258- }
305+ fprintf (stderr , "[C DEBUG] Setting thread-local worker handle to %lu\n" , handle );
306+ fflush (stderr );
307+ current_go_worker_handle = handle ;
308+ }
309+
310+ /* Function to get current worker handle */
311+ uintptr_t get_current_worker_handle () {
312+ return current_go_worker_handle ;
259313}
260314
261315/* Function to clear current worker */
262316void clear_current_worker () {
263- if (current_worker != NULL ) {
264- zend_object_release (& current_worker -> std );
265- current_worker = NULL ;
266- }
317+ fprintf (stderr , "[C DEBUG] Clearing thread-local worker handle\n" );
318+ fflush (stderr );
319+ current_go_worker_handle = 0 ;
267320}
268321
269322PHP_MINIT_FUNCTION (ext ) {
270- register_all_classes ();
323+ fprintf (stderr , "[C DEBUG] MINIT starting\n" );
324+ fflush (stderr );
271325
326+ register_all_classes ();
327+ fprintf (stderr , "[C DEBUG] Classes registered\n" );
328+ fflush (stderr );
272329
273330 go_init_module ();
274-
331+ fprintf (stderr , "[C DEBUG] Go module initialized\n" );
332+ fflush (stderr );
275333
276334 return SUCCESS ;
277335}
0 commit comments