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)
curs_col = vstat.curs_col;
charheight = vstat.charheight;
charwidth = vstat.charwidth;
pthread_mutex_unlock(&vstatlock);
if (cursor_visible_locked()) {
pthread_mutex_unlock(&vstatlock);
cv = color_value(ciolib_fg);
for (y = curs_start; y <= curs_end; y++) {
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)
}
}
}
else
pthread_mutex_unlock(&vstatlock);
pthread_mutex_lock(&callbacks.lock);
callbacks.drawrect(data);
callbacks.rects++;
......@@ -674,10 +676,16 @@ static void blinker_thread(void *data)
while(1) {
curs_changed = 0;
blink_changed = 0;
do {
for (;;) {
SLEEP(10);
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++;
if (count==25) {
pthread_mutex_lock(&vstatlock);
......@@ -716,24 +724,26 @@ static void blinker_thread(void *data)
if (update_from_vmem(FALSE))
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?
if (screen->update_pixels || curs_changed || blink_changed) {
// If the other screen is update_pixels == 2, clear it.
pthread_mutex_lock(&ncscreen->screenlock);
if (ncscreen->update_pixels == 2)
ncscreen->update_pixels = 0;
pthread_mutex_unlock(&ncscreen->screenlock);
rect = get_full_rectangle_locked(screen);
screen->update_pixels = 0;
pthread_mutex_unlock(&screen->screenlock);
pthread_mutex_unlock(&screenb.screenlock);
pthread_mutex_unlock(&screena.screenlock);
cb_drawrect(rect);
}
else {
if (force_cursor) {
rect = get_full_rectangle_locked(screen);
}
pthread_mutex_unlock(&screen->screenlock);
pthread_mutex_unlock(&screenb.screenlock);
pthread_mutex_unlock(&screena.screenlock);
if (force_cursor) {
cb_drawrect(rect);
force_cursor = 0;
......
......@@ -660,7 +660,7 @@ enum {
#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
extern "C" {
......@@ -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_wait(void);
CIOLIBEXPORT int mouse_pending(void);
CIOLIBEXPORT void init_mouse(void);
CIOLIBEXPORT int ciolib_getmouse(struct mouse_event *mevent);
CIOLIBEXPORT int ciolib_ungetmouse(struct mouse_event *mevent);
CIOLIBEXPORT void ciolib_mouse_thread(void *data);
......
......@@ -107,7 +107,7 @@ struct mouse_state {
struct mouse_state state;
uint64_t mouse_events=0;
int ciolib_mouse_initialized=0;
pthread_once_t ciolib_mouse_initialized = PTHREAD_ONCE_INIT;
static int ungot=0;
pthread_mutex_t unget_mutex;
......@@ -119,18 +119,11 @@ void init_mouse(void)
listInit(&state.input,LINK_LIST_SEMAPHORE|LINK_LIST_MUTEX);
listInit(&state.output,LINK_LIST_SEMAPHORE|LINK_LIST_MUTEX);
pthread_mutex_init(&unget_mutex, NULL);
ciolib_mouse_initialized=1;
}
void mousestate(int *x, int *y, uint8_t *buttons)
{
if (!ciolib_mouse_initialized) {
if (x)
*x = -1;
if (y)
*y = -1;
return;
}
pthread_once(&ciolib_mouse_initialized, init_mouse);
if (x)
*x = state.curx;
if (y)
......@@ -142,13 +135,7 @@ void mousestate(int *x, int *y, uint8_t *buttons)
void mousestate_res(int *x, int *y, uint8_t *buttons)
{
if (!ciolib_mouse_initialized) {
if (x)
*x = -1;
if (y)
*y = -1;
return;
}
pthread_once(&ciolib_mouse_initialized, init_mouse);
if (x)
*x = state.curx_res;
if (y)
......@@ -192,8 +179,7 @@ void ciomouse_gotevent(int event, int x, int y, int x_res, int y_res)
{
struct in_mouse_event *ime;
while(!ciolib_mouse_initialized)
SLEEP(1);
pthread_once(&ciolib_mouse_initialized, init_mouse);
ime=(struct in_mouse_event *)malloc(sizeof(struct in_mouse_event));
if(ime) {
ime->ts=MSEC_CLOCK();
......@@ -269,7 +255,7 @@ void ciolib_mouse_thread(void *data)
long long ttime=0;
SetThreadName("Mouse");
init_mouse();
pthread_once(&ciolib_mouse_initialized, init_mouse);
while(1) {
timedout=0;
if(timeout_button) {
......@@ -521,8 +507,7 @@ int mouse_trywait(void)
{
int result;
while(!ciolib_mouse_initialized)
SLEEP(1);
pthread_once(&ciolib_mouse_initialized, init_mouse);
while(1) {
result=listSemTryWait(&state.output);
pthread_mutex_lock(&unget_mutex);
......@@ -539,8 +524,7 @@ int mouse_wait(void)
{
int result;
while(!ciolib_mouse_initialized)
SLEEP(1);
pthread_once(&ciolib_mouse_initialized, init_mouse);
while(1) {
result=listSemWait(&state.output);
pthread_mutex_lock(&unget_mutex);
......@@ -555,8 +539,7 @@ int mouse_wait(void)
int mouse_pending(void)
{
while(!ciolib_mouse_initialized)
SLEEP(1);
pthread_once(&ciolib_mouse_initialized, init_mouse);
return(listCountNodes(&state.output));
}
......@@ -564,8 +547,7 @@ int ciolib_getmouse(struct mouse_event *mevent)
{
int retval=0;
while(!ciolib_mouse_initialized)
SLEEP(1);
pthread_once(&ciolib_mouse_initialized, init_mouse);
if(listCountNodes(&state.output)) {
struct out_mouse_event *out;
out=listShiftNode(&state.output);
......
......@@ -973,13 +973,11 @@ void sdl_video_event_thread(void *data)
block_text = 0;
break;
case SDL_MOUSEMOTION:
if(!ciolib_mouse_initialized)
break;
pthread_once(&ciolib_mouse_initialized, init_mouse);
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;
case SDL_MOUSEBUTTONDOWN:
if(!ciolib_mouse_initialized)
break;
pthread_once(&ciolib_mouse_initialized, init_mouse);
switch(ev.button.button) {
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));
......@@ -993,8 +991,7 @@ void sdl_video_event_thread(void *data)
}
break;
case SDL_MOUSEWHEEL:
if (!ciolib_mouse_initialized)
break;
pthread_once(&ciolib_mouse_initialized, init_mouse);
if (ev.wheel.y) {
#if (SDL_MINOR_VERSION > 0) || (SDL_PATCHLEVEL > 3)
if (ev.wheel.direction == SDL_MOUSEWHEEL_FLIPPED)
......@@ -1007,8 +1004,7 @@ void sdl_video_event_thread(void *data)
}
break;
case SDL_MOUSEBUTTONUP:
if(!ciolib_mouse_initialized)
break;
pthread_once(&ciolib_mouse_initialized, init_mouse);
switch(ev.button.button) {
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));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment