(root)/
glibc-2.38/
sysdeps/
htl/
pt-rwlock-timedwrlock.c
       1  /* Acquire a rwlock for writing.  Generic version.
       2     Copyright (C) 2002-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
       7     License as published by the Free Software Foundation; either
       8     version 2.1 of the 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;  if not, see
      17     <https://www.gnu.org/licenses/>.  */
      18  
      19  #include <pthread.h>
      20  #include <assert.h>
      21  #include <time.h>
      22  
      23  #include <pt-internal.h>
      24  
      25  /* Acquire RWLOCK for writing blocking until *ABSTIME if we cannot get
      26     it.  As a special GNU extension, if ABSTIME is NULL then the wait
      27     shall not time out.  */
      28  int
      29  __pthread_rwlock_timedwrlock_internal (struct __pthread_rwlock *rwlock,
      30  				       clockid_t clockid,
      31  				       const struct timespec *abstime)
      32  {
      33    error_t err;
      34    int drain;
      35    struct __pthread *self;
      36  
      37    __pthread_spin_wait (&rwlock->__lock);
      38    if (__pthread_spin_trylock (&rwlock->__held) == 0)
      39      /* Successfully acquired the lock.  */
      40      {
      41        assert (rwlock->__readerqueue == 0);
      42        assert (rwlock->__writerqueue == 0);
      43        assert (rwlock->__readers == 0);
      44  
      45        __pthread_spin_unlock (&rwlock->__lock);
      46        return 0;
      47      }
      48  
      49    /* The lock is busy.  */
      50  
      51    if (abstime != NULL && ! valid_nanoseconds (abstime->tv_nsec))
      52      {
      53        __pthread_spin_unlock (&rwlock->__lock);
      54        return EINVAL;
      55      }
      56  
      57    self = _pthread_self ();
      58  
      59    /* Add ourselves to the queue.  */
      60    __pthread_enqueue (&rwlock->__writerqueue, self);
      61    __pthread_spin_unlock (&rwlock->__lock);
      62  
      63    /* Block the thread.  */
      64    if (abstime != NULL)
      65      err = __pthread_timedblock (self, abstime, clockid);
      66    else
      67      {
      68        err = 0;
      69        __pthread_block (self);
      70      }
      71  
      72    __pthread_spin_wait (&rwlock->__lock);
      73    if (self->prevp == NULL)
      74      /* Another thread removed us from the queue, which means a wakeup message
      75         has been sent.  It was either consumed while we were blocking, or
      76         queued after we timed out and before we acquired the rwlock lock, in
      77         which case the message queue must be drained.  */
      78      drain = err ? 1 : 0;
      79    else
      80      {
      81        /* We're still in the queue.  No one attempted to wake us up, i.e. we
      82           timed out.  */
      83        __pthread_dequeue (self);
      84        drain = 0;
      85      }
      86    __pthread_spin_unlock (&rwlock->__lock);
      87  
      88    if (drain)
      89      __pthread_block (self);
      90  
      91    if (err)
      92      {
      93        assert (err == ETIMEDOUT);
      94        return err;
      95      }
      96  
      97    assert (rwlock->__readers == 0);
      98  
      99    return 0;
     100  }
     101  
     102  int
     103  __pthread_rwlock_timedwrlock (struct __pthread_rwlock *rwlock,
     104  			      const struct timespec *abstime)
     105  {
     106    return __pthread_rwlock_timedwrlock_internal (rwlock, CLOCK_REALTIME, abstime);
     107  }
     108  weak_alias (__pthread_rwlock_timedwrlock, pthread_rwlock_timedwrlock)
     109  
     110  int
     111  __pthread_rwlock_clockwrlock (struct __pthread_rwlock *rwlock,
     112  			      clockid_t clockid,
     113  			      const struct timespec *abstime)
     114  {
     115    return __pthread_rwlock_timedwrlock_internal (rwlock, clockid, abstime);
     116  }
     117  weak_alias (__pthread_rwlock_clockwrlock, pthread_rwlock_clockwrlock)