/* location.c * Hack to show how to get location from gpsd * when you don't have a convenient main loop. */ #include #include /* Stuff that should be defined someplace else. */ struct location { double lat; double lon; }; typedef int* gpsd_t; extern void start_location(gpsd_t arg); extern struct location GetLatestLocation(); extern struct location GetGpsLocation(gpsd_t arg); /* end stuff */ struct location latest; pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; pthread_t thread; static void* worker(void* arg); void start_location(gpsd_t arg) { int rc; latest = GetGpsLocation(arg); /* avoid initial garbage */ rc = pthread_create(&thread, NULL, worker, arg); if (rc) { exit(-3); } }; struct location GetLatestLocation(void) { struct location temp; pthread_mutex_lock(&mutex); temp = latest; pthread_mutex_unlock(&mutex); return(temp); }; static void* worker(void* arg) { gpsd_t gps = (gpsd_t) arg; struct location temp; while (1) { temp = GetGpsLocation(gps); pthread_mutex_lock(&mutex); latest = temp; pthread_mutex_unlock(&mutex); } };