Lines Matching refs:h
98 static inline void close_cmd_fd(int h);
100 static inline void add_poll(int h, int fd, int type, int flags,
145 static void free_thread_slot(int h) { in free_thread_slot() argument
146 if (0 <= h && h < MAX_THREAD) { in free_thread_slot()
147 close_cmd_fd(h); in free_thread_slot()
148 ts[h].used = 0; in free_thread_slot()
150 APPL_TRACE_ERROR("invalid thread handle:%d", h); in free_thread_slot()
157 int h; in btsock_thread_init() local
158 for (h = 0; h < MAX_THREAD; h++) { in btsock_thread_init()
159 ts[h].cmd_fdr = ts[h].cmd_fdw = -1; in btsock_thread_init()
160 ts[h].used = 0; in btsock_thread_init()
161 ts[h].thread_id = -1; in btsock_thread_init()
162 ts[h].poll_count = 0; in btsock_thread_init()
163 ts[h].callback = NULL; in btsock_thread_init()
164 ts[h].cmd_callback = NULL; in btsock_thread_init()
172 int h = alloc_thread_slot(); in btsock_thread_create() local
173 APPL_TRACE_DEBUG("alloc_thread_slot ret:%d", h); in btsock_thread_create()
174 if (h >= 0) { in btsock_thread_create()
175 init_poll(h); in btsock_thread_create()
177 int status = create_thread(sock_poll_thread, (void*)(uintptr_t)h, &thread); in btsock_thread_create()
180 free_thread_slot(h); in btsock_thread_create()
184 ts[h].thread_id = thread; in btsock_thread_create()
185 APPL_TRACE_DEBUG("h:%d, thread id:%d", h, ts[h].thread_id); in btsock_thread_create()
186 ts[h].callback = callback; in btsock_thread_create()
187 ts[h].cmd_callback = cmd_callback; in btsock_thread_create()
189 return h; in btsock_thread_create()
193 static inline void init_cmd_fd(int h) { in init_cmd_fd() argument
194 asrt(ts[h].cmd_fdr == -1 && ts[h].cmd_fdw == -1); in init_cmd_fd()
195 if (socketpair(AF_UNIX, SOCK_STREAM, 0, &ts[h].cmd_fdr) < 0) { in init_cmd_fd()
199 APPL_TRACE_DEBUG("h:%d, cmd_fdr:%d, cmd_fdw:%d", h, ts[h].cmd_fdr, in init_cmd_fd()
200 ts[h].cmd_fdw); in init_cmd_fd()
202 add_poll(h, ts[h].cmd_fdr, 0, SOCK_THREAD_FD_RD, 0); in init_cmd_fd()
204 static inline void close_cmd_fd(int h) { in close_cmd_fd() argument
205 if (ts[h].cmd_fdr != -1) { in close_cmd_fd()
206 close(ts[h].cmd_fdr); in close_cmd_fd()
207 ts[h].cmd_fdr = -1; in close_cmd_fd()
209 if (ts[h].cmd_fdw != -1) { in close_cmd_fd()
210 close(ts[h].cmd_fdw); in close_cmd_fd()
211 ts[h].cmd_fdw = -1; in close_cmd_fd()
221 int btsock_thread_add_fd(int h, int fd, int type, int flags, uint32_t user_id) { in btsock_thread_add_fd() argument
222 if (h < 0 || h >= MAX_THREAD) { in btsock_thread_add_fd()
223 APPL_TRACE_ERROR("invalid bt thread handle:%d", h); in btsock_thread_add_fd()
226 if (ts[h].cmd_fdw == -1) { in btsock_thread_add_fd()
233 if (ts[h].thread_id == pthread_self()) { in btsock_thread_add_fd()
236 add_poll(h, fd, type, flags, user_id); in btsock_thread_add_fd()
246 OSI_NO_INTR(ret = send(ts[h].cmd_fdw, &cmd, sizeof(cmd), 0)); in btsock_thread_add_fd()
269 int btsock_thread_post_cmd(int h, int type, const unsigned char* data, int size, in btsock_thread_post_cmd() argument
271 if (h < 0 || h >= MAX_THREAD) { in btsock_thread_post_cmd()
272 APPL_TRACE_ERROR("invalid bt thread handle:%d", h); in btsock_thread_post_cmd()
275 if (ts[h].cmd_fdw == -1) { in btsock_thread_post_cmd()
281 APPL_TRACE_DEBUG("post cmd type:%d, size:%d, h:%d, ", type, size, h); in btsock_thread_post_cmd()
291 APPL_TRACE_ERROR("alloca failed at h:%d, cmd type:%d, size:%d", h, type, in btsock_thread_post_cmd()
298 OSI_NO_INTR(ret = send(ts[h].cmd_fdw, cmd_send, size_send, 0)); in btsock_thread_post_cmd()
302 int btsock_thread_wakeup(int h) { in btsock_thread_wakeup() argument
303 if (h < 0 || h >= MAX_THREAD) { in btsock_thread_wakeup()
304 APPL_TRACE_ERROR("invalid bt thread handle:%d", h); in btsock_thread_wakeup()
307 if (ts[h].cmd_fdw == -1) { in btsock_thread_wakeup()
308 APPL_TRACE_ERROR("thread handle:%d, cmd socket is not created", h); in btsock_thread_wakeup()
314 OSI_NO_INTR(ret = send(ts[h].cmd_fdw, &cmd, sizeof(cmd), 0)); in btsock_thread_wakeup()
318 int btsock_thread_exit(int h) { in btsock_thread_exit() argument
319 if (h < 0 || h >= MAX_THREAD) { in btsock_thread_exit()
320 APPL_TRACE_ERROR("invalid bt thread slot:%d", h); in btsock_thread_exit()
323 if (ts[h].cmd_fdw == -1) { in btsock_thread_exit()
330 OSI_NO_INTR(ret = send(ts[h].cmd_fdw, &cmd, sizeof(cmd), 0)); in btsock_thread_exit()
333 if (ts[h].thread_id != -1) { in btsock_thread_exit()
334 pthread_join(ts[h].thread_id, 0); in btsock_thread_exit()
335 ts[h].thread_id = -1; in btsock_thread_exit()
337 free_thread_slot(h); in btsock_thread_exit()
342 static void init_poll(int h) { in init_poll() argument
344 ts[h].poll_count = 0; in init_poll()
345 ts[h].thread_id = -1; in init_poll()
346 ts[h].callback = NULL; in init_poll()
347 ts[h].cmd_callback = NULL; in init_poll()
349 ts[h].ps[i].pfd.fd = -1; in init_poll()
350 ts[h].psi[i] = -1; in init_poll()
352 init_cmd_fd(h); in init_poll()
375 static inline void add_poll(int h, int fd, int type, int flags, in add_poll() argument
380 poll_slot_t* ps = ts[h].ps; in add_poll()
384 asrt(ts[h].poll_count < MAX_POLL); in add_poll()
392 asrt(ts[h].poll_count < MAX_POLL); in add_poll()
394 ++ts[h].poll_count; in add_poll()
399 static inline void remove_poll(int h, poll_slot_t* ps, int flags) { in remove_poll() argument
402 --ts[h].poll_count; in remove_poll()
412 static int process_cmd_sock(int h) { in process_cmd_sock() argument
414 int fd = ts[h].cmd_fdr; in process_cmd_sock()
426 add_poll(h, cmd.fd, cmd.type, cmd.flags, cmd.user_id); in process_cmd_sock()
430 poll_slot_t* poll_slot = &ts[h].ps[i]; in process_cmd_sock()
432 remove_poll(h, poll_slot, poll_slot->flags); in process_cmd_sock()
441 asrt(ts[h].cmd_callback); in process_cmd_sock()
442 if (ts[h].cmd_callback) in process_cmd_sock()
443 ts[h].cmd_callback(fd, cmd.type, cmd.flags, cmd.user_id); in process_cmd_sock()
466 static void process_data_sock(int h, struct pollfd* pfds, int count) { in process_data_sock() argument
467 asrt(count <= ts[h].poll_count); in process_data_sock()
469 for (i = 1; i < ts[h].poll_count; i++) { in process_data_sock()
471 int ps_i = ts[h].psi[i]; in process_data_sock()
472 asrt(pfds[i].fd == ts[h].ps[ps_i].pfd.fd); in process_data_sock()
473 uint32_t user_id = ts[h].ps[ps_i].user_id; in process_data_sock()
474 int type = ts[h].ps[ps_i].type; in process_data_sock()
486 remove_poll(h, &ts[h].ps[ps_i], ts[h].ps[ps_i].flags); in process_data_sock()
488 remove_poll(h, &ts[h].ps[ps_i], in process_data_sock()
490 if (flags) ts[h].callback(pfds[i].fd, type, flags, user_id); in process_data_sock()
495 static void prepare_poll_fds(int h, struct pollfd* pfds) { in prepare_poll_fds() argument
499 asrt(ts[h].poll_count <= MAX_POLL); in prepare_poll_fds()
500 memset(pfds, 0, sizeof(pfds[0]) * ts[h].poll_count); in prepare_poll_fds()
501 while (count < ts[h].poll_count) { in prepare_poll_fds()
506 ps_i, MAX_POLL, count, ts[h].poll_count); in prepare_poll_fds()
509 if (ts[h].ps[ps_i].pfd.fd >= 0) { in prepare_poll_fds()
510 pfds[pfd_i] = ts[h].ps[ps_i].pfd; in prepare_poll_fds()
511 ts[h].psi[pfd_i] = ps_i; in prepare_poll_fds()
521 int h = (intptr_t)arg; in sock_poll_thread() local
523 prepare_poll_fds(h, pfds); in sock_poll_thread()
525 OSI_NO_INTR(ret = poll(pfds, ts[h].poll_count, -1)); in sock_poll_thread()
535 asrt(pfds[0].fd == ts[h].cmd_fdr); in sock_poll_thread()
536 if (!process_cmd_sock(h)) { in sock_poll_thread()
537 APPL_TRACE_DEBUG("h:%d, process_cmd_sock return false, exit...", h); in sock_poll_thread()
545 if (need_process_data_fd) process_data_sock(h, pfds, ret); in sock_poll_thread()
550 APPL_TRACE_DEBUG("socket poll thread exiting, h:%d", h); in sock_poll_thread()