(root)/
glibc-2.38/
sysdeps/
pthread/
tst-timer.c
       1  /* Tests for POSIX timer implementation.
       2     Copyright (C) 2000-2023 Free Software Foundation, Inc.
       3     This file is part of the GNU C Library.
       4  
       5     The GNU C Library is free software; you can redistribute it and/or
       6     modify it under the terms of the GNU Lesser General Public License as
       7     published by the Free Software Foundation; either version 2.1 of the
       8     License, or (at your option) any later version.
       9  
      10     The GNU C Library is distributed in the hope that it will be useful,
      11     but WITHOUT ANY WARRANTY; without even the implied warranty of
      12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      13     Lesser General Public License for more details.
      14  
      15     You should have received a copy of the GNU Lesser General Public
      16     License along with the GNU C Library; see the file COPYING.LIB.  If
      17     not, see <https://www.gnu.org/licenses/>.  */
      18  
      19  #include <errno.h>
      20  #include <signal.h>
      21  #include <stdio.h>
      22  #include <time.h>
      23  #include <unistd.h>
      24  #include <stdlib.h>
      25  #include <stdint.h>
      26  
      27  #include <support/xunistd.h>
      28  
      29  static void
      30  notify_func1 (union sigval sigval)
      31  {
      32    puts ("notify_func1");
      33  }
      34  
      35  
      36  static void
      37  notify_func2 (union sigval sigval)
      38  {
      39    puts ("notify_func2");
      40  }
      41  
      42  
      43  static void
      44  signal_func (int sig)
      45  {
      46    static const char text[] = "signal_func\n";
      47    signal (sig, signal_func);
      48    xwrite (STDOUT_FILENO, text, sizeof text - 1);
      49  }
      50  
      51  static void
      52  intr_sleep (int sec)
      53  {
      54    struct timespec ts;
      55  
      56    ts.tv_sec = sec;
      57    ts.tv_nsec = 0;
      58  
      59    while (nanosleep (&ts, &ts) == -1 && errno == EINTR)
      60      ;
      61  }
      62  
      63  #define ZSIGALRM 14
      64  
      65  
      66  int
      67  main (void)
      68  {
      69    struct timespec ts;
      70    timer_t timer_sig, timer_thr1, timer_thr2;
      71    int retval;
      72    struct sigevent sigev1 =
      73    {
      74      .sigev_notify = SIGEV_SIGNAL,
      75      .sigev_signo = ZSIGALRM
      76    };
      77    struct sigevent sigev2;
      78    struct itimerspec itimer1 = { { 0, 200000000 }, { 0, 200000000 } };
      79    struct itimerspec itimer2 = { { 0, 100000000 }, { 0, 500000000 } };
      80    struct itimerspec itimer3 = { { 0, 150000000 }, { 0, 300000000 } };
      81    struct itimerspec old;
      82  
      83    retval = clock_gettime (CLOCK_REALTIME, &ts);
      84  
      85    sigev2.sigev_notify = SIGEV_THREAD;
      86    sigev2.sigev_notify_function = notify_func1;
      87    sigev2.sigev_notify_attributes = NULL;
      88    /* It is unnecessary to do the following but to set a good example
      89       we do it anyhow.  */
      90    sigev2.sigev_value.sival_ptr = NULL;
      91  
      92    setvbuf (stdout, 0, _IOLBF, 0);
      93  
      94    printf ("clock_gettime returned %d, timespec = { %jd, %jd }\n",
      95  	  retval, (intmax_t) ts.tv_sec, (intmax_t) ts.tv_nsec);
      96  
      97    retval = clock_getres (CLOCK_REALTIME, &ts);
      98  
      99    printf ("clock_getres returned %d, timespec = { %jd, %jd }\n",
     100  	  retval, (intmax_t) ts.tv_sec, (intmax_t) ts.tv_nsec);
     101  
     102    if (timer_create (CLOCK_REALTIME, &sigev1, &timer_sig) != 0)
     103      {
     104        printf ("timer_create for timer_sig failed: %m\n");
     105        exit (1);
     106      }
     107    if (timer_create (CLOCK_REALTIME, &sigev2, &timer_thr1) != 0)
     108      {
     109        printf ("timer_create for timer_thr1 failed: %m\n");
     110        exit (1);
     111      }
     112    sigev2.sigev_notify_function = notify_func2;
     113    if (timer_create (CLOCK_REALTIME, &sigev2, &timer_thr2) != 0)
     114      {
     115        printf ("timer_create for timer_thr2 failed: %m\n");
     116        exit (1);
     117      }
     118  
     119    if (timer_settime (timer_thr1, 0, &itimer2, &old) != 0)
     120      {
     121        printf ("timer_settime for timer_thr1 failed: %m\n");
     122        exit (1);
     123      }
     124    if (timer_settime (timer_thr2, 0, &itimer3, &old) != 0)
     125      {
     126        printf ("timer_settime for timer_thr2 failed: %m\n");
     127        exit (1);
     128      }
     129  
     130    signal (ZSIGALRM, signal_func);
     131  
     132    if (timer_settime (timer_sig, 0, &itimer1, &old) != 0)
     133      {
     134        printf ("timer_settime for timer_sig failed: %m\n");
     135        exit (1);
     136      }
     137  
     138    intr_sleep (3);
     139  
     140    if (timer_delete (timer_sig) != 0)
     141      {
     142        printf ("timer_delete for timer_sig failed: %m\n");
     143        exit (1);
     144      }
     145    if (timer_delete (timer_thr1) != 0)
     146      {
     147        printf ("timer_delete for timer_thr1 failed: %m\n");
     148        exit (1);
     149      }
     150  
     151    intr_sleep (3);
     152  
     153    if (timer_delete (timer_thr2) != 0)
     154      {
     155        printf ("timer_delete for timer_thr2 failed: %m\n");
     156        exit (1);
     157      }
     158  
     159    return 0;
     160  }