(root)/
coreutils-9.4/
lib/
pthread_mutex_timedlock.c
       1  /* Lock a mutex, abandoning after a certain time.
       2     Copyright (C) 2019-2023 Free Software Foundation, Inc.
       3  
       4     This file is free software: you can redistribute it and/or modify
       5     it under the terms of the GNU Lesser General Public License as
       6     published by the Free Software Foundation; either version 2.1 of the
       7     License, or (at your option) any later version.
       8  
       9     This file is distributed in the hope that it will be useful,
      10     but WITHOUT ANY WARRANTY; without even the implied warranty of
      11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12     GNU Lesser General Public License for more details.
      13  
      14     You should have received a copy of the GNU Lesser General Public License
      15     along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
      16  
      17  #include <config.h>
      18  
      19  /* Specification.  */
      20  #include <pthread.h>
      21  
      22  #include <errno.h>
      23  #include <limits.h>
      24  #include <sys/time.h>
      25  #include <time.h>
      26  
      27  #ifndef MIN
      28  # define MIN(a, b) ((a) < (b) ? (a) : (b))
      29  #endif
      30  
      31  int
      32  pthread_mutex_timedlock (pthread_mutex_t *mutex, const struct timespec *abstime)
      33  {
      34    /* Poll the mutex's state in regular intervals.  Ugh.  */
      35    /* POSIX says:
      36        "Under no circumstance shall the function fail with a timeout if
      37         the mutex can be locked immediately. The validity of the abstime
      38         parameter need not be checked if the mutex can be locked
      39         immediately."
      40       Therefore start the loop with a pthread_mutex_trylock call.  */
      41    for (;;)
      42      {
      43        int err;
      44        struct timeval currtime;
      45        unsigned long remaining;
      46  
      47        err = pthread_mutex_trylock (mutex);
      48        if (err != EBUSY)
      49          return err;
      50  
      51        gettimeofday (&currtime, NULL);
      52  
      53        if (currtime.tv_sec > abstime->tv_sec)
      54          remaining = 0;
      55        else
      56          {
      57            unsigned long seconds = abstime->tv_sec - currtime.tv_sec;
      58            remaining = seconds * 1000000000;
      59            if (remaining / 1000000000 != seconds) /* overflow? */
      60              remaining = ULONG_MAX;
      61            else
      62              {
      63                long nanoseconds =
      64                  abstime->tv_nsec - currtime.tv_usec * 1000;
      65                if (nanoseconds >= 0)
      66                  {
      67                    remaining += nanoseconds;
      68                    if (remaining < nanoseconds) /* overflow? */
      69                      remaining = ULONG_MAX;
      70                  }
      71                else
      72                  {
      73                    if (remaining >= - nanoseconds)
      74                      remaining -= (- nanoseconds);
      75                    else
      76                      remaining = 0;
      77                  }
      78              }
      79          }
      80        if (remaining == 0)
      81          return ETIMEDOUT;
      82  
      83        /* Sleep 1 ms.  */
      84        struct timespec duration =
      85          {
      86            .tv_sec = 0,
      87            .tv_nsec = MIN (1000000, remaining)
      88          };
      89        nanosleep (&duration, NULL);
      90      }
      91  }