This repository has been archived by the owner on May 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathpthread_dependency.c
114 lines (100 loc) · 3.16 KB
/
pthread_dependency.c
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* Copyright (c) 2018 Apple Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this
* file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
#include "resolver.h"
#include "internal.h"
#define PREREQUISITE_FULFILLED (~0u)
#if !VARIANT_DYLD
void
pthread_dependency_init_np(pthread_dependency_t *pr, pthread_t pth,
pthread_dependency_attr_t *attrs)
{
if (attrs) *(volatile char *)attrs;
*pr = (pthread_dependency_t)PTHREAD_DEPENDENCY_INITIALIZER_NP(pth);
}
OS_NOINLINE
void
_pthread_dependency_fulfill_slow(pthread_dependency_t *pr, uint32_t old)
{
if (old == PREREQUISITE_FULFILLED) {
PTHREAD_CLIENT_CRASH(0, "Fufilling pthread_dependency_t twice");
}
if (os_unlikely(old != _pthread_mach_thread_self_direct())) {
PTHREAD_CLIENT_CRASH(old, "Fulfilled a dependency "
"not owned by current thread");
}
int ret = __ulock_wake(UL_UNFAIR_LOCK | ULF_NO_ERRNO, &pr->__pdep_opaque1, 0);
switch (-ret) {
case 0:
case ENOENT:
return;
default:
PTHREAD_INTERNAL_CRASH(-ret, "__ulock_wake() failed");
}
}
PTHREAD_NOEXPORT_VARIANT
void
pthread_dependency_fulfill_np(pthread_dependency_t *pr, void *value)
{
uint32_t old;
pr->__pdep_opaque2 = (uint64_t)(uintptr_t)value;
old = os_atomic_xchg(&pr->__pdep_opaque1, PREREQUISITE_FULFILLED, release);
if (old != 0) _pthread_dependency_fulfill_slow(pr, old);
}
PTHREAD_NOEXPORT_VARIANT
void *
pthread_dependency_wait_np(pthread_dependency_t *pr)
{
if (os_atomic_cmpxchg(&pr->__pdep_opaque1, 0, pr->__pdep_owner, relaxed)) {
int ret;
again:
ret = __ulock_wait(UL_UNFAIR_LOCK | ULF_NO_ERRNO, &pr->__pdep_opaque1,
pr->__pdep_owner, 0);
switch (-ret) {
case EFAULT:
case EINTR:
case 0:
if (pr->__pdep_opaque1 == pr->__pdep_owner) goto again;
break;
case EOWNERDEAD:
PTHREAD_CLIENT_CRASH(pr->__pdep_owner, "Waiting on orphaned dependency");
default:
PTHREAD_CLIENT_CRASH(-ret, "__ulock_wait() failed");
}
}
uint32_t cur = os_atomic_load(&pr->__pdep_opaque1, acquire);
if (cur == PREREQUISITE_FULFILLED) {
return (void *)(uintptr_t)pr->__pdep_opaque2;
}
PTHREAD_CLIENT_CRASH(cur, "Corrupted pthread_dependency_t");
}
PTHREAD_NOEXPORT_VARIANT void*
_pthread_atomic_xchg_ptr(void **p, void *v)
{
return os_atomic_xchg(p, v, seq_cst);
}
PTHREAD_NOEXPORT_VARIANT uint32_t
_pthread_atomic_xchg_uint32_relaxed(uint32_t *p, uint32_t v)
{
return os_atomic_xchg(p, v, relaxed);
}
#endif // !VARIANT_DYLD