Support C11 __STDC_SOURCE__ and _ISOC11_SOURCE
[glibc.git] / nptl / pthread_rwlock_init.c
blob9ecd48c347286b9e9c64d1d8d4a83bb373300b2a
1 /* Copyright (C) 2002,2007,2009,2011 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 <string.h>
22 #include <kernel-features.h>
25 static const struct pthread_rwlockattr default_attr =
27 .lockkind = PTHREAD_RWLOCK_DEFAULT_NP,
28 .pshared = PTHREAD_PROCESS_PRIVATE
32 int
33 __pthread_rwlock_init (rwlock, attr)
34 pthread_rwlock_t *rwlock;
35 const pthread_rwlockattr_t *attr;
37 const struct pthread_rwlockattr *iattr;
39 iattr = ((const struct pthread_rwlockattr *) attr) ?: &default_attr;
41 memset (rwlock, '\0', sizeof (*rwlock));
43 rwlock->__data.__flags
44 = iattr->lockkind == PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP;
46 /* The __SHARED field is computed to minimize the work that needs to
47 be done while handling the futex. There are two inputs: the
48 availability of private futexes and whether the rwlock is shared
49 or private. Unfortunately the value of a private rwlock is
50 fixed: it must be zero. The PRIVATE_FUTEX flag has the value
51 0x80 in case private futexes are available and zero otherwise.
52 This leads to the following table:
54 | pshared | result
55 | shared private | shared private |
56 ------------+-----------------+-----------------+
57 !avail 0 | 0 0 | 0 0 |
58 avail 0x80 | 0x80 0 | 0 0x80 |
60 If the pshared value is in locking functions XORed with avail
61 we get the expected result. */
62 #ifdef __ASSUME_PRIVATE_FUTEX
63 rwlock->__data.__shared = (iattr->pshared == PTHREAD_PROCESS_PRIVATE
64 ? 0 : FUTEX_PRIVATE_FLAG);
65 #else
66 rwlock->__data.__shared = (iattr->pshared == PTHREAD_PROCESS_PRIVATE
67 ? 0
68 : THREAD_GETMEM (THREAD_SELF,
69 header.private_futex));
70 #endif
72 return 0;
74 strong_alias (__pthread_rwlock_init, pthread_rwlock_init)