Skip to content

Commit 16f5588

Browse files
committed
auto init lock for event_handler
1 parent 681ab7c commit 16f5588

File tree

6 files changed

+22
-5
lines changed

6 files changed

+22
-5
lines changed

port/linux/.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
// "--gtest_filter=vm.test_cmodule_import_as"
2525
// "--gtest_filter=vm.subsrc_import"
2626
// "--gtest_filter=event.event_thread"
27-
"--gtest_filter=socket.socket_download"
27+
"--gtest_filter=*mqtt*"
2828
],
2929
"stopAtEntry": false,
3030
"cwd": "${workspaceFolder}",

port/linux/package/pikascript/pikascript-lib/mqtt/_mqtt__MQTT.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,7 @@ void Subscribe_Handler(void* client, message_data_t* msg) {
490490
return;
491491
}
492492

493+
pika_GIL_ENTER();
493494
Arg* evt_obj_arg = arg_newDirectObj(New_TinyObj);
494495
PikaObj* evt_obj = arg_getPtr(evt_obj_arg);
495496
obj_setStr(evt_obj, "topic", (char*)msg->topic_name);
@@ -498,6 +499,7 @@ void Subscribe_Handler(void* client, message_data_t* msg) {
498499

499500
pika_eventListener_send(g_mqtt_event_listener, hash_time33(msg->topic_name),
500501
evt_obj_arg);
502+
pika_GIL_EXIT();
501503

502504
// MQTT_LOG_I("\n>>>------------------");
503505
// MQTT_LOG_I("Topic:%s \nlen:%d,message: %s", msg->topic_name,

port/linux/test/python/event/event_thread.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
EVENT_SIGAL_IO_FALLING_EDGE = 0x02
1313

1414
callback_times = 0
15+
loop_times = 0
1516

1617

1718
def callBack1(signal):
@@ -42,4 +43,4 @@ def insert_task():
4243

4344

4445
# launch a thread to enable evnet_thread
45-
_thread.start_new_thread(thread_test, ())
46+
# _thread.start_new_thread(thread_test, ())

src/PikaObj.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2987,10 +2987,14 @@ PIKA_RES _do_pika_eventListener_send(PikaEventListener* self,
29872987
};
29882988
#else
29892989
if (NULL != eventData && !_VM_is_first_lock()) {
2990+
#if PIKA_EVENT_THREAD_ENABLE
2991+
_VM_lock_init();
2992+
#else
29902993
pika_platform_printf(
29912994
"Error: can not send arg event data without thread support\r\n");
29922995
arg_deinit(eventData);
29932996
return PIKA_RES_ERR_RUNTIME_ERROR;
2997+
#endif
29942998
}
29952999
if (NULL == eventData) {
29963000
// for event signal
@@ -3000,11 +3004,19 @@ PIKA_RES _do_pika_eventListener_send(PikaEventListener* self,
30003004
}
30013005
}
30023006
/* using multi thread */
3003-
if (_VM_is_first_lock()) {
3007+
if (pika_GIL_isInit()) {
3008+
/* python thread is running */
3009+
if (g_PikaVMState.vm_cnt != 0) {
3010+
/* wait python thread get first lock */
3011+
while (!_VM_is_first_lock()) {
3012+
pika_platform_thread_yield();
3013+
}
3014+
}
30043015
pika_GIL_ENTER();
30053016
#if PIKA_EVENT_THREAD_ENABLE
30063017
if (!g_PikaVMState.event_thread) {
3007-
// avoid _VMEvent_pickupEvent() in _time.c as soon as possible
3018+
// avoid _VMEvent_pickupEvent() in _time.c as soon as
3019+
// possible
30083020
g_PikaVMState.event_thread = pika_platform_thread_init(
30093021
"pika_event", _thread_event, NULL, PIKA_EVENT_THREAD_STACK_SIZE,
30103022
PIKA_THREAD_PRIO, PIKA_THREAD_TICK);

src/PikaVM.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,8 @@ void _VMEvent_deinit(void) {
237237
while (!g_PikaVMState.event_thread_exit_done) {
238238
pika_platform_thread_yield();
239239
}
240+
g_PikaVMState.event_thread_exit = 0;
241+
g_PikaVMState.event_thread_exit_done = 0;
240242
pika_GIL_ENTER();
241243
}
242244
#endif

src/PikaVersion.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
#define PIKA_VERSION_MINOR 13
33
#define PIKA_VERSION_MICRO 3
44

5-
#define PIKA_EDIT_TIME "2024/04/26 14:28:02"
5+
#define PIKA_EDIT_TIME "2024/05/02 23:58:57"

0 commit comments

Comments
 (0)