static void * timer_routine(void * args) {
while (!timer_stop) {
printf("Time slot %3lu\n", current_time());
usleep(100000);
int fsh = 0;
int event = 0;
/* Wait for all devices have done the job in current
* time slot */
struct timer_id_container_t * temp;
for (temp = dev_list; temp != NULL; temp = temp->next) {
pthread_mutex_lock(&temp->id.event_lock);
while (!temp->id.done && !temp->id.fsh) {
pthread_cond_wait(
&temp->id.event_cond,
&temp->id.event_lock
);
}
if (temp->id.fsh) {
fsh++;
}
event++;
pthread_mutex_unlock(&temp->id.event_lock);
}
/* Increase the time slot */
_time++;
/* Let devices continue their job */
for (temp = dev_list; temp != NULL; temp = temp->next) {
pthread_mutex_lock(&temp->id.timer_lock);
temp->id.done = 0;
pthread_cond_signal(&temp->id.timer_cond);
pthread_mutex_unlock(&temp->id.timer_lock);
}
if (fsh == event) {
break;
}
}
pthread_exit(args);
}
void next_slot(struct timer_id_t * timer_id) {
/* Tell to timer that we have done our job in current slot */
pthread_mutex_lock(&timer_id->event_lock);
timer_id->done = 1;
pthread_cond_signal(&timer_id->event_cond);
pthread_mutex_unlock(&timer_id->event_lock);
/* Wait for going to next slot */
pthread_mutex_lock(&timer_id->timer_lock);
while (timer_id->done) {
pthread_cond_wait(
&timer_id->timer_cond,
&timer_id->timer_lock
);
}
pthread_mutex_unlock(&timer_id->timer_lock);
}