+ struct hotplug_msg *loop_msg;
+ struct hotplug_msg *tmp_msg;
+ time_t msg_age = 0;
+ struct timespec tv;
+
+ while (1) {
+ dbg("msg queue manager, next expected is %d", expected_seqnum);
+ pthread_mutex_lock(&msg_lock);
+ pthread_mutex_lock(&exec_lock);
+recheck:
+ list_for_each_entry_safe(loop_msg, tmp_msg, &msg_list, list) {
+ /* move event with expected sequence to the exec list */
+ if (loop_msg->seqnum == expected_seqnum) {
+ msg_move_exec(&loop_msg->list);
+ expected_seqnum++;
+ dbg("moved seq %d to exec, next expected is %d",
+ loop_msg->seqnum, expected_seqnum);
+ continue;
+ }
+
+ /* move event with expired timeout to the exec list */
+ msg_age = time(NULL) - loop_msg->queue_time;
+ if (msg_age > EVENT_TIMEOUT_SEC-1) {
+ msg_move_exec(&loop_msg->list);
+ expected_seqnum = loop_msg->seqnum+1;
+ dbg("moved seq %d to exec, reset next expected to %d",
+ loop_msg->seqnum, expected_seqnum);
+ goto recheck;
+ } else {
+ break;
+ }
+ }
+
+ msg_dump_queue();
+ pthread_mutex_unlock(&exec_lock);
+ pthread_mutex_unlock(&msg_lock);
+
+ /* wait until queue gets active or next message timeout expires */
+ pthread_mutex_lock(&msg_active_lock);
+
+ if (list_empty(&msg_list) == 0) {
+ tv.tv_sec = time(NULL) + EVENT_TIMEOUT_SEC - msg_age;
+ tv.tv_nsec = 0;
+ dbg("next event expires in %li seconds",
+ EVENT_TIMEOUT_SEC - msg_age);
+ pthread_cond_timedwait(&msg_active, &msg_active_lock, &tv);
+ } else {
+ pthread_cond_wait(&msg_active, &msg_active_lock);
+ }
+ pthread_mutex_unlock(&msg_active_lock);