]> git.saurik.com Git - apple/xnu.git/blob - bsd/sys/pthread_internal.h
xnu-1699.24.8.tar.gz
[apple/xnu.git] / bsd / sys / pthread_internal.h
1 /*
2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #ifndef _SYS_PTHREAD_INTERNAL_H_
30 #define _SYS_PTHREAD_INTERNAL_H_
31
32 #include <sys/user.h>
33 #include <kern/thread_call.h>
34
35 struct ksyn_waitq_element {
36 TAILQ_ENTRY(ksyn_waitq_element) kwe_list; /* link to other list members */
37 void * kwe_kwqqueue; /* queue blocked on */
38 uint32_t kwe_flags; /* flags */
39 uint32_t kwe_lockseq; /* the sequence of the entry */
40 uint32_t kwe_count; /* upper bound on number of matches still pending */
41 uint32_t kwe_psynchretval; /* thread retval */
42 void *kwe_uth; /* uthread */
43 };
44 typedef struct ksyn_waitq_element * ksyn_waitq_element_t;
45
46 /* kew_flags defns */
47 #define KWE_THREAD_INWAIT 1
48 #define KWE_THREAD_PREPOST 2
49 #define KWE_THREAD_BROADCAST 4
50
51
52 #define WORKITEM_SIZE 64
53
54 #define WORKQUEUE_HIGH_PRIOQUEUE 0 /* high priority queue */
55 #define WORKQUEUE_DEFAULT_PRIOQUEUE 1 /* default priority queue */
56 #define WORKQUEUE_LOW_PRIOQUEUE 2 /* low priority queue */
57 #define WORKQUEUE_BG_PRIOQUEUE 3 /* background priority queue */
58
59 #define WORKQUEUE_NUMPRIOS 4
60
61 #define WORKQUEUE_OVERCOMMIT 0x10000
62
63 struct threadlist {
64 TAILQ_ENTRY(threadlist) th_entry;
65 thread_t th_thread;
66 int th_flags;
67 uint16_t th_affinity_tag;
68 uint8_t th_priority;
69 uint8_t th_policy;
70 struct workqueue *th_workq;
71 mach_vm_size_t th_stacksize;
72 mach_vm_size_t th_allocsize;
73 mach_vm_offset_t th_stackaddr;
74 mach_port_name_t th_thport;
75 };
76 #define TH_LIST_INITED 0x01
77 #define TH_LIST_RUNNING 0x02
78 #define TH_LIST_BLOCKED 0x04
79 #define TH_LIST_SUSPENDED 0x08
80 #define TH_LIST_BUSY 0x10
81 #define TH_LIST_NEED_WAKEUP 0x20
82 #define TH_LIST_CONSTRAINED 0x40
83
84
85 struct workitem {
86 TAILQ_ENTRY(workitem) wi_entry;
87 user_addr_t wi_item;
88 uint32_t wi_affinity;
89 };
90
91 struct workitemlist {
92 TAILQ_HEAD(, workitem) wl_itemlist;
93 TAILQ_HEAD(, workitem) wl_freelist;
94 };
95
96 struct workqueue {
97 struct workitem wq_array[WORKITEM_SIZE * WORKQUEUE_NUMPRIOS];
98 proc_t wq_proc;
99 vm_map_t wq_map;
100 task_t wq_task;
101 thread_call_t wq_atimer_call;
102 int wq_flags;
103 int wq_lflags;
104 int wq_itemcount;
105 uint64_t wq_thread_yielded_timestamp;
106 uint32_t wq_thread_yielded_count;
107 uint32_t wq_timer_interval;
108 uint32_t wq_affinity_max;
109 uint32_t wq_threads_scheduled;
110 uint32_t wq_constrained_threads_scheduled;
111 uint32_t wq_nthreads;
112 uint32_t wq_thidlecount;
113 uint32_t wq_reqconc[WORKQUEUE_NUMPRIOS]; /* requested concurrency for each priority level */
114 struct workitemlist wq_list[WORKQUEUE_NUMPRIOS]; /* priority based item list */
115 uint32_t wq_list_bitmap;
116 TAILQ_HEAD(, threadlist) wq_thrunlist;
117 TAILQ_HEAD(, threadlist) wq_thidlelist;
118 uint32_t *wq_thactive_count[WORKQUEUE_NUMPRIOS];
119 uint32_t *wq_thscheduled_count[WORKQUEUE_NUMPRIOS];
120 uint64_t *wq_lastblocked_ts[WORKQUEUE_NUMPRIOS];
121 };
122 #define WQ_LIST_INITED 0x01
123 #define WQ_ATIMER_RUNNING 0x02
124 #define WQ_EXITING 0x04
125
126 #define WQL_ATIMER_BUSY 0x01
127 #define WQL_ATIMER_WAITING 0x02
128 #define WQL_EXCEEDED_CONSTRAINED_THREAD_LIMIT 0x04
129 #define WQL_EXCEEDED_TOTAL_THREAD_LIMIT 0x08
130
131
132 #define WQ_VECT_SET_BIT(vector, bit) \
133 vector[(bit) / 32] |= (1 << ((bit) % 32))
134
135 #define WQ_VECT_CLEAR_BIT(vector, bit) \
136 vector[(bit) / 32] &= ~(1 << ((bit) % 32))
137
138 #define WQ_VECT_TEST_BIT(vector, bit) \
139 vector[(bit) / 32] & (1 << ((bit) % 32))
140
141
142 #define WORKQUEUE_MAXTHREADS 512
143 #define WQ_YIELDED_THRESHOLD 2000
144 #define WQ_YIELDED_WINDOW_USECS 30000
145 #define WQ_STALLED_WINDOW_USECS 200
146 #define WQ_REDUCE_POOL_WINDOW_USECS 5000000
147 #define WQ_MAX_TIMER_INTERVAL_USECS 50000
148
149 /* workq_kernreturn commands */
150 #define WQOPS_QUEUE_ADD 1
151 #define WQOPS_QUEUE_REMOVE_OBSOLETE 2
152 #define WQOPS_THREAD_RETURN 4
153 #define WQOPS_THREAD_SETCONC 8
154
155 #define PTH_DEFAULT_STACKSIZE 512*1024
156 #define PTH_DEFAULT_GUARDSIZE 4*1024
157 #define MAX_PTHREAD_SIZE 64*1024
158
159 extern lck_grp_attr_t *pthread_lck_grp_attr;
160 extern lck_grp_t *pthread_lck_grp;
161 extern lck_attr_t *pthread_lck_attr;
162
163 void workqueue_exit(struct proc *);
164 void pthread_init(void);
165 void psynch_zoneinit(void);
166 #endif /* _SYS_PTHREAD_INTERNAL_H_ */
167