Skip to content

Commit b41ab20

Browse files
committed
Fix data race in webui_script
* Fix data race in webui_script() * Adding missing webui_interface_bind()
1 parent 44a4880 commit b41ab20

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

include/webui.h

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -694,9 +694,6 @@ WEBUI_EXPORT void webui_return_bool(webui_event_t* e, bool b);
694694

695695
// -- Wrapper's Interface -------------
696696

697-
// Bind a specific html element click event with a function. Empty element means
698-
// all events. This replaces `webui_bind()`. The func is (Window, EventType,
699-
// Element, EventNumber, BindID).
700697
/**
701698
* @brief Bind a specific HTML element click event with a function. Empty element means all events.
702699
*
@@ -768,7 +765,6 @@ WEBUI_EXPORT const char* webui_interface_get_string_at(size_t window, size_t eve
768765
*/
769766
WEBUI_EXPORT long long int webui_interface_get_int_at(size_t window, size_t event_number, size_t index);
770767

771-
// Get an argument as boolean at a specific index
772768
/**
773769
* @brief Get an argument as boolean at a specific index
774770
*
@@ -782,4 +778,17 @@ WEBUI_EXPORT long long int webui_interface_get_int_at(size_t window, size_t even
782778
*/
783779
WEBUI_EXPORT bool webui_interface_get_bool_at(size_t window, size_t event_number, size_t index);
784780

781+
/**
782+
* @brief Get the size in bytes of an argument at a specific index
783+
*
784+
* @param window The window number
785+
* @param event_number The event number
786+
* @param index The argument position
787+
*
788+
* @return Returns size in bytes
789+
*
790+
* @example size_t argLen = webui_interface_get_size_at(myWindow, e->event_number, 0);
791+
*/
792+
WEBUI_EXPORT size_t webui_interface_get_size_at(size_t window, size_t event_number, size_t index);
793+
785794
#endif /* _WEBUI_H */

src/webui.c

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ typedef struct _webui_core_t {
200200
webui_mutex_t mutex_receive;
201201
webui_mutex_t mutex_wait;
202202
webui_mutex_t mutex_bridge;
203+
webui_mutex_t mutex_js_run;
203204
webui_condition_t condition_wait;
204205
char* default_server_root_path;
205206
bool ui;
@@ -468,7 +469,9 @@ bool webui_script(size_t window, const char* script, size_t timeout_second, char
468469

469470
// Initializing pipe
470471
uint16_t run_id = _webui_get_run_id();
472+
_webui_mutex_lock( & _webui_core.mutex_js_run);
471473
_webui_core.run_done[run_id] = false;
474+
_webui_mutex_unlock( & _webui_core.mutex_js_run);
472475
_webui_core.run_error[run_id] = false;
473476
_webui_core.run_userBuffer[run_id] = buffer;
474477
_webui_core.run_userBufferLen[run_id] = buffer_length;
@@ -481,13 +484,18 @@ bool webui_script(size_t window, const char* script, size_t timeout_second, char
481484
// Send the packet
482485
_webui_send(win, win->token, run_id, WEBUI_CMD_JS, script, js_len);
483486

487+
bool js_status = false;
488+
484489
// Wait for UI response
485490
if (timeout_second < 1 || timeout_second > 86400) {
486491

487492
// Wait forever
488493
for (;;) {
489494
_webui_sleep(1);
490-
if (_webui_core.run_done[run_id])
495+
_webui_mutex_lock( & _webui_core.mutex_js_run);
496+
js_status = _webui_core.run_done[run_id];
497+
_webui_mutex_unlock( & _webui_core.mutex_js_run);
498+
if (js_status)
491499
break;
492500
}
493501
} else {
@@ -497,14 +505,17 @@ bool webui_script(size_t window, const char* script, size_t timeout_second, char
497505
_webui_timer_start( & timer);
498506
for (;;) {
499507
_webui_sleep(1);
500-
if (_webui_core.run_done[run_id])
508+
_webui_mutex_lock( & _webui_core.mutex_js_run);
509+
js_status = _webui_core.run_done[run_id];
510+
_webui_mutex_unlock( & _webui_core.mutex_js_run);
511+
if (js_status)
501512
break;
502513
if (_webui_timer_is_end( & timer, (timeout_second * 1000)))
503514
break;
504515
}
505516
}
506517

507-
if (_webui_core.run_done[run_id]) {
518+
if (js_status) {
508519

509520
#ifdef WEBUI_LOG
510521
printf(
@@ -2288,6 +2299,23 @@ bool webui_interface_get_bool_at(size_t window, size_t event_number, size_t inde
22882299
return webui_get_bool_at(&e, index);
22892300
}
22902301

2302+
size_t webui_interface_get_size_at(size_t window, size_t event_number, size_t index) {
2303+
2304+
#ifdef WEBUI_LOG
2305+
printf("[User] webui_interface_get_size_at([%zu], [%zu], [%zu])...\n", window, event_number, index);
2306+
#endif
2307+
2308+
// New Event
2309+
webui_event_t e;
2310+
e.window = window;
2311+
e.event_type = 0;
2312+
e.element = NULL;
2313+
e.event_number = event_number;
2314+
e.bind_id = 0;
2315+
2316+
return webui_get_size_at(&e, index);
2317+
}
2318+
22912319
size_t webui_interface_bind(size_t window, const char* element, void( * func)(size_t, size_t, char* , size_t, size_t)) {
22922320

22932321
#ifdef WEBUI_LOG
@@ -4623,6 +4651,7 @@ static void _webui_clean(void) {
46234651
_webui_mutex_destroy( & _webui_core.mutex_send);
46244652
_webui_mutex_destroy( & _webui_core.mutex_receive);
46254653
_webui_mutex_destroy( & _webui_core.mutex_wait);
4654+
_webui_mutex_destroy( & _webui_core.mutex_js_run);
46264655
_webui_condition_destroy( & _webui_core.condition_wait);
46274656

46284657
#ifdef WEBUI_LOG
@@ -6028,6 +6057,7 @@ static void _webui_init(void) {
60286057
_webui_mutex_init( & _webui_core.mutex_receive);
60296058
_webui_mutex_init( & _webui_core.mutex_wait);
60306059
_webui_mutex_init( & _webui_core.mutex_bridge);
6060+
_webui_mutex_init( & _webui_core.mutex_js_run);
60316061
_webui_condition_init( & _webui_core.condition_wait);
60326062

60336063
// // Determine whether the current device
@@ -7131,7 +7161,9 @@ static WEBUI_THREAD_RECEIVE {
71317161

71327162
if (_webui_core.run_userBuffer[packet_id] != NULL) {
71337163

7164+
_webui_mutex_lock( & _webui_core.mutex_js_run);
71347165
_webui_core.run_done[packet_id] = false;
7166+
_webui_mutex_unlock( & _webui_core.mutex_js_run);
71357167

71367168
// Get js-error
71377169
bool error = true;
@@ -7195,7 +7227,9 @@ static WEBUI_THREAD_RECEIVE {
71957227
}
71967228

71977229
// Send ready signal to webui_script()
7230+
_webui_mutex_lock( & _webui_core.mutex_js_run);
71987231
_webui_core.run_done[packet_id] = true;
7232+
_webui_mutex_unlock( & _webui_core.mutex_js_run);
71997233
}
72007234
}
72017235
} else if ((unsigned char) packet[WEBUI_PROTOCOL_CMD] == WEBUI_CMD_NAVIGATION) {

0 commit comments

Comments
 (0)