Skip to content
Snippets Groups Projects
Commit 7309e270 authored by Deucе's avatar Deucе :ok_hand_tone4:
Browse files

Some minor synchronization fiddling

Use pthread_once() for init_mouse() instead of a global
Fix some lock issues in cb_drawrect()
parent db8aca66
No related branches found
No related tags found
1 merge request!463MRC mods by Codefenix (2024-10-20)
...@@ -384,8 +384,8 @@ static void cb_drawrect(struct rectlist *data) ...@@ -384,8 +384,8 @@ static void cb_drawrect(struct rectlist *data)
curs_col = vstat.curs_col; curs_col = vstat.curs_col;
charheight = vstat.charheight; charheight = vstat.charheight;
charwidth = vstat.charwidth; charwidth = vstat.charwidth;
pthread_mutex_unlock(&vstatlock);
if (cursor_visible_locked()) { if (cursor_visible_locked()) {
pthread_mutex_unlock(&vstatlock);
cv = color_value(ciolib_fg); cv = color_value(ciolib_fg);
for (y = curs_start; y <= curs_end; y++) { for (y = curs_start; y <= curs_end; y++) {
pixel = &data->data[((curs_row - 1) * charheight + y) * data->rect.width + (curs_col - 1) * charwidth]; pixel = &data->data[((curs_row - 1) * charheight + y) * data->rect.width + (curs_col - 1) * charwidth];
...@@ -394,6 +394,8 @@ static void cb_drawrect(struct rectlist *data) ...@@ -394,6 +394,8 @@ static void cb_drawrect(struct rectlist *data)
} }
} }
} }
else
pthread_mutex_unlock(&vstatlock);
pthread_mutex_lock(&callbacks.lock); pthread_mutex_lock(&callbacks.lock);
callbacks.drawrect(data); callbacks.drawrect(data);
callbacks.rects++; callbacks.rects++;
...@@ -674,10 +676,16 @@ static void blinker_thread(void *data) ...@@ -674,10 +676,16 @@ static void blinker_thread(void *data)
while(1) { while(1) {
curs_changed = 0; curs_changed = 0;
blink_changed = 0; blink_changed = 0;
do { for (;;) {
SLEEP(10); SLEEP(10);
both_screens(&screen, &ncscreen); both_screens(&screen, &ncscreen);
} while (screen->rect == NULL); pthread_mutex_lock(&screen->screenlock);
if (screen->rect != NULL) {
pthread_mutex_unlock(&screen->screenlock);
break;
}
pthread_mutex_unlock(&screen->screenlock);
}
count++; count++;
if (count==25) { if (count==25) {
pthread_mutex_lock(&vstatlock); pthread_mutex_lock(&vstatlock);
...@@ -716,24 +724,26 @@ static void blinker_thread(void *data) ...@@ -716,24 +724,26 @@ static void blinker_thread(void *data)
if (update_from_vmem(FALSE)) if (update_from_vmem(FALSE))
request_redraw(); request_redraw();
} }
pthread_mutex_lock(&screen->screenlock); // Lock both screens in same order every time...
pthread_mutex_lock(&screena.screenlock);
pthread_mutex_lock(&screenb.screenlock);
// TODO: Maybe we can optimize the blink_changed forced update? // TODO: Maybe we can optimize the blink_changed forced update?
if (screen->update_pixels || curs_changed || blink_changed) { if (screen->update_pixels || curs_changed || blink_changed) {
// If the other screen is update_pixels == 2, clear it. // If the other screen is update_pixels == 2, clear it.
pthread_mutex_lock(&ncscreen->screenlock);
if (ncscreen->update_pixels == 2) if (ncscreen->update_pixels == 2)
ncscreen->update_pixels = 0; ncscreen->update_pixels = 0;
pthread_mutex_unlock(&ncscreen->screenlock);
rect = get_full_rectangle_locked(screen); rect = get_full_rectangle_locked(screen);
screen->update_pixels = 0; screen->update_pixels = 0;
pthread_mutex_unlock(&screen->screenlock); pthread_mutex_unlock(&screenb.screenlock);
pthread_mutex_unlock(&screena.screenlock);
cb_drawrect(rect); cb_drawrect(rect);
} }
else { else {
if (force_cursor) { if (force_cursor) {
rect = get_full_rectangle_locked(screen); rect = get_full_rectangle_locked(screen);
} }
pthread_mutex_unlock(&screen->screenlock); pthread_mutex_unlock(&screenb.screenlock);
pthread_mutex_unlock(&screena.screenlock);
if (force_cursor) { if (force_cursor) {
cb_drawrect(rect); cb_drawrect(rect);
force_cursor = 0; force_cursor = 0;
......
...@@ -660,7 +660,7 @@ enum { ...@@ -660,7 +660,7 @@ enum {
#define CIOLIB_BUTTON_BASE(x) (x!=CIOLIB_MOUSE_MOVE?x-9*(CIOLIB_BUTTON_NUMBER(x)-1):CIOLIB_MOUSE_MOVE) #define CIOLIB_BUTTON_BASE(x) (x!=CIOLIB_MOUSE_MOVE?x-9*(CIOLIB_BUTTON_NUMBER(x)-1):CIOLIB_MOUSE_MOVE)
extern int ciolib_mouse_initialized; extern pthread_once_t ciolib_mouse_initialized;
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
...@@ -669,6 +669,7 @@ CIOLIBEXPORT void ciomouse_gotevent(int event, int x, int y, int x_res, int y_re ...@@ -669,6 +669,7 @@ CIOLIBEXPORT void ciomouse_gotevent(int event, int x, int y, int x_res, int y_re
CIOLIBEXPORT int mouse_trywait(void); CIOLIBEXPORT int mouse_trywait(void);
CIOLIBEXPORT int mouse_wait(void); CIOLIBEXPORT int mouse_wait(void);
CIOLIBEXPORT int mouse_pending(void); CIOLIBEXPORT int mouse_pending(void);
CIOLIBEXPORT void init_mouse(void);
CIOLIBEXPORT int ciolib_getmouse(struct mouse_event *mevent); CIOLIBEXPORT int ciolib_getmouse(struct mouse_event *mevent);
CIOLIBEXPORT int ciolib_ungetmouse(struct mouse_event *mevent); CIOLIBEXPORT int ciolib_ungetmouse(struct mouse_event *mevent);
CIOLIBEXPORT void ciolib_mouse_thread(void *data); CIOLIBEXPORT void ciolib_mouse_thread(void *data);
......
...@@ -107,7 +107,7 @@ struct mouse_state { ...@@ -107,7 +107,7 @@ struct mouse_state {
struct mouse_state state; struct mouse_state state;
uint64_t mouse_events=0; uint64_t mouse_events=0;
int ciolib_mouse_initialized=0; pthread_once_t ciolib_mouse_initialized = PTHREAD_ONCE_INIT;
static int ungot=0; static int ungot=0;
pthread_mutex_t unget_mutex; pthread_mutex_t unget_mutex;
...@@ -119,18 +119,11 @@ void init_mouse(void) ...@@ -119,18 +119,11 @@ void init_mouse(void)
listInit(&state.input,LINK_LIST_SEMAPHORE|LINK_LIST_MUTEX); listInit(&state.input,LINK_LIST_SEMAPHORE|LINK_LIST_MUTEX);
listInit(&state.output,LINK_LIST_SEMAPHORE|LINK_LIST_MUTEX); listInit(&state.output,LINK_LIST_SEMAPHORE|LINK_LIST_MUTEX);
pthread_mutex_init(&unget_mutex, NULL); pthread_mutex_init(&unget_mutex, NULL);
ciolib_mouse_initialized=1;
} }
void mousestate(int *x, int *y, uint8_t *buttons) void mousestate(int *x, int *y, uint8_t *buttons)
{ {
if (!ciolib_mouse_initialized) { pthread_once(&ciolib_mouse_initialized, init_mouse);
if (x)
*x = -1;
if (y)
*y = -1;
return;
}
if (x) if (x)
*x = state.curx; *x = state.curx;
if (y) if (y)
...@@ -142,13 +135,7 @@ void mousestate(int *x, int *y, uint8_t *buttons) ...@@ -142,13 +135,7 @@ void mousestate(int *x, int *y, uint8_t *buttons)
void mousestate_res(int *x, int *y, uint8_t *buttons) void mousestate_res(int *x, int *y, uint8_t *buttons)
{ {
if (!ciolib_mouse_initialized) { pthread_once(&ciolib_mouse_initialized, init_mouse);
if (x)
*x = -1;
if (y)
*y = -1;
return;
}
if (x) if (x)
*x = state.curx_res; *x = state.curx_res;
if (y) if (y)
...@@ -192,8 +179,7 @@ void ciomouse_gotevent(int event, int x, int y, int x_res, int y_res) ...@@ -192,8 +179,7 @@ void ciomouse_gotevent(int event, int x, int y, int x_res, int y_res)
{ {
struct in_mouse_event *ime; struct in_mouse_event *ime;
while(!ciolib_mouse_initialized) pthread_once(&ciolib_mouse_initialized, init_mouse);
SLEEP(1);
ime=(struct in_mouse_event *)malloc(sizeof(struct in_mouse_event)); ime=(struct in_mouse_event *)malloc(sizeof(struct in_mouse_event));
if(ime) { if(ime) {
ime->ts=MSEC_CLOCK(); ime->ts=MSEC_CLOCK();
...@@ -269,7 +255,7 @@ void ciolib_mouse_thread(void *data) ...@@ -269,7 +255,7 @@ void ciolib_mouse_thread(void *data)
long long ttime=0; long long ttime=0;
SetThreadName("Mouse"); SetThreadName("Mouse");
init_mouse(); pthread_once(&ciolib_mouse_initialized, init_mouse);
while(1) { while(1) {
timedout=0; timedout=0;
if(timeout_button) { if(timeout_button) {
...@@ -521,8 +507,7 @@ int mouse_trywait(void) ...@@ -521,8 +507,7 @@ int mouse_trywait(void)
{ {
int result; int result;
while(!ciolib_mouse_initialized) pthread_once(&ciolib_mouse_initialized, init_mouse);
SLEEP(1);
while(1) { while(1) {
result=listSemTryWait(&state.output); result=listSemTryWait(&state.output);
pthread_mutex_lock(&unget_mutex); pthread_mutex_lock(&unget_mutex);
...@@ -539,8 +524,7 @@ int mouse_wait(void) ...@@ -539,8 +524,7 @@ int mouse_wait(void)
{ {
int result; int result;
while(!ciolib_mouse_initialized) pthread_once(&ciolib_mouse_initialized, init_mouse);
SLEEP(1);
while(1) { while(1) {
result=listSemWait(&state.output); result=listSemWait(&state.output);
pthread_mutex_lock(&unget_mutex); pthread_mutex_lock(&unget_mutex);
...@@ -555,8 +539,7 @@ int mouse_wait(void) ...@@ -555,8 +539,7 @@ int mouse_wait(void)
int mouse_pending(void) int mouse_pending(void)
{ {
while(!ciolib_mouse_initialized) pthread_once(&ciolib_mouse_initialized, init_mouse);
SLEEP(1);
return(listCountNodes(&state.output)); return(listCountNodes(&state.output));
} }
...@@ -564,8 +547,7 @@ int ciolib_getmouse(struct mouse_event *mevent) ...@@ -564,8 +547,7 @@ int ciolib_getmouse(struct mouse_event *mevent)
{ {
int retval=0; int retval=0;
while(!ciolib_mouse_initialized) pthread_once(&ciolib_mouse_initialized, init_mouse);
SLEEP(1);
if(listCountNodes(&state.output)) { if(listCountNodes(&state.output)) {
struct out_mouse_event *out; struct out_mouse_event *out;
out=listShiftNode(&state.output); out=listShiftNode(&state.output);
......
...@@ -973,13 +973,11 @@ void sdl_video_event_thread(void *data) ...@@ -973,13 +973,11 @@ void sdl_video_event_thread(void *data)
block_text = 0; block_text = 0;
break; break;
case SDL_MOUSEMOTION: case SDL_MOUSEMOTION:
if(!ciolib_mouse_initialized) pthread_once(&ciolib_mouse_initialized, init_mouse);
break;
ciomouse_gotevent(CIOLIB_MOUSE_MOVE,win_to_text_xpos(ev.motion.x, &cvstat),win_to_text_ypos(ev.motion.y, &cvstat), win_to_res_xpos(ev.motion.x, &cvstat), win_to_res_ypos(ev.motion.y, &cvstat)); ciomouse_gotevent(CIOLIB_MOUSE_MOVE,win_to_text_xpos(ev.motion.x, &cvstat),win_to_text_ypos(ev.motion.y, &cvstat), win_to_res_xpos(ev.motion.x, &cvstat), win_to_res_ypos(ev.motion.y, &cvstat));
break; break;
case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONDOWN:
if(!ciolib_mouse_initialized) pthread_once(&ciolib_mouse_initialized, init_mouse);
break;
switch(ev.button.button) { switch(ev.button.button) {
case SDL_BUTTON_LEFT: case SDL_BUTTON_LEFT:
ciomouse_gotevent(CIOLIB_BUTTON_PRESS(1),win_to_text_xpos(ev.button.x, &cvstat),win_to_text_ypos(ev.button.y, &cvstat), win_to_res_xpos(ev.button.x, &cvstat), win_to_res_ypos(ev.button.y, &cvstat)); ciomouse_gotevent(CIOLIB_BUTTON_PRESS(1),win_to_text_xpos(ev.button.x, &cvstat),win_to_text_ypos(ev.button.y, &cvstat), win_to_res_xpos(ev.button.x, &cvstat), win_to_res_ypos(ev.button.y, &cvstat));
...@@ -993,8 +991,7 @@ void sdl_video_event_thread(void *data) ...@@ -993,8 +991,7 @@ void sdl_video_event_thread(void *data)
} }
break; break;
case SDL_MOUSEWHEEL: case SDL_MOUSEWHEEL:
if (!ciolib_mouse_initialized) pthread_once(&ciolib_mouse_initialized, init_mouse);
break;
if (ev.wheel.y) { if (ev.wheel.y) {
#if (SDL_MINOR_VERSION > 0) || (SDL_PATCHLEVEL > 3) #if (SDL_MINOR_VERSION > 0) || (SDL_PATCHLEVEL > 3)
if (ev.wheel.direction == SDL_MOUSEWHEEL_FLIPPED) if (ev.wheel.direction == SDL_MOUSEWHEEL_FLIPPED)
...@@ -1007,8 +1004,7 @@ void sdl_video_event_thread(void *data) ...@@ -1007,8 +1004,7 @@ void sdl_video_event_thread(void *data)
} }
break; break;
case SDL_MOUSEBUTTONUP: case SDL_MOUSEBUTTONUP:
if(!ciolib_mouse_initialized) pthread_once(&ciolib_mouse_initialized, init_mouse);
break;
switch(ev.button.button) { switch(ev.button.button) {
case SDL_BUTTON_LEFT: case SDL_BUTTON_LEFT:
ciomouse_gotevent(CIOLIB_BUTTON_RELEASE(1),win_to_text_xpos(ev.button.x, &cvstat),win_to_text_ypos(ev.button.y, &cvstat), win_to_res_xpos(ev.button.x, &cvstat), win_to_res_ypos(ev.button.y, &cvstat)); ciomouse_gotevent(CIOLIB_BUTTON_RELEASE(1),win_to_text_xpos(ev.button.x, &cvstat),win_to_text_ypos(ev.button.y, &cvstat), win_to_res_xpos(ev.button.x, &cvstat), win_to_res_ypos(ev.button.y, &cvstat));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment