summaryrefslogtreecommitdiffstats
path: root/cpukit/posix
diff options
context:
space:
mode:
authorJoel Sherrill <joel.sherrill@OARcorp.com>2007-12-17 16:01:42 +0000
committerJoel Sherrill <joel.sherrill@OARcorp.com>2007-12-17 16:01:42 +0000
commita6cbc9b8690825aebde9694e9132230d57d53847 (patch)
tree0974c40531822c54d167e4bdea37e57dca40ec9e /cpukit/posix
parentFormatting. (diff)
downloadrtems-a6cbc9b8690825aebde9694e9132230d57d53847.tar.bz2
2007-12-17 Joel Sherrill <joel.sherrill@OARcorp.com>
* posix/Makefile.am, posix/preinstall.am, posix/include/rtems/posix/timer.h, score/src/objectget.c: Split POSIX Timer implementation into multiple files. Add obvious error checks for NULL parameters. Attempt to reduce include files. * posix/src/timercreate.c, posix/src/timerdelete.c, posix/src/timergetoverrun.c, posix/src/timergettime.c, posix/src/timerinserthelper.c, posix/src/timersettime.c, posix/src/timertsr.c: New files. * posix/src/ptimer1.c: Removed.
Diffstat (limited to 'cpukit/posix')
-rw-r--r--cpukit/posix/Makefile.am13
-rw-r--r--cpukit/posix/include/rtems/posix/timer.h16
-rw-r--r--cpukit/posix/preinstall.am4
-rw-r--r--cpukit/posix/src/ptimer1.c479
-rw-r--r--cpukit/posix/src/timercreate.c97
-rw-r--r--cpukit/posix/src/timerdelete.c63
-rw-r--r--cpukit/posix/src/timergetoverrun.c58
-rw-r--r--cpukit/posix/src/timergettime.c75
-rw-r--r--cpukit/posix/src/timerinserthelper.c57
-rw-r--r--cpukit/posix/src/timersettime.c148
-rw-r--r--cpukit/posix/src/timertsr.c79
11 files changed, 605 insertions, 484 deletions
diff --git a/cpukit/posix/Makefile.am b/cpukit/posix/Makefile.am
index 4708af7989..bc8ae0b411 100644
--- a/cpukit/posix/Makefile.am
+++ b/cpukit/posix/Makefile.am
@@ -35,10 +35,11 @@ include_rtems_posix_HEADERS = include/rtems/posix/cancel.h \
include/rtems/posix/key.h include/rtems/posix/mqueue.h \
include/rtems/posix/mutex.h include/rtems/posix/posixapi.h \
include/rtems/posix/priority.h include/rtems/posix/psignal.h \
- include/rtems/posix/pthread.h include/rtems/posix/semaphore.h \
- include/rtems/posix/threadsup.h include/rtems/posix/time.h \
- include/rtems/posix/timer.h include/rtems/posix/barrier.h \
- include/rtems/posix/rwlock.h include/rtems/posix/spinlock.h
+ include/rtems/posix/pthread.h include/rtems/posix/ptimer.h \
+ include/rtems/posix/semaphore.h include/rtems/posix/threadsup.h \
+ include/rtems/posix/time.h include/rtems/posix/timer.h \
+ include/rtems/posix/barrier.h include/rtems/posix/rwlock.h \
+ include/rtems/posix/spinlock.h
include_rtems_posix_HEADERS += inline/rtems/posix/cond.inl \
inline/rtems/posix/key.inl inline/rtems/posix/mqueue.inl \
@@ -161,7 +162,9 @@ libposix_a_SOURCES += src/adjtime.c src/posixtimespecabsolutetimeout.c \
# the timer manager needs to be split further but only after its
# dependence on the Classic API Timer Manager is removed.
## TIMER_C_FILES
-libposix_a_SOURCES += src/ptimer.c src/ptimer1.c
+libposix_a_SOURCES += src/ptimer.c src/timercreate.c src/timerdelete.c \
+ src/timergetoverrun.c src/timergettime.c src/timersettime.c \
+ src/timertsr.c src/timerinserthelper.c
EXTRA_DIST += src/README.mqueue
diff --git a/cpukit/posix/include/rtems/posix/timer.h b/cpukit/posix/include/rtems/posix/timer.h
index 1ecc560249..1ff52d5230 100644
--- a/cpukit/posix/include/rtems/posix/timer.h
+++ b/cpukit/posix/include/rtems/posix/timer.h
@@ -61,6 +61,22 @@ typedef struct {
void _POSIX_Timer_Manager_initialization ( int max_timers );
/*
+ * Timer TSR
+ */
+void _POSIX_Timer_TSR(Objects_Id timer, void *data);
+
+/*
+ * Watchdog Insert helper
+ */
+boolean _POSIX_Timer_Insert_helper(
+ Watchdog_Control *timer,
+ Watchdog_Interval ticks,
+ Objects_Id id,
+ Watchdog_Service_routine_entry TSR,
+ void *arg
+);
+
+/*
* The following defines the information control block used to manage
* this class of objects.
*/
diff --git a/cpukit/posix/preinstall.am b/cpukit/posix/preinstall.am
index 5531e7c852..eae3fa7ab7 100644
--- a/cpukit/posix/preinstall.am
+++ b/cpukit/posix/preinstall.am
@@ -85,6 +85,10 @@ $(PROJECT_INCLUDE)/rtems/posix/pthread.h: include/rtems/posix/pthread.h $(PROJEC
$(INSTALL_DATA) $< $(PROJECT_INCLUDE)/rtems/posix/pthread.h
PREINSTALL_FILES += $(PROJECT_INCLUDE)/rtems/posix/pthread.h
+$(PROJECT_INCLUDE)/rtems/posix/ptimer.h: include/rtems/posix/ptimer.h $(PROJECT_INCLUDE)/rtems/posix/$(dirstamp)
+ $(INSTALL_DATA) $< $(PROJECT_INCLUDE)/rtems/posix/ptimer.h
+PREINSTALL_FILES += $(PROJECT_INCLUDE)/rtems/posix/ptimer.h
+
$(PROJECT_INCLUDE)/rtems/posix/semaphore.h: include/rtems/posix/semaphore.h $(PROJECT_INCLUDE)/rtems/posix/$(dirstamp)
$(INSTALL_DATA) $< $(PROJECT_INCLUDE)/rtems/posix/semaphore.h
PREINSTALL_FILES += $(PROJECT_INCLUDE)/rtems/posix/semaphore.h
diff --git a/cpukit/posix/src/ptimer1.c b/cpukit/posix/src/ptimer1.c
deleted file mode 100644
index 1f3dabbb01..0000000000
--- a/cpukit/posix/src/ptimer1.c
+++ /dev/null
@@ -1,479 +0,0 @@
-/*
- * COPYRIGHT (c) 1989-2007.
- * On-Line Applications Research Corporation (OAR).
- *
- * The license and distribution terms for this file may be
- * found in the file LICENSE in this distribution or at
- * http://www.rtems.com/license/LICENSE.
- *
- * $Id$
- */
-
-#if HAVE_CONFIG_H
-#include "config.h"
-#endif
-
-#include <time.h>
-#include <errno.h>
-
-#include <rtems/system.h>
-#include <rtems/score/isr.h>
-#include <rtems/score/thread.h>
-#include <rtems/score/tod.h>
-
-#include <rtems/posix/time.h>
-
-/************************************/
-/* These includes are now necessary */
-/************************************/
-
-#include <unistd.h>
-#include <rtems/rtems/status.h>
-#include <rtems/rtems/types.h>
-#include <rtems/rtems/timer.h>
-#include <rtems/rtems/clock.h>
-#include <rtems/posix/psignal.h>
-#include <pthread.h>
-#include <stdio.h>
-#include <signal.h>
-
-#include <rtems/seterr.h>
-#include <rtems/posix/timer.h>
-
-boolean _Watchdog_Insert_ticks_helper(
- Watchdog_Control *timer,
- Watchdog_Interval ticks,
- Objects_Id id,
- Watchdog_Service_routine_entry TSR,
- void *arg
-)
-{
- ISR_Level level;
-
- (void) _Watchdog_Remove( timer );
- _ISR_Disable( level );
-
- /*
- * Check to see if the watchdog has just been inserted by a
- * higher priority interrupt. If so, abandon this insert.
- */
- if ( timer->state != WATCHDOG_INACTIVE ) {
- _ISR_Enable( level );
- return FALSE;
- }
-
- /*
- * OK. Now we now the timer was not rescheduled by an interrupt
- * so we can atomically initialize it as in use.
- */
- _Watchdog_Initialize( timer, TSR, id, arg );
- _Watchdog_Insert_ticks( timer, ticks );
- _ISR_Enable( level );
- return TRUE;
-}
-
-/* #define DEBUG_MESSAGES */
-
-/* ***************************************************************************
- * _POSIX_Timer_TSR
- *
- * Description: This is the operation that is ran when a timer expires
- * ***************************************************************************/
-
-
-void _POSIX_Timer_TSR(Objects_Id timer, void *data)
-{
- POSIX_Timer_Control *ptimer;
- boolean activated;
-
- ptimer = (POSIX_Timer_Control *)data;
-
- /* Increment the number of expirations. */
- ptimer->overrun = ptimer->overrun + 1;
- /* The timer must be reprogrammed */
- if ( ( ptimer->timer_data.it_interval.tv_sec != 0 ) ||
- ( ptimer->timer_data.it_interval.tv_nsec != 0 ) ) {
-#if 0
- status = rtems_timer_fire_after(
- ptimer->timer_id, ptimer->ticks, _POSIX_Timer_TSR, ptimer );
-#endif
- activated = _Watchdog_Insert_ticks_helper(
- &ptimer->Timer,
- ptimer->ticks,
- ptimer->Object.id,
- _POSIX_Timer_TSR,
- ptimer
- );
- if ( !activated )
- return;
-
- /* Store the time when the timer was started again */
- _TOD_Get( &ptimer->time );
-
- /* The state really did not change but just to be safe */
- ptimer->state = POSIX_TIMER_STATE_CREATE_RUN;
- } else {
- /* Indicates that the timer is stopped */
- ptimer->state = POSIX_TIMER_STATE_CREATE_STOP;
- }
-
- /*
- * The sending of the signal to the process running the handling function
- * specified for that signal is simulated
- */
-
- if ( pthread_kill ( ptimer->thread_id, ptimer->inf.sigev_signo ) ) {
- /* XXX error handling */
- }
-
- /* After the signal handler returns, the count of expirations of the
- * timer must be set to 0.
- */
- ptimer->overrun = 0;
-}
-
-/* *********************************************************************
- * 14.2.2 Create a Per-Process Timer, P1003.1b-1993, p. 264
- * ********************************************************************/
-
-/* **************
- * timer_create
- * **************/
-
-int timer_create(
- clockid_t clock_id,
- struct sigevent *evp,
- timer_t *timerid
-)
-{
- POSIX_Timer_Control *ptimer;
-
- if ( clock_id != CLOCK_REALTIME )
- rtems_set_errno_and_return_minus_one( EINVAL );
-
- /*
- * The data of the structure evp are checked in order to verify if they
- * are coherent.
- */
-
- if (evp != NULL) {
- /* The structure has data */
- if ( ( evp->sigev_notify != SIGEV_NONE ) &&
- ( evp->sigev_notify != SIGEV_SIGNAL ) ) {
- /* The value of the field sigev_notify is not valid */
- rtems_set_errno_and_return_minus_one( EINVAL );
- }
-
- if ( !evp->sigev_signo )
- rtems_set_errno_and_return_minus_one( EINVAL );
-
- if ( !is_valid_signo(evp->sigev_signo) )
- rtems_set_errno_and_return_minus_one( EINVAL );
- }
-
- _Thread_Disable_dispatch(); /* to prevent deletion */
-
- /*
- * Allocate a timer
- */
- ptimer = _POSIX_Timer_Allocate();
- if ( !ptimer ) {
- _Thread_Enable_dispatch();
- rtems_set_errno_and_return_minus_one( EAGAIN );
- }
-
- /* The data of the created timer are stored to use them later */
-
- ptimer->state = POSIX_TIMER_STATE_CREATE_NEW;
- ptimer->thread_id = _Thread_Executing->Object.id;
-
- if ( evp != NULL ) {
- ptimer->inf.sigev_notify = evp->sigev_notify;
- ptimer->inf.sigev_signo = evp->sigev_signo;
- ptimer->inf.sigev_value = evp->sigev_value;
- }
-
- ptimer->overrun = 0;
- ptimer->timer_data.it_value.tv_sec = 0;
- ptimer->timer_data.it_value.tv_nsec = 0;
- ptimer->timer_data.it_interval.tv_sec = 0;
- ptimer->timer_data.it_interval.tv_nsec = 0;
-
- _Watchdog_Initialize( &ptimer->Timer, NULL, 0, NULL );
- _Objects_Open(&_POSIX_Timer_Information, &ptimer->Object, (Objects_Name) 0);
-
- *timerid = ptimer->Object.id;
- _Thread_Enable_dispatch();
- return 0;
-}
-
-/*
- * 14.2.3 Delete a Per_process Timer, P1003.1b-1993, p. 266
- */
-
-int timer_delete(
- timer_t timerid
-)
-{
- /*
- * IDEA: This function must probably stop the timer first and then delete it
- *
- * It will have to do a call to rtems_timer_cancel and then another
- * call to rtems_timer_delete.
- * The call to rtems_timer_delete will be probably unnecessary,
- * because rtems_timer_delete stops the timer before deleting it.
- */
- POSIX_Timer_Control *ptimer;
- Objects_Locations location;
-
- ptimer = _POSIX_Timer_Get( timerid, &location );
- switch ( location ) {
-
- case OBJECTS_LOCAL:
- _Objects_Close( &_POSIX_Timer_Information, &ptimer->Object );
- ptimer->state = POSIX_TIMER_STATE_FREE;
- (void) _Watchdog_Remove( &ptimer->Timer );
- _POSIX_Timer_Free( ptimer );
- _Thread_Enable_dispatch();
- return 0;
-
-#if defined(RTEMS_MULTIPROCESSING)
- case OBJECTS_REMOTE:
-#endif
- case OBJECTS_ERROR:
- break;
- }
-
- rtems_set_errno_and_return_minus_one( EINVAL );
-}
-
-/*
- * 14.2.4 Per-Process Timers, P1003.1b-1993, p. 267
- */
-
-/* **************
- * timer_settime
- * **************/
-
-
-int timer_settime(
- timer_t timerid,
- int flags,
- const struct itimerspec *value,
- struct itimerspec *ovalue
-)
-{
- POSIX_Timer_Control *ptimer;
- Objects_Locations location;
- boolean activated;
-
- if ( value == NULL ) {
- rtems_set_errno_and_return_minus_one( EINVAL );
- }
-
- /* First, it verifies if the structure "value" is correct */
- if ( ( value->it_value.tv_nsec > TOD_NANOSECONDS_PER_SECOND ) ||
- ( value->it_value.tv_nsec < 0 ) ) {
- /* The number of nanoseconds is not correct */
- rtems_set_errno_and_return_minus_one( EINVAL );
- }
-
- /* XXX check for seconds in the past */
-
- if ( flags != TIMER_ABSTIME && flags != POSIX_TIMER_RELATIVE ) {
- rtems_set_errno_and_return_minus_one( EINVAL );
- }
-
- /* If the function reaches this point, then it will be necessary to do
- * something with the structure of times of the timer: to stop, start
- * or start it again
- */
-
- ptimer = _POSIX_Timer_Get( timerid, &location );
- switch ( location ) {
-
- case OBJECTS_LOCAL:
- /* First, it verifies if the timer must be stopped */
- if ( value->it_value.tv_sec == 0 && value->it_value.tv_nsec == 0 ) {
- /* Stop the timer */
- (void) _Watchdog_Remove( &ptimer->Timer );
- /* The old data of the timer are returned */
- if ( ovalue )
- *ovalue = ptimer->timer_data;
- /* The new data are set */
- ptimer->timer_data = *value;
- /* Indicates that the timer is created and stopped */
- ptimer->state = POSIX_TIMER_STATE_CREATE_STOP;
- /* Returns with success */
- _Thread_Enable_dispatch();
- return 0;
- }
-
- /* absolute or relative? */
- switch (flags) {
- case TIMER_ABSTIME:
-
- /* The fire time is absolute: use "rtems_time_fire_when" */
- /* First, it converts from struct itimerspec to rtems_time_of_day */
-
- _Watchdog_Initialize(
- &ptimer->Timer, _POSIX_Timer_TSR, ptimer->Object.id, ptimer );
-
- _Watchdog_Insert_seconds(
- &ptimer->Timer,
- value->it_value.tv_sec - _TOD_Seconds_since_epoch
- );
-
- /* Returns the old ones in "ovalue" */
- if ( ovalue )
- *ovalue = ptimer->timer_data;
- ptimer->timer_data = *value;
-
- /* Indicate that the time is running */
- ptimer->state = POSIX_TIMER_STATE_CREATE_RUN;
-
- /* Stores the time in which the timer was started again */
- _TOD_Get( &ptimer->time );
- _Thread_Enable_dispatch();
- return 0;
- break;
-
- /* The fire time is relative: use "rtems_time_fire_after" */
- case POSIX_TIMER_RELATIVE:
- /* First, convert from seconds and nanoseconds to ticks */
- ptimer->ticks = _Timespec_To_ticks( &value->it_value );
-
- activated = _Watchdog_Insert_ticks_helper(
- &ptimer->Timer,
- ptimer->ticks,
- ptimer->Object.id,
- _POSIX_Timer_TSR,
- ptimer
- );
- if ( !activated )
- return 0;
-
- /* The timer has been started and is running */
- /* return the old ones in "ovalue" */
- if ( ovalue )
- *ovalue = ptimer->timer_data;
- ptimer->timer_data = *value;
-
- /* Indicate that the time is running */
- ptimer->state = POSIX_TIMER_STATE_CREATE_RUN;
- _TOD_Get( &ptimer->time );
- _Thread_Enable_dispatch();
- return 0;
- }
- _Thread_Enable_dispatch();
- break;
-
-#if defined(RTEMS_MULTIPROCESSING)
- case OBJECTS_REMOTE:
-#endif
- case OBJECTS_ERROR:
- break;
- }
-
- rtems_set_errno_and_return_minus_one( EINVAL );
-}
-
-/*
- * 14.2.4 Per-Process Timers, P1003.1b-1993, p. 267
- */
-
-/* **************
- * timer_gettime
- * **************/
-
-int timer_gettime(
- timer_t timerid,
- struct itimerspec *value
-)
-{
-
- /*
- * IDEA: This function does not use functions of RTEMS to the handle
- * of timers. It uses some functions for managing the time.
- *
- * A possible form to do this is the following:
- *
- * - When a timer is initialized, the value of the time in
- * that moment is stored.
- * - When this function is called, it returns the difference
- * between the current time and the initialization time.
- */
-
- POSIX_Timer_Control *ptimer;
- Objects_Locations location;
- struct timespec current_time;
-
- /* Reads the current time */
- _TOD_Get( &current_time );
-
- ptimer = _POSIX_Timer_Get( timerid, &location );
- switch ( location ) {
-
- case OBJECTS_LOCAL:
-
- /* Calculates the time left before the timer finishes */
-
- _Timespec_Subtract(
- &ptimer->timer_data.it_value,
- &current_time,
- &value->it_value
- );
-
- value->it_interval = ptimer->timer_data.it_interval;
-
- _Thread_Enable_dispatch();
- return 0;
-
-#if defined(RTEMS_MULTIPROCESSING)
- case OBJECTS_REMOTE:
-#endif
- case OBJECTS_ERROR:
- break;
- }
-
- rtems_set_errno_and_return_minus_one( EINVAL );
-}
-
-/*
- * 14.2.4 Per-Process Timers, P1003.1b-1993, p. 267
- */
-
-/*
- * timer_getoverrun
- *
- * The expiration of a timer must increase by one a counter.
- * After the signal handler associated to the timer finishes
- * its execution, _POSIX_Timer_TSR will have to set this counter to 0.
- */
-
-int timer_getoverrun(
- timer_t timerid
-)
-{
- int overrun;
- POSIX_Timer_Control *ptimer;
- Objects_Locations location;
-
- ptimer = _POSIX_Timer_Get( timerid, &location );
- switch ( location ) {
-
- case OBJECTS_LOCAL:
- overrun = ptimer->overrun;
- ptimer->overrun = 0;
- _Thread_Enable_dispatch();
- return overrun;
-
-#if defined(RTEMS_MULTIPROCESSING)
- case OBJECTS_REMOTE:
-#endif
- case OBJECTS_ERROR:
- break;
- }
-
- rtems_set_errno_and_return_minus_one( EINVAL );
-}
diff --git a/cpukit/posix/src/timercreate.c b/cpukit/posix/src/timercreate.c
new file mode 100644
index 0000000000..90cb9b0f8b
--- /dev/null
+++ b/cpukit/posix/src/timercreate.c
@@ -0,0 +1,97 @@
+/*
+ * 14.2.2 Create a Per-Process Timer, P1003.1b-1993, p. 264
+ *
+ * COPYRIGHT (c) 1989-2007.
+ * On-Line Applications Research Corporation (OAR).
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.com/license/LICENSE.
+ *
+ * $Id$
+ */
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <time.h>
+#include <errno.h>
+#include <signal.h>
+
+#include <rtems/system.h>
+#include <rtems/seterr.h>
+#include <rtems/score/thread.h>
+#include <rtems/posix/psignal.h>
+#include <rtems/posix/time.h>
+#include <rtems/posix/timer.h>
+
+int timer_create(
+ clockid_t clock_id,
+ struct sigevent *evp,
+ timer_t *timerid
+)
+{
+ POSIX_Timer_Control *ptimer;
+
+ if ( clock_id != CLOCK_REALTIME )
+ rtems_set_errno_and_return_minus_one( EINVAL );
+
+ if ( !timerid )
+ rtems_set_errno_and_return_minus_one( EINVAL );
+
+ /*
+ * The data of the structure evp are checked in order to verify if they
+ * are coherent.
+ */
+
+ if (evp != NULL) {
+ /* The structure has data */
+ if ( ( evp->sigev_notify != SIGEV_NONE ) &&
+ ( evp->sigev_notify != SIGEV_SIGNAL ) ) {
+ /* The value of the field sigev_notify is not valid */
+ rtems_set_errno_and_return_minus_one( EINVAL );
+ }
+
+ if ( !evp->sigev_signo )
+ rtems_set_errno_and_return_minus_one( EINVAL );
+
+ if ( !is_valid_signo(evp->sigev_signo) )
+ rtems_set_errno_and_return_minus_one( EINVAL );
+ }
+
+ _Thread_Disable_dispatch(); /* to prevent deletion */
+
+ /*
+ * Allocate a timer
+ */
+ ptimer = _POSIX_Timer_Allocate();
+ if ( !ptimer ) {
+ _Thread_Enable_dispatch();
+ rtems_set_errno_and_return_minus_one( EAGAIN );
+ }
+
+ /* The data of the created timer are stored to use them later */
+
+ ptimer->state = POSIX_TIMER_STATE_CREATE_NEW;
+ ptimer->thread_id = _Thread_Executing->Object.id;
+
+ if ( evp != NULL ) {
+ ptimer->inf.sigev_notify = evp->sigev_notify;
+ ptimer->inf.sigev_signo = evp->sigev_signo;
+ ptimer->inf.sigev_value = evp->sigev_value;
+ }
+
+ ptimer->overrun = 0;
+ ptimer->timer_data.it_value.tv_sec = 0;
+ ptimer->timer_data.it_value.tv_nsec = 0;
+ ptimer->timer_data.it_interval.tv_sec = 0;
+ ptimer->timer_data.it_interval.tv_nsec = 0;
+
+ _Watchdog_Initialize( &ptimer->Timer, NULL, 0, NULL );
+ _Objects_Open(&_POSIX_Timer_Information, &ptimer->Object, (Objects_Name) 0);
+
+ *timerid = ptimer->Object.id;
+ _Thread_Enable_dispatch();
+ return 0;
+}
diff --git a/cpukit/posix/src/timerdelete.c b/cpukit/posix/src/timerdelete.c
new file mode 100644
index 0000000000..cc70871e3c
--- /dev/null
+++ b/cpukit/posix/src/timerdelete.c
@@ -0,0 +1,63 @@
+/*
+ * 14.2.3 Delete a Per_process Timer, P1003.1b-1993, p. 266
+ *
+ * COPYRIGHT (c) 1989-2007.
+ * On-Line Applications Research Corporation (OAR).
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.com/license/LICENSE.
+ *
+ * $Id$
+ */
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <time.h>
+#include <errno.h>
+#include <pthread.h>
+
+#include <rtems/system.h>
+#include <rtems/seterr.h>
+#include <rtems/score/thread.h>
+#include <rtems/posix/time.h>
+#include <rtems/posix/timer.h>
+
+
+int timer_delete(
+ timer_t timerid
+)
+{
+ /*
+ * IDEA: This function must probably stop the timer first and then delete it
+ *
+ * It will have to do a call to rtems_timer_cancel and then another
+ * call to rtems_timer_delete.
+ * The call to rtems_timer_delete will be probably unnecessary,
+ * because rtems_timer_delete stops the timer before deleting it.
+ */
+ POSIX_Timer_Control *ptimer;
+ Objects_Locations location;
+
+ ptimer = _POSIX_Timer_Get( timerid, &location );
+ switch ( location ) {
+
+ case OBJECTS_LOCAL:
+ _Objects_Close( &_POSIX_Timer_Information, &ptimer->Object );
+ ptimer->state = POSIX_TIMER_STATE_FREE;
+ (void) _Watchdog_Remove( &ptimer->Timer );
+ _POSIX_Timer_Free( ptimer );
+ _Thread_Enable_dispatch();
+ return 0;
+
+#if defined(RTEMS_MULTIPROCESSING)
+ case OBJECTS_REMOTE:
+#endif
+ case OBJECTS_ERROR:
+ break;
+ }
+
+ rtems_set_errno_and_return_minus_one( EINVAL );
+}
diff --git a/cpukit/posix/src/timergetoverrun.c b/cpukit/posix/src/timergetoverrun.c
new file mode 100644
index 0000000000..1ad7bc4d04
--- /dev/null
+++ b/cpukit/posix/src/timergetoverrun.c
@@ -0,0 +1,58 @@
+/*
+ * 14.2.4 Per-Process Timers, P1003.1b-1993, p. 267
+ *
+ * COPYRIGHT (c) 1989-2007.
+ * On-Line Applications Research Corporation (OAR).
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.com/license/LICENSE.
+ *
+ * $Id$
+ */
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <time.h>
+#include <errno.h>
+
+#include <rtems/system.h>
+#include <rtems/seterr.h>
+#include <rtems/score/thread.h>
+#include <rtems/posix/timer.h>
+
+/*
+ * timer_getoverrun
+ *
+ * The expiration of a timer must increase by one a counter.
+ * After the signal handler associated to the timer finishes
+ * its execution, _POSIX_Timer_TSR will have to set this counter to 0.
+ */
+int timer_getoverrun(
+ timer_t timerid
+)
+{
+ int overrun;
+ POSIX_Timer_Control *ptimer;
+ Objects_Locations location;
+
+ ptimer = _POSIX_Timer_Get( timerid, &location );
+ switch ( location ) {
+
+ case OBJECTS_LOCAL:
+ overrun = ptimer->overrun;
+ ptimer->overrun = 0;
+ _Thread_Enable_dispatch();
+ return overrun;
+
+#if defined(RTEMS_MULTIPROCESSING)
+ case OBJECTS_REMOTE:
+#endif
+ case OBJECTS_ERROR:
+ break;
+ }
+
+ rtems_set_errno_and_return_minus_one( EINVAL );
+}
diff --git a/cpukit/posix/src/timergettime.c b/cpukit/posix/src/timergettime.c
new file mode 100644
index 0000000000..2f759abcb3
--- /dev/null
+++ b/cpukit/posix/src/timergettime.c
@@ -0,0 +1,75 @@
+/*
+ * 14.2.4 Per-Process Timers, P1003.1b-1993, p. 267
+ *
+ * COPYRIGHT (c) 1989-2007.
+ * On-Line Applications Research Corporation (OAR).
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.com/license/LICENSE.
+ *
+ * $Id$
+ */
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <time.h>
+#include <errno.h>
+
+#include <rtems/system.h>
+#include <rtems/seterr.h>
+#include <rtems/score/thread.h>
+#include <rtems/score/tod.h>
+#include <rtems/posix/timer.h>
+
+/*
+ * - When a timer is initialized, the value of the time in
+ * that moment is stored.
+ * - When this function is called, it returns the difference
+ * between the current time and the initialization time.
+ */
+
+int timer_gettime(
+ timer_t timerid,
+ struct itimerspec *value
+)
+{
+ POSIX_Timer_Control *ptimer;
+ Objects_Locations location;
+ struct timespec current_time;
+
+ if ( !value )
+ rtems_set_errno_and_return_minus_one( EINVAL );
+
+ /* Reads the current time */
+ _TOD_Get( &current_time );
+
+ ptimer = _POSIX_Timer_Get( timerid, &location );
+ switch ( location ) {
+
+ case OBJECTS_LOCAL:
+
+ /* Calculates the time left before the timer finishes */
+
+ _Timespec_Subtract(
+ &ptimer->timer_data.it_value,
+ &current_time,
+ &value->it_value
+ );
+
+ value->it_interval = ptimer->timer_data.it_interval;
+
+ _Thread_Enable_dispatch();
+ return 0;
+
+#if defined(RTEMS_MULTIPROCESSING)
+ case OBJECTS_REMOTE:
+#endif
+ case OBJECTS_ERROR:
+ break;
+ }
+
+ rtems_set_errno_and_return_minus_one( EINVAL );
+}
diff --git a/cpukit/posix/src/timerinserthelper.c b/cpukit/posix/src/timerinserthelper.c
new file mode 100644
index 0000000000..79178ca605
--- /dev/null
+++ b/cpukit/posix/src/timerinserthelper.c
@@ -0,0 +1,57 @@
+/*
+ * Helper routine for POSIX timers
+ *
+ * COPYRIGHT (c) 1989-2007.
+ * On-Line Applications Research Corporation (OAR).
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.com/license/LICENSE.
+ *
+ * $Id$
+ */
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <time.h>
+
+#include <rtems/system.h>
+#include <rtems/seterr.h>
+#include <rtems/score/isr.h>
+#include <rtems/score/watchdog.h>
+#include <rtems/posix/timer.h>
+#include <rtems/posix/ptimer.h>
+
+boolean _POSIX_Timer_Insert_helper(
+ Watchdog_Control *timer,
+ Watchdog_Interval ticks,
+ Objects_Id id,
+ Watchdog_Service_routine_entry TSR,
+ void *arg
+)
+{
+ ISR_Level level;
+
+ (void) _Watchdog_Remove( timer );
+ _ISR_Disable( level );
+
+ /*
+ * Check to see if the watchdog has just been inserted by a
+ * higher priority interrupt. If so, abandon this insert.
+ */
+ if ( timer->state != WATCHDOG_INACTIVE ) {
+ _ISR_Enable( level );
+ return FALSE;
+ }
+
+ /*
+ * OK. Now we now the timer was not rescheduled by an interrupt
+ * so we can atomically initialize it as in use.
+ */
+ _Watchdog_Initialize( timer, TSR, id, arg );
+ _Watchdog_Insert_ticks( timer, ticks );
+ _ISR_Enable( level );
+ return TRUE;
+}
diff --git a/cpukit/posix/src/timersettime.c b/cpukit/posix/src/timersettime.c
new file mode 100644
index 0000000000..c1cea14484
--- /dev/null
+++ b/cpukit/posix/src/timersettime.c
@@ -0,0 +1,148 @@
+/*
+ * 14.2.4 Per-Process Timers, P1003.1b-1993, p. 267
+ *
+ * COPYRIGHT (c) 1989-2007.
+ * On-Line Applications Research Corporation (OAR).
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.com/license/LICENSE.
+ *
+ * $Id$
+ */
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <time.h>
+#include <errno.h>
+
+#include <rtems/system.h>
+#include <rtems/seterr.h>
+#include <rtems/score/thread.h>
+#include <rtems/score/tod.h>
+#include <rtems/posix/time.h>
+#include <rtems/posix/ptimer.h>
+#include <rtems/posix/timer.h>
+
+int timer_settime(
+ timer_t timerid,
+ int flags,
+ const struct itimerspec *value,
+ struct itimerspec *ovalue
+)
+{
+ POSIX_Timer_Control *ptimer;
+ Objects_Locations location;
+ boolean activated;
+
+ if ( !value )
+ rtems_set_errno_and_return_minus_one( EINVAL );
+
+ /* First, it verifies if the structure "value" is correct */
+ if ( ( value->it_value.tv_nsec > TOD_NANOSECONDS_PER_SECOND ) ||
+ ( value->it_value.tv_nsec < 0 ) ) {
+ /* The number of nanoseconds is not correct */
+ rtems_set_errno_and_return_minus_one( EINVAL );
+ }
+
+ /* XXX check for seconds in the past */
+
+ if ( flags != TIMER_ABSTIME && flags != POSIX_TIMER_RELATIVE ) {
+ rtems_set_errno_and_return_minus_one( EINVAL );
+ }
+
+ /* If the function reaches this point, then it will be necessary to do
+ * something with the structure of times of the timer: to stop, start
+ * or start it again
+ */
+
+ ptimer = _POSIX_Timer_Get( timerid, &location );
+ switch ( location ) {
+
+ case OBJECTS_LOCAL:
+ /* First, it verifies if the timer must be stopped */
+ if ( value->it_value.tv_sec == 0 && value->it_value.tv_nsec == 0 ) {
+ /* Stop the timer */
+ (void) _Watchdog_Remove( &ptimer->Timer );
+ /* The old data of the timer are returned */
+ if ( ovalue )
+ *ovalue = ptimer->timer_data;
+ /* The new data are set */
+ ptimer->timer_data = *value;
+ /* Indicates that the timer is created and stopped */
+ ptimer->state = POSIX_TIMER_STATE_CREATE_STOP;
+ /* Returns with success */
+ _Thread_Enable_dispatch();
+ return 0;
+ }
+
+ /* absolute or relative? */
+ switch (flags) {
+ case TIMER_ABSTIME:
+
+ /* The fire time is absolute: use "rtems_time_fire_when" */
+ /* First, it converts from struct itimerspec to rtems_time_of_day */
+
+ _Watchdog_Initialize(
+ &ptimer->Timer, _POSIX_Timer_TSR, ptimer->Object.id, ptimer );
+
+ _Watchdog_Insert_seconds(
+ &ptimer->Timer,
+ value->it_value.tv_sec - _TOD_Seconds_since_epoch
+ );
+
+ /* Returns the old ones in "ovalue" */
+ if ( ovalue )
+ *ovalue = ptimer->timer_data;
+ ptimer->timer_data = *value;
+
+ /* Indicate that the time is running */
+ ptimer->state = POSIX_TIMER_STATE_CREATE_RUN;
+
+ /* Stores the time in which the timer was started again */
+ _TOD_Get( &ptimer->time );
+ _Thread_Enable_dispatch();
+ return 0;
+ break;
+
+ /* The fire time is relative: use "rtems_time_fire_after" */
+ case POSIX_TIMER_RELATIVE:
+ /* First, convert from seconds and nanoseconds to ticks */
+ ptimer->ticks = _Timespec_To_ticks( &value->it_value );
+
+ activated = _POSIX_Timer_Insert_helper(
+ &ptimer->Timer,
+ ptimer->ticks,
+ ptimer->Object.id,
+ _POSIX_Timer_TSR,
+ ptimer
+ );
+ if ( !activated )
+ return 0;
+
+ /* The timer has been started and is running */
+ /* return the old ones in "ovalue" */
+ if ( ovalue )
+ *ovalue = ptimer->timer_data;
+ ptimer->timer_data = *value;
+
+ /* Indicate that the time is running */
+ ptimer->state = POSIX_TIMER_STATE_CREATE_RUN;
+ _TOD_Get( &ptimer->time );
+ _Thread_Enable_dispatch();
+ return 0;
+ }
+ _Thread_Enable_dispatch();
+ break;
+
+#if defined(RTEMS_MULTIPROCESSING)
+ case OBJECTS_REMOTE:
+#endif
+ case OBJECTS_ERROR:
+ break;
+ }
+
+ rtems_set_errno_and_return_minus_one( EINVAL );
+}
diff --git a/cpukit/posix/src/timertsr.c b/cpukit/posix/src/timertsr.c
new file mode 100644
index 0000000000..87bd718853
--- /dev/null
+++ b/cpukit/posix/src/timertsr.c
@@ -0,0 +1,79 @@
+/*
+ * _POSIX_Timer_TSR
+ *
+ * COPYRIGHT (c) 1989-2007.
+ * On-Line Applications Research Corporation (OAR).
+ *
+ * The license and distribution terms for this file may be
+ * found in the file LICENSE in this distribution or at
+ * http://www.rtems.com/license/LICENSE.
+ *
+ * $Id$
+ */
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <time.h>
+#include <pthread.h>
+#include <signal.h>
+
+#include <rtems/system.h>
+#include <rtems/seterr.h>
+#include <rtems/score/thread.h>
+#include <rtems/score/tod.h>
+#include <rtems/posix/time.h>
+#include <rtems/posix/ptimer.h>
+#include <rtems/posix/timer.h>
+
+/*
+ * This is the operation that is run when a timer expires
+ */
+void _POSIX_Timer_TSR(Objects_Id timer, void *data)
+{
+ POSIX_Timer_Control *ptimer;
+ boolean activated;
+
+ ptimer = (POSIX_Timer_Control *)data;
+
+ /* Increment the number of expirations. */
+ ptimer->overrun = ptimer->overrun + 1;
+
+ /* The timer must be reprogrammed */
+ if ( ( ptimer->timer_data.it_interval.tv_sec != 0 ) ||
+ ( ptimer->timer_data.it_interval.tv_nsec != 0 ) ) {
+ activated = _POSIX_Timer_Insert_helper(
+ &ptimer->Timer,
+ ptimer->ticks,
+ ptimer->Object.id,
+ _POSIX_Timer_TSR,
+ ptimer
+ );
+ if ( !activated )
+ return;
+
+ /* Store the time when the timer was started again */
+ _TOD_Get( &ptimer->time );
+
+ /* The state really did not change but just to be safe */
+ ptimer->state = POSIX_TIMER_STATE_CREATE_RUN;
+ } else {
+ /* Indicates that the timer is stopped */
+ ptimer->state = POSIX_TIMER_STATE_CREATE_STOP;
+ }
+
+ /*
+ * The sending of the signal to the process running the handling function
+ * specified for that signal is simulated
+ */
+
+ if ( pthread_kill ( ptimer->thread_id, ptimer->inf.sigev_signo ) ) {
+ /* XXX error handling */
+ }
+
+ /* After the signal handler returns, the count of expirations of the
+ * timer must be set to 0.
+ */
+ ptimer->overrun = 0;
+}