00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include <unistd.h>
00044 #include <time.h>
00045
00046
00047 struct timespec event_queue_length;
00048
00049
00050
00051 void queue_init(event_queue_t* queue, int fpause) {
00052 init_heap(&(queue->heap));
00053 queue->pause = fpause;
00054 pthread_mutex_init(&(queue->lock), NULL);
00055 }
00056
00057 void queue_insert_event(event_queue_t* queue, event_t* event) {
00058 pthread_mutex_lock(&(queue->lock));
00059 heap_insert(&(queue->heap), event, event->time);
00060 pthread_mutex_unlock(&(queue->lock));
00061 }
00062
00063 event_t* queue_pop_event(event_queue_t* queue) {
00064 long long ftime;
00065 event_t* event;
00066
00067 pthread_mutex_lock(&(queue->lock));
00068 event = (event_t*)(heap_pop_min_data(&(queue->heap), &ftime));
00069 pthread_mutex_unlock(&(queue->lock));
00070
00071 if(dbg_active(DBG_QUEUE)) {
00072 char timeStr[128];
00073 timeStr[0] = 0;
00074 printOtherTime(timeStr, 128, ftime);
00075 dbg(DBG_QUEUE, "Popping event for mote %i with time %s.\n", event->mote, timeStr);
00076 }
00077
00078 if (queue->pause > 0 && event->pause) {
00079 sleep(queue->pause);
00080
00081 }
00082
00083 return event;
00084 }
00085
00086 int queue_is_empty(event_queue_t* queue) {
00087 int rval;
00088 pthread_mutex_lock(&(queue->lock));
00089 rval = heap_is_empty(&(queue->heap));
00090 pthread_mutex_unlock(&(queue->lock));
00091 return rval;
00092 }
00093
00094 long long queue_peek_event_time(event_queue_t* queue) {
00095 long long rval;
00096
00097 pthread_mutex_lock(&(queue->lock));
00098 if (heap_is_empty(&(queue->heap))) {
00099 rval = -1;
00100 }
00101 else {
00102 rval = heap_get_min_key(&(queue->heap));
00103 }
00104
00105 pthread_mutex_unlock(&(queue->lock));
00106 return rval;
00107 }
00108
00109 void queue_handle_next_event(event_queue_t* queue) {
00110 event_t* event = queue_pop_event(queue);
00111 if (event != NULL) {
00112 if (tos_state.moteOn[event->mote] || event->force) {
00113 NODE_NUM = event->mote;
00114 dbg(DBG_QUEUE, "Setting TOS_LOCAL_ADDRESS to %hi\n", (short)((event->mote + tos_state.first_node) & 0xffff));
00115 if (event->mote > TOSNODES)
00116 dbg(DBG_ERROR, "Setting TOS_LOCAL_ADDRESS to %hi\n", (short)((event->mote + tos_state.first_node) & 0xffff));
00117 atomic TOS_LOCAL_ADDRESS = (short)((event->mote + tos_state.first_node) & 0xffff);
00118 event->handle(event, &tos_state);
00119 }
00120 }
00121 }
00122
00123