Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 1 | #ifndef _PTHREAD_IMPL_H |
| 2 | #define _PTHREAD_IMPL_H |
| 3 | |
| 4 | #include <pthread.h> |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 5 | #include <signal.h> |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 6 | #include <errno.h> |
| 7 | #include <limits.h> |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 8 | #include "libc.h" |
| 9 | #include "syscall.h" |
| 10 | #include "atomic.h" |
| 11 | #include "futex.h" |
| 12 | |
| 13 | #define pthread __pthread |
| 14 | |
| 15 | struct pthread { |
Rich Felker | 0b2006c | 2011-02-15 03:24:58 -0500 | [diff] [blame] | 16 | struct pthread *self; |
Rich Felker | 9b153c0 | 2012-10-04 21:01:56 -0400 | [diff] [blame] | 17 | void **dtv, *unused1, *unused2; |
Rich Felker | 58aa5f4 | 2012-05-03 20:42:45 -0400 | [diff] [blame] | 18 | uintptr_t sysinfo; |
Rich Felker | 484194d | 2015-05-06 18:37:19 -0400 | [diff] [blame] | 19 | uintptr_t canary, canary2; |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 20 | pid_t tid, pid; |
Rich Felker | ac31bf2 | 2014-06-10 04:49:49 -0400 | [diff] [blame] | 21 | int tsd_used, errno_val; |
Rich Felker | feee989 | 2011-04-17 11:43:03 -0400 | [diff] [blame] | 22 | volatile int cancel, canceldisable, cancelasync; |
Rich Felker | bbbe87e | 2012-07-12 11:23:43 -0400 | [diff] [blame] | 23 | int detached; |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 24 | unsigned char *map_base; |
| 25 | size_t map_size; |
Rich Felker | 14a835b | 2013-03-31 23:25:55 -0400 | [diff] [blame] | 26 | void *stack; |
| 27 | size_t stack_size; |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 28 | void *start_arg; |
| 29 | void *(*start)(void *); |
| 30 | void *result; |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 31 | struct __ptcb *cancelbuf; |
| 32 | void **tsd; |
Rich Felker | 5fcebcd | 2011-03-10 18:31:37 -0500 | [diff] [blame] | 33 | volatile int dead; |
Rich Felker | 047e434 | 2011-03-17 20:41:37 -0400 | [diff] [blame] | 34 | struct { |
Rich Felker | de7e99c | 2014-08-17 00:46:26 -0400 | [diff] [blame] | 35 | volatile void *volatile head; |
Rich Felker | 047e434 | 2011-03-17 20:41:37 -0400 | [diff] [blame] | 36 | long off; |
Rich Felker | de7e99c | 2014-08-17 00:46:26 -0400 | [diff] [blame] | 37 | volatile void *volatile pending; |
Rich Felker | 047e434 | 2011-03-17 20:41:37 -0400 | [diff] [blame] | 38 | } robust_list; |
Rich Felker | bf619d8 | 2011-03-29 12:58:22 -0400 | [diff] [blame] | 39 | int unblock_cancel; |
Rich Felker | 56fbaa3 | 2015-03-03 22:50:02 -0500 | [diff] [blame] | 40 | volatile int timer_id; |
Rich Felker | 11c531e | 2011-05-30 01:41:23 -0400 | [diff] [blame] | 41 | locale_t locale; |
Rich Felker | 56fbaa3 | 2015-03-03 22:50:02 -0500 | [diff] [blame] | 42 | volatile int killlock[2]; |
| 43 | volatile int exitlock[2]; |
| 44 | volatile int startlock[2]; |
Rich Felker | ccc7b4c | 2013-03-26 23:07:31 -0400 | [diff] [blame] | 45 | unsigned long sigmask[_NSIG/8/sizeof(long)]; |
Rich Felker | 01d4274 | 2015-04-18 18:00:22 -0400 | [diff] [blame] | 46 | char *dlerror_buf; |
| 47 | int dlerror_flag; |
Rich Felker | 5345c9b | 2014-08-23 23:35:10 -0400 | [diff] [blame] | 48 | void *stdio_locks; |
Rich Felker | 484194d | 2015-05-06 18:37:19 -0400 | [diff] [blame] | 49 | uintptr_t canary_at_end; |
Szabolcs Nagy | 204a69d | 2015-03-11 12:48:12 +0000 | [diff] [blame] | 50 | void **dtv_copy; |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 51 | }; |
| 52 | |
Rich Felker | 70c31c7 | 2011-03-29 10:05:57 -0400 | [diff] [blame] | 53 | struct __timer { |
Rich Felker | bf619d8 | 2011-03-29 12:58:22 -0400 | [diff] [blame] | 54 | int timerid; |
Rich Felker | bf619d8 | 2011-03-29 12:58:22 -0400 | [diff] [blame] | 55 | pthread_t thread; |
Rich Felker | 70c31c7 | 2011-03-29 10:05:57 -0400 | [diff] [blame] | 56 | }; |
| 57 | |
Rich Felker | e882756 | 2011-02-17 17:16:20 -0500 | [diff] [blame] | 58 | #define __SU (sizeof(size_t)/sizeof(int)) |
| 59 | |
| 60 | #define _a_stacksize __u.__s[0] |
| 61 | #define _a_guardsize __u.__s[1] |
Rich Felker | 819006a | 2012-06-09 19:53:29 -0400 | [diff] [blame] | 62 | #define _a_stackaddr __u.__s[2] |
| 63 | #define _a_detach __u.__i[3*__SU+0] |
Rich Felker | 1e21e78 | 2012-11-11 15:38:04 -0500 | [diff] [blame] | 64 | #define _a_sched __u.__i[3*__SU+1] |
| 65 | #define _a_policy __u.__i[3*__SU+2] |
| 66 | #define _a_prio __u.__i[3*__SU+3] |
Rich Felker | e882756 | 2011-02-17 17:16:20 -0500 | [diff] [blame] | 67 | #define _m_type __u.__i[0] |
Rich Felker | 56fbaa3 | 2015-03-03 22:50:02 -0500 | [diff] [blame] | 68 | #define _m_lock __u.__vi[1] |
| 69 | #define _m_waiters __u.__vi[2] |
Rich Felker | 93cc986 | 2011-03-17 13:17:15 -0400 | [diff] [blame] | 70 | #define _m_prev __u.__p[3] |
| 71 | #define _m_next __u.__p[4] |
| 72 | #define _m_count __u.__i[5] |
Rich Felker | 37195db | 2014-08-17 22:09:47 -0400 | [diff] [blame] | 73 | #define _c_shared __u.__p[0] |
Rich Felker | 56fbaa3 | 2015-03-03 22:50:02 -0500 | [diff] [blame] | 74 | #define _c_seq __u.__vi[2] |
| 75 | #define _c_waiters __u.__vi[3] |
Rich Felker | cba4e1c | 2011-09-25 02:38:03 -0400 | [diff] [blame] | 76 | #define _c_clock __u.__i[4] |
Rich Felker | 56fbaa3 | 2015-03-03 22:50:02 -0500 | [diff] [blame] | 77 | #define _c_lock __u.__vi[8] |
Rich Felker | 37195db | 2014-08-17 22:09:47 -0400 | [diff] [blame] | 78 | #define _c_head __u.__p[1] |
| 79 | #define _c_tail __u.__p[5] |
Rich Felker | 56fbaa3 | 2015-03-03 22:50:02 -0500 | [diff] [blame] | 80 | #define _rw_lock __u.__vi[0] |
| 81 | #define _rw_waiters __u.__vi[1] |
Rich Felker | bc09d58 | 2014-08-15 23:54:52 -0400 | [diff] [blame] | 82 | #define _rw_shared __u.__i[2] |
Rich Felker | 56fbaa3 | 2015-03-03 22:50:02 -0500 | [diff] [blame] | 83 | #define _b_lock __u.__vi[0] |
| 84 | #define _b_waiters __u.__vi[1] |
Rich Felker | f16a308 | 2011-05-06 20:00:59 -0400 | [diff] [blame] | 85 | #define _b_limit __u.__i[2] |
Rich Felker | 56fbaa3 | 2015-03-03 22:50:02 -0500 | [diff] [blame] | 86 | #define _b_count __u.__vi[3] |
| 87 | #define _b_waiters2 __u.__vi[4] |
Rich Felker | 9ae1cf6 | 2012-05-21 22:51:30 -0400 | [diff] [blame] | 88 | #define _b_inst __u.__p[3] |
Rich Felker | e882756 | 2011-02-17 17:16:20 -0500 | [diff] [blame] | 89 | |
Rich Felker | 7b2dd22 | 2011-02-15 03:56:52 -0500 | [diff] [blame] | 90 | #include "pthread_arch.h" |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 91 | |
Rich Felker | 484194d | 2015-05-06 18:37:19 -0400 | [diff] [blame] | 92 | #ifndef CANARY |
| 93 | #define CANARY canary |
| 94 | #endif |
| 95 | |
Rich Felker | 6ba5517 | 2015-06-25 22:22:00 +0000 | [diff] [blame] | 96 | #ifndef DTP_OFFSET |
| 97 | #define DTP_OFFSET 0 |
| 98 | #endif |
| 99 | |
rofl0r | 1f53e7d | 2017-01-13 10:28:46 +0000 | [diff] [blame] | 100 | #ifndef tls_mod_off_t |
| 101 | #define tls_mod_off_t size_t |
| 102 | #endif |
| 103 | |
Rich Felker | 99b8a25 | 2011-05-07 23:23:58 -0400 | [diff] [blame] | 104 | #define SIGTIMER 32 |
| 105 | #define SIGCANCEL 33 |
Rich Felker | acb0480 | 2011-07-29 22:59:44 -0400 | [diff] [blame] | 106 | #define SIGSYNCCALL 34 |
Rich Felker | 99b8a25 | 2011-05-07 23:23:58 -0400 | [diff] [blame] | 107 | |
Rich Felker | 2f43704 | 2012-08-09 22:52:13 -0400 | [diff] [blame] | 108 | #define SIGALL_SET ((sigset_t *)(const unsigned long long [2]){ -1,-1 }) |
| 109 | #define SIGPT_SET \ |
Rich Felker | ccc7b4c | 2013-03-26 23:07:31 -0400 | [diff] [blame] | 110 | ((sigset_t *)(const unsigned long [_NSIG/8/sizeof(long)]){ \ |
Rich Felker | f09e78d | 2011-06-13 20:37:52 -0400 | [diff] [blame] | 111 | [sizeof(long)==4] = 3UL<<(32*(sizeof(long)>4)) }) |
Rich Felker | 2f43704 | 2012-08-09 22:52:13 -0400 | [diff] [blame] | 112 | #define SIGTIMER_SET \ |
Rich Felker | ccc7b4c | 2013-03-26 23:07:31 -0400 | [diff] [blame] | 113 | ((sigset_t *)(const unsigned long [_NSIG/8/sizeof(long)]){ \ |
Rich Felker | 4c4e22d | 2011-05-07 23:37:10 -0400 | [diff] [blame] | 114 | 0x80000000 }) |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 115 | |
Rich Felker | dba68bf | 2011-07-30 08:02:14 -0400 | [diff] [blame] | 116 | pthread_t __pthread_self_init(void); |
| 117 | |
Rich Felker | 3f72cda | 2011-09-18 10:14:37 -0400 | [diff] [blame] | 118 | int __clone(int (*)(void *), void *, int, void *, ...); |
Rich Felker | 7b2dd22 | 2011-02-15 03:56:52 -0500 | [diff] [blame] | 119 | int __set_thread_area(void *); |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 120 | int __libc_sigaction(int, const struct sigaction *, struct sigaction *); |
| 121 | int __libc_sigprocmask(int, const sigset_t *, sigset_t *); |
| 122 | void __lock(volatile int *); |
| 123 | void __unmapself(void *, size_t); |
| 124 | |
Alexander Monakov | fa80787 | 2015-04-15 01:44:02 +0300 | [diff] [blame] | 125 | void __vm_wait(void); |
| 126 | void __vm_lock(void); |
| 127 | void __vm_unlock(void); |
Rich Felker | f08ab9e | 2015-04-10 02:27:52 -0400 | [diff] [blame] | 128 | |
Rich Felker | 0fc317d | 2015-03-02 17:46:22 -0500 | [diff] [blame] | 129 | int __timedwait(volatile int *, int, clockid_t, const struct timespec *, int); |
| 130 | int __timedwait_cp(volatile int *, int, clockid_t, const struct timespec *, int); |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 131 | void __wait(volatile int *, volatile int *, int, int); |
Rich Felker | bc09d58 | 2014-08-15 23:54:52 -0400 | [diff] [blame] | 132 | static inline void __wake(volatile void *addr, int cnt, int priv) |
| 133 | { |
Jens Gustedt | d906fa3 | 2017-06-24 10:18:05 +0200 | [diff] [blame] | 134 | if (priv) priv = FUTEX_PRIVATE; |
Rich Felker | bc09d58 | 2014-08-15 23:54:52 -0400 | [diff] [blame] | 135 | if (cnt<0) cnt = INT_MAX; |
Rich Felker | b8ca9eb | 2014-08-22 23:49:54 -0400 | [diff] [blame] | 136 | __syscall(SYS_futex, addr, FUTEX_WAKE|priv, cnt) != -ENOSYS || |
Rich Felker | bc09d58 | 2014-08-15 23:54:52 -0400 | [diff] [blame] | 137 | __syscall(SYS_futex, addr, FUTEX_WAKE, cnt); |
| 138 | } |
Jens Gustedt | 47d0bcd | 2018-01-03 14:17:12 +0100 | [diff] [blame^] | 139 | static inline void __futexwait(volatile void *addr, int val, int priv) |
| 140 | { |
| 141 | if (priv) priv = FUTEX_PRIVATE; |
| 142 | __syscall(SYS_futex, addr, FUTEX_WAIT|priv, val) != -ENOSYS || |
| 143 | __syscall(SYS_futex, addr, FUTEX_WAIT, val); |
| 144 | } |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 145 | |
Alexander Monakov | fa80787 | 2015-04-15 01:44:02 +0300 | [diff] [blame] | 146 | void __acquire_ptc(void); |
| 147 | void __release_ptc(void); |
| 148 | void __inhibit_ptc(void); |
Rich Felker | b2486a8 | 2011-04-06 20:27:07 -0400 | [diff] [blame] | 149 | |
Rich Felker | 2c074b0 | 2013-04-26 19:48:01 -0400 | [diff] [blame] | 150 | void __block_all_sigs(void *); |
| 151 | void __block_app_sigs(void *); |
| 152 | void __restore_sigs(void *); |
| 153 | |
Rich Felker | 13b3645 | 2012-06-02 20:15:37 -0400 | [diff] [blame] | 154 | #define DEFAULT_STACK_SIZE 81920 |
Rich Felker | 4078a5c | 2016-11-08 18:03:42 -0500 | [diff] [blame] | 155 | #define DEFAULT_GUARD_SIZE 4096 |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 156 | |
Rich Felker | 23614b0 | 2014-09-07 10:28:08 -0400 | [diff] [blame] | 157 | #define __ATTRP_C11_THREAD ((void*)(uintptr_t)-1) |
| 158 | |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 159 | #endif |