summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordaniel <danieruru@gmail.com>2013-02-02 13:21:38 +0900
committerdaniel <danieruru@gmail.com>2013-02-02 13:21:38 +0900
commitabafd66ba6297c8962808a07c05219cce7ba3c5f (patch)
treecb87c776633d7d3fa34aa117283e06b768d574bb
parent83745b93506adc234d795a2d7456fd6cf6a1f1c4 (diff)
flash write backgrounding
-rw-r--r--flash.c57
-rw-r--r--flash.h1
-rw-r--r--instr-daemon.c2
3 files changed, 59 insertions, 1 deletions
diff --git a/flash.c b/flash.c
index db5a870..a46a511 100644
--- a/flash.c
+++ b/flash.c
@@ -1,6 +1,7 @@
#include "globals.h"
#include "lcd.h"
#include "version.h"
+#include "flash.h"
#include <stdint.h>
#include <stdio.h>
#include <stdbool.h>
@@ -28,6 +29,16 @@
#define PERSIST_ERR_COULDNTWRITE 9
#define PERSIST_ERR_COUNDNTOPENFILE 10
+typedef struct {
+ FlashStruct *mem;
+ int addr;
+ int numbytes;
+} userflashjob;
+
+static bool alive = true;
+static GThread* userflashwritethread;
+static GAsyncQueue* userflashwritequeue;
+
// crc32 routine
static uint32_t crc32(uint8_t* buf, int count)
@@ -287,7 +298,7 @@ static int readUserBlock(FlashStruct *mem)
return 0;
}
-void writeUserBlock(FlashStruct *mem, int addr, int numbytes)
+void writeUserBlockNow(FlashStruct *mem, int addr, int numbytes)
{
// *** There is a potential issue here.. if the mainfile is corrupt ***
// *** and this gets called before readUserBlock then the ***
@@ -327,6 +338,19 @@ void writeUserBlock(FlashStruct *mem, int addr, int numbytes)
g_static_mutex_unlock (&mutex);
}
+void writeUserBlock(FlashStruct *mem, int addr, int numbytes) {
+ if (alive) {
+ userflashjob* job = g_malloc(sizeof(userflashjob));
+ job->mem = mem;
+ job->addr = addr;
+ job->numbytes = numbytes;
+ g_async_queue_push(userflashwritequeue, job);
+ while (g_async_queue_length(userflashwritequeue) > 1) {
+ // spin
+ }
+ }
+}
+
static void initFlashValues(FlashStruct *mem)
{
@@ -774,9 +798,35 @@ static void initFlashValues(FlashStruct *mem)
}
+static gpointer userflashwritethreadfunc(gpointer data) {
+
+ printf("userflash write thread start\n");
+ while (alive || g_async_queue_length(userflashwritequeue) > 0) { // make sure the last job in the queue gets written
+ GTimeVal whentotimeout;
+ g_get_current_time(&whentotimeout);
+ g_time_val_add(&whentotimeout, 250 * 1000);
+ userflashjob* job = (userflashjob*) g_async_queue_timed_pop(userflashwritequeue, &whentotimeout);
+ if(job != NULL){
+ // process job
+ writeUserBlockNow(job->mem, job->addr, job->numbytes);
+ g_free(job);
+ }
+ }
+ printf("userflash write thread stop\n");
+
+ return NULL ;
+}
+
+void startFlashWriterThread(){
+ alive= true;
+ userflashwritequeue = g_async_queue_new();
+ userflashwritethread = g_thread_create(userflashwritethreadfunc, NULL, true, NULL);
+}
void initFlash(FlashStruct *mem, gboolean reset_to_defaults, int starting_location)
{
+ startFlashWriterThread();
+
int read_size = readUserBlock(mem);
if ( (read_size == 0) ||
@@ -806,9 +856,14 @@ void initFlash(FlashStruct *mem, gboolean reset_to_defaults, int starting_locati
}
}
+void stopFlashWriterThread(){
+ alive = false;
+ g_thread_join(userflashwritethread); // block until the write thread is totally finished.
+}
void fixFlash(FlashStruct *mem)
{
+
int i, fix_initial_constants;
float composite_min_burst_time[max_channels];
diff --git a/flash.h b/flash.h
index c2d3b0e..2432c07 100644
--- a/flash.h
+++ b/flash.h
@@ -7,6 +7,7 @@
void initFlash(FlashStruct *mem, gboolean reset_to_defaults, int starting_location);
void writeUserBlock(FlashStruct *mem, int addr, int numbytes);
void fixFlash(FlashStruct *mem);
+void stopFlashWriterThread();
#endif
diff --git a/instr-daemon.c b/instr-daemon.c
index e110863..2e67c2c 100644
--- a/instr-daemon.c
+++ b/instr-daemon.c
@@ -302,6 +302,8 @@ int main(int argc, char **argv)
g_main_loop_run (loop);
+ stopFlashWriterThread();
+
bus_shutdown();
free(stdinQueue);
free(peers);