source: rtems/cpukit/posix/src/timersettime.c @ 3bacb250

4.104.115
Last change on this file since 3bacb250 was dc09e380, checked in by Joel Sherrill <joel.sherrill@…>, on 12/10/09 at 22:20:11

2009-12-10 Joel Sherrill <joel.sherrill@…>

PR 1482

  • posix/src/timersettime.c: Exit dispatching critical section.
  • Property mode set to 100644
File size: 3.8 KB
Line 
1/*
2 *  14.2.4 Per-Process Timers, P1003.1b-1993, p. 267
3 *
4 *  COPYRIGHT (c) 1989-2008.
5 *  On-Line Applications Research Corporation (OAR).
6 *
7 *  The license and distribution terms for this file may be
8 *  found in the file LICENSE in this distribution or at
9 *  http://www.rtems.com/license/LICENSE.
10 *
11 *  $Id$
12 */
13
14#if HAVE_CONFIG_H
15#include "config.h"
16#endif
17
18#include <time.h>
19#include <errno.h>
20
21#include <rtems/system.h>
22#include <rtems/seterr.h>
23#include <rtems/score/thread.h>
24#include <rtems/score/tod.h>
25#include <rtems/posix/time.h>
26#include <rtems/posix/ptimer.h>
27#include <rtems/posix/timer.h>
28
29int timer_settime(
30  timer_t                  timerid,
31  int                      flags,
32  const struct itimerspec *value,
33  struct itimerspec       *ovalue
34)
35{
36  POSIX_Timer_Control *ptimer;
37  Objects_Locations    location;
38  bool                 activated;
39  uint32_t             initial_period;
40  struct itimerspec    normalize;
41
42  if ( !value )
43    rtems_set_errno_and_return_minus_one( EINVAL );
44
45  /* First, it verifies if the structure "value" is correct */
46  if ( ( value->it_value.tv_nsec >= TOD_NANOSECONDS_PER_SECOND ) ||
47       ( value->it_value.tv_nsec < 0 ) ||
48       ( value->it_interval.tv_nsec >= TOD_NANOSECONDS_PER_SECOND) ||
49       ( value->it_interval.tv_nsec < 0 )) {
50    /* The number of nanoseconds is not correct */
51    rtems_set_errno_and_return_minus_one( EINVAL );
52  }
53
54  if ( flags != TIMER_ABSTIME && flags != POSIX_TIMER_RELATIVE ) {
55    rtems_set_errno_and_return_minus_one( EINVAL );
56  }
57
58  normalize = *value;
59
60  /* Convert absolute to relative time */
61  if (flags == TIMER_ABSTIME) {
62    struct timespec now;
63    _TOD_Get( &now );
64    /* Check for seconds in the past */
65    if ( _Timespec_Greater_than( &now, &normalize.it_value ) )
66      rtems_set_errno_and_return_minus_one( EINVAL );
67    _Timespec_Subtract( &now, &normalize.it_value, &normalize.it_value );
68  }
69
70  /* If the function reaches this point, then it will be necessary to do
71   * something with the structure of times of the timer: to stop, start
72   * or start it again
73   */
74
75  ptimer = _POSIX_Timer_Get( timerid, &location );
76  switch ( location ) {
77
78    case OBJECTS_LOCAL:
79      /* First, it verifies if the timer must be stopped */
80      if ( normalize.it_value.tv_sec == 0 && normalize.it_value.tv_nsec == 0 ) {
81         /* Stop the timer */
82         (void) _Watchdog_Remove( &ptimer->Timer );
83         /* The old data of the timer are returned */
84         if ( ovalue )
85           *ovalue = ptimer->timer_data;
86         /* The new data are set */
87         ptimer->timer_data = normalize;
88         /* Indicates that the timer is created and stopped */
89         ptimer->state = POSIX_TIMER_STATE_CREATE_STOP;
90         /* Returns with success */
91        _Thread_Enable_dispatch();
92        return 0;
93       }
94
95       /* Convert from seconds and nanoseconds to ticks */
96       ptimer->ticks  = _Timespec_To_ticks( &value->it_interval );
97       initial_period = _Timespec_To_ticks( &normalize.it_value );
98
99
100       activated = _POSIX_Timer_Insert_helper(
101         &ptimer->Timer,
102         initial_period,
103         ptimer->Object.id,
104         _POSIX_Timer_TSR,
105         ptimer
106       );
107       if ( !activated ) {
108         _Thread_Enable_dispatch();
109         return 0;
110       }
111
112       /*
113        * The timer has been started and is running.  So we return the
114        * old ones in "ovalue"
115        */
116       if ( ovalue )
117         *ovalue = ptimer->timer_data;
118       ptimer->timer_data = normalize;
119
120       /* Indicate that the time is running */
121       ptimer->state = POSIX_TIMER_STATE_CREATE_RUN;
122       _TOD_Get( &ptimer->time );
123       _Thread_Enable_dispatch();
124       return 0;
125
126#if defined(RTEMS_MULTIPROCESSING)
127    case OBJECTS_REMOTE:
128#endif
129    case OBJECTS_ERROR:
130      break;
131  }
132
133  rtems_set_errno_and_return_minus_one( EINVAL );
134}
Note: See TracBrowser for help on using the repository browser.