summaryrefslogtreecommitdiff
path: root/sysdeps/mach/hurd/pt-rwlock-unlock.c
blob: 31622792279ad131655fcf3a89159571ea3dcc6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/* Copyright (C) 2016 Free Software Foundation, Inc.
   Contributed by Agustina Arzille <avarzille@riseup.net>, 2016.

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License
   as published by the Free Software Foundation; either
   version 2 of the license, or (at your option) any later version.

   This program 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 Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with this program; if not, see
   <http://www.gnu.org/licenses/>.
*/

#include <pthread.h>
#include <pt-internal.h>
#include <bits/pt-atomic.h>
#include <hurdlock.h>
#include "pt-rwlock.h"

int __pthread_rwlock_unlock (pthread_rwlock_t *rwp)
{
  int flags = rwp->__flags & GSYNC_SHARED;
  atomic_read_barrier ();
  unsigned int owner = rwl_oid (rwp->__oid_nrd);

  if ((owner & ID_MASK) != 0)
    {
      /* A writer holds the lock. */
      if (!rwl_owned_p (rwp, early_pthread_self(), flags))
        /* ... But it isn't us. */
        return (EPERM);

      rwl_spid(rwp) = 0;
      rwl_oid(rwp->__oid_nrd) = RWLOCK_UNOWNED;
      atomic_write_barrier ();

      /* The exclusive lock has been released. Now decide whether
       * to wake a queued writer (preferred), or all the queued readers. */
      if (rwl_qwr (rwp->__oid_nrd) > 0)
        lll_wake (&rwl_qwr(rwp->__oid_nrd), flags);
      else if (rwl_nrd (rwp->__oid_nrd) > 0)
        lll_wake (&rwl_oid(rwp->__oid_nrd), flags | GSYNC_BROADCAST);
    }
  else if (rwl_nrd (rwp->__oid_nrd) == 0)
    return (EPERM);
  else
    {
      union __hurd_xint tmp;
      while (1)
        {
          tmp.__qv = atomic_loadx (&rwp->__oid_nrd.__qv);
          if (catomic_casx_bool (&rwp->__oid_nrd.__qv,
              hurd_xint_pair (tmp.__lo, tmp.__hi),
              hurd_xint_pair (rwl_nrd (tmp) == 1 ?
                RWLOCK_UNOWNED : RWLOCK_RO, rwl_nrd (tmp) - 1)))
            break;
        }

      /* As a reader, we only need to do a wakeup iff:
       * - We were the last one.
       * - There's at least a writer queued. */
      if (rwl_nrd (tmp) == 1 && rwl_qwr (rwp->__oid_nrd) > 0)
        lll_wake (&rwl_qwr(rwp->__oid_nrd), flags);
    }

  return (0);
}

strong_alias (__pthread_rwlock_unlock, pthread_rwlock_unlock)