-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathqb_thread.h
205 lines (171 loc) · 4.68 KB
/
qb_thread.h
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2012 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Chung Leong <cleong@cal.berkeley.edu> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifndef QB_THREAD_H_
#define QB_THREAD_H_
#ifdef WIN32
#include <windows.h>
#else
#include <pthread.h>
#endif
#ifdef ZTS
#define QB_GLOBAL_THREAD_COUNT_MULTIPLIER 8
#else
#define QB_GLOBAL_THREAD_COUNT_MULTIPLIER 1
#endif
typedef struct qb_condition qb_condition;
typedef struct qb_mutex qb_mutex;
typedef struct qb_event qb_event;
typedef struct qb_event_sink qb_event_sink;
typedef struct qb_task qb_task;
typedef struct qb_task_group qb_task_group;
typedef struct qb_thread qb_thread;
typedef struct qb_worker_thread qb_worker_thread;
typedef struct qb_main_thread qb_main_thread;
typedef struct qb_timeout_thread qb_timeout_thread;
typedef struct qb_thread_pool qb_thread_pool;
typedef void (*qb_thread_proc)(void *param1, void *param2, int param3);
typedef enum qb_thread_type qb_thread_type;
typedef enum qb_event_type qb_event_type;
enum qb_event_type {
QB_EVENT_LISTENING,
QB_EVENT_NOT_LISTENING,
QB_EVENT_WORKER_ADDED,
QB_EVENT_TASK_GROUP_ADDED,
QB_EVENT_TASK_GROUP_PROCESSED,
QB_EVENT_REQUEST_SENT,
QB_EVENT_REQUEST_PROCESSED,
QB_EVENT_TERMINATION,
QB_EVENT_TIMEOUT,
};
enum qb_thread_type {
QB_THREAD_UNINITIALIZED,
QB_THREAD_MAIN,
QB_THREAD_WORKER,
};
struct qb_event {
qb_event_type type;
qb_thread *sender;
};
struct qb_mutex {
#ifndef WIN32
pthread_mutex_t mutex;
#else
HANDLE mutex;
#endif
};
struct qb_condition {
#ifndef WIN32
pthread_cond_t condition;
#else
HANDLE event;
#endif
};
struct qb_event_sink {
qb_event message;
qb_condition condition;
qb_mutex mutex;
};
struct qb_task {
qb_thread_proc proc;
void *param1;
void *param2;
int param3;
qb_task_group *group;
};
struct qb_task_group {
qb_task *tasks;
long task_count;
long task_index;
long completion_count;
qb_thread *owner;
void *extra_memory;
qb_task_group *previous_group;
qb_task_group *next_group;
};
struct qb_thread {
qb_thread_type type;
qb_event_sink event_sink;
#ifndef WIN32
pthread_t thread;
#else
HANDLE thread;
#endif
};
struct qb_worker_thread {
qb_thread_type type;
qb_event_sink event_sink;
#ifndef WIN32
pthread_t thread;
#else
HANDLE thread;
#endif
int allow_termination;
volatile int terminated;
qb_thread *creator;
qb_main_thread *current_owner;
qb_task *current_task;
qb_task *request;
};
struct qb_main_thread {
qb_thread_type type;
qb_event_sink event_sink;
#ifndef WIN32
pthread_t thread;
#else
HANDLE thread;
#endif
long worker_count;
#ifndef WIN32
sigset_t signal_mask;
#endif
#ifdef ZTS
void ***tsrm_ls;
#endif
};
struct qb_thread_pool {
qb_worker_thread *workers;
long worker_count;
qb_task_group *task_queue_head;
qb_task_group *task_queue_tail;
qb_mutex task_queue_mutex;
long global_thread_limit;
long per_request_thread_limit;
#ifndef WIN32
qb_main_thread *current_main_thread;
pthread_t signal_thread;
int signal_thread_state;
#endif
};
long qb_get_cpu_count(void);
int qb_initialize_main_thread(qb_main_thread *thread TSRMLS_DC);
void qb_free_main_thread(qb_main_thread *thread);
#ifdef ZTS
void ***qb_get_tsrm_ls(void);
#endif
qb_task_group * qb_allocate_task_group(long task_count, long extra_bytes);
void qb_free_task_group(qb_task_group *group);
void qb_add_task(qb_task_group *group, qb_thread_proc proc, void *param1, void *param2, int param3);
void qb_run_task_group(qb_task_group *group, int iterative);
void qb_run_in_main_thread(qb_thread_proc proc, void *param1, void *param2, int param3);
void qb_terminate_associated_workers(qb_main_thread *main_thread);
qb_thread *qb_get_current_thread(void);
int qb_in_main_thread(void);
int qb_initialize_thread_pool(TSRMLS_D);
void qb_free_thread_pool(void);
#endif