Hi,

Attached patch adds a new implementation of timers for kFreeBSD.  It
is heavily based on the Linux/NPTL counterpart.

I've verified that this fixes #642112 (timer_settime fails on kfreebsd
when timeout approaches max(time_t)) and #662018 (No support for
CLOCK_MONOTONIC).  It also addresses a great deal (but not all) of
#665287 (<bits/time.h> advertises unsupported clockid_t types).

It's also likely that it fixes some of the problems outlined in
http://lists.debian.org/debian-bsd/2012/03/msg00149.html

Review and testing is much welcome, please do if you can.

Please don't commit to glibc-bsd repository yet.  I haven't finished
testing it myself.

Thanks

-- 
Robert Millan
--- /dev/null
+++ b/ports/sysdeps/unix/bsd/bsd4.4/kfreebsd/linuxthreads/kernel-posix-timers.h
@@ -0,0 +1,72 @@
+/* Copyright (C) 2003, 2007 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+   Contributed by Ulrich Drepper <drep...@redhat.com>, 2003.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public License as
+   published by the Free Software Foundation; either version 2.1 of the
+   License, or (at your option) any later version.
+
+   The GNU C Library 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
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; see the file COPYING.LIB.  If not,
+   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+   Boston, MA 02111-1307, USA.  */
+
+#include <pthread.h>
+#include <setjmp.h>
+#include <signal.h>
+#include <sys/types.h>
+
+/* The signal used for asynchronous cancelation.  */
+#define SIGCANCEL      (SIGTHR+1)
+
+/* Signal needed for the kernel-supported POSIX timer implementation.
+   We can reuse the cancellation signal since we can distinguish
+   cancellation from timer expirations.  */
+#define SIGTIMER       SIGCANCEL
+
+/* Callback to start helper thread.  */
+extern void __start_helper_thread (void) attribute_hidden;
+
+/* Control variable for helper thread creation.  */
+extern pthread_once_t __helper_once attribute_hidden;
+
+/* Whether helper thread is running.  */
+extern int __helper_is_running attribute_hidden;
+
+/* List of active SIGEV_THREAD timers.  */
+extern struct timer *__active_timer_sigev_thread attribute_hidden;
+/* Lock for the __active_timer_sigev_thread.  */
+extern pthread_mutex_t __active_timer_sigev_thread_lock attribute_hidden;
+
+
+/* Type of timers in the kernel.  */
+typedef int kernel_timer_t;
+
+
+/* Internal representation of timer.  */
+struct timer
+{
+  /* Notification mechanism.  */
+  int sigev_notify;
+
+  /* Timer ID returned by the kernel.  */
+  kernel_timer_t ktimerid;
+
+  /* All new elements must be added after ktimerid.  And if the thrfunc
+     element is not the third element anymore the memory allocation in
+     timer_create needs to be changed.  */
+
+  /* Parameters for the thread to be started for SIGEV_THREAD.  */
+  void (*thrfunc) (sigval_t);
+  sigval_t sival;
+  pthread_attr_t attr;
+
+  /* Next element in list of active SIGEV_THREAD timers.  */
+  struct timer *next;
+};
--- /dev/null
+++ b/ports/sysdeps/unix/bsd/bsd4.4/kfreebsd/linuxthreads/timer_create.c
@@ -0,0 +1,232 @@
+/* Copyright (C) 2003,2004, 2007, 2009 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+   Contributed by Ulrich Drepper <drep...@redhat.com>, 2003.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public License as
+   published by the Free Software Foundation; either version 2.1 of the
+   License, or (at your option) any later version.
+
+   The GNU C Library 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
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; see the file COPYING.LIB.  If not,
+   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+   Boston, MA 02111-1307, USA.  */
+
+#include <errno.h>
+#include <pthread.h>
+#include <signal.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <sysdep.h>
+#include <kernel-features.h>
+#include "kernel-posix-timers.h"
+#include "kernel-posix-cpu-timers.h"
+
+#define __ASSUME_POSIX_TIMERS 1
+
+#ifdef SYS_ktimer_create
+# ifndef __ASSUME_POSIX_TIMERS
+static int compat_timer_create (clockid_t clock_id, struct sigevent *evp,
+                               timer_t *timerid);
+#  define timer_create static compat_timer_create
+#  include <nptl/sysdeps/pthread/timer_create.c>
+#  undef timer_create
+
+/* Nonzero if the system calls are not available.  */
+int __no_posix_timers attribute_hidden;
+# endif
+
+# ifdef timer_create_alias
+#  define timer_create timer_create_alias
+# endif
+
+
+int
+timer_create (clock_id, evp, timerid)
+     clockid_t clock_id;
+     struct sigevent *evp;
+     timer_t *timerid;
+{
+# undef timer_create
+# ifndef __ASSUME_POSIX_TIMERS
+  if  (__no_posix_timers >= 0)
+# endif
+    {
+      /* If the user wants notification via a thread we need to handle
+        this special.  */
+      if (evp == NULL
+         || __builtin_expect (evp->sigev_notify != SIGEV_THREAD, 1))
+       {
+         struct sigevent local_evp;
+
+         /* We avoid allocating too much memory by basically
+            using struct timer as a derived class with the
+            first two elements being in the superclass.  We only
+            need these two elements here.  */
+         struct timer *newp = (struct timer *) malloc (offsetof (struct timer,
+                                                                 thrfunc));
+         if (newp == NULL)
+           /* No more memory.  */
+           return -1;
+
+         if (evp == NULL)
+           {
+             /* The kernel has to pass up the timer ID which is a
+                userlevel object.  Therefore we cannot leave it up to
+                the kernel to determine it.  */
+             local_evp.sigev_notify = SIGEV_SIGNAL;
+             local_evp.sigev_signo = SIGALRM;
+             local_evp.sigev_value.sival_ptr = newp;
+
+             evp = &local_evp;
+           }
+
+         kernel_timer_t ktimerid;
+         int retval = INLINE_SYSCALL (ktimer_create, 3, clock_id, evp,
+                                      &ktimerid);
+
+# ifndef __ASSUME_POSIX_TIMERS
+         if (retval != -1 || errno != ENOSYS)
+# endif
+           {
+# ifndef __ASSUME_POSIX_TIMERS
+             __no_posix_timers = 1;
+# endif
+
+             if (retval != -1)
+               {
+                 newp->sigev_notify = (evp != NULL
+                                       ? evp->sigev_notify : SIGEV_SIGNAL);
+                 newp->ktimerid = ktimerid;
+
+                 *timerid = (timer_t) newp;
+               }
+             else
+               {
+                 /* Cannot allocate the timer, fail.  */
+                 free (newp);
+                 retval = -1;
+               }
+
+             return retval;
+           }
+
+         free (newp);
+
+# ifndef __ASSUME_POSIX_TIMERS
+         /* When we come here the syscall does not exist.  Make sure we
+            do not try to use it again.  */
+         __no_posix_timers = -1;
+# endif
+       }
+      else
+       {
+# ifndef __ASSUME_POSIX_TIMERS
+         /* Make sure we have the necessary kernel support.  */
+         if (__no_posix_timers == 0)
+           {
+             INTERNAL_SYSCALL_DECL (err);
+             struct timespec ts;
+             int res;
+             res = INTERNAL_SYSCALL (clock_getres, err, 2,
+                                     CLOCK_REALTIME, &ts);
+             __no_posix_timers = (INTERNAL_SYSCALL_ERROR_P (res, err)
+                                  ? -1 : 1);
+           }
+
+         if (__no_posix_timers > 0)
+# endif
+           {
+             /* Create the helper thread.  */
+             pthread_once (&__helper_once, __start_helper_thread);
+             if (__helper_is_running == 0)
+               {
+                 /* No resources to start the helper thread.  */
+                 __set_errno (EAGAIN);
+                 return -1;
+               }
+
+             struct timer *newp;
+             newp = (struct timer *) malloc (sizeof (struct timer));
+             if (newp == NULL)
+               return -1;
+
+             /* Copy the thread parameters the user provided.  */
+             newp->sival = evp->sigev_value;
+             newp->thrfunc = evp->sigev_notify_function;
+             newp->sigev_notify = SIGEV_THREAD;
+
+             /* We cannot simply copy the thread attributes since the
+                implementation might keep internal information for
+                each instance.  */
+             (void) pthread_attr_init (&newp->attr);
+             if (evp->sigev_notify_attributes != NULL)
+               {
+                 pthread_attr_t *nattr;
+                 pthread_attr_t *oattr;
+
+                 nattr = (pthread_attr_t *) &newp->attr;
+                 oattr = (pthread_attr_t *) evp->sigev_notify_attributes;
+
+                 nattr->__schedparam = oattr->__schedparam;
+                 nattr->__schedpolicy = oattr->__schedpolicy;
+                 nattr->__guardsize = oattr->__guardsize;
+                 nattr->__stackaddr = oattr->__stackaddr;
+                 nattr->__stacksize = oattr->__stacksize;
+               }
+
+             /* In any case set the detach flag.  */
+             (void) pthread_attr_setdetachstate (&newp->attr,
+                                                 PTHREAD_CREATE_DETACHED);
+
+             /* Create the event structure for the kernel timer.  */
+             struct sigevent sev =
+               { .sigev_value.sival_ptr = newp,
+                 .sigev_signo = SIGTIMER,
+                 .sigev_notify = SIGEV_SIGNAL | SIGEV_THREAD_ID,
+               };
+
+             /* Create the timer.  */
+             int res;
+             res = INLINE_SYSCALL (ktimer_create, 3,
+                                     clock_id, &sev, &newp->ktimerid);
+             if (res != -1)
+               {
+                 /* Add to the queue of active timers with thread
+                    delivery.  */
+                 pthread_mutex_lock (&__active_timer_sigev_thread_lock);
+                 newp->next = __active_timer_sigev_thread;
+                 __active_timer_sigev_thread = newp;
+                 pthread_mutex_unlock (&__active_timer_sigev_thread_lock);
+
+                 *timerid = (timer_t) newp;
+                 return 0;
+               }
+
+             /* Free the resources.  */
+             free (newp);
+
+             return -1;
+           }
+       }
+    }
+
+# ifndef __ASSUME_POSIX_TIMERS
+  /* Compatibility code.  */
+  return compat_timer_create (clock_id, evp, timerid);
+# endif
+}
+#else
+# ifdef timer_create_alias
+#  define timer_create timer_create_alias
+# endif
+/* The new system calls are not available.  Use the userlevel
+   implementation.  */
+# include <nptl/sysdeps/pthread/timer_create.c>
+#endif
--- /dev/null
+++ b/ports/sysdeps/unix/bsd/bsd4.4/kfreebsd/linuxthreads/timer_delete.c
@@ -0,0 +1,116 @@
+/* Copyright (C) 2003, 2007 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+   Contributed by Ulrich Drepper <drep...@redhat.com>, 2003.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public License as
+   published by the Free Software Foundation; either version 2.1 of the
+   License, or (at your option) any later version.
+
+   The GNU C Library 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
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; see the file COPYING.LIB.  If not,
+   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+   Boston, MA 02111-1307, USA.  */
+
+#include <errno.h>
+#include <stdlib.h>
+#include <time.h>
+#include <sysdep.h>
+#include <kernel-features.h>
+#include "kernel-posix-timers.h"
+
+#define __ASSUME_POSIX_TIMERS 1
+
+#ifdef SYS_ktimer_delete
+# ifndef __ASSUME_POSIX_TIMERS
+static int compat_timer_delete (timer_t timerid);
+#  define timer_delete static compat_timer_delete
+#  include <nptl/sysdeps/pthread/timer_delete.c>
+#  undef timer_delete
+# endif
+
+# ifdef timer_delete_alias
+#  define timer_delete timer_delete_alias
+# endif
+
+
+int
+timer_delete (timerid)
+     timer_t timerid;
+{
+# undef timer_delete
+# ifndef __ASSUME_POSIX_TIMERS
+  if (__no_posix_timers >= 0)
+# endif
+    {
+      struct timer *kt = (struct timer *) timerid;
+
+      /* Delete the kernel timer object.  */
+      int res = INLINE_SYSCALL (ktimer_delete, 1, kt->ktimerid);
+
+      if (res == 0)
+       {
+         if (kt->sigev_notify == SIGEV_THREAD)
+           {
+             /* Remove the timer from the list.  */
+             pthread_mutex_lock (&__active_timer_sigev_thread_lock);
+             if (__active_timer_sigev_thread == kt)
+               __active_timer_sigev_thread = kt->next;
+             else
+               {
+                 struct timer *prevp = __active_timer_sigev_thread;
+                 while (prevp->next != NULL)
+                   if (prevp->next == kt)
+                     {
+                       prevp->next = kt->next;
+                       break;
+                     }
+                   else
+                     prevp = prevp->next;
+               }
+             pthread_mutex_unlock (&__active_timer_sigev_thread_lock);
+           }
+
+# ifndef __ASSUME_POSIX_TIMERS
+         /* We know the syscall support is available.  */
+         __no_posix_timers = 1;
+# endif
+
+         /* Free the memory.  */
+         (void) free (kt);
+
+         return 0;
+       }
+
+      /* The kernel timer is not known or something else bad happened.
+        Return the error.  */
+# ifndef __ASSUME_POSIX_TIMERS
+      if (errno != ENOSYS)
+       {
+         __no_posix_timers = 1;
+# endif
+         return -1;
+# ifndef __ASSUME_POSIX_TIMERS
+       }
+
+      __no_posix_timers = -1;
+# endif
+    }
+
+# ifndef __ASSUME_POSIX_TIMERS
+  return compat_timer_delete (timerid);
+# endif
+}
+#else
+# ifdef timer_delete_alias
+#  define timer_delete timer_delete_alias
+# endif
+/* The new system calls are not available.  Use the userlevel
+   implementation.  */
+# include <nptl/sysdeps/pthread/timer_delete.c>
+#endif
--- /dev/null
+++ b/ports/sysdeps/unix/bsd/bsd4.4/kfreebsd/linuxthreads/timer_gettime.c
@@ -0,0 +1,84 @@
+/* Copyright (C) 2003 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+   Contributed by Ulrich Drepper <drep...@redhat.com>, 2003.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public License as
+   published by the Free Software Foundation; either version 2.1 of the
+   License, or (at your option) any later version.
+
+   The GNU C Library 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
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; see the file COPYING.LIB.  If not,
+   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+   Boston, MA 02111-1307, USA.  */
+
+#include <errno.h>
+#include <stdlib.h>
+#include <time.h>
+#include <sysdep.h>
+#include <kernel-features.h>
+#include "kernel-posix-timers.h"
+
+#define __ASSUME_POSIX_TIMERS 1
+
+#ifdef SYS_ktimer_gettime
+# ifndef __ASSUME_POSIX_TIMERS
+static int compat_timer_gettime (timer_t timerid, struct itimerspec *value);
+#  define timer_gettime static compat_timer_gettime
+#  include <nptl/sysdeps/pthread/timer_gettime.c>
+#  undef timer_gettime
+# endif
+
+# ifdef timer_gettime_alias
+#  define timer_gettime timer_gettime_alias
+# endif
+
+
+int
+timer_gettime (timerid, value)
+     timer_t timerid;
+     struct itimerspec *value;
+{
+# undef timer_gettime
+# ifndef __ASSUME_POSIX_TIMERS
+  if (__no_posix_timers >= 0)
+# endif
+    {
+      struct timer *kt = (struct timer *) timerid;
+
+      /* Delete the kernel timer object.  */
+      int res = INLINE_SYSCALL (ktimer_gettime, 2, kt->ktimerid, value);
+
+# ifndef __ASSUME_POSIX_TIMERS
+      if (res != -1 || errno != ENOSYS)
+       {
+         /* We know the syscall support is available.  */
+         __no_posix_timers = 1;
+# endif
+         return res;
+# ifndef __ASSUME_POSIX_TIMERS
+       }
+# endif
+
+# ifndef __ASSUME_POSIX_TIMERS
+      __no_posix_timers = -1;
+# endif
+    }
+
+# ifndef __ASSUME_POSIX_TIMERS
+  return compat_timer_gettime (timerid, value);
+# endif
+}
+#else
+# ifdef timer_gettime_alias
+#  define timer_gettime timer_gettime_alias
+# endif
+/* The new system calls are not available.  Use the userlevel
+   implementation.  */
+# include <nptl/sysdeps/pthread/timer_gettime.c>
+#endif
--- /dev/null
+++ b/ports/sysdeps/unix/bsd/bsd4.4/kfreebsd/linuxthreads/timer_settime.c
@@ -0,0 +1,90 @@
+/* Copyright (C) 2003 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+   Contributed by Ulrich Drepper <drep...@redhat.com>, 2003.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public License as
+   published by the Free Software Foundation; either version 2.1 of the
+   License, or (at your option) any later version.
+
+   The GNU C Library 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
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; see the file COPYING.LIB.  If not,
+   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+   Boston, MA 02111-1307, USA.  */
+
+#include <errno.h>
+#include <stdlib.h>
+#include <time.h>
+#include <sysdep.h>
+#include <kernel-features.h>
+#include "kernel-posix-timers.h"
+
+
+#define __ASSUME_POSIX_TIMERS 1
+
+#ifdef SYS_ktimer_settime
+# ifndef __ASSUME_POSIX_TIMERS
+static int compat_timer_settime (timer_t timerid, int flags,
+                                const struct itimerspec *value,
+                                struct itimerspec *ovalue);
+#  define timer_settime static compat_timer_settime
+#  include <nptl/sysdeps/pthread/timer_settime.c>
+#  undef timer_settime
+# endif
+
+# ifdef timer_settime_alias
+#  define timer_settime timer_settime_alias
+# endif
+
+
+int
+timer_settime (timerid, flags, value, ovalue)
+     timer_t timerid;
+     int flags;
+     const struct itimerspec *value;
+     struct itimerspec *ovalue;
+{
+# undef timer_settime
+# ifndef __ASSUME_POSIX_TIMERS
+  if (__no_posix_timers >= 0)
+# endif
+    {
+      struct timer *kt = (struct timer *) timerid;
+
+      /* Delete the kernel timer object.  */
+      int res = INLINE_SYSCALL (ktimer_settime, 4, kt->ktimerid, flags,
+                               value, ovalue);
+
+# ifndef __ASSUME_POSIX_TIMERS
+      if (res != -1 || errno != ENOSYS)
+       {
+         /* We know the syscall support is available.  */
+         __no_posix_timers = 1;
+# endif
+         return res;
+# ifndef __ASSUME_POSIX_TIMERS
+       }
+# endif
+
+# ifndef __ASSUME_POSIX_TIMERS
+      __no_posix_timers = -1;
+# endif
+    }
+
+# ifndef __ASSUME_POSIX_TIMERS
+  return compat_timer_settime (timerid, flags, value, ovalue);
+# endif
+}
+#else
+# ifdef timer_settime_alias
+#  define timer_settime timer_settime_alias
+# endif
+/* The new system calls are not available.  Use the userlevel
+   implementation.  */
+# include <nptl/sysdeps/pthread/timer_settime.c>
+#endif
--- a/ports/sysdeps/unix/bsd/bsd4.4/kfreebsd/syscalls.list
+++ b/ports/sysdeps/unix/bsd/bsd4.4/kfreebsd/syscalls.list
@@ -18,6 +18,11 @@
 sys_clock_getres       -       clock_getres            i:ip            
__syscall_clock_getres
 sys_clock_gettime      -       clock_gettime           i:ip            
__syscall_clock_gettime
 sys_clock_settime      -       clock_settime           i:ip            
__syscall_clock_settime
+sys_ktimer_create      -       ktimer_create           i:iPp           
__syscall_ktimer_create
+sys_ktimer_gettime     -       ktimer_gettime          i:ip            
__syscall_ktimer_gettime
+sys_ktimer_settime     -       ktimer_settime          i:ip            
__syscall_ktimer_settime
+sys_ktimer_getoverrun  -       ktimer_getoverrun       i:i             
__syscall_ktimer_getoverrun
+sys_ktimer_delete      -       ktimer_delete           i:i             
__syscall_ktimer_delete
 sys_execve             -       execve                  i:ppp           
__syscall_execve
 extattrctl             -       extattrctl              i:sisI          
extattrctl
 extattr_delete_file    -       extattr_delete_file     i:ss            
extattr_delete_file
--- a/ports/sysdeps/unix/bsd/bsd4.4/kfreebsd/Makefile
+++ b/ports/sysdeps/unix/bsd/bsd4.4/kfreebsd/Makefile
@@ -90,6 +90,7 @@
 # for INLINE_SYSCALL
 sysdep_routines += sys_fork sys_execve sys_sigaction sys_close sys_fcntl
 sysdep_routines += sys_clock_getres sys_clock_gettime sys_clock_settime
+sysdep_routines += sys_ktimer_create sys_ktimer_gettime sys_ktimer_settime 
sys_ktimer_getoverrun sys_ktimer_delete
 sysdep_routines += sys_shm_open sys_shm_unlink sys_pselect sys_semctl
 endif
 
--- a/ports/sysdeps/unix/bsd/bsd4.4/kfreebsd/Versions
+++ b/ports/sysdeps/unix/bsd/bsd4.4/kfreebsd/Versions
@@ -108,6 +108,7 @@
     __syscall_wait4; __syscall_fcntl;
     # needed by librt as INLINE_SYSCALL:
     __syscall_clock_getres; __syscall_clock_gettime; __syscall_clock_settime;
+    __syscall_ktimer_create; __syscall_ktimer_gettime; 
__syscall_ktimer_settime; __syscall_ktimer_getoverrun; __syscall_ktimer_delete;
     __syscall_shm_open; __syscall_shm_unlink;
     # misc fixes for FreeBSD:
     __syscall_freebsd6_lseek; __syscall_freebsd6_pread; 
__syscall_freebsd6_pwrite;
--- a/ports/sysdeps/unix/bsd/bsd4.4/kfreebsd/bits/signum.h
+++ b/ports/sysdeps/unix/bsd/bsd4.4/kfreebsd/bits/signum.h
@@ -65,6 +65,8 @@
 #define SIGINFO                29      /* Information request (4.4 BSD).  */
 #define        SIGUSR1         30      /* User-defined signal 1 (POSIX).  */
 #define        SIGUSR2         31      /* User-defined signal 2 (POSIX).  */
+#define SIGTHR         32      /* Reserved by thread library. */
+#define SIGLWP         SIGTHR
 
 #define        _NSIG           129     /* Biggest signal number + 1
                                   (including real-time signals).  */
--- a/ports/sysdeps/unix/bsd/bsd4.4/kfreebsd/linuxthreads/Makefile
+++ b/ports/sysdeps/unix/bsd/bsd4.4/kfreebsd/linuxthreads/Makefile
@@ -2,3 +2,7 @@
 sysdep_routines += register-atfork unregister-atfork
 libpthread-routines += ptw-sigprocmask ptw-ioctl
 endif
+
+ifeq ($(subdir),rt)
+librt-sysdep_routines += timer_routines
+endif
--- /dev/null
+++ b/ports/sysdeps/unix/bsd/bsd4.4/kfreebsd/linuxthreads/timer_routines.c
@@ -0,0 +1,200 @@
+/* Copyright (C) 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+   Contributed by Ulrich Drepper <drep...@redhat.com>, 2003.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public License as
+   published by the Free Software Foundation; either version 2.1 of the
+   License, or (at your option) any later version.
+
+   The GNU C Library 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
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; see the file COPYING.LIB.  If not,
+   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+   Boston, MA 02111-1307, USA.  */
+
+#include <errno.h>
+#include <setjmp.h>
+#include <signal.h>
+#include <stdbool.h>
+#include <sysdep.h>
+#include <linuxthreads/internals.h>    /* LIBC_CANCEL_ASYNC */
+#include <kernel-features.h>
+#include "kernel-posix-timers.h"
+
+#define __ASSUME_POSIX_TIMERS 1
+
+/* List of active SIGEV_THREAD timers.  */
+struct timer *__active_timer_sigev_thread;
+/* Lock for the __active_timer_sigev_thread.  */
+pthread_mutex_t __active_timer_sigev_thread_lock = PTHREAD_MUTEX_INITIALIZER;
+
+
+struct thread_start_data
+{
+  void (*thrfunc) (sigval_t);
+  sigval_t sival;
+};
+
+
+#ifdef SYS_ktimer_create
+/* Helper thread to call the user-provided function.  */
+static void *
+timer_sigev_thread (void *arg)
+{
+  /* The parent thread has all signals blocked.  This is a bit
+     surprising for user code, although valid.  We unblock all
+     signals.  */
+  sigset_t ss;
+  sigemptyset (&ss);
+  sigprocmask (SIG_SETMASK, &ss, NULL);
+
+  struct thread_start_data *td = (struct thread_start_data *) arg;
+
+  void (*thrfunc) (sigval_t) = td->thrfunc;
+  sigval_t sival = td->sival;
+
+  /* The TD object was allocated in timer_helper_thread.  */
+  free (td);
+
+  /* Call the user-provided function.  */
+  thrfunc (sival);
+
+  return NULL;
+}
+
+
+/* Helper function to support starting threads for SIGEV_THREAD.  */
+static void *
+timer_helper_thread (void *arg)
+{
+  /* Wait for the SIGTIMER signal, allowing the setXid signal, and
+     none else.  */
+  sigset_t ss;
+  sigemptyset (&ss);
+  __sigaddset (&ss, SIGTIMER);
+
+  /* Endless loop of waiting for signals.  The loop is only ended when
+     the thread is canceled.  */
+  while (1)
+    {
+      siginfo_t si;
+
+      /* sigwaitinfo cannot be used here, since it deletes
+        SIGCANCEL == SIGTIMER from the set.  */
+
+      int oldtype = LIBC_CANCEL_ASYNC ();
+
+      /* XXX The size argument hopefully will have to be changed to the
+        real size of the user-level sigset_t.  */
+      int result = sigtimedwait (&ss, &si, NULL);
+
+      LIBC_CANCEL_RESET (oldtype);
+
+      if (result > 0)
+       {
+         if (si.si_code == SI_TIMER)
+           {
+             struct timer *tk = (struct timer *) si.si_value.sival_ptr;
+
+             /* Check the timer is still used and will not go away
+                while we are reading the values here.  */
+             pthread_mutex_lock (&__active_timer_sigev_thread_lock);
+
+             struct timer *runp = __active_timer_sigev_thread;
+             while (runp != NULL)
+               if (runp == tk)
+                 break;
+               else
+                 runp = runp->next;
+
+             if (runp != NULL)
+               {
+                 struct thread_start_data *td = malloc (sizeof (*td));
+
+                 /* There is not much we can do if the allocation fails.  */
+                 if (td != NULL)
+                   {
+                     /* This is the signal we are waiting for.  */
+                     td->thrfunc = tk->thrfunc;
+                     td->sival = tk->sival;
+
+                     pthread_t th;
+                     (void) pthread_create (&th, &tk->attr,
+                                            timer_sigev_thread, td);
+                   }
+               }
+
+             pthread_mutex_unlock (&__active_timer_sigev_thread_lock);
+           }
+         else if (si.si_code == SI_LWP)
+           /* The thread is canceled.  */
+           pthread_exit (NULL);
+       }
+    }
+}
+
+
+/* Control variable for helper thread creation.  */
+pthread_once_t __helper_once attribute_hidden;
+
+
+/* TID of the helper thread.  */
+int __helper_is_running attribute_hidden;
+
+
+/* Reset variables so that after a fork a new helper thread gets started.  */
+static void
+reset_helper_control (void)
+{
+  __helper_once = PTHREAD_ONCE_INIT;
+  __helper_is_running = 0;
+}
+
+void
+attribute_hidden
+__start_helper_thread (void)
+{
+  /* The helper thread needs only very little resources
+     and should go away automatically when canceled.  */
+  pthread_attr_t attr;
+  (void) pthread_attr_init (&attr);
+  (void) pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN);
+
+  /* Block all signals in the helper thread but SIGSETXID.  To do this
+     thoroughly we temporarily have to block all signals here.  The
+     helper can lose wakeups if SIGCANCEL is not blocked throughout,
+     but sigfillset omits it SIGSETXID.  So, we add SIGCANCEL back
+     explicitly here.  */
+  sigset_t ss;
+  sigset_t oss;
+  sigfillset (&ss);
+  __sigaddset (&ss, SIGCANCEL);
+  sigprocmask (SIG_SETMASK, &ss, &oss);
+
+  /* Create the helper thread for this timer.  */
+  pthread_t th;
+  int res = pthread_create (&th, &attr, timer_helper_thread, NULL);
+  if (res == 0)
+    /* We managed to start the helper thread.  */
+    __helper_is_running = 1;
+
+  /* Restore the signal mask.  */
+  sigprocmask (SIG_SETMASK, &oss, NULL);
+
+  /* No need for the attribute anymore.  */
+  (void) pthread_attr_destroy (&attr);
+
+  /* We have to make sure that after fork()ing a new helper thread can
+     be created.  */
+  pthread_atfork (NULL, NULL, reset_helper_control);
+}
+#endif
+
+#ifndef __ASSUME_POSIX_TIMERS
+# include <nptl/sysdeps/pthread/timer_routines.c>
+#endif
--- a/ports/sysdeps/unix/bsd/bsd4.4/kfreebsd/bits/siginfo.h
+++ b/ports/sysdeps/unix/bsd/bsd4.4/kfreebsd/bits/siginfo.h
@@ -69,6 +69,8 @@
 # define SI_QUEUE      SI_QUEUE
   SI_USER = 0x10001,           /* Sent by kill, sigsend, raise.  */
 # define SI_USER       SI_USER
+  SI_LWP = 0x10007,            /* Sent by thr_kill.  */
+# define SI_LWP                SI_LWP
   SI_UNDEFINED = 0
 # define SI_UNDEFINED  SI_UNDEFINED
 };
--- /dev/null
+++ b/ports/sysdeps/unix/bsd/bsd4.4/kfreebsd/linuxthreads/timer_getoverr.c
@@ -0,0 +1,82 @@
+/* Copyright (C) 2003 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+   Contributed by Ulrich Drepper <drep...@redhat.com>, 2003.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public License as
+   published by the Free Software Foundation; either version 2.1 of the
+   License, or (at your option) any later version.
+
+   The GNU C Library 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
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; see the file COPYING.LIB.  If not,
+   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+   Boston, MA 02111-1307, USA.  */
+
+#include <errno.h>
+#include <time.h>
+#include <sysdep.h>
+#include <kernel-features.h>
+#include "kernel-posix-timers.h"
+
+#define __ASSUME_POSIX_TIMERS 1
+
+#ifdef SYS_ktimer_getoverrun
+# ifndef __ASSUME_POSIX_TIMERS
+static int compat_timer_getoverrun (timer_t timerid);
+#  define timer_getoverrun static compat_timer_getoverrun
+#  include <nptl/sysdeps/pthread/timer_getoverr.c>
+#  undef timer_getoverrun
+# endif
+
+# ifdef timer_getoverrun_alias
+#  define timer_getoverrun timer_getoverrun_alias
+# endif
+
+
+int
+timer_getoverrun (timerid)
+     timer_t timerid;
+{
+# undef timer_getoverrun
+# ifndef __ASSUME_POSIX_TIMERS
+  if (__no_posix_timers >= 0)
+# endif
+    {
+      struct timer *kt = (struct timer *) timerid;
+
+      /* Get the information from the kernel.  */
+      int res = INLINE_SYSCALL (ktimer_getoverrun, 1, kt->ktimerid);
+
+# ifndef __ASSUME_POSIX_TIMERS
+      if (res != -1 || errno != ENOSYS)
+       {
+         /* We know the syscall support is available.  */
+         __no_posix_timers = 1;
+# endif
+         return res;
+# ifndef __ASSUME_POSIX_TIMERS
+       }
+# endif
+
+# ifndef __ASSUME_POSIX_TIMERS
+      __no_posix_timers = -1;
+# endif
+    }
+
+# ifndef __ASSUME_POSIX_TIMERS
+  return compat_timer_getoverrun (timerid);
+# endif
+}
+#else
+# ifdef timer_getoverrun_alias
+#  define timer_getoverrun timer_getoverrun_alias
+# endif
+/* The new system calls are not available.  Use the userlevel
+   implementation.  */
+# include <nptl/sysdeps/pthread/timer_getoverr.c>
+#endif

Reply via email to