|
| 1 | +/* |
| 2 | + * alarm_cond.c |
| 3 | + * |
| 4 | + * This is an enhancement to the alarm_mutex.c program, which |
| 5 | + * used only a mutex to synchronize access to the shared alarm |
| 6 | + * list. This version adds a condition variable. The alarm |
| 7 | + * thread waits on this condition variable, with a timeout that |
| 8 | + * corresponds to the earliest timer request. If the main thread |
| 9 | + * enters an earlier timeout, it signals the condition variable |
| 10 | + * so that the alarm thread will wake up and process the earlier |
| 11 | + * timeout first, requeueing the later request. |
| 12 | + */ |
| 13 | +#include <pthread.h> |
| 14 | +#include <time.h> |
| 15 | +#include "errors.h" |
| 16 | + |
| 17 | +/* |
| 18 | + * The "alarm" structure now contains the time_t (time since the |
| 19 | + * Epoch, in seconds) for each alarm, so that they can be |
| 20 | + * sorted. Storing the requested number of seconds would not be |
| 21 | + * enough, since the "alarm thread" cannot tell how long it has |
| 22 | + * been on the list. |
| 23 | + */ |
| 24 | +typedef struct alarm_tag { |
| 25 | + struct alarm_tag *link; |
| 26 | + int seconds; |
| 27 | + time_t time; /* seconds from EPOCH */ |
| 28 | + char message[64]; |
| 29 | +} alarm_t; |
| 30 | + |
| 31 | +pthread_mutex_t alarm_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 32 | +pthread_cond_t alarm_cond = PTHREAD_COND_INITIALIZER; |
| 33 | +alarm_t *alarm_list = NULL; |
| 34 | +time_t current_alarm = 0; |
| 35 | + |
| 36 | +/* |
| 37 | + * Insert alarm entry on list, in order. |
| 38 | + */ |
| 39 | +void alarm_insert (alarm_t *alarm) |
| 40 | +{ |
| 41 | + int status; |
| 42 | + alarm_t **last, *next; |
| 43 | + |
| 44 | + /* |
| 45 | + * LOCKING PROTOCOL: |
| 46 | + * |
| 47 | + * This routine requires that the caller have locked the |
| 48 | + * alarm_mutex! |
| 49 | + */ |
| 50 | + last = &alarm_list; |
| 51 | + next = *last; |
| 52 | + while (next != NULL) { |
| 53 | + if (next->time >= alarm->time) { |
| 54 | + alarm->link = next; |
| 55 | + *last = alarm; |
| 56 | + break; |
| 57 | + } |
| 58 | + last = &next->link; |
| 59 | + next = next->link; |
| 60 | + } |
| 61 | + /* |
| 62 | + * If we reached the end of the list, insert the new alarm |
| 63 | + * there. ("next" is NULL, and "last" points to the link |
| 64 | + * field of the last item, or to the list header.) |
| 65 | + */ |
| 66 | + if (next == NULL) { |
| 67 | + *last = alarm; |
| 68 | + alarm->link = NULL; |
| 69 | + } |
| 70 | +#ifdef DEBUG |
| 71 | + printf ("[list: "); |
| 72 | + for (next = alarm_list; next != NULL; next = next->link) |
| 73 | + printf ("%d(%d)[\"%s\"] ", next->time, |
| 74 | + next->time - time (NULL), next->message); |
| 75 | + printf ("]\n"); |
| 76 | +#endif |
| 77 | + /* |
| 78 | + * Wake the alarm thread if it is not busy (that is, if |
| 79 | + * current_alarm is 0, signifying that it's waiting for |
| 80 | + * work), or if the new alarm comes before the one on |
| 81 | + * which the alarm thread is waiting. |
| 82 | + */ |
| 83 | + if (current_alarm == 0 || alarm->time < current_alarm) { |
| 84 | + current_alarm = alarm->time; |
| 85 | + status = pthread_cond_signal (&alarm_cond); |
| 86 | + if (status != 0) |
| 87 | + err_abort (status, "Signal cond"); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +/* |
| 92 | + * The alarm thread's start routine. |
| 93 | + */ |
| 94 | +void *alarm_thread (void *arg) |
| 95 | +{ |
| 96 | + alarm_t *alarm; |
| 97 | + struct timespec cond_time; |
| 98 | + time_t now; |
| 99 | + int status, expired; |
| 100 | + |
| 101 | + /* |
| 102 | + * Loop forever, processing commands. The alarm thread will |
| 103 | + * be disintegrated when the process exits. Lock the mutex |
| 104 | + * at the start -- it will be unlocked during condition |
| 105 | + * waits, so the main thread can insert alarms. |
| 106 | + */ |
| 107 | + status = pthread_mutex_lock (&alarm_mutex); |
| 108 | + if (status != 0) |
| 109 | + err_abort (status, "Lock mutex"); |
| 110 | + while (1) { |
| 111 | + /* |
| 112 | + * If the alarm list is empty, wait until an alarm is |
| 113 | + * added. Setting current_alarm to 0 informs the insert |
| 114 | + * routine that the thread is not busy. |
| 115 | + */ |
| 116 | + current_alarm = 0; |
| 117 | + while (alarm_list == NULL) { |
| 118 | + status = pthread_cond_wait (&alarm_cond, &alarm_mutex); |
| 119 | + if (status != 0) |
| 120 | + err_abort (status, "Wait on cond"); |
| 121 | + } |
| 122 | + alarm = alarm_list; |
| 123 | + alarm_list = alarm->link; |
| 124 | + now = time (NULL); |
| 125 | + expired = 0; |
| 126 | + if (alarm->time > now) { |
| 127 | +#ifdef DEBUG |
| 128 | + printf ("[waiting: %d(%d)\"%s\"]\n", alarm->time, |
| 129 | + alarm->time - time (NULL), alarm->message); |
| 130 | +#endif |
| 131 | + cond_time.tv_sec = alarm->time; |
| 132 | + cond_time.tv_nsec = 0; |
| 133 | + current_alarm = alarm->time; |
| 134 | + while (current_alarm == alarm->time) { |
| 135 | + status = pthread_cond_timedwait ( |
| 136 | + &alarm_cond, &alarm_mutex, &cond_time); |
| 137 | + if (status == ETIMEDOUT) { |
| 138 | + expired = 1; |
| 139 | + break; |
| 140 | + } |
| 141 | + if (status != 0) |
| 142 | + err_abort (status, "Cond timedwait"); |
| 143 | + } |
| 144 | + if (!expired) |
| 145 | + alarm_insert (alarm); |
| 146 | + } else |
| 147 | + expired = 1; |
| 148 | + if (expired) { |
| 149 | + printf ("(%d) %s\n", alarm->seconds, alarm->message); |
| 150 | + free (alarm); |
| 151 | + } |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +int main (int argc, char *argv[]) |
| 156 | +{ |
| 157 | + int status; |
| 158 | + char line[128]; |
| 159 | + alarm_t *alarm; |
| 160 | + pthread_t thread; |
| 161 | + |
| 162 | + status = pthread_create ( |
| 163 | + &thread, NULL, alarm_thread, NULL); |
| 164 | + if (status != 0) |
| 165 | + err_abort (status, "Create alarm thread"); |
| 166 | + while (1) { |
| 167 | + printf ("Alarm> "); |
| 168 | + if (fgets (line, sizeof (line), stdin) == NULL) exit (0); |
| 169 | + if (strlen (line) <= 1) continue; |
| 170 | + alarm = (alarm_t*)malloc (sizeof (alarm_t)); |
| 171 | + if (alarm == NULL) |
| 172 | + errno_abort ("Allocate alarm"); |
| 173 | + |
| 174 | + /* |
| 175 | + * Parse input line into seconds (%d) and a message |
| 176 | + * (%64[^\n]), consisting of up to 64 characters |
| 177 | + * separated from the seconds by whitespace. |
| 178 | + */ |
| 179 | + if (sscanf (line, "%d %64[^\n]", |
| 180 | + &alarm->seconds, alarm->message) < 2) { |
| 181 | + fprintf (stderr, "Bad command\n"); |
| 182 | + free (alarm); |
| 183 | + } else { |
| 184 | + status = pthread_mutex_lock (&alarm_mutex); |
| 185 | + if (status != 0) |
| 186 | + err_abort (status, "Lock mutex"); |
| 187 | + alarm->time = time (NULL) + alarm->seconds; |
| 188 | + /* |
| 189 | + * Insert the new alarm into the list of alarms, |
| 190 | + * sorted by expiration time. |
| 191 | + */ |
| 192 | + alarm_insert (alarm); |
| 193 | + status = pthread_mutex_unlock (&alarm_mutex); |
| 194 | + if (status != 0) |
| 195 | + err_abort (status, "Unlock mutex"); |
| 196 | + } |
| 197 | + } |
| 198 | +} |
0 commit comments