Skip to content

Commit

Permalink
Merge branch 'corruptionfixing'
Browse files Browse the repository at this point in the history
multiple changes to mitigate the corruption/segfaults:
- rework ext_kill slightly to avoid lockups in edgecases
- rewrite the threading model for preview thread
- no longer (l)stat files on selection
  • Loading branch information
Ckath committed Sep 12, 2019
2 parents c502d41 + da1978b commit d7a48d4
Show file tree
Hide file tree
Showing 10 changed files with 271 additions and 109 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ another cli filebrowser made just for myself with the following ideas:
- more respect for `LS_COLORS`
- no fancy file edit operations, at most drop to shell
- no fancy in depth configuration besides the scripts
- minimize hanging due to unneeded disk io

## keybinds
### vim - work mostly as expected
Expand Down
11 changes: 6 additions & 5 deletions ext/colors.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <wchar.h>
#include <unistd.h>
#include "colors.h"
#include "sncurses.h"

void
init_colors()
Expand All @@ -20,7 +21,7 @@ init_colors()
* fg: first 4 bits, bg: last 4 bits */
for (int fg = COLOR_DEFAULT; fg <= COLOR_WHITE; ++fg) {
for (int bg = COLOR_DEFAULT; bg <= COLOR_WHITE; ++bg) {
init_pair(COL(fg, bg), fg, bg);
sinit_pair(COL(fg, bg), fg, bg);
}
}
}
Expand Down Expand Up @@ -150,7 +151,7 @@ mvwaddcolitem(WINDOW *win, int y, int x, const char *name, mode_t mode)
}

if (!getenv("LS_COLORS")) {
mvwaddstr(win, y, x-1, fname);
smvwaddstr(win, y, x-1, fname);
return;
}

Expand Down Expand Up @@ -210,7 +211,7 @@ mvwaddcolitem(WINDOW *win, int y, int x, const char *name, mode_t mode)
attrs = find_lsattrs("no");
}

wattron(win, attrs);
mvwaddstr(win, y, x-1, fname);
wattroff(win, attrs);
swattron(win, attrs);
smvwaddstr(win, y, x-1, fname);
swattroff(win, attrs);
}
119 changes: 119 additions & 0 deletions ext/sncurses.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/* sncurses.c
* safe(r) ncurses wrappers that should be fine across threads */

#include <ncurses.h>
#include <pthread.h>
#include "sncurses.h"

pthread_mutex_t ncurses_lock = PTHREAD_MUTEX_INITIALIZER;

int
sinit_pair(short pair, short f, short b)
{
pthread_mutex_lock(&ncurses_lock);
int r = init_pair(pair, f, b);
pthread_mutex_unlock(&ncurses_lock);
return r;
}

WINDOW *
snewwin(int nlines, int ncols, int begin_y, int begin_x)
{
pthread_mutex_lock(&ncurses_lock);
WINDOW *r = newwin(nlines, ncols, begin_y, begin_x);
pthread_mutex_unlock(&ncurses_lock);
return r;
}

int
sdelwin(WINDOW *win)
{
pthread_mutex_lock(&ncurses_lock);
int r = delwin(win);
pthread_mutex_unlock(&ncurses_lock);
return r;
}

int
sbox(WINDOW *win, chtype verch, chtype horch)
{
pthread_mutex_lock(&ncurses_lock);
int r = box(win, verch, horch);
pthread_mutex_unlock(&ncurses_lock);
return r;
}

int
swaddch(WINDOW *win, const chtype ch)
{
pthread_mutex_lock(&ncurses_lock);
int r = waddch(win, ch);
pthread_mutex_unlock(&ncurses_lock);
return r;
}

int
smvwaddch(WINDOW *win, int y, int x, const chtype ch)
{
pthread_mutex_lock(&ncurses_lock);
int r = mvwaddch(win, y, x, ch);
pthread_mutex_unlock(&ncurses_lock);
return r;
}

int
smvwdelch(WINDOW *win, int y, int x)
{
pthread_mutex_lock(&ncurses_lock);
int r = mvwdelch(win, y, x);
pthread_mutex_unlock(&ncurses_lock);
return r;
}

int
smvwaddstr(WINDOW *win, int y, int x, const char *str)
{
pthread_mutex_lock(&ncurses_lock);
int r = mvwaddstr(win, y, x, str);
pthread_mutex_unlock(&ncurses_lock);
return r;
}

int
smvwprintw(WINDOW *win, int y, int x, const char *fmt, ...)
{
char buf[999];
va_list args;
va_start(args, fmt);
vsnprintf(buf, 999, fmt, args);
va_end(args);

return smvwaddstr(win, y, x, buf);
}

int
swattron(WINDOW *win, int attrs)
{
pthread_mutex_lock(&ncurses_lock);
int r = wattron(win, attrs);
pthread_mutex_unlock(&ncurses_lock);
return r;
}

int
swattroff(WINDOW *win, int attrs)
{
pthread_mutex_lock(&ncurses_lock);
int r = wattroff(win, attrs);
pthread_mutex_unlock(&ncurses_lock);
return r;
}

int
swrefresh(WINDOW *win)
{
pthread_mutex_lock(&ncurses_lock);
int r = wrefresh(win);
pthread_mutex_unlock(&ncurses_lock);
return r;
}
18 changes: 18 additions & 0 deletions ext/sncurses.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef SNCURSES
#define SNCURSES
#include <ncurses.h>

int sinit_pair(short pair, short f, short b);
WINDOW *snewwin(int nlines, int ncols, int begin_y, int begin_x);
int sdelwin(WINDOW *win);
int sbox(WINDOW *win, chtype verch, chtype horch);
int swaddch(WINDOW *win, const chtype ch);
int smvwaddch(WINDOW *win, int y, int x, const chtype ch);
int smvwdelch(WINDOW *win, int y, int x);
int smvwaddstr(WINDOW *win, int y, int x, const char *str);
int smvwprintw(WINDOW *win, int y, int x, const char *fmt, ...);
int swattron(WINDOW *win, int attrs);
int swattroff(WINDOW *win, int attrs);
int swrefresh(WINDOW *win);

#endif
2 changes: 1 addition & 1 deletion ext/sysext.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ ext_kill(pid_t pid, int sig)
fscanf(f, "%d", &child_pid);
fclose(f);
kill(pid, sig);
pid = child_pid;
pid = pid == child_pid ? 0 : child_pid;
}
}

Expand Down
18 changes: 13 additions & 5 deletions ext/thr.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
pthread_t load_thr;
pthread_t display_thr;
pthread_t preview_thr;
pthread_cond_t run_preview = PTHREAD_COND_INITIALIZER;
pthread_mutex_t preview_lock = PTHREAD_MUTEX_INITIALIZER;
pid_t preview_pid = 0;
bool items_loading = false;

Expand All @@ -30,17 +32,23 @@ stop_load()
}

void
start_preview(void *load_preview)
init_preview(void *load_preview)
{
pthread_create(&preview_thr, NULL, load_preview, NULL);
}

void
stop_preview()
queue_preview()
{
pthread_mutex_lock(&preview_lock);
pthread_cond_signal(&run_preview);
pthread_mutex_unlock(&preview_lock);
}

void
cancel_preview()
{
if (preview_pid) {
ext_kill(preview_pid, SIGTERM);
} if (preview_thr) {
pthread_join(preview_thr, NULL);
ext_kill(preview_pid, SIGKILL);
}
}
5 changes: 3 additions & 2 deletions ext/thr.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

void start_load(void *load_items, void *display_load);
void stop_load();
void start_preview(void *load_preview);
void stop_preview();
void init_preview(void *load_preview);
void queue_preview();
void cancel_preview();

#endif
Loading

0 comments on commit d7a48d4

Please sign in to comment.