summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2012-10-18 23:50:26 +0200
committerLennart Poettering <lennart@poettering.net>2012-10-19 00:07:55 +0200
commit28917d7dc711746795f7e6468c06c1983a5cdf53 (patch)
tree61d0e8fb926c3a411cfbc4705a622135bfbbb381
parent774471b68a948e58c9fc79d61b6f79dbc2e01b37 (diff)
util: simplify column caching logic
-rw-r--r--src/cgtop/cgtop.c4
-rw-r--r--src/shared/util.c47
-rw-r--r--src/shared/util.h1
3 files changed, 19 insertions, 33 deletions
diff --git a/src/cgtop/cgtop.c b/src/cgtop/cgtop.c
index 9eb2d2fdeb..ee421e383b 100644
--- a/src/cgtop/cgtop.c
+++ b/src/cgtop/cgtop.c
@@ -447,7 +447,7 @@ static int display(Hashmap *a) {
if (rows <= 0)
rows = 25;
- path_columns = columns_uncached() - 42;
+ path_columns = columns() - 42;
if (path_columns < 10)
path_columns = 10;
@@ -653,6 +653,8 @@ int main(int argc, char *argv[]) {
goto finish;
}
+ signal(SIGWINCH, columns_cache_reset);
+
while (!quit) {
Hashmap *c;
usec_t t;
diff --git a/src/shared/util.c b/src/shared/util.c
index 1c97a8a94a..462b541b41 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -72,7 +72,7 @@
int saved_argc = 0;
char **saved_argv = NULL;
-static int parsed_columns = 0;
+static volatile unsigned cached_columns = 0;
size_t page_size(void) {
static __thread size_t pgsz = 0;
@@ -3793,46 +3793,31 @@ int fd_columns(int fd) {
return ws.ws_col;
}
-static unsigned columns_cached(bool cached) {
- static __thread int env_columns = -1;
+unsigned columns(void) {
const char *e;
+ unsigned c;
- if (_likely_(parsed_columns > 0 && cached))
- return parsed_columns;
-
- if (_unlikely_(env_columns == -1)) {
- e = getenv("COLUMNS");
- if (e)
- env_columns = atoi(e);
- else
- env_columns = 0;
- }
-
- if (env_columns > 0) {
- parsed_columns = env_columns;
- return parsed_columns;
- }
+ if (_likely_(cached_columns > 0))
+ return cached_columns;
- if (parsed_columns <= 0 || !cached)
- parsed_columns = fd_columns(STDOUT_FILENO);
-
- if (parsed_columns <= 0)
- parsed_columns = 80;
+ c = 0;
+ e = getenv("COLUMNS");
+ if (e)
+ safe_atou(e, &c);
- return parsed_columns;
-}
+ if (c <= 0)
+ c = fd_columns(STDOUT_FILENO);
-unsigned columns(void) {
- return columns_cached(true);
-}
+ if (c <= 0)
+ c = 80;
-unsigned columns_uncached(void) {
- return columns_cached(false);
+ cached_columns = c;
+ return c;
}
/* intended to be used as a SIGWINCH sighandler */
void columns_cache_reset(int signum) {
- parsed_columns = 0;
+ cached_columns = 0;
}
int fd_lines(int fd) {
diff --git a/src/shared/util.h b/src/shared/util.h
index 50911ebb34..662c3d1f39 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -384,7 +384,6 @@ int status_welcome(void);
int fd_columns(int fd);
unsigned columns(void);
-unsigned columns_uncached(void);
void columns_cache_reset(int _unused_ signum);
int fd_lines(int fd);