~ubuntu-branches/ubuntu/wily/sflphone/wily

« back to all changes in this revision

Viewing changes to daemon/libs/pjproject-2.1.0/pjlib/src/pj/timer.c

  • Committer: Package Import Robot
  • Author(s): Mark Purcell
  • Date: 2014-01-28 18:23:36 UTC
  • mfrom: (1.1.11)
  • mto: This revision was merged to the branch mainline in revision 24.
  • Revision ID: package-import@ubuntu.com-20140128182336-3xenud1kbnwmf3mz
* New upstream release 
  - Fixes "New Upstream Release" (Closes: #735846)
  - Fixes "Ringtone does not stop" (Closes: #727164)
  - Fixes "[sflphone-kde] crash on startup" (Closes: #718178)
  - Fixes "sflphone GUI crashes when call is hung up" (Closes: #736583)
* Build-Depends: ensure GnuTLS 2.6
  - libucommon-dev (>= 6.0.7-1.1), libccrtp-dev (>= 2.0.6-3)
  - Fixes "FTBFS Build-Depends libgnutls{26,28}-dev" (Closes: #722040)
* Fix "boost 1.49 is going away" unversioned Build-Depends: (Closes: #736746)
* Add Build-Depends: libsndfile-dev, nepomuk-core-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: timer.c 4359 2013-02-21 11:18:36Z bennylp $ */
 
2
/* 
 
3
 * The PJLIB's timer heap is based (or more correctly, copied and modied)
 
4
 * from ACE library by Douglas C. Schmidt. ACE is an excellent OO framework
 
5
 * that implements many core patterns for concurrent communication software.
 
6
 * If you're looking for C++ alternative of PJLIB, then ACE is your best
 
7
 * solution.
 
8
 *
 
9
 * You may use this file according to ACE open source terms or PJLIB open
 
10
 * source terms. You can find the fine ACE library at:
 
11
 *  http://www.cs.wustl.edu/~schmidt/ACE.html
 
12
 *
 
13
 * ACE is Copyright (C)1993-2006 Douglas C. Schmidt <d.schmidt@vanderbilt.edu>
 
14
 *
 
15
 * GNU Public License:
 
16
 * This program is free software; you can redistribute it and/or modify
 
17
 * it under the terms of the GNU General Public License as published by
 
18
 * the Free Software Foundation; either version 2 of the License, or
 
19
 * (at your option) any later version.
 
20
 *
 
21
 * This program is distributed in the hope that it will be useful,
 
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
24
 * GNU General Public License for more details.
 
25
 *
 
26
 * You should have received a copy of the GNU General Public License
 
27
 * along with this program; if not, write to the Free Software
 
28
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 
29
 */
 
30
#include <pj/timer.h>
 
31
#include <pj/pool.h>
 
32
#include <pj/os.h>
 
33
#include <pj/string.h>
 
34
#include <pj/assert.h>
 
35
#include <pj/errno.h>
 
36
#include <pj/lock.h>
 
37
#include <pj/log.h>
 
38
#include <pj/rand.h>
 
39
 
 
40
#define THIS_FILE       "timer.c"
 
41
 
 
42
#define HEAP_PARENT(X)  (X == 0 ? 0 : (((X) - 1) / 2))
 
43
#define HEAP_LEFT(X)    (((X)+(X))+1)
 
44
 
 
45
 
 
46
#define DEFAULT_MAX_TIMED_OUT_PER_POLL  (64)
 
47
 
 
48
 
 
49
/**
 
50
 * The implementation of timer heap.
 
51
 */
 
52
struct pj_timer_heap_t
 
53
{
 
54
    /** Pool from which the timer heap resize will get the storage from */
 
55
    pj_pool_t *pool;
 
56
 
 
57
    /** Maximum size of the heap. */
 
58
    pj_size_t max_size;
 
59
 
 
60
    /** Current size of the heap. */
 
61
    pj_size_t cur_size;
 
62
 
 
63
    /** Max timed out entries to process per poll. */
 
64
    unsigned max_entries_per_poll;
 
65
 
 
66
    /** Lock object. */
 
67
    pj_lock_t *lock;
 
68
 
 
69
    /** Autodelete lock. */
 
70
    pj_bool_t auto_delete_lock;
 
71
 
 
72
    /**
 
73
     * Current contents of the Heap, which is organized as a "heap" of
 
74
     * pj_timer_entry *'s.  In this context, a heap is a "partially
 
75
     * ordered, almost complete" binary tree, which is stored in an
 
76
     * array.
 
77
     */
 
78
    pj_timer_entry **heap;
 
79
 
 
80
    /**
 
81
     * An array of "pointers" that allows each pj_timer_entry in the
 
82
     * <heap_> to be located in O(1) time.  Basically, <timer_id_[i]>
 
83
     * contains the slot in the <heap_> array where an pj_timer_entry
 
84
     * with timer id <i> resides.  Thus, the timer id passed back from
 
85
     * <schedule_entry> is really an slot into the <timer_ids> array.  The
 
86
     * <timer_ids_> array serves two purposes: negative values are
 
87
     * treated as "pointers" for the <freelist_>, whereas positive
 
88
     * values are treated as "pointers" into the <heap_> array.
 
89
     */
 
90
    pj_timer_id_t *timer_ids;
 
91
 
 
92
    /**
 
93
     * "Pointer" to the first element in the freelist contained within
 
94
     * the <timer_ids_> array, which is organized as a stack.
 
95
     */
 
96
    pj_timer_id_t timer_ids_freelist;
 
97
 
 
98
    /** Callback to be called when a timer expires. */
 
99
    pj_timer_heap_callback *callback;
 
100
 
 
101
};
 
102
 
 
103
 
 
104
 
 
105
PJ_INLINE(void) lock_timer_heap( pj_timer_heap_t *ht )
 
106
{
 
107
    if (ht->lock) {
 
108
        pj_lock_acquire(ht->lock);
 
109
    }
 
110
}
 
111
 
 
112
PJ_INLINE(void) unlock_timer_heap( pj_timer_heap_t *ht )
 
113
{
 
114
    if (ht->lock) {
 
115
        pj_lock_release(ht->lock);
 
116
    }
 
117
}
 
118
 
 
119
 
 
120
static void copy_node( pj_timer_heap_t *ht, int slot, pj_timer_entry *moved_node )
 
121
{
 
122
    PJ_CHECK_STACK();
 
123
 
 
124
    // Insert <moved_node> into its new location in the heap.
 
125
    ht->heap[slot] = moved_node;
 
126
    
 
127
    // Update the corresponding slot in the parallel <timer_ids_> array.
 
128
    ht->timer_ids[moved_node->_timer_id] = slot;
 
129
}
 
130
 
 
131
static pj_timer_id_t pop_freelist( pj_timer_heap_t *ht )
 
132
{
 
133
    // We need to truncate this to <int> for backwards compatibility.
 
134
    pj_timer_id_t new_id = ht->timer_ids_freelist;
 
135
    
 
136
    PJ_CHECK_STACK();
 
137
 
 
138
    // The freelist values in the <timer_ids_> are negative, so we need
 
139
    // to negate them to get the next freelist "pointer."
 
140
    ht->timer_ids_freelist =
 
141
        -ht->timer_ids[ht->timer_ids_freelist];
 
142
    
 
143
    return new_id;
 
144
    
 
145
}
 
146
 
 
147
static void push_freelist (pj_timer_heap_t *ht, pj_timer_id_t old_id)
 
148
{
 
149
    PJ_CHECK_STACK();
 
150
 
 
151
    // The freelist values in the <timer_ids_> are negative, so we need
 
152
    // to negate them to get the next freelist "pointer."
 
153
    ht->timer_ids[old_id] = -ht->timer_ids_freelist;
 
154
    ht->timer_ids_freelist = old_id;
 
155
}
 
156
 
 
157
 
 
158
static void reheap_down(pj_timer_heap_t *ht, pj_timer_entry *moved_node,
 
159
                        size_t slot, size_t child)
 
160
{
 
161
    PJ_CHECK_STACK();
 
162
 
 
163
    // Restore the heap property after a deletion.
 
164
    
 
165
    while (child < ht->cur_size)
 
166
    {
 
167
        // Choose the smaller of the two children.
 
168
        if (child + 1 < ht->cur_size
 
169
            && PJ_TIME_VAL_LT(ht->heap[child + 1]->_timer_value, ht->heap[child]->_timer_value))
 
170
            child++;
 
171
        
 
172
        // Perform a <copy> if the child has a larger timeout value than
 
173
        // the <moved_node>.
 
174
        if (PJ_TIME_VAL_LT(ht->heap[child]->_timer_value, moved_node->_timer_value))
 
175
        {
 
176
            copy_node( ht, slot, ht->heap[child]);
 
177
            slot = child;
 
178
            child = HEAP_LEFT(child);
 
179
        }
 
180
        else
 
181
            // We've found our location in the heap.
 
182
            break;
 
183
    }
 
184
    
 
185
    copy_node( ht, slot, moved_node);
 
186
}
 
187
 
 
188
static void reheap_up( pj_timer_heap_t *ht, pj_timer_entry *moved_node,
 
189
                       size_t slot, size_t parent)
 
190
{
 
191
    // Restore the heap property after an insertion.
 
192
    
 
193
    while (slot > 0)
 
194
    {
 
195
        // If the parent node is greater than the <moved_node> we need
 
196
        // to copy it down.
 
197
        if (PJ_TIME_VAL_LT(moved_node->_timer_value, ht->heap[parent]->_timer_value))
 
198
        {
 
199
            copy_node(ht, slot, ht->heap[parent]);
 
200
            slot = parent;
 
201
            parent = HEAP_PARENT(slot);
 
202
        }
 
203
        else
 
204
            break;
 
205
    }
 
206
    
 
207
    // Insert the new node into its proper resting place in the heap and
 
208
    // update the corresponding slot in the parallel <timer_ids> array.
 
209
    copy_node(ht, slot, moved_node);
 
210
}
 
211
 
 
212
 
 
213
static pj_timer_entry * remove_node( pj_timer_heap_t *ht, size_t slot)
 
214
{
 
215
    pj_timer_entry *removed_node = ht->heap[slot];
 
216
    
 
217
    // Return this timer id to the freelist.
 
218
    push_freelist( ht, removed_node->_timer_id );
 
219
    
 
220
    // Decrement the size of the heap by one since we're removing the
 
221
    // "slot"th node.
 
222
    ht->cur_size--;
 
223
    
 
224
    // Set the ID
 
225
    removed_node->_timer_id = -1;
 
226
 
 
227
    // Only try to reheapify if we're not deleting the last entry.
 
228
    
 
229
    if (slot < ht->cur_size)
 
230
    {
 
231
        int parent;
 
232
        pj_timer_entry *moved_node = ht->heap[ht->cur_size];
 
233
        
 
234
        // Move the end node to the location being removed and update
 
235
        // the corresponding slot in the parallel <timer_ids> array.
 
236
        copy_node( ht, slot, moved_node);
 
237
        
 
238
        // If the <moved_node->time_value_> is great than or equal its
 
239
        // parent it needs be moved down the heap.
 
240
        parent = HEAP_PARENT (slot);
 
241
        
 
242
        if (PJ_TIME_VAL_GTE(moved_node->_timer_value, ht->heap[parent]->_timer_value))
 
243
            reheap_down( ht, moved_node, slot, HEAP_LEFT(slot));
 
244
        else
 
245
            reheap_up( ht, moved_node, slot, parent);
 
246
    }
 
247
    
 
248
    return removed_node;
 
249
}
 
250
 
 
251
static void grow_heap(pj_timer_heap_t *ht)
 
252
{
 
253
    // All the containers will double in size from max_size_
 
254
    size_t new_size = ht->max_size * 2;
 
255
    pj_timer_id_t *new_timer_ids;
 
256
    pj_size_t i;
 
257
    
 
258
    // First grow the heap itself.
 
259
    
 
260
    pj_timer_entry **new_heap = 0;
 
261
    
 
262
    new_heap = (pj_timer_entry**) 
 
263
               pj_pool_alloc(ht->pool, sizeof(pj_timer_entry*) * new_size);
 
264
    memcpy(new_heap, ht->heap, ht->max_size * sizeof(pj_timer_entry*));
 
265
    //delete [] this->heap_;
 
266
    ht->heap = new_heap;
 
267
    
 
268
    // Grow the array of timer ids.
 
269
    
 
270
    new_timer_ids = 0;
 
271
    new_timer_ids = (pj_timer_id_t*)
 
272
                    pj_pool_alloc(ht->pool, new_size * sizeof(pj_timer_id_t));
 
273
    
 
274
    memcpy( new_timer_ids, ht->timer_ids, ht->max_size * sizeof(pj_timer_id_t));
 
275
    
 
276
    //delete [] timer_ids_;
 
277
    ht->timer_ids = new_timer_ids;
 
278
    
 
279
    // And add the new elements to the end of the "freelist".
 
280
    for (i = ht->max_size; i < new_size; i++)
 
281
        ht->timer_ids[i] = -((pj_timer_id_t) (i + 1));
 
282
    
 
283
    ht->max_size = new_size;
 
284
}
 
285
 
 
286
static void insert_node(pj_timer_heap_t *ht, pj_timer_entry *new_node)
 
287
{
 
288
    if (ht->cur_size + 2 >= ht->max_size)
 
289
        grow_heap(ht);
 
290
    
 
291
    reheap_up( ht, new_node, ht->cur_size, HEAP_PARENT(ht->cur_size));
 
292
    ht->cur_size++;
 
293
}
 
294
 
 
295
 
 
296
static pj_status_t schedule_entry( pj_timer_heap_t *ht,
 
297
                                   pj_timer_entry *entry, 
 
298
                                   const pj_time_val *future_time )
 
299
{
 
300
    if (ht->cur_size < ht->max_size)
 
301
    {
 
302
        // Obtain the next unique sequence number.
 
303
        // Set the entry
 
304
        entry->_timer_id = pop_freelist(ht);
 
305
        entry->_timer_value = *future_time;
 
306
        insert_node( ht, entry);
 
307
        return 0;
 
308
    }
 
309
    else
 
310
        return -1;
 
311
}
 
312
 
 
313
 
 
314
static int cancel( pj_timer_heap_t *ht, 
 
315
                   pj_timer_entry *entry, 
 
316
                   int dont_call)
 
317
{
 
318
  long timer_node_slot;
 
319
 
 
320
  PJ_CHECK_STACK();
 
321
 
 
322
  // Check to see if the timer_id is out of range
 
323
  if (entry->_timer_id < 0 || (pj_size_t)entry->_timer_id > ht->max_size)
 
324
    return 0;
 
325
 
 
326
  timer_node_slot = ht->timer_ids[entry->_timer_id];
 
327
 
 
328
  if (timer_node_slot < 0) // Check to see if timer_id is still valid.
 
329
    return 0;
 
330
 
 
331
  if (entry != ht->heap[timer_node_slot])
 
332
    {
 
333
      pj_assert(entry == ht->heap[timer_node_slot]);
 
334
      return 0;
 
335
    }
 
336
  else
 
337
    {
 
338
      remove_node( ht, timer_node_slot);
 
339
 
 
340
      if (dont_call == 0)
 
341
        // Call the close hook.
 
342
        (*ht->callback)(ht, entry);
 
343
      return 1;
 
344
    }
 
345
}
 
346
 
 
347
 
 
348
/*
 
349
 * Calculate memory size required to create a timer heap.
 
350
 */
 
351
PJ_DEF(pj_size_t) pj_timer_heap_mem_size(pj_size_t count)
 
352
{
 
353
    return /* size of the timer heap itself: */
 
354
           sizeof(pj_timer_heap_t) + 
 
355
           /* size of each entry: */
 
356
           (count+2) * (sizeof(pj_timer_entry*)+sizeof(pj_timer_id_t)) +
 
357
           /* lock, pool etc: */
 
358
           132;
 
359
}
 
360
 
 
361
/*
 
362
 * Create a new timer heap.
 
363
 */
 
364
PJ_DEF(pj_status_t) pj_timer_heap_create( pj_pool_t *pool,
 
365
                                          pj_size_t size,
 
366
                                          pj_timer_heap_t **p_heap)
 
367
{
 
368
    pj_timer_heap_t *ht;
 
369
    pj_size_t i;
 
370
 
 
371
    PJ_ASSERT_RETURN(pool && p_heap, PJ_EINVAL);
 
372
 
 
373
    *p_heap = NULL;
 
374
 
 
375
    /* Magic? */
 
376
    size += 2;
 
377
 
 
378
    /* Allocate timer heap data structure from the pool */
 
379
    ht = PJ_POOL_ALLOC_T(pool, pj_timer_heap_t);
 
380
    if (!ht)
 
381
        return PJ_ENOMEM;
 
382
 
 
383
    /* Initialize timer heap sizes */
 
384
    ht->max_size = size;
 
385
    ht->cur_size = 0;
 
386
    ht->max_entries_per_poll = DEFAULT_MAX_TIMED_OUT_PER_POLL;
 
387
    ht->timer_ids_freelist = 1;
 
388
    ht->pool = pool;
 
389
 
 
390
    /* Lock. */
 
391
    ht->lock = NULL;
 
392
    ht->auto_delete_lock = 0;
 
393
 
 
394
    // Create the heap array.
 
395
    ht->heap = (pj_timer_entry**)
 
396
               pj_pool_alloc(pool, sizeof(pj_timer_entry*) * size);
 
397
    if (!ht->heap)
 
398
        return PJ_ENOMEM;
 
399
 
 
400
    // Create the parallel
 
401
    ht->timer_ids = (pj_timer_id_t *)
 
402
                    pj_pool_alloc( pool, sizeof(pj_timer_id_t) * size);
 
403
    if (!ht->timer_ids)
 
404
        return PJ_ENOMEM;
 
405
 
 
406
    // Initialize the "freelist," which uses negative values to
 
407
    // distinguish freelist elements from "pointers" into the <heap_>
 
408
    // array.
 
409
    for (i=0; i<size; ++i)
 
410
        ht->timer_ids[i] = -((pj_timer_id_t) (i + 1));
 
411
 
 
412
    *p_heap = ht;
 
413
    return PJ_SUCCESS;
 
414
}
 
415
 
 
416
PJ_DEF(void) pj_timer_heap_destroy( pj_timer_heap_t *ht )
 
417
{
 
418
    if (ht->lock && ht->auto_delete_lock) {
 
419
        pj_lock_destroy(ht->lock);
 
420
        ht->lock = NULL;
 
421
    }
 
422
}
 
423
 
 
424
PJ_DEF(void) pj_timer_heap_set_lock(  pj_timer_heap_t *ht,
 
425
                                      pj_lock_t *lock,
 
426
                                      pj_bool_t auto_del )
 
427
{
 
428
    if (ht->lock && ht->auto_delete_lock)
 
429
        pj_lock_destroy(ht->lock);
 
430
 
 
431
    ht->lock = lock;
 
432
    ht->auto_delete_lock = auto_del;
 
433
}
 
434
 
 
435
 
 
436
PJ_DEF(unsigned) pj_timer_heap_set_max_timed_out_per_poll(pj_timer_heap_t *ht,
 
437
                                                          unsigned count )
 
438
{
 
439
    unsigned old_count = ht->max_entries_per_poll;
 
440
    ht->max_entries_per_poll = count;
 
441
    return old_count;
 
442
}
 
443
 
 
444
PJ_DEF(pj_timer_entry*) pj_timer_entry_init( pj_timer_entry *entry,
 
445
                                             int id,
 
446
                                             void *user_data,
 
447
                                             pj_timer_heap_callback *cb )
 
448
{
 
449
    pj_assert(entry && cb);
 
450
 
 
451
    entry->_timer_id = -1;
 
452
    entry->id = id;
 
453
    entry->user_data = user_data;
 
454
    entry->cb = cb;
 
455
    entry->_grp_lock = NULL;
 
456
 
 
457
    return entry;
 
458
}
 
459
 
 
460
#if PJ_TIMER_DEBUG
 
461
static pj_status_t schedule_w_grp_lock_dbg(pj_timer_heap_t *ht,
 
462
                                           pj_timer_entry *entry,
 
463
                                           const pj_time_val *delay,
 
464
                                           pj_bool_t set_id,
 
465
                                           int id_val,
 
466
                                           pj_grp_lock_t *grp_lock,
 
467
                                           const char *src_file,
 
468
                                           int src_line)
 
469
#else
 
470
static pj_status_t schedule_w_grp_lock(pj_timer_heap_t *ht,
 
471
                                       pj_timer_entry *entry,
 
472
                                       const pj_time_val *delay,
 
473
                                       pj_bool_t set_id,
 
474
                                       int id_val,
 
475
                                       pj_grp_lock_t *grp_lock)
 
476
#endif
 
477
{
 
478
    pj_status_t status;
 
479
    pj_time_val expires;
 
480
 
 
481
    PJ_ASSERT_RETURN(ht && entry && delay, PJ_EINVAL);
 
482
    PJ_ASSERT_RETURN(entry->cb != NULL, PJ_EINVAL);
 
483
 
 
484
    /* Prevent same entry from being scheduled more than once */
 
485
    PJ_ASSERT_RETURN(entry->_timer_id < 1, PJ_EINVALIDOP);
 
486
 
 
487
#if PJ_TIMER_DEBUG
 
488
    entry->src_file = src_file;
 
489
    entry->src_line = src_line;
 
490
#endif
 
491
    pj_gettickcount(&expires);
 
492
    PJ_TIME_VAL_ADD(expires, *delay);
 
493
    
 
494
    lock_timer_heap(ht);
 
495
    status = schedule_entry(ht, entry, &expires);
 
496
    if (status == PJ_SUCCESS) {
 
497
        if (set_id)
 
498
            entry->id = id_val;
 
499
        entry->_grp_lock = grp_lock;
 
500
        if (entry->_grp_lock) {
 
501
            pj_grp_lock_add_ref(entry->_grp_lock);
 
502
        }
 
503
    }
 
504
    unlock_timer_heap(ht);
 
505
 
 
506
    return status;
 
507
}
 
508
 
 
509
 
 
510
#if PJ_TIMER_DEBUG
 
511
PJ_DEF(pj_status_t) pj_timer_heap_schedule_dbg( pj_timer_heap_t *ht,
 
512
                                                pj_timer_entry *entry,
 
513
                                                const pj_time_val *delay,
 
514
                                                const char *src_file,
 
515
                                                int src_line)
 
516
{
 
517
    return schedule_w_grp_lock_dbg(ht, entry, delay, PJ_FALSE, 1, NULL,
 
518
                                   src_file, src_line);
 
519
}
 
520
 
 
521
PJ_DEF(pj_status_t) pj_timer_heap_schedule_w_grp_lock_dbg(
 
522
                                                pj_timer_heap_t *ht,
 
523
                                                pj_timer_entry *entry,
 
524
                                                const pj_time_val *delay,
 
525
                                                int id_val,
 
526
                                                pj_grp_lock_t *grp_lock,
 
527
                                                const char *src_file,
 
528
                                                int src_line)
 
529
{
 
530
    return schedule_w_grp_lock_dbg(ht, entry, delay, PJ_TRUE, id_val,
 
531
                                   grp_lock, src_file, src_line);
 
532
}
 
533
 
 
534
#else
 
535
PJ_DEF(pj_status_t) pj_timer_heap_schedule( pj_timer_heap_t *ht,
 
536
                                            pj_timer_entry *entry,
 
537
                                            const pj_time_val *delay)
 
538
{
 
539
    return schedule_w_grp_lock(ht, entry, delay, PJ_FALSE, 1, NULL);
 
540
}
 
541
 
 
542
PJ_DEF(pj_status_t) pj_timer_heap_schedule_w_grp_lock(pj_timer_heap_t *ht,
 
543
                                                      pj_timer_entry *entry,
 
544
                                                      const pj_time_val *delay,
 
545
                                                      int id_val,
 
546
                                                      pj_grp_lock_t *grp_lock)
 
547
{
 
548
    return schedule_w_grp_lock(ht, entry, delay, PJ_TRUE, id_val, grp_lock);
 
549
}
 
550
#endif
 
551
 
 
552
static int cancel_timer(pj_timer_heap_t *ht,
 
553
                        pj_timer_entry *entry,
 
554
                        pj_bool_t set_id,
 
555
                        int id_val)
 
556
{
 
557
    int count;
 
558
 
 
559
    PJ_ASSERT_RETURN(ht && entry, PJ_EINVAL);
 
560
 
 
561
    lock_timer_heap(ht);
 
562
    count = cancel(ht, entry, 1);
 
563
    if (set_id) {
 
564
        entry->id = id_val;
 
565
    }
 
566
    if (entry->_grp_lock) {
 
567
        pj_grp_lock_t *grp_lock = entry->_grp_lock;
 
568
        entry->_grp_lock = NULL;
 
569
        pj_grp_lock_dec_ref(grp_lock);
 
570
    }
 
571
    unlock_timer_heap(ht);
 
572
 
 
573
    return count;
 
574
}
 
575
 
 
576
PJ_DEF(int) pj_timer_heap_cancel( pj_timer_heap_t *ht,
 
577
                                  pj_timer_entry *entry)
 
578
{
 
579
    return cancel_timer(ht, entry, PJ_FALSE, 0);
 
580
}
 
581
 
 
582
PJ_DEF(int) pj_timer_heap_cancel_if_active(pj_timer_heap_t *ht,
 
583
                                           pj_timer_entry *entry,
 
584
                                           int id_val)
 
585
{
 
586
    return cancel_timer(ht, entry, PJ_TRUE, id_val);
 
587
}
 
588
 
 
589
PJ_DEF(unsigned) pj_timer_heap_poll( pj_timer_heap_t *ht, 
 
590
                                     pj_time_val *next_delay )
 
591
{
 
592
    pj_time_val now;
 
593
    unsigned count;
 
594
 
 
595
    PJ_ASSERT_RETURN(ht, 0);
 
596
 
 
597
    lock_timer_heap(ht);
 
598
    if (!ht->cur_size && next_delay) {
 
599
        next_delay->sec = next_delay->msec = PJ_MAXINT32;
 
600
        unlock_timer_heap(ht);
 
601
        return 0;
 
602
    }
 
603
 
 
604
    count = 0;
 
605
    pj_gettickcount(&now);
 
606
 
 
607
    while ( ht->cur_size && 
 
608
            PJ_TIME_VAL_LTE(ht->heap[0]->_timer_value, now) &&
 
609
            count < ht->max_entries_per_poll ) 
 
610
    {
 
611
        pj_timer_entry *node = remove_node(ht, 0);
 
612
        pj_grp_lock_t *grp_lock;
 
613
 
 
614
        ++count;
 
615
 
 
616
        grp_lock = node->_grp_lock;
 
617
        node->_grp_lock = NULL;
 
618
 
 
619
        unlock_timer_heap(ht);
 
620
 
 
621
        PJ_RACE_ME(5);
 
622
 
 
623
        if (node->cb)
 
624
            (*node->cb)(ht, node);
 
625
 
 
626
        if (grp_lock)
 
627
            pj_grp_lock_dec_ref(grp_lock);
 
628
 
 
629
        lock_timer_heap(ht);
 
630
    }
 
631
    if (ht->cur_size && next_delay) {
 
632
        *next_delay = ht->heap[0]->_timer_value;
 
633
        PJ_TIME_VAL_SUB(*next_delay, now);
 
634
        if (next_delay->sec < 0 || next_delay->msec < 0)
 
635
            next_delay->sec = next_delay->msec = 0;
 
636
    } else if (next_delay) {
 
637
        next_delay->sec = next_delay->msec = PJ_MAXINT32;
 
638
    }
 
639
    unlock_timer_heap(ht);
 
640
 
 
641
    return count;
 
642
}
 
643
 
 
644
PJ_DEF(pj_size_t) pj_timer_heap_count( pj_timer_heap_t *ht )
 
645
{
 
646
    PJ_ASSERT_RETURN(ht, 0);
 
647
 
 
648
    return ht->cur_size;
 
649
}
 
650
 
 
651
PJ_DEF(pj_status_t) pj_timer_heap_earliest_time( pj_timer_heap_t * ht,
 
652
                                                 pj_time_val *timeval)
 
653
{
 
654
    pj_assert(ht->cur_size != 0);
 
655
    if (ht->cur_size == 0)
 
656
        return PJ_ENOTFOUND;
 
657
 
 
658
    lock_timer_heap(ht);
 
659
    *timeval = ht->heap[0]->_timer_value;
 
660
    unlock_timer_heap(ht);
 
661
 
 
662
    return PJ_SUCCESS;
 
663
}
 
664
 
 
665
#if PJ_TIMER_DEBUG
 
666
PJ_DEF(void) pj_timer_heap_dump(pj_timer_heap_t *ht)
 
667
{
 
668
    lock_timer_heap(ht);
 
669
 
 
670
    PJ_LOG(3,(THIS_FILE, "Dumping timer heap:"));
 
671
    PJ_LOG(3,(THIS_FILE, "  Cur size: %d entries, max: %d",
 
672
                         (int)ht->cur_size, (int)ht->max_size));
 
673
 
 
674
    if (ht->cur_size) {
 
675
        unsigned i;
 
676
        pj_time_val now;
 
677
 
 
678
        PJ_LOG(3,(THIS_FILE, "  Entries: "));
 
679
        PJ_LOG(3,(THIS_FILE, "    _id\tId\tElapsed\tSource"));
 
680
        PJ_LOG(3,(THIS_FILE, "    ----------------------------------"));
 
681
 
 
682
        pj_gettickcount(&now);
 
683
 
 
684
        for (i=0; i<(unsigned)ht->cur_size; ++i) {
 
685
            pj_timer_entry *e = ht->heap[i];
 
686
            pj_time_val delta;
 
687
 
 
688
            if (PJ_TIME_VAL_LTE(e->_timer_value, now))
 
689
                delta.sec = delta.msec = 0;
 
690
            else {
 
691
                delta = e->_timer_value;
 
692
                PJ_TIME_VAL_SUB(delta, now);
 
693
            }
 
694
 
 
695
            PJ_LOG(3,(THIS_FILE, "    %d\t%d\t%d.%03d\t%s:%d",
 
696
                      e->_timer_id, e->id,
 
697
                      (int)delta.sec, (int)delta.msec,
 
698
                      e->src_file, e->src_line));
 
699
        }
 
700
    }
 
701
 
 
702
    unlock_timer_heap(ht);
 
703
}
 
704
#endif
 
705