Skip to content

Commit 3281479

Browse files
committed
cache board type to avoid poor performance (#196)
cache board type to avoid poor performance in functions that are called frequently like gpio_set_value() in source/event_gpio.c Signed-off-by: Drew Fustini <drew@pdp7.com>
1 parent b328cdd commit 3281479

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

source/common.c

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -601,23 +601,30 @@ int pocketbeagle(void) {
601601
*/
602602
int beaglebone_blue(void) {
603603
const char *cmd = "/bin/grep -c 'TI AM335x BeagleBone Blue' /proc/device-tree/model";
604-
char blue;
604+
// cache the value to avoid poor performance
605+
// in functions that are called frequently like
606+
// gpio_set_value() in source/event_gpio.c
607+
static int initialized = 0;
608+
static int retval = 0;
605609
FILE *file = NULL;
606610

607-
file = popen(cmd, "r");
608-
if (file == NULL) {
609-
fprintf(stderr, "error: beaglebone_blue() failed to run cmd=%s\n", cmd);
610-
syslog(LOG_ERR, "Adafruit_BBIO: error: beaglebone_blue() failed to run cmd=%s\n", cmd);
611-
return -1;
611+
//fprintf(stderr, "beaglebone_blue(): initialized=[%d] retval=[%d]\n", initialized, retval);
612+
if(!initialized) {
613+
initialized = 1;
614+
//fprintf(stderr, "beaglebone_blue(): not initialized\n");
615+
file = popen(cmd, "r");
616+
if (file == NULL) {
617+
fprintf(stderr, "Adafruit_BBIO: error in beaglebone_blue(): failed to run cmd=%s\n", cmd);
618+
syslog(LOG_ERR, "Adafruit_BBIO: error in beaglebone_blue(): failed to run cmd=%s\n", cmd);
619+
return -1;
620+
}
621+
if( fgetc(file) == '1' ) {
622+
retval = 1;
623+
}
624+
pclose(file);
612625
}
613-
blue = fgetc(file);
614-
pclose(file);
615626

616-
if(blue == '1') {
617-
return 1;
618-
} else {
619-
return 0;
620-
}
627+
return retval;
621628
}
622629

623630

0 commit comments

Comments
 (0)