~vorlon/ubuntu/raring/upstart/lp.1199778

« back to all changes in this revision

Viewing changes to init/event.c

  • Committer: Scott James Remnant
  • Date: 2008-04-12 10:11:22 UTC
  • Revision ID: scott@netsplit.com-20080412101122-tmxc5kdukj2xlz9z
* init/event.h (Event): We don't use the id field for anything;
and it can't be guaranteed to be unique since it can wrap over
and get reused.  Drop it.
* init/event.c (event_next_id): Drop this function.
(event_new): Don't assign an id anymore.
(event_find_by_id): Drop this function.
* init/tests/test_event.c (test_new): Drop checks on the id field
(test_find_by_id): Drop test.
(test_poll): Drop id setting which was needless anyway.

Show diffs side-by-side

added added

removed removed

Lines of Context:
57
57
 **/
58
58
int paused = FALSE;
59
59
 
60
 
 
61
 
/**
62
 
 * event_id
63
 
 *
64
 
 * This counter is used to assign unique event ids and is incremented
65
 
 * each time we use it.  After a while (4 billion events) it'll wrap over,
66
 
 * in which case you should set event_id_wrapped and take care to check
67
 
 * an id isn't taken.
68
 
 **/
69
 
unsigned int event_id = 0;
70
 
int          event_id_wrapped = FALSE;
71
 
 
72
60
/**
73
61
 * events:
74
62
 *
92
80
 
93
81
 
94
82
/**
95
 
 * event_next_id:
96
 
 *
97
 
 * Returns the current value of the event_id counter, unless that has
98
 
 * been wrapped before, in which case it checks whether the value is
99
 
 * currently in use before returning it.  If the value is in use, it
100
 
 * increments the counter until it finds a value that isn't, or until it
101
 
 * has checked the entire value space.
102
 
 *
103
 
 * This is most efficient while less than 4 billion events have been
104
 
 * generated, at which point it becomes slightly less efficient.  If there
105
 
 * are currently 4 billion events being handled (!!) we lose the ability
106
 
 * to generate unique ids, and emit an error -- if we start seeing this in
107
 
 * the field, we can always to a larger type or something.
108
 
 *
109
 
 * Returns: next usable id.
110
 
 **/
111
 
static inline unsigned int
112
 
event_next_id (void)
113
 
{
114
 
        unsigned int id;
115
 
 
116
 
        /* If we've wrapped the event_id counter, we can't just assume that
117
 
         * the current value isn't taken, we need to make sure that nothing
118
 
         * is using it first.
119
 
         */
120
 
        if (event_id_wrapped) {
121
 
                unsigned int start_id = event_id;
122
 
 
123
 
                while (event_find_by_id (event_id)) {
124
 
                        event_id++;
125
 
 
126
 
                        /* Make sure we don't end up in an infinite loop if
127
 
                         * we're currently handling 4 billion events.
128
 
                         */
129
 
                        if (event_id == start_id) {
130
 
                                nih_error (_("Event id %u is not unique"),
131
 
                                           event_id);
132
 
                                break;
133
 
                        }
134
 
                }
135
 
        }
136
 
 
137
 
        /* Use the current value of the counter, it's unique as we're ever
138
 
         * going to get; increment the counter afterwards so the next time
139
 
         * this runs, we have moved forwards.
140
 
         */
141
 
        id = event_id++;
142
 
 
143
 
        /* If incrementing the counter gave us zero, we consumed the entire
144
 
         * id space.  This means that in future we can't assume that the ids
145
 
         * are unique, next time we'll have to be more careful.
146
 
         */
147
 
        if (! event_id) {
148
 
                if (! event_id_wrapped)
149
 
                        nih_debug ("Wrapped event_id counter");
150
 
 
151
 
                event_id_wrapped = TRUE;
152
 
        }
153
 
 
154
 
        return id;
155
 
}
156
 
 
157
 
/**
158
83
 * event_new:
159
84
 * @parent: parent of new event,
160
85
 * @name: name of event to emit,
198
123
 
199
124
        nih_list_init (&event->entry);
200
125
 
201
 
        event->id = event_next_id ();
202
126
        event->progress = EVENT_PENDING;
203
127
        event->failed = FALSE;
204
128
 
222
146
        return event;
223
147
}
224
148
 
225
 
/**
226
 
 * event_find_by_id:
227
 
 * @id: id to find.
228
 
 *
229
 
 * Finds the event with the given id in the list of events currently being
230
 
 * dealt with.
231
 
 *
232
 
 * Returns: Event found or NULL if not found.
233
 
 **/
234
 
Event *
235
 
event_find_by_id (unsigned int id)
236
 
{
237
 
        Event *event;
238
 
 
239
 
        event_init ();
240
 
 
241
 
        NIH_LIST_FOREACH (events, iter) {
242
 
                event = (Event *)iter;
243
 
 
244
 
                if (event->id == id)
245
 
                        return event;
246
 
        }
247
 
 
248
 
        return NULL;
249
 
}
250
 
 
251
149
 
252
150
/**
253
151
 * event_block: