2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #ifndef _SYS_PTHREAD_INTERNAL_H_
30 #define _SYS_PTHREAD_INTERNAL_H_
33 #include <kern/thread_call.h>
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 */
44 typedef struct ksyn_waitq_element
* ksyn_waitq_element_t
;
47 #define KWE_THREAD_INWAIT 1
48 #define KWE_THREAD_PREPOST 2
49 #define KWE_THREAD_BROADCAST 4
52 #define WORKITEM_SIZE 64
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 */
59 #define WORKQUEUE_NUMPRIOS 4
61 #define WORKQUEUE_OVERCOMMIT 0x10000
64 TAILQ_ENTRY(threadlist
) th_entry
;
67 uint16_t th_affinity_tag
;
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
;
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
86 TAILQ_ENTRY(workitem
) wi_entry
;
92 TAILQ_HEAD(, workitem
) wl_itemlist
;
93 TAILQ_HEAD(, workitem
) wl_freelist
;
97 struct workitem wq_array
[WORKITEM_SIZE
* WORKQUEUE_NUMPRIOS
];
101 thread_call_t wq_atimer_call
;
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
];
122 #define WQ_LIST_INITED 0x01
123 #define WQ_ATIMER_RUNNING 0x02
124 #define WQ_EXITING 0x04
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
132 #define WQ_VECT_SET_BIT(vector, bit) \
133 vector[(bit) / 32] |= (1 << ((bit) % 32))
135 #define WQ_VECT_CLEAR_BIT(vector, bit) \
136 vector[(bit) / 32] &= ~(1 << ((bit) % 32))
138 #define WQ_VECT_TEST_BIT(vector, bit) \
139 vector[(bit) / 32] & (1 << ((bit) % 32))
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
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
155 #define PTH_DEFAULT_STACKSIZE 512*1024
156 #define PTH_DEFAULT_GUARDSIZE 4*1024
157 #define MAX_PTHREAD_SIZE 64*1024
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
;
163 void workqueue_exit(struct proc
*);
164 void pthread_init(void);
165 void psynch_zoneinit(void);
166 #endif /* _SYS_PTHREAD_INTERNAL_H_ */