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
35struct 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};
44typedef 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 WORKQUEUE_HIGH_PRIOQUEUE    0       /* high priority queue */
53#define WORKQUEUE_DEFAULT_PRIOQUEUE 1       /* default priority queue */
54#define WORKQUEUE_LOW_PRIOQUEUE     2       /* low priority queue */
55#define WORKQUEUE_BG_PRIOQUEUE      3       /* background priority queue */
56
57#define WORKQUEUE_NUMPRIOS 4
58
59#define WORKQUEUE_OVERCOMMIT	0x10000
60
61struct threadlist {
62	TAILQ_ENTRY(threadlist) th_entry;
63	thread_t th_thread;
64	int	 th_flags;
65	uint16_t th_affinity_tag;
66	uint8_t	 th_priority;
67	uint8_t  th_policy;
68	struct workqueue *th_workq;
69	mach_vm_size_t th_stacksize;
70	mach_vm_size_t th_allocsize;
71	mach_vm_offset_t th_stackaddr;
72	mach_port_name_t th_thport;
73};
74#define TH_LIST_INITED 		0x01
75#define TH_LIST_RUNNING 	0x02
76#define TH_LIST_BLOCKED 	0x04
77#define TH_LIST_SUSPENDED 	0x08
78#define TH_LIST_BUSY		0x10
79#define TH_LIST_NEED_WAKEUP	0x20
80#define TH_LIST_CONSTRAINED	0x40
81
82
83struct workqueue {
84        proc_t		wq_proc;
85        vm_map_t	wq_map;
86        task_t		wq_task;
87        thread_call_t	wq_atimer_call;
88	int 		wq_flags;
89	int		wq_lflags;
90	uint64_t 	wq_thread_yielded_timestamp;
91	uint32_t	wq_thread_yielded_count;
92	uint32_t	wq_timer_interval;
93        uint32_t	wq_affinity_max;
94        uint32_t	wq_threads_scheduled;
95	uint32_t	wq_constrained_threads_scheduled;
96	uint32_t	wq_nthreads;
97        uint32_t      	wq_thidlecount;
98	uint32_t	wq_reqcount;
99	TAILQ_HEAD(, threadlist) wq_thrunlist;
100	TAILQ_HEAD(, threadlist) wq_thidlelist;
101	uint16_t	wq_requests[WORKQUEUE_NUMPRIOS];
102	uint16_t	wq_ocrequests[WORKQUEUE_NUMPRIOS];
103	uint16_t	wq_reqconc[WORKQUEUE_NUMPRIOS];	  		/* requested concurrency for each priority level */
104        uint16_t	*wq_thscheduled_count[WORKQUEUE_NUMPRIOS];
105        uint32_t	*wq_thactive_count[WORKQUEUE_NUMPRIOS];		/* must be uint32_t since we OSAddAtomic on these */
106        uint64_t	*wq_lastblocked_ts[WORKQUEUE_NUMPRIOS];
107};
108#define WQ_LIST_INITED		0x01
109#define WQ_ATIMER_RUNNING	0x02
110#define WQ_EXITING		0x04
111
112#define WQL_ATIMER_BUSY		0x01
113#define WQL_ATIMER_WAITING	0x02
114#define WQL_EXCEEDED_CONSTRAINED_THREAD_LIMIT    0x04
115#define WQL_EXCEEDED_TOTAL_THREAD_LIMIT          0x08
116
117
118#define WQ_VECT_SET_BIT(vector, bit)	\
119	vector[(bit) / 32] |= (1 << ((bit) % 32))
120
121#define WQ_VECT_CLEAR_BIT(vector, bit)	\
122	vector[(bit) / 32] &= ~(1 << ((bit) % 32))
123
124#define WQ_VECT_TEST_BIT(vector, bit)	\
125	vector[(bit) / 32] & (1 << ((bit) % 32))
126
127
128#define WORKQUEUE_MAXTHREADS		512
129#define WQ_YIELDED_THRESHOLD		2000
130#define WQ_YIELDED_WINDOW_USECS		30000
131#define WQ_STALLED_WINDOW_USECS		200
132#define WQ_REDUCE_POOL_WINDOW_USECS	5000000
133#define	WQ_MAX_TIMER_INTERVAL_USECS	50000
134
135/* workq_kernreturn commands */
136#define WQOPS_QUEUE_ADD 1
137#define WQOPS_QUEUE_REMOVE_OBSOLETE 2
138#define WQOPS_THREAD_RETURN 4
139#define WQOPS_THREAD_SETCONC  8
140#define WQOPS_QUEUE_NEWSPISUPP  0x10	/* this is to check for newer SPI support */
141#define WQOPS_QUEUE_REQTHREADS  0x20	/* request number of threads of a prio */
142
143#define PTH_DEFAULT_STACKSIZE 512*1024
144#define PTH_DEFAULT_GUARDSIZE 4*1024
145#define MAX_PTHREAD_SIZE 64*1024
146
147extern lck_grp_attr_t   *pthread_lck_grp_attr;
148extern lck_grp_t    *pthread_lck_grp;
149extern lck_attr_t   *pthread_lck_attr;
150
151void workqueue_mark_exiting(struct proc *);
152void workqueue_exit(struct proc *);
153void pthread_init(void);
154void psynch_zoneinit(void);
155#endif /* _SYS_PTHREAD_INTERNAL_H_ */
156
157