|
From: <mla...@us...> - 2008-01-02 06:32:42
|
Revision: 391
http://g15daemon.svn.sourceforge.net/g15daemon/?rev=391&view=rev
Author: mlampard
Date: 2008-01-01 22:32:46 -0800 (Tue, 01 Jan 2008)
Log Message:
-----------
use pthread_cond_timedwait() etc to wakeup LCD thread.
Modified Paths:
--------------
trunk/g15daemon-wip/ChangeLog
trunk/g15daemon-wip/g15daemon/g15daemon.h
trunk/g15daemon-wip/g15daemon/main.c
trunk/g15daemon-wip/g15daemon/utility_funcs.c
trunk/g15daemon-wip/plugins/g15_plugin_clock.c
trunk/g15daemon-wip/plugins/g15_plugin_net.c
Modified: trunk/g15daemon-wip/ChangeLog
===================================================================
--- trunk/g15daemon-wip/ChangeLog 2008-01-02 03:48:34 UTC (rev 390)
+++ trunk/g15daemon-wip/ChangeLog 2008-01-02 06:32:46 UTC (rev 391)
@@ -147,3 +147,5 @@
- TidyUp: Make all plugin-internal functions static.
- Portability: Remove mutexes on Solaris, as libusb (and therefore libg15)
blocks on read.
+- Optimisation: Use pthread conditional variable to signal LCD state change.
+ Further reduces unnecessary wakeups.
Modified: trunk/g15daemon-wip/g15daemon/g15daemon.h
===================================================================
--- trunk/g15daemon-wip/g15daemon/g15daemon.h 2008-01-02 03:48:34 UTC (rev 390)
+++ trunk/g15daemon-wip/g15daemon/g15daemon.h 2008-01-02 06:32:46 UTC (rev 391)
@@ -220,6 +220,10 @@
#define SERV_HELO "G15 daemon HELLO"
#ifdef G15DAEMON_BUILD
+void g15daemon_init_refresh();
+void g15daemon_send_refresh(lcd_t *lcd);
+void g15daemon_wait_refresh();
+void g15daemon_quit_refresh();
/* internal g15daemon-only functions */
int uf_write_buf_to_g15(lcd_t *lcd);
int uf_read_keypresses(unsigned int *keypresses, unsigned int timeout);
Modified: trunk/g15daemon-wip/g15daemon/main.c
===================================================================
--- trunk/g15daemon-wip/g15daemon/main.c 2008-01-02 03:48:34 UTC (rev 390)
+++ trunk/g15daemon-wip/g15daemon/main.c 2008-01-02 06:32:46 UTC (rev 391)
@@ -198,7 +198,7 @@
}
if(!leaving) {
masterlist->current->lcd->state_changed=1;
- masterlist->current->lcd->ident=random();
+ g15daemon_send_refresh(masterlist->current->lcd);
}
pthread_mutex_unlock(&g15lib_mutex);
}
@@ -220,10 +220,12 @@
g15daemon_sleep(2);
while (!leaving) {
+ /* wait until a client has updated */
+ g15daemon_wait_refresh();
+
pthread_mutex_lock(&lcdlist_mutex);
-
displaying = masterlist->current->lcd;
-
+
if(displaying->ident != lastlcd){
/* monitor 'fps' - due to the TCP protocol, some frames will be bunched up.
discard excess to reduce load on the bus */
@@ -255,7 +257,6 @@
}
pthread_mutex_unlock(&lcdlist_mutex);
- g15daemon_msleep(5);
}
return NULL;
}
@@ -479,7 +480,9 @@
seteuid(nobody->pw_uid);
setegid(nobody->pw_gid);
}
-#endif
+#endif
+ /* initialise the pthread condition for the LCD thread */
+ g15daemon_init_refresh();
pthread_mutex_init(&g15lib_mutex, NULL);
pthread_attr_init(&attr);
@@ -552,6 +555,7 @@
seteuid(0);
setegid(0);
closelog();
+ g15daemon_quit_refresh();
uf_conf_write(lcdlist,"/etc/g15daemon.conf");
uf_conf_free(lcdlist);
unlink("/var/run/g15daemon.pid");
Modified: trunk/g15daemon-wip/g15daemon/utility_funcs.c
===================================================================
--- trunk/g15daemon-wip/g15daemon/utility_funcs.c 2008-01-02 03:48:34 UTC (rev 390)
+++ trunk/g15daemon-wip/g15daemon/utility_funcs.c 2008-01-02 06:32:46 UTC (rev 391)
@@ -46,9 +46,12 @@
#include <stdarg.h>
extern unsigned int g15daemon_debug;
+extern volatile int leaving;
#define G15DAEMON_PIDFILE "/var/run/g15daemon.pid"
+pthread_cond_t lcd_refresh = PTHREAD_COND_INITIALIZER;
+
/* if no exitfunc or eventhandler, member should be NULL */
const plugin_info_t generic_info[] = {
/* TYPE, name, initfunc, updatefreq, exitfunc, eventhandler */
@@ -71,6 +74,40 @@
return ptr;
}
+
+void g15daemon_init_refresh() {
+ pthread_condattr_t attr;
+ pthread_cond_init(&lcd_refresh, &attr);
+}
+
+void g15daemon_send_refresh(lcd_t *lcd) {
+ lcd->ident=random();
+ pthread_cond_broadcast(&lcd_refresh);
+}
+
+void g15daemon_wait_refresh() {
+ pthread_mutex_t dummy_mutex;
+ struct timespec timeout;
+ int retval;
+ /* Create a dummy mutex which doesn't unlock for sure while waiting. */
+ pthread_mutex_init(&dummy_mutex, NULL);
+ pthread_mutex_lock(&dummy_mutex);
+start:
+ time(&timeout.tv_sec);
+ timeout.tv_sec += 1;
+ timeout.tv_nsec = 0L;
+
+ retval=pthread_cond_timedwait(&lcd_refresh, &dummy_mutex, &timeout);
+ if(!leaving && retval == ETIMEDOUT)
+ goto start;
+ pthread_mutex_unlock(&dummy_mutex);
+ pthread_mutex_destroy(&dummy_mutex);
+}
+
+void g15daemon_quit_refresh() {
+ pthread_cond_destroy(&lcd_refresh);
+}
+
int uf_return_running(){
int fd;
char pidtxt[128];
Modified: trunk/g15daemon-wip/plugins/g15_plugin_clock.c
===================================================================
--- trunk/g15daemon-wip/plugins/g15_plugin_clock.c 2008-01-02 03:48:34 UTC (rev 390)
+++ trunk/g15daemon-wip/plugins/g15_plugin_clock.c 2008-01-02 06:32:46 UTC (rev 391)
@@ -282,7 +282,7 @@
ret = draw_analog(canvas);
memcpy (lcd->buf, canvas->buffer, G15_BUFFER_LEN);
- lcd->ident = random();
+ g15daemon_send_refresh(lcd);
free(canvas);
return G15_PLUGIN_OK;
}
Modified: trunk/g15daemon-wip/plugins/g15_plugin_net.c
===================================================================
--- trunk/g15daemon-wip/plugins/g15_plugin_net.c 2008-01-02 03:48:34 UTC (rev 390)
+++ trunk/g15daemon-wip/plugins/g15_plugin_net.c 2008-01-02 06:32:46 UTC (rev 391)
@@ -267,7 +267,7 @@
pthread_mutex_lock(&lcdlist_mutex);
memset(client_lcd->buf,0,1024);
g15daemon_convert_buf(client_lcd,tmpbuf);
- client_lcd->ident = random();
+ g15daemon_send_refresh(&client_lcd);
pthread_mutex_unlock(&lcdlist_mutex);
}
}
@@ -279,7 +279,7 @@
}
pthread_mutex_lock(&lcdlist_mutex);
memcpy(client_lcd->buf,tmpbuf,sizeof(client_lcd->buf));
- client_lcd->ident = random();
+ g15daemon_send_refresh(&client_lcd);
pthread_mutex_unlock(&lcdlist_mutex);
}
}
@@ -312,7 +312,7 @@
pthread_mutex_lock(&lcdlist_mutex);
memcpy(client_lcd->buf,tmpbuf+header,buflen+header);
- client_lcd->ident = random();
+ g15daemon_send_refresh(&client_lcd);
pthread_mutex_unlock(&lcdlist_mutex);
}
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|