Skip to content
Snippets Groups Projects
Commit 98b66277 authored by deuce's avatar deuce
Browse files

Actually mutex protect update requests rather than have everyone flailing

at shared memory.

Cuts down CPU usage on my system from ~30% on an idle screen to ~3%.
parent c6192849
No related branches found
No related tags found
No related merge requests found
......@@ -48,8 +48,8 @@ pthread_rwlock_t vstatlock;
static struct bitmap_callbacks callbacks;
static unsigned char *font[4];
static unsigned char space=' ';
int force_redraws=0;
int update_pixels = 0;
static int force_redraws=0;
static int update_pixels = 0;
struct rectangle {
int x;
......@@ -100,6 +100,44 @@ static void unlock_vmem(struct vstat_vmem *vm)
release_vmem(vm);
}
static pthread_mutex_t redraw_lock;
static void request_redraw(void)
{
pthread_mutex_lock(&redraw_lock);
force_redraws = 1;
pthread_mutex_unlock(&redraw_lock);
}
static int check_redraw(void)
{
int ret;
pthread_mutex_lock(&redraw_lock);
ret = force_redraws;
force_redraws = 0;
pthread_mutex_unlock(&redraw_lock);
return ret;
}
static pthread_mutex_t pixels_lock;
void request_pixels(void)
{
pthread_mutex_lock(&pixels_lock);
update_pixels = 1;
pthread_mutex_unlock(&pixels_lock);
}
static int check_pixels(void)
{
int ret;
pthread_mutex_lock(&pixels_lock);
ret = update_pixels;
update_pixels = 0;
pthread_mutex_unlock(&pixels_lock);
return ret;
}
/* Blinker Thread */
static void blinker_thread(void *data)
{
......@@ -120,13 +158,13 @@ static void blinker_thread(void *data)
count=0;
pthread_rwlock_unlock(&vstatlock);
}
if(force_redraws)
update_rect(0,0,0,0,force_redraws--);
if(check_redraw())
update_rect(0,0,0,0,TRUE);
else
update_rect(0,0,0,0,FALSE);
if (update_pixels) {
if (check_pixels()) {
pthread_rwlock_rdlock(&screen.screenlock);
send_rectangle(&vstat, 0, 0, screen.screenwidth, screen.screenheight, update_pixels--);
send_rectangle(&vstat, 0, 0, screen.screenwidth, screen.screenheight, TRUE);
pthread_rwlock_unlock(&screen.screenlock);
}
callbacks.flush();
......@@ -141,6 +179,8 @@ int bitmap_init(void (*drawrect_cb) (int xpos, int ypos, int width, int height,
{
if(bitmap_initialized)
return(-1);
pthread_mutex_init(&redraw_lock, NULL);
pthread_mutex_init(&pixels_lock, NULL);
pthread_rwlock_init(&vmem_lock, NULL);
pthread_rwlock_init(&vstatlock, NULL);
pthread_rwlock_init(&screen.screenlock, NULL);
......@@ -361,7 +401,7 @@ void bitmap_setvideoflags(int flags)
else
vstat.blink_altcharset=0;
pthread_rwlock_unlock(&vstatlock);
force_redraws++;
request_redraw();
}
static int bitmap_attr2palette_locked(uint8_t attr, uint32_t *fgp, uint32_t *bgp)
......@@ -403,7 +443,7 @@ static void set_vmem_cell(struct vstat_vmem *vmem_ptr, size_t pos, uint16_t cell
vmem_ptr->vmem[pos] = cell;
vmem_ptr->fgvmem[pos] = fg;
vmem_ptr->bgvmem[pos] = bg;
update_pixels++;
request_pixels();
}
int bitmap_movetext(int x, int y, int ex, int ey, int tox, int toy)
......@@ -942,7 +982,7 @@ static int bitmap_loadfont_locked(char *filename)
}
}
force_redraws++;
request_redraw();
return(0);
error_return:
......@@ -1275,7 +1315,7 @@ int bitmap_setpixel(uint32_t x, uint32_t y, uint32_t colour)
if (x < screen.screenwidth && y < screen.screenheight)
screen.screen[PIXEL_OFFSET(screen, x, y)]=colour;
pthread_rwlock_unlock(&screen.screenlock);
update_pixels++;
request_pixels();
return 1;
}
......@@ -1347,6 +1387,6 @@ int bitmap_setpixels(uint32_t sx, uint32_t sy, uint32_t ex, uint32_t ey, uint32_
for (y = sy; y <= ey; y++)
memcpy(&screen.screen[PIXEL_OFFSET(screen, sx, y)], &pixels->pixels[pixels->width*(y-sy+y_off)+x_off], width * sizeof(pixels->pixels[0]));
pthread_rwlock_unlock(&screen.screenlock);
update_pixels++;
request_pixels();
return 1;
}
......@@ -23,8 +23,6 @@ typedef pthread_mutex_t pthread_rwlock_t;
extern struct video_stats vstat;
extern pthread_rwlock_t vstatlock;
extern sem_t drawn_sem;
extern int force_redraws;
extern int update_pixels;
int bitmap_pgettext(int sx, int sy, int ex, int ey, void *fill, uint32_t *fg, uint32_t *bg);
int bitmap_puttext(int sx, int sy, int ex, int ey, void *fill);
......@@ -52,5 +50,6 @@ int bitmap_attr2palette(uint8_t attr, uint32_t *fgp, uint32_t *bgp);
int bitmap_setpixel(uint32_t x, uint32_t y, uint32_t colour);
struct ciolib_pixels *bitmap_getpixels(uint32_t sx, uint32_t sy, uint32_t ex, uint32_t ey);
int bitmap_setpixels(uint32_t sx, uint32_t sy, uint32_t ex, uint32_t ey, uint32_t x_off, uint32_t y_off, struct ciolib_pixels *);
void request_pixels(void);
#endif
......@@ -654,7 +654,7 @@ int sdl_setpalette(uint32_t index, uint16_t r, uint16_t g, uint16_t b)
pal->g = g>>8;
pal->b = b>>8;
sdl_user_func(SDL_USEREVENT_SETPALETTE, pal);
update_pixels++;
request_pixels();
}
}
return 0;
......@@ -1077,7 +1077,7 @@ void setup_surfaces(void)
sdl_setup_colours(new_rect);
sdl.mutexV(newrect_mutex);
sdl_setup_colours(win);
update_pixels++;
request_pixels();
}
else if(sdl_init_good) {
ev.type=SDL_QUIT;
......@@ -1733,7 +1733,7 @@ int sdl_video_event_thread(void *data)
case SDL_VIDEOEXPOSE:
{
if(yuv.enabled) {
update_pixels++;
request_pixels();
}
else {
if(upd_rects) {
......
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