.
[glibc/history.git] / nptl / pthread_rwlock_init.c
blob27f25ac2abaaf5fe1fc9a209977298f864ad3b15
1 /* Copyright (C) 2002, 2007 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
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.
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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include "pthreadP.h"
21 #include <kernel-features.h>
24 static const struct pthread_rwlockattr default_attr =
26 .lockkind = PTHREAD_RWLOCK_DEFAULT_NP,
27 .pshared = PTHREAD_PROCESS_PRIVATE
31 int
32 __pthread_rwlock_init (rwlock, attr)
33 pthread_rwlock_t *rwlock;
34 const pthread_rwlockattr_t *attr;
36 const struct pthread_rwlockattr *iattr;
38 iattr = ((const struct pthread_rwlockattr *) attr) ?: &default_attr;
40 rwlock->__data.__lock = 0;
41 rwlock->__data.__nr_readers = 0;
42 rwlock->__data.__readers_wakeup = 0;
43 rwlock->__data.__writer_wakeup = 0;
44 rwlock->__data.__nr_readers_queued = 0;
45 rwlock->__data.__nr_writers_queued = 0;
46 rwlock->__data.__writer = 0;
48 rwlock->__data.__flags
49 = iattr->lockkind == PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP;
51 /* The __SHARED field is computed to minimize the work that needs to
52 be done while handling the futex. There are two inputs: the
53 availability of private futexes and whether the rwlock is shared
54 or private. Unfortunately the value of a private rwlock is
55 fixed: it must be zero. The PRIVATE_FUTEX flag has the value
56 0x80 in case private futexes are available and zero otherwise.
57 This leads to the following table:
59 | pshared | result
60 | shared private | shared private |
61 ------------+-----------------+-----------------+
62 !avail 0 | 0 0 | 0 0 |
63 avail 0x80 | 0x80 0 | 0 0x80 |
65 If the pshared value is in locking functions XORed with avail
66 we get the expected result. */
67 #ifdef __ASSUME_PRIVATE_FUTEX
68 rwlock->__data.__shared = (iattr->pshared == PTHREAD_PROCESS_PRIVATE
69 ? 0 : FUTEX_PRIVATE_FLAG);
70 #else
71 rwlock->__data.__shared = (iattr->pshared == PTHREAD_PROCESS_PRIVATE
72 ? 0
73 : THREAD_GETMEM (THREAD_SELF,
74 header.private_futex));
75 #endif
77 rwlock->__data.__pad1 = 0;
78 rwlock->__data.__pad2 = 0;
80 return 0;
82 strong_alias (__pthread_rwlock_init, pthread_rwlock_init)