/* $Id$ */ /* * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com) * Copyright (C) 2003-2008 Benny Prijono * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "test.h" #if INCLUDE_IOQUEUE_UNREG_TEST /* * This tests the thread safety of ioqueue unregistration operation. */ #include #include #include #include #include #include #include #include #define THIS_FILE "ioq_unreg.c" enum test_method { UNREGISTER_IN_APP, UNREGISTER_IN_CALLBACK, }; static int thread_quitting; static enum test_method test_method; static pj_time_val time_to_unregister; struct sock_data { pj_sock_t sock; pj_sock_t csock; pj_pool_t *pool; pj_ioqueue_key_t *key; pj_mutex_t *mutex; pj_ioqueue_op_key_t *op_key; char *buffer; pj_size_t bufsize; pj_bool_t unregistered; pj_ssize_t received; } sock_data; static void on_read_complete(pj_ioqueue_key_t *key, pj_ioqueue_op_key_t *op_key, pj_ssize_t bytes_read) { pj_ssize_t size; char *sendbuf = "Hello world"; pj_status_t status; if (sock_data.unregistered) return; pj_mutex_lock(sock_data.mutex); if (sock_data.unregistered) { pj_mutex_unlock(sock_data.mutex); return; } if (bytes_read < 0) { if (-bytes_read != PJ_STATUS_FROM_OS(PJ_BLOCKING_ERROR_VAL)) app_perror("ioqueue reported recv error", (pj_status_t)-bytes_read); } else { sock_data.received += bytes_read; } if (test_method == UNREGISTER_IN_CALLBACK) { pj_time_val now; pj_gettimeofday(&now); if (PJ_TIME_VAL_GTE(now, time_to_unregister)) { sock_data.unregistered = 1; pj_ioqueue_unregister(key); pj_mutex_unlock(sock_data.mutex); return; } } do { size = sock_data.bufsize; status = pj_ioqueue_recv(key, op_key, sock_data.buffer, &size, 0); if (status != PJ_EPENDING && status != PJ_SUCCESS) app_perror("recv() error", status); } while (status == PJ_SUCCESS); pj_mutex_unlock(sock_data.mutex); size = pj_ansi_strlen(sendbuf); status = pj_sock_send(sock_data.csock, sendbuf, &size, 0); if (status != PJ_SUCCESS) app_perror("send() error", status); size = pj_ansi_strlen(sendbuf); status = pj_sock_send(sock_data.csock, sendbuf, &size, 0); if (status != PJ_SUCCESS) app_perror("send() error", status); } static int worker_thread(void *arg) { pj_ioqueue_t *ioqueue = (pj_ioqueue_t*) arg; while (!thread_quitting) { pj_time_val timeout = { 0, 20 }; pj_ioqueue_poll(ioqueue, &timeout); } return 0; } /* * Perform unregistration test. * * This will create ioqueue and register a server socket. Depending * on the test method, either the callback or the main thread will * unregister and destroy the server socket after some period of time. */ static int perform_unreg_test(pj_ioqueue_t *ioqueue, pj_pool_t *test_pool, const char *title, pj_bool_t other_socket) { enum { WORKER_CNT = 1, MSEC = 500, QUIT_MSEC = 500 }; int i; pj_thread_t *thread[WORKER_CNT]; struct sock_data osd; pj_ioqueue_callback callback; pj_time_val end_time; pj_status_t status; /* Sometimes its important to have other sockets registered to * the ioqueue, because when no sockets are registered, the ioqueue * will return from the poll early. */ if (other_socket) { status = app_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, 56127, &osd.sock); if (status != PJ_SUCCESS) { app_perror("Error creating other socket", status); return -12; } pj_bzero(&callback, sizeof(callback)); status = pj_ioqueue_register_sock(test_pool, ioqueue, osd.sock, NULL, &callback, &osd.key); if (status != PJ_SUCCESS) { app_perror("Error registering other socket", status); return -13; } } else { osd.key = NULL; osd.sock = PJ_INVALID_SOCKET; } /* Init both time duration of testing */ thread_quitting = 0; pj_gettimeofday(&time_to_unregister); time_to_unregister.msec += MSEC; pj_time_val_normalize(&time_to_unregister); end_time = time_to_unregister; end_time.msec += QUIT_MSEC; pj_time_val_normalize(&end_time); /* Create polling thread */ for (i=0; i