nas_timer.c 35.3 KB
Newer Older
gauthier's avatar
gauthier committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*******************************************************************************
    OpenAirInterface
    Copyright(c) 1999 - 2014 Eurecom

    OpenAirInterface is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.


    OpenAirInterface is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
Cedric Roux's avatar
Cedric Roux committed
15

gauthier's avatar
gauthier committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
    You should have received a copy of the GNU General Public License
    along with OpenAirInterface.The full GNU General Public License is
   included in this distribution in the file called "COPYING". If not,
   see <http://www.gnu.org/licenses/>.

  Contact Information
  OpenAirInterface Admin: openair_admin@eurecom.fr
  OpenAirInterface Tech : openair_tech@eurecom.fr
  OpenAirInterface Dev  : openair4g-devel@eurecom.fr

  Address      : Eurecom, Compus SophiaTech 450, route des chappes, 06451 Biot, France.

 *******************************************************************************/
/*****************************************************************************
Source      nas_timer.c
Cedric Roux's avatar
Cedric Roux committed
31

32
Version     0.1
Cedric Roux's avatar
Cedric Roux committed
33

34
Date        2012/10/09
Cedric Roux's avatar
Cedric Roux committed
35

36
Product     NAS stack
Cedric Roux's avatar
Cedric Roux committed
37

38
Subsystem   Utilities
Cedric Roux's avatar
Cedric Roux committed
39

40
Author      Frederic Maurel
Cedric Roux's avatar
Cedric Roux committed
41

42
Description Timer utilities
Cedric Roux's avatar
Cedric Roux committed
43 44 45

*****************************************************************************/

46 47 48 49 50 51
#include <pthread.h>
#include <assert.h>
#include <stdint.h>

#include <string.h> // memset
#include <stdlib.h> // malloc, free
52 53
#include <sys/time.h>   // setitimer

54
#if defined(ENABLE_ITTI)
55 56
# include "intertask_interface.h"
# include "timer.h"
57 58 59
#else
# include <signal.h>
# include <time.h>   // clock_gettime
Cedric Roux's avatar
Cedric Roux committed
60 61 62
#endif
#include "nas_timer.h"
#include "commonDef.h"
63
#include "msc.h"
Cedric Roux's avatar
Cedric Roux committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

/****************************************************************************/
/****************  E X T E R N A L    D E F I N I T I O N S  ****************/
/****************************************************************************/

/****************************************************************************/
/*******************  L O C A L    D E F I N I T I O N S  *******************/
/****************************************************************************/

/* Structure of an interval timer entry
 * ------------------------------------
 * The callback function is scheduled to be executed upon expiration of
 * the timer that has been previously setup to the initial interval time
 * value when the timer entry was allocated.
 */
typedef struct {
80
#if defined(ENABLE_ITTI)
81
  long timer_id;          /* Timer id returned by the timer API from ITTI */
82
#else
83
  pthread_t pid;          /* Thread identifier of the callback    */
84 85
#endif

86 87
  struct timeval itv;     /* Initial interval timer value         */
  struct timeval tv;      /* Interval timer value                 */
88

89 90
  nas_timer_callback_t cb;    /* Callback executed at timer expiration */
  void *args;                 /* Callback argument parameters          */
Cedric Roux's avatar
Cedric Roux committed
91 92 93 94 95 96 97 98 99
} nas_timer_entry_t;

/* Structure of a timer queue - list of active interval timer entries
 * ------------------------------------------------------------------
 * At any time, the first entry of the queue is always associated to the
 * first timer that will come to expire. Upon timer expiration, the first
 * entry is removed from the queue and freed.
 */
typedef struct _nas_timer_queue_t {
100 101 102 103
  int id;         /* Identifier of the current timer entry */
  nas_timer_entry_t *entry;   /* The current timer entry       */
  struct _nas_timer_queue_t *prev;/* The previous timer entry in the queue */
  struct _nas_timer_queue_t *next;/* The next timer entry in the queue     */
Cedric Roux's avatar
Cedric Roux committed
104 105 106 107 108 109 110 111
} timer_queue_t;

/* Structure of a timer database
 * -----------------------------
 * The timer database is managed to provide unique identifier to timer at
 * startup and to maintain an ordered queue of active timer entries.
 */
typedef struct {
112
  int timer_id;   /* Identifier of the first available timer entry */
113
#define TIMER_DATABASE_SIZE 256
114 115
  timer_queue_t tq[TIMER_DATABASE_SIZE];
  timer_queue_t *head;/* Pointer to the first timer entry to be fired  */
116

117
#if !defined(ENABLE_ITTI)
118
  pthread_mutex_t mutex;
119
#endif
Cedric Roux's avatar
Cedric Roux committed
120 121 122 123 124
} nas_timer_database_t;

/*
 * The timer database
 */
125
static nas_timer_database_t _nas_timer_db = {
126 127 128
  0,
  {},
  NULL
129
#if !defined(ENABLE_ITTI)
130
  , PTHREAD_MUTEX_INITIALIZER
131 132 133
#endif
};

134
#if defined(ENABLE_ITTI)
135 136 137 138 139 140
#define nas_timer_lock_db()
#define nas_timer_unlock_db()
#else
#define nas_timer_lock_db()     pthread_mutex_lock(&_nas_timer_db.mutex)
#define nas_timer_unlock_db()   pthread_mutex_unlock(&_nas_timer_db.mutex)
#endif
Cedric Roux's avatar
Cedric Roux committed
141 142 143 144

/*
 * The handler executed whenever the system timer expires
 */
145
#if !defined(ENABLE_ITTI)
Cedric Roux's avatar
Cedric Roux committed
146
static void _nas_timer_handler(int signal);
147
#endif
Cedric Roux's avatar
Cedric Roux committed
148 149 150

/*
 * -----------------------------------------------------------------------------
151
 *      Functions used to manage the timer database
Cedric Roux's avatar
Cedric Roux committed
152 153 154 155 156 157
 * -----------------------------------------------------------------------------
 */
static void _nas_timer_db_init(void);

static int _nas_timer_db_get_id(void);
static int _nas_timer_db_is_active(int id);
158
static nas_timer_entry_t *_nas_timer_db_create_entry(long sec,
159
    nas_timer_callback_t cb, void *args);
Cedric Roux's avatar
Cedric Roux committed
160 161
static void _nas_timer_db_delete_entry(int id);

162 163
static void _nas_timer_db_insert_entry(int id, nas_timer_entry_t *te);
static int _nas_timer_db_insert(timer_queue_t *entry);
Cedric Roux's avatar
Cedric Roux committed
164

165 166
static nas_timer_entry_t *_nas_timer_db_remove_entry(int id);
static int _nas_timer_db_remove(timer_queue_t *entry);
Cedric Roux's avatar
Cedric Roux committed
167 168 169

/*
 * -----------------------------------------------------------------------------
170
 *      Operator functions for timeval structures
Cedric Roux's avatar
Cedric Roux committed
171 172
 * -----------------------------------------------------------------------------
 */
173 174 175 176 177
static int _nas_timer_cmp(const struct timeval *a, const struct timeval *b);
static void _nas_timer_add(const struct timeval *a, const struct timeval *b,
                           struct timeval *result);
static int _nas_timer_sub(const struct timeval *a, const struct timeval *b,
                          struct timeval *result);
Cedric Roux's avatar
Cedric Roux committed
178 179 180 181 182 183 184

/****************************************************************************/
/******************  E X P O R T E D    F U N C T I O N S  ******************/
/****************************************************************************/

/****************************************************************************
 **                                                                        **
185
 ** Name:    timer_init()                                              **
Cedric Roux's avatar
Cedric Roux committed
186 187 188
 **                                                                        **
 ** Description: Initializes internal data used to manage timers           **
 **                                                                        **
189 190
 ** Inputs:  None                                                      **
 **      Others:    None                                       **
Cedric Roux's avatar
Cedric Roux committed
191
 **                                                                        **
192 193 194
 ** Outputs:     None                                                      **
 **      Return:    RETURNok, RETURNerror                      **
 **      Others:    _nas_timer_db                              **
Cedric Roux's avatar
Cedric Roux committed
195 196 197 198
 **                                                                        **
 ***************************************************************************/
int nas_timer_init(void)
{
199 200
  /* Initialize the timer database */
  _nas_timer_db_init();
Cedric Roux's avatar
Cedric Roux committed
201

202
#if !defined(ENABLE_ITTI)
203 204 205 206 207 208 209 210 211 212 213
  /* Setup the timer database handler */
  struct sigaction act;

  (void) memset (&act, 0, sizeof (act));
  (void) sigfillset (&act.sa_mask);
  (void) sigdelset (&act.sa_mask, SIGHUP);
  (void) sigdelset (&act.sa_mask, SIGINT);
  (void) sigdelset (&act.sa_mask, SIGTERM);
  (void) sigdelset (&act.sa_mask, SIGILL);
  (void) sigdelset (&act.sa_mask, SIGTRAP);
  (void) sigdelset (&act.sa_mask, SIGIOT);
214
# ifndef LINUX
215
  (void) sigdelset (&act.sa_mask, SIGEMT);
216
# endif
217 218 219 220 221 222 223 224 225 226 227
  (void) sigdelset (&act.sa_mask, SIGFPE);
  (void) sigdelset (&act.sa_mask, SIGBUS);
  (void) sigdelset (&act.sa_mask, SIGSEGV);
  (void) sigdelset (&act.sa_mask, SIGSYS);

  act.sa_handler = _nas_timer_handler;

  if ( sigaction (SIGALRM, &act, 0) < 0 ) {
    return (RETURNerror);
  }

228
#endif
229
  return (RETURNok);
Cedric Roux's avatar
Cedric Roux committed
230 231 232 233
}

/****************************************************************************
 **                                                                        **
234
 ** Name:    timer_start()                                             **
Cedric Roux's avatar
Cedric Roux committed
235 236
 **                                                                        **
 ** Description: Schedules the execution of the given callback function    **
237
 **      upon expiration of the specified time interval            **
Cedric Roux's avatar
Cedric Roux committed
238
 **                                                                        **
239 240 241 242
 ** Inputs:  sec:       The value of the time interval in seconds  **
 **      cb:        Function executed upon timer expiration    **
 **      args:      Callback argument parameters               **
 **      Others:    None                                       **
Cedric Roux's avatar
Cedric Roux committed
243
 **                                                                        **
244 245 246 247
 ** Outputs:     None                                                      **
 **      Return:    The timer identifier when successfully     **
 **             started; NAS_TIMER_INACTIVE_ID otherwise.  **
 **      Others:    None                                       **
Cedric Roux's avatar
Cedric Roux committed
248 249
 **                                                                        **
 ***************************************************************************/
250
int nas_timer_start(long sec, nas_timer_callback_t cb, void *args)
Cedric Roux's avatar
Cedric Roux committed
251
{
252 253
  int id;
  nas_timer_entry_t *te;
254
#if defined(ENABLE_ITTI)
255 256
  int ret;
  long timer_id;
257 258
#endif

259 260 261 262
  /* Do not start null timer */
  if (sec == 0) {
    return (NAS_TIMER_INACTIVE_ID);
  }
Cedric Roux's avatar
Cedric Roux committed
263

264 265
  /* Get an identifier for the new timer entry */
  id = _nas_timer_db_get_id();
Cedric Roux's avatar
Cedric Roux committed
266

267 268 269 270
  if (id < 0) {
    /* No available timer entry found */
    return (NAS_TIMER_INACTIVE_ID);
  }
Cedric Roux's avatar
Cedric Roux committed
271

272 273 274 275 276 277 278 279 280
  /* Create a new timer entry */
  te = _nas_timer_db_create_entry(sec, cb, args);

  if (te == NULL) {
    return (NAS_TIMER_INACTIVE_ID);
  }

  /* Insert the new entry into the timer queue */
  _nas_timer_db_insert_entry(id, te);
281
#if defined(ENABLE_ITTI)
282
# if defined(NAS_BUILT_IN_EPC)
283
  ret = timer_setup(sec, 0, TASK_NAS_MME, INSTANCE_DEFAULT, TIMER_PERIODIC, args, &timer_id);
284
# else
285
  ret = timer_setup(sec, 0, TASK_NAS_UE, INSTANCE_DEFAULT, TIMER_PERIODIC, args, &timer_id);
286
# endif
287

288 289 290 291 292
  if (ret == -1) {
    return NAS_TIMER_INACTIVE_ID;
  }

  te->timer_id = timer_id;
293 294
#endif

295
  return (id);
Cedric Roux's avatar
Cedric Roux committed
296 297 298 299
}

/****************************************************************************
 **                                                                        **
300
 ** Name:    timer_stop()                                              **
Cedric Roux's avatar
Cedric Roux committed
301 302 303
 **                                                                        **
 ** Description: Stop the timer with the specified identifier              **
 **                                                                        **
304 305
 ** Inputs:  id:        The identifier of the timer to be stopped  **
 **      Others:    None                                       **
Cedric Roux's avatar
Cedric Roux committed
306
 **                                                                        **
307 308 309 310
 ** Outputs:     None                                                      **
 **      Return:    NAS_TIMER_INACTIVE_ID when successfully stop-  **
 **             ped; The timer identifier otherwise.       **
 **      Others:    None                                       **
Cedric Roux's avatar
Cedric Roux committed
311 312 313 314
 **                                                                        **
 ***************************************************************************/
int nas_timer_stop(int id)
{
315 316 317 318 319
  /* Check if the timer entry is active */
  if (_nas_timer_db_is_active(id)) {
    nas_timer_entry_t *entry;
    /* Remove the entry from the timer queue */
    entry = _nas_timer_db_remove_entry(id);
320
#if defined(ENABLE_ITTI)
321
    timer_remove(entry->timer_id);
322
#else
323
    (void)entry;
324
#endif
325 326 327 328 329 330
    /* Delete the timer entry */
    _nas_timer_db_delete_entry(id);
    return (NAS_TIMER_INACTIVE_ID);
  }

  return (id);
Cedric Roux's avatar
Cedric Roux committed
331 332 333 334
}

/****************************************************************************
 **                                                                        **
335
 ** Name:    timer_restart()                                           **
Cedric Roux's avatar
Cedric Roux committed
336 337
 **                                                                        **
 ** Description: Restart the timer with the specified identifier. The ti-  **
338 339 340
 **      mer is scheduled to expire after the same period of time  **
 **      and will execute the callback function that has been set  **
 **      when it was started.                                      **
Cedric Roux's avatar
Cedric Roux committed
341
 **                                                                        **
342 343 344
 ** Inputs:  id:        The identifier of the timer to be started  **
 **             again                                      **
 **      Others:    None                                       **
Cedric Roux's avatar
Cedric Roux committed
345
 **                                                                        **
346 347 348 349
 ** Outputs:     None                                                      **
 **      Return:    The timer identifier when successfully     **
 **             re-started; NAS_TIMER_INACTIVE_ID otherwise.   **
 **      Others:    None                                       **
Cedric Roux's avatar
Cedric Roux committed
350 351 352 353
 **                                                                        **
 ***************************************************************************/
int nas_timer_restart(int id)
{
354 355 356 357 358 359 360 361 362 363 364 365
  /* Check if the timer entry is active */
  if (_nas_timer_db_is_active(id)) {
    /* Remove the entry from the timer queue */
    nas_timer_entry_t *te = _nas_timer_db_remove_entry(id);
    /* Initialize its interval timer value */
    te->tv = te->itv;
    /* Insert again the entry into the timer queue */
    _nas_timer_db_insert_entry(id, te);
    return (id);
  }

  return (NAS_TIMER_INACTIVE_ID);
Cedric Roux's avatar
Cedric Roux committed
366 367 368 369 370 371 372 373
}

/****************************************************************************/
/*********************  L O C A L    F U N C T I O N S  *********************/
/****************************************************************************/

/****************************************************************************
 **                                                                        **
374
 ** Name:    _nas_timer_handler()                                      **
Cedric Roux's avatar
Cedric Roux committed
375 376
 **                                                                        **
 ** Description: The timer handler is executed whenever the system deli-   **
377 378 379 380 381
 **      vers signal SIGALARM. It starts execution of the callback **
 **      function of the first entry within the queue of active    **
 **      timer entries. The entry is not removed from the queue of **
 **      active timer entries and shall be explicitly removed when **
 **      the timer expires.                                        **
Cedric Roux's avatar
Cedric Roux committed
382
 **                                                                        **
383 384
 ** Inputs:  None                                                      **
 **      Others:    None                                       **
Cedric Roux's avatar
Cedric Roux committed
385
 **                                                                        **
386 387 388
 ** Outputs:     None                                                      **
 **      Return:    None                                       **
 **      Others:    _nas_timer_db                              **
Cedric Roux's avatar
Cedric Roux committed
389 390
 **                                                                        **
 ***************************************************************************/
391
#if defined(ENABLE_ITTI)
392 393
void nas_timer_handle_signal_expiry(long timer_id, void *arg_p)
{
394 395
  /* Get the timer entry for which the system timer expired */
  nas_timer_entry_t *te = _nas_timer_db.head->entry;
396

397
  te->cb(te->args);
398 399
}
#else
Cedric Roux's avatar
Cedric Roux committed
400 401
static void _nas_timer_handler(int signal)
{
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
  /* At least one timer has been started */
  assert( (_nas_timer_db.head != NULL) && (_nas_timer_db.head->entry != NULL) );

  /* Get the timer entry for which the system timer expired */
  nas_timer_entry_t *te = _nas_timer_db.head->entry;

  /* Execute the callback function */
  pthread_attr_t attr;
  pthread_attr_init(&attr);
  pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
  int rc = pthread_create (&te->pid, &attr, te->cb, te->args);
  pthread_attr_destroy(&attr);

  /* Wait for the thread to terminate before releasing the timer entry */
  if (rc == 0) {
    void *result = NULL;
    (void) pthread_join(te->pid, &result);

    /* TODO: Check returned result ??? */
    if (result) {
      free(result);
Cedric Roux's avatar
Cedric Roux committed
424
    }
425
  }
Cedric Roux's avatar
Cedric Roux committed
426
}
427
#endif
Cedric Roux's avatar
Cedric Roux committed
428 429 430

/*
 * -----------------------------------------------------------------------------
431
 *      Functions used to manage the timer database
Cedric Roux's avatar
Cedric Roux committed
432 433 434 435
 * -----------------------------------------------------------------------------
 */
/****************************************************************************
 **                                                                        **
436
 ** Name:    _nas_timer_db_init()                                      **
Cedric Roux's avatar
Cedric Roux committed
437 438 439
 **                                                                        **
 ** Description: Initializes the timer database                            **
 **                                                                        **
440 441
 ** Inputs:  None                                                      **
 **      Others:    None                                       **
Cedric Roux's avatar
Cedric Roux committed
442
 **                                                                        **
443 444 445
 ** Outputs:     None                                                      **
 **      Return:    None                                       **
 **      Others:    _nas_timer_db                              **
Cedric Roux's avatar
Cedric Roux committed
446 447 448 449
 **                                                                        **
 ***************************************************************************/
static void _nas_timer_db_init(void)
{
450 451 452 453 454
  int i;

  for (i = 0; i < TIMER_DATABASE_SIZE; i++) {
    _nas_timer_db.tq[i].id = NAS_TIMER_INACTIVE_ID;
  }
Cedric Roux's avatar
Cedric Roux committed
455 456 457 458
}

/****************************************************************************
 **                                                                        **
459
 ** Name:    _nas_timer_db_get_id()                                    **
Cedric Roux's avatar
Cedric Roux committed
460 461
 **                                                                        **
 ** Description: Gets the identifier of the first available timer entry in **
462
 **      the queue of active timer entries                         **
Cedric Roux's avatar
Cedric Roux committed
463
 **                                                                        **
464 465
 ** Inputs:  None                                                      **
 **      Others:    _nas_timer_db                              **
Cedric Roux's avatar
Cedric Roux committed
466
 **                                                                        **
467 468 469 470 471
 ** Outputs:     None                                                      **
 **      Return:    The identifier of the first available      **
 **             timer entry if the queue of active timers  **
 **             is not full; -1 otherwise.                 **
 **      Others:    _nas_timer_db                              **
Cedric Roux's avatar
Cedric Roux committed
472 473 474 475
 **                                                                        **
 ***************************************************************************/
static int _nas_timer_db_get_id(void)
{
476 477 478 479 480 481 482
  int i;

  /* Search from the current timer entry to the last timer entry */
  for (i = _nas_timer_db.timer_id; i < TIMER_DATABASE_SIZE; i++) {
    if (_nas_timer_db.tq[i].id < 0 ) {
      _nas_timer_db.timer_id = i+1;
      return i;
Cedric Roux's avatar
Cedric Roux committed
483
    }
484 485 486 487 488 489 490
  }

  /* Search from the first timer entry to the current timer entry */
  for (i = 0; i < _nas_timer_db.timer_id; i++) {
    if (_nas_timer_db.tq[i].id < 0 ) {
      _nas_timer_db.timer_id = i+1;
      return i;
Cedric Roux's avatar
Cedric Roux committed
491
    }
492 493 494 495
  }

  /* No available timer entry found */
  return (-1);
Cedric Roux's avatar
Cedric Roux committed
496 497 498 499
}

/****************************************************************************
 **                                                                        **
500
 ** Name:    _nas_timer_db_is_active()                                 **
Cedric Roux's avatar
Cedric Roux committed
501 502
 **                                                                        **
 ** Description: Checks whether the entry with the given identifier is     **
503
 **      active within the queue of active timer entries           **
Cedric Roux's avatar
Cedric Roux committed
504
 **                                                                        **
505 506
 ** Inputs:  id:        Identifier of the timer entry to check     **
 **      Others:    _nas_timer_db                              **
Cedric Roux's avatar
Cedric Roux committed
507
 **                                                                        **
508 509 510 511
 ** Outputs:     None                                                      **
 **      Return:    TRUE if the timer entry is active; FALSE   **
 **             if it is not an active timer entry.        **
 **      Others:    None                                       **
Cedric Roux's avatar
Cedric Roux committed
512 513 514 515
 **                                                                        **
 ***************************************************************************/
static int _nas_timer_db_is_active(int id)
{
516 517
  return ( (id != NAS_TIMER_INACTIVE_ID) &&
           (_nas_timer_db.tq[id].id != NAS_TIMER_INACTIVE_ID) );
Cedric Roux's avatar
Cedric Roux committed
518 519 520 521
}

/****************************************************************************
 **                                                                        **
522
 ** Name:    _nas_timer_db_create_entry()                              **
Cedric Roux's avatar
Cedric Roux committed
523 524 525
 **                                                                        **
 ** Description: Creates a new timer entry                                 **
 **                                                                        **
526 527 528 529
 ** Inputs:  sec:       Time interval value                        **
 **      cb:        Function executed upon timer expiration    **
 **      args:      Callback argument parameters               **
 **      Others:    None                                       **
Cedric Roux's avatar
Cedric Roux committed
530
 **                                                                        **
531 532 533 534
 ** Outputs:     None                                                      **
 **      Return:    A pointer to the new timer entry if        **
 **             successfully allocated; NULL otherwise     **
 **      Others:    None                                       **
Cedric Roux's avatar
Cedric Roux committed
535 536
 **                                                                        **
 ***************************************************************************/
537
static nas_timer_entry_t *_nas_timer_db_create_entry(
538 539
  long sec, nas_timer_callback_t cb,
  void *args)
Cedric Roux's avatar
Cedric Roux committed
540
{
541 542 543 544 545 546 547 548 549 550 551
  nas_timer_entry_t *te = (nas_timer_entry_t *)malloc(sizeof(nas_timer_entry_t));

  if (te != NULL) {
    te->itv.tv_sec = sec;
    te->itv.tv_usec = 0;
    te->tv = te->itv;
    te->cb = cb;
    te->args = args;
  }

  return (te);
Cedric Roux's avatar
Cedric Roux committed
552 553 554 555
}

/****************************************************************************
 **                                                                        **
556
 ** Name:    _nas_timer_db_delete_entry()                              **
Cedric Roux's avatar
Cedric Roux committed
557 558
 **                                                                        **
 ** Description: Deletes the entry with the given identifier from the ti-  **
559
 **      mer database.                                             **
Cedric Roux's avatar
Cedric Roux committed
560
 **                                                                        **
561 562
 ** Inputs:  id:        Identifier of the entry to be deleted      **
 **      Others:    None                                       **
Cedric Roux's avatar
Cedric Roux committed
563
 **                                                                        **
564 565 566
 ** Outputs:     None                                                      **
 **      Return:    None                                       **
 **      Others:    _nas_timer_db                              **
Cedric Roux's avatar
Cedric Roux committed
567 568 569 570
 **                                                                        **
 ***************************************************************************/
static void _nas_timer_db_delete_entry(int id)
{
571 572
  /* The identifier of the timer is valid within the timer queue */
  assert(_nas_timer_db.tq[id].id == id);
Cedric Roux's avatar
Cedric Roux committed
573

574 575 576 577
  /* Delete the timer entry from the queue */
  _nas_timer_db.tq[id].id = NAS_TIMER_INACTIVE_ID;
  free(_nas_timer_db.tq[id].entry);
  _nas_timer_db.tq[id].entry = NULL;
Cedric Roux's avatar
Cedric Roux committed
578 579 580 581
}

/****************************************************************************
 **                                                                        **
582
 ** Name:    _nas_timer_db_insert_entry()                              **
Cedric Roux's avatar
Cedric Roux committed
583 584
 **                                                                        **
 ** Description: Inserts the entry with the given identifier into the      **
585 586 587
 **      queue of active timer entries and restarts the system     **
 **      timer if the new entry is the next entry for which the    **
 **      timer should be scheduled to expire.                      **
Cedric Roux's avatar
Cedric Roux committed
588
 **                                                                        **
589 590 591
 ** Inputs:  id:        Identifier of the new entry                **
 **      te:        Pointer to the entry to be inserted        **
 **      Others:    None                                       **
Cedric Roux's avatar
Cedric Roux committed
592
 **                                                                        **
593 594 595
 ** Outputs:     None                                                      **
 **      Return:    None                                       **
 **      Others:    _nas_timer_db                              **
Cedric Roux's avatar
Cedric Roux committed
596 597
 **                                                                        **
 ***************************************************************************/
598
static void _nas_timer_db_insert_entry(int id, nas_timer_entry_t *te)
Cedric Roux's avatar
Cedric Roux committed
599
{
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
  struct itimerval it;
  struct timespec  ts;
  struct timeval   current_time;
  int restart;

  /* Enqueue the new timer entry */
  _nas_timer_db.tq[id].id = id;
  _nas_timer_db.tq[id].entry = te;

  /* Save its interval timer value */
  it.it_interval.tv_sec = it.it_interval.tv_usec = 0;
  it.it_value = te->tv;

  /* Update its interval timer value */
  clock_gettime(CLOCK_MONOTONIC, &ts);
  current_time.tv_sec = ts.tv_sec;
  current_time.tv_usec = ts.tv_nsec/1000;
  /* tv = tv + time() */
  _nas_timer_add(&te->tv, &current_time, &te->tv);

  /* Insert the new timer entry into the list of active entries */
  nas_timer_lock_db();
  restart = _nas_timer_db_insert(&_nas_timer_db.tq[id]);
  nas_timer_unlock_db();
624

625
#if !defined(ENABLE_ITTI)
Cedric Roux's avatar
Cedric Roux committed
626

627 628 629 630 631
  if (restart) {
    /* The new entry is the first entry of the list;
     * restart the system timer */
    setitimer(ITIMER_REAL, &it, 0);
  }
Cedric Roux's avatar
Cedric Roux committed
632

633
#else
634
  (void)(restart);
635
#endif
Cedric Roux's avatar
Cedric Roux committed
636 637
}

638
static int _nas_timer_db_insert(timer_queue_t *entry)
Cedric Roux's avatar
Cedric Roux committed
639
{
640 641 642 643 644 645 646 647 648
  timer_queue_t *prev, *next; /* previous and next entry in the list  */

  /*
   * Search the list of timer entries for the first entry with an interval
   * timer value greater than the interval timer value of the new timer entry
   */
  for (prev = NULL, next = _nas_timer_db.head; next != NULL; next = next->next) {
    if (_nas_timer_cmp(&next->entry->tv, &entry->entry->tv) > 0) {
      break;
Cedric Roux's avatar
Cedric Roux committed
649
    }
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676

    prev = next;
  }

  /* Insert the new entry in the list of active timer entries */
  /* prev <-- entry --> next */
  entry->prev = prev;
  entry->next = next;

  /* Update the pointer from the previous entry */
  if (entry->next != NULL) {
    /* prev <-- entry <--> next */
    entry->next->prev = entry;
  }

  /* Update the pointer from the next entry */
  if (entry->prev != NULL) {
    /* prev <--> entry <--> next */
    entry->prev->next = entry;
  } else {
    /* The new entry is the first entry of the list */
    _nas_timer_db.head = entry;
    return TRUE;
  }

  /* The new entry is NOT the first entry of the list */
  return FALSE;
Cedric Roux's avatar
Cedric Roux committed
677 678 679 680
}

/****************************************************************************
 **                                                                        **
681
 ** Name:    _nas_timer_db_remove_entry()                              **
Cedric Roux's avatar
Cedric Roux committed
682 683
 **                                                                        **
 ** Description: Removes the entry with the given identifier from the      **
684 685 686
 **      queue of active timer entries and restarts the system     **
 **      timer if the entry was the next entry for which the timer **
 **      was scheduled to expire.                                  **
Cedric Roux's avatar
Cedric Roux committed
687
 **                                                                        **
688 689
 ** Inputs:  id:        Identifier of the entry to be removed      **
 **      Others:    None                                       **
Cedric Roux's avatar
Cedric Roux committed
690
 **                                                                        **
691 692 693
 ** Outputs:     None                                                      **
 **      Return:    A pointer to the removed entry             **
 **      Others:    _nas_timer_db                              **
Cedric Roux's avatar
Cedric Roux committed
694 695
 **                                                                        **
 ***************************************************************************/
696
static nas_timer_entry_t *_nas_timer_db_remove_entry(int id)
Cedric Roux's avatar
Cedric Roux committed
697
{
698
  int restart;
699

700 701
  /* The identifier of the timer is valid within the timer queue */
  assert(_nas_timer_db.tq[id].id == id);
Cedric Roux's avatar
Cedric Roux committed
702

703 704 705 706
  /* Remove the timer entry from the list of active entries */
  nas_timer_lock_db();
  restart = _nas_timer_db_remove(&_nas_timer_db.tq[id]);
  nas_timer_unlock_db();
Cedric Roux's avatar
Cedric Roux committed
707

708 709 710 711 712 713 714
  if (restart) {
    int rc;
    /* The entry was the first entry of the list;
     * the system timer needs to be restarted */
    struct itimerval it;
    struct timeval tv;
    struct timespec ts;
715

716
    it.it_interval.tv_sec = it.it_interval.tv_usec = 0;
717

718
    clock_gettime(CLOCK_MONOTONIC, &ts);
719

720 721 722 723
    tv.tv_sec = ts.tv_sec;
    tv.tv_usec = ts.tv_nsec/1000;
    /* tv = tv - time() */
    rc = _nas_timer_sub(&_nas_timer_db.head->entry->tv, &tv, &it.it_value);
724

725
#if defined(ENABLE_ITTI)
726 727
    timer_remove(_nas_timer_db.head->entry->timer_id);
    (void) (rc);
728
#else
729 730 731 732 733 734 735

    if (rc < 0) {
      /* The system timer should have already expired */
      _nas_timer_handler(SIGALRM);
    } else {
      /* Restart the system timer */
      setitimer(ITIMER_REAL, &it, 0);
Cedric Roux's avatar
Cedric Roux committed
736 737
    }

738 739 740 741 742
#endif
  }

  /* Return a pointer to the removed entry */
  return (_nas_timer_db.tq[id].entry);
Cedric Roux's avatar
Cedric Roux committed
743 744
}

745
static int _nas_timer_db_remove(timer_queue_t *entry)
Cedric Roux's avatar
Cedric Roux committed
746
{
747 748 749 750
  /* Update the pointer from the previous entry */
  /* prev ---> entry ---> next */
  /* prev <--- entry <--- next */
  if (entry->next != NULL) {
Cedric Roux's avatar
Cedric Roux committed
751
    /* prev ---> entry ---> next */
752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
    /* prev <-------------- next */
    entry->next->prev = entry->prev;
  }

  /* Update the pointer from the next entry */
  if (entry->prev != NULL) {
    /* prev --------------> next */
    /* prev <-------------- next */
    entry->prev->next = entry->next;
  } else {
    /* The entry was the first entry of the list */
    _nas_timer_db.head = entry->next;

    if (_nas_timer_db.head != NULL) {
      /* Other timers are scheduled to expire */
      return TRUE;
Cedric Roux's avatar
Cedric Roux committed
768
    }
769

770
#if !defined(ENABLE_ITTI)
771 772 773 774 775 776 777
    {
      /* No more timer is scheduled to expire; stop the system timer */
      struct itimerval it;
      it.it_interval.tv_sec = it.it_interval.tv_usec = 0;
      it.it_value.tv_sec = it.it_value.tv_usec = 0;
      setitimer(ITIMER_REAL, &it, 0);
      return FALSE;
Cedric Roux's avatar
Cedric Roux committed
778
    }
779 780 781 782 783
#endif
  }

  /* The entry was NOT the first entry of the list */
  return FALSE;
Cedric Roux's avatar
Cedric Roux committed
784 785 786 787
}

/*
 * -----------------------------------------------------------------------------
788
 *      Operator functions for timeval structures
Cedric Roux's avatar
Cedric Roux committed
789 790 791 792
 * -----------------------------------------------------------------------------
 */
/****************************************************************************
 **                                                                        **
793
 ** Name:        _nas_timer_cmp()                                          **
Cedric Roux's avatar
Cedric Roux committed
794 795 796
 **                                                                        **
 ** Description: Performs timeval comparaison                              **
 **                                                                        **
797 798 799
 ** Inputs:              a:     The first timeval structure                **
 **                      b:     The second timeval structure               **
 **                  Others:    None                                       **
Cedric Roux's avatar
Cedric Roux committed
800
 **                                                                        **
801
 ** Outputs:     None                                                      **
802 803
 **                  Return:    -1 if a < b; 1 if a > b; 0 if a == b       **
 **                  Others:    None                                       **
Cedric Roux's avatar
Cedric Roux committed
804 805
 **                                                                        **
 ***************************************************************************/
806
static int _nas_timer_cmp(const struct timeval *a, const struct timeval *b)
Cedric Roux's avatar
Cedric Roux committed
807
{
808 809 810 811 812 813 814 815 816 817 818
  if (a->tv_sec < b->tv_sec) {
    return -1;
  } else if (a->tv_sec > b->tv_sec) {
    return 1;
  } else if (a->tv_usec < b->tv_usec) {
    return -1;
  } else if (a->tv_usec > b->tv_usec) {
    return 1;
  }

  return 0;
Cedric Roux's avatar
Cedric Roux committed
819 820 821 822
}

/****************************************************************************
 **                                                                        **
823
 ** Name:    _nas_timer_add()                                          **
Cedric Roux's avatar
Cedric Roux committed
824 825 826
 **                                                                        **
 ** Description: Performs timeval addition                                 **
 **                                                                        **
827 828 829
 ** Inputs:  a:     The first timeval structure                **
 **      b:     The second timeval structure               **
 **      Others:    None                                       **
Cedric Roux's avatar
Cedric Roux committed
830
 **                                                                        **
831 832 833
 ** Outputs:     result:    result = timeval(a + b)                    **
 **      Return:    None                                       **
 **      Others:    None                                       **
Cedric Roux's avatar
Cedric Roux committed
834 835
 **                                                                        **
 ***************************************************************************/
836 837
static void _nas_timer_add(const struct timeval *a, const struct timeval *b,
                           struct timeval *result)
Cedric Roux's avatar
Cedric Roux committed
838
{
839 840 841 842 843 844 845
  result->tv_sec = a->tv_sec + b->tv_sec;
  result->tv_usec = a->tv_usec + b->tv_usec;

  if (result->tv_usec > 1000000) {
    result->tv_sec++;
    result->tv_usec -= 1000000;
  }
Cedric Roux's avatar
Cedric Roux committed
846 847 848 849
}

/****************************************************************************
 **                                                                        **
850
 ** Name:    _nas_timer_sub()                                          **
Cedric Roux's avatar
Cedric Roux committed
851 852 853
 **                                                                        **
 ** Description: Performs timeval substraction                             **
 **                                                                        **
854 855 856
 ** Inputs:  a:     The first timeval structure                **
 **      b:     The second timeval structure               **
 **      Others:    None                                       **
Cedric Roux's avatar
Cedric Roux committed
857
 **                                                                        **
858 859 860
 ** Outputs:     result:    a >= b, result = timeval(a - b)            **
 **      Return:    -1 if a < b; 0 otherwise                   **
 **      Others:    None                                       **
Cedric Roux's avatar
Cedric Roux committed
861 862
 **                                                                        **
 ***************************************************************************/
863 864
static int _nas_timer_sub(const struct timeval *a, const struct timeval *b,
                          struct timeval *result)
Cedric Roux's avatar
Cedric Roux committed
865
{
866 867 868 869 870 871 872
  if (_nas_timer_cmp(a,b) > 0 ) {
    result->tv_sec = a->tv_sec - b->tv_sec;
    result->tv_usec = a->tv_usec - b->tv_usec;

    if (result->tv_usec < 0) {
      result->tv_sec--;
      result->tv_usec += 1000000;
Cedric Roux's avatar
Cedric Roux committed
873
    }
874 875 876 877 878

    return 0;
  }

  return -1;
Cedric Roux's avatar
Cedric Roux committed
879 880
}