michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * File: multiacc.c michael@0: * michael@0: * Description: michael@0: * This test creates multiple threads that accept on the michael@0: * same listening socket. michael@0: */ michael@0: michael@0: #include "nspr.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #define NUM_SERVER_THREADS 10 michael@0: michael@0: static int num_server_threads = NUM_SERVER_THREADS; michael@0: static PRThreadScope thread_scope = PR_GLOBAL_THREAD; michael@0: static PRBool exit_flag = PR_FALSE; michael@0: michael@0: static void ServerThreadFunc(void *arg) michael@0: { michael@0: PRFileDesc *listenSock = (PRFileDesc *) arg; michael@0: PRFileDesc *acceptSock; michael@0: PRErrorCode err; michael@0: PRStatus status; michael@0: michael@0: while (!exit_flag) { michael@0: acceptSock = PR_Accept(listenSock, NULL, PR_INTERVAL_NO_TIMEOUT); michael@0: if (NULL == acceptSock) { michael@0: err = PR_GetError(); michael@0: if (PR_PENDING_INTERRUPT_ERROR == err) { michael@0: printf("server thread is interrupted\n"); michael@0: fflush(stdout); michael@0: continue; michael@0: } michael@0: fprintf(stderr, "PR_Accept failed: %d\n", err); michael@0: exit(1); michael@0: } michael@0: status = PR_Close(acceptSock); michael@0: if (PR_FAILURE == status) { michael@0: fprintf(stderr, "PR_Close failed\n"); michael@0: exit(1); michael@0: } michael@0: } michael@0: } michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: PRNetAddr serverAddr; michael@0: PRFileDesc *dummySock; michael@0: PRFileDesc *listenSock; michael@0: PRFileDesc *clientSock; michael@0: PRThread *dummyThread; michael@0: PRThread **serverThreads; michael@0: PRStatus status; michael@0: PRUint16 port; michael@0: int idx; michael@0: PRInt32 nbytes; michael@0: char buf[1024]; michael@0: michael@0: serverThreads = (PRThread **) michael@0: PR_Malloc(num_server_threads * sizeof(PRThread *)); michael@0: if (NULL == serverThreads) { michael@0: fprintf(stderr, "PR_Malloc failed\n"); michael@0: exit(1); michael@0: } michael@0: michael@0: /* michael@0: * Create a dummy listening socket and have the first michael@0: * (dummy) thread listen on it. This is to ensure that michael@0: * the first thread becomes the I/O continuation thread michael@0: * in the pthreads implementation (see ptio.c) and remains michael@0: * so throughout the test, so that we never have to michael@0: * recycle the I/O continuation thread. michael@0: */ michael@0: dummySock = PR_NewTCPSocket(); michael@0: if (NULL == dummySock) { michael@0: fprintf(stderr, "PR_NewTCPSocket failed\n"); michael@0: exit(1); michael@0: } michael@0: memset(&serverAddr, 0, sizeof(serverAddr)); michael@0: status = PR_InitializeNetAddr(PR_IpAddrAny, 0, &serverAddr); michael@0: if (PR_FAILURE == status) { michael@0: fprintf(stderr, "PR_InitializeNetAddr failed\n"); michael@0: exit(1); michael@0: } michael@0: status = PR_Bind(dummySock, &serverAddr); michael@0: if (PR_FAILURE == status) { michael@0: fprintf(stderr, "PR_Bind failed\n"); michael@0: exit(1); michael@0: } michael@0: status = PR_Listen(dummySock, 5); michael@0: if (PR_FAILURE == status) { michael@0: fprintf(stderr, "PR_Listen failed\n"); michael@0: exit(1); michael@0: } michael@0: michael@0: listenSock = PR_NewTCPSocket(); michael@0: if (NULL == listenSock) { michael@0: fprintf(stderr, "PR_NewTCPSocket failed\n"); michael@0: exit(1); michael@0: } michael@0: memset(&serverAddr, 0, sizeof(serverAddr)); michael@0: status = PR_InitializeNetAddr(PR_IpAddrAny, 0, &serverAddr); michael@0: if (PR_FAILURE == status) { michael@0: fprintf(stderr, "PR_InitializeNetAddr failed\n"); michael@0: exit(1); michael@0: } michael@0: status = PR_Bind(listenSock, &serverAddr); michael@0: if (PR_FAILURE == status) { michael@0: fprintf(stderr, "PR_Bind failed\n"); michael@0: exit(1); michael@0: } michael@0: status = PR_GetSockName(listenSock, &serverAddr); michael@0: if (PR_FAILURE == status) { michael@0: fprintf(stderr, "PR_GetSockName failed\n"); michael@0: exit(1); michael@0: } michael@0: port = PR_ntohs(serverAddr.inet.port); michael@0: status = PR_Listen(listenSock, 5); michael@0: if (PR_FAILURE == status) { michael@0: fprintf(stderr, "PR_Listen failed\n"); michael@0: exit(1); michael@0: } michael@0: michael@0: printf("creating dummy thread\n"); michael@0: fflush(stdout); michael@0: dummyThread = PR_CreateThread(PR_USER_THREAD, michael@0: ServerThreadFunc, dummySock, PR_PRIORITY_NORMAL, michael@0: thread_scope, PR_JOINABLE_THREAD, 0); michael@0: if (NULL == dummyThread) { michael@0: fprintf(stderr, "PR_CreateThread failed\n"); michael@0: exit(1); michael@0: } michael@0: printf("sleeping one second before creating server threads\n"); michael@0: fflush(stdout); michael@0: PR_Sleep(PR_SecondsToInterval(1)); michael@0: for (idx = 0; idx < num_server_threads; idx++) { michael@0: serverThreads[idx] = PR_CreateThread(PR_USER_THREAD, michael@0: ServerThreadFunc, listenSock, PR_PRIORITY_NORMAL, michael@0: thread_scope, PR_JOINABLE_THREAD, 0); michael@0: if (NULL == serverThreads[idx]) { michael@0: fprintf(stderr, "PR_CreateThread failed\n"); michael@0: exit(1); michael@0: } michael@0: } michael@0: michael@0: memset(&serverAddr, 0, sizeof(serverAddr)); michael@0: PR_InitializeNetAddr(PR_IpAddrLoopback, port, &serverAddr); michael@0: clientSock = PR_NewTCPSocket(); michael@0: if (NULL == clientSock) { michael@0: fprintf(stderr, "PR_NewTCPSocket failed\n"); michael@0: exit(1); michael@0: } michael@0: printf("sleeping one second before connecting\n"); michael@0: fflush(stdout); michael@0: PR_Sleep(PR_SecondsToInterval(1)); michael@0: status = PR_Connect(clientSock, &serverAddr, PR_INTERVAL_NO_TIMEOUT); michael@0: if (PR_FAILURE == status) { michael@0: fprintf(stderr, "PR_Connect failed\n"); michael@0: exit(1); michael@0: } michael@0: nbytes = PR_Read(clientSock, buf, sizeof(buf)); michael@0: if (nbytes != 0) { michael@0: fprintf(stderr, "expected 0 bytes but got %d bytes\n", nbytes); michael@0: exit(1); michael@0: } michael@0: status = PR_Close(clientSock); michael@0: if (PR_FAILURE == status) { michael@0: fprintf(stderr, "PR_Close failed\n"); michael@0: exit(1); michael@0: } michael@0: printf("sleeping one second before shutting down server threads\n"); michael@0: fflush(stdout); michael@0: PR_Sleep(PR_SecondsToInterval(1)); michael@0: michael@0: exit_flag = PR_TRUE; michael@0: status = PR_Interrupt(dummyThread); michael@0: if (PR_FAILURE == status) { michael@0: fprintf(stderr, "PR_Interrupt failed\n"); michael@0: exit(1); michael@0: } michael@0: status = PR_JoinThread(dummyThread); michael@0: if (PR_FAILURE == status) { michael@0: fprintf(stderr, "PR_JoinThread failed\n"); michael@0: exit(1); michael@0: } michael@0: for (idx = 0; idx < num_server_threads; idx++) { michael@0: status = PR_Interrupt(serverThreads[idx]); michael@0: if (PR_FAILURE == status) { michael@0: fprintf(stderr, "PR_Interrupt failed\n"); michael@0: exit(1); michael@0: } michael@0: status = PR_JoinThread(serverThreads[idx]); michael@0: if (PR_FAILURE == status) { michael@0: fprintf(stderr, "PR_JoinThread failed\n"); michael@0: exit(1); michael@0: } michael@0: } michael@0: PR_Free(serverThreads); michael@0: status = PR_Close(dummySock); michael@0: if (PR_FAILURE == status) { michael@0: fprintf(stderr, "PR_Close failed\n"); michael@0: exit(1); michael@0: } michael@0: status = PR_Close(listenSock); michael@0: if (PR_FAILURE == status) { michael@0: fprintf(stderr, "PR_Close failed\n"); michael@0: exit(1); michael@0: } michael@0: michael@0: printf("PASS\n"); michael@0: return 0; michael@0: }