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: intrio.c michael@0: * Purpose: testing i/o interrupts (see Bugzilla bug #31120) michael@0: */ michael@0: michael@0: #include "nspr.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: /* for synchronization between the main thread and iothread */ michael@0: static PRLock *lock; michael@0: static PRCondVar *cvar; michael@0: static PRBool iothread_ready; michael@0: michael@0: static void PR_CALLBACK AbortIO(void *arg) michael@0: { michael@0: PRStatus rv; michael@0: PR_Sleep(PR_SecondsToInterval(2)); michael@0: rv = PR_Interrupt((PRThread*)arg); michael@0: PR_ASSERT(PR_SUCCESS == rv); michael@0: } /* AbortIO */ michael@0: michael@0: static void PR_CALLBACK IOThread(void *arg) michael@0: { michael@0: PRFileDesc *sock, *newsock; michael@0: PRNetAddr addr; michael@0: michael@0: sock = PR_OpenTCPSocket(PR_AF_INET6); michael@0: if (sock == NULL) { michael@0: fprintf(stderr, "PR_OpenTCPSocket failed\n"); michael@0: exit(1); michael@0: } michael@0: memset(&addr, 0, sizeof(addr)); michael@0: if (PR_SetNetAddr(PR_IpAddrAny, PR_AF_INET6, 0, &addr) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_SetNetAddr failed\n"); michael@0: exit(1); michael@0: } michael@0: if (PR_Bind(sock, &addr) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_Bind failed\n"); michael@0: exit(1); michael@0: } michael@0: if (PR_Listen(sock, 5) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_Listen failed\n"); michael@0: exit(1); michael@0: } michael@0: /* tell the main thread that we are ready */ michael@0: PR_Lock(lock); michael@0: iothread_ready = PR_TRUE; michael@0: PR_NotifyCondVar(cvar); michael@0: PR_Unlock(lock); michael@0: newsock = PR_Accept(sock, NULL, PR_INTERVAL_NO_TIMEOUT); michael@0: if (newsock != NULL) { michael@0: fprintf(stderr, "PR_Accept shouldn't have succeeded\n"); michael@0: exit(1); michael@0: } michael@0: if (PR_GetError() != PR_PENDING_INTERRUPT_ERROR) { michael@0: fprintf(stderr, "PR_Accept failed (%d, %d)\n", michael@0: PR_GetError(), PR_GetOSError()); michael@0: exit(1); michael@0: } michael@0: printf("PR_Accept() is interrupted as expected\n"); michael@0: if (PR_Close(sock) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_Close failed\n"); michael@0: exit(1); michael@0: } michael@0: } michael@0: michael@0: static void Test(PRThreadScope scope1, PRThreadScope scope2) michael@0: { michael@0: PRThread *iothread, *abortio; michael@0: michael@0: printf("A %s thread will be interrupted by a %s thread\n", michael@0: (scope1 == PR_LOCAL_THREAD ? "local" : "global"), michael@0: (scope2 == PR_LOCAL_THREAD ? "local" : "global")); michael@0: iothread_ready = PR_FALSE; michael@0: iothread = PR_CreateThread( michael@0: PR_USER_THREAD, IOThread, NULL, PR_PRIORITY_NORMAL, michael@0: scope1, PR_JOINABLE_THREAD, 0); michael@0: if (iothread == NULL) { michael@0: fprintf(stderr, "cannot create thread\n"); michael@0: exit(1); michael@0: } michael@0: PR_Lock(lock); michael@0: while (!iothread_ready) michael@0: PR_WaitCondVar(cvar, PR_INTERVAL_NO_TIMEOUT); michael@0: PR_Unlock(lock); michael@0: abortio = PR_CreateThread( michael@0: PR_USER_THREAD, AbortIO, iothread, PR_PRIORITY_NORMAL, michael@0: scope2, PR_JOINABLE_THREAD, 0); michael@0: if (abortio == NULL) { michael@0: fprintf(stderr, "cannot create thread\n"); michael@0: exit(1); michael@0: } michael@0: if (PR_JoinThread(iothread) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_JoinThread failed\n"); michael@0: exit(1); michael@0: } michael@0: if (PR_JoinThread(abortio) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_JoinThread failed\n"); michael@0: exit(1); michael@0: } michael@0: } michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: PR_STDIO_INIT(); michael@0: lock = PR_NewLock(); michael@0: if (lock == NULL) { michael@0: fprintf(stderr, "PR_NewLock failed\n"); michael@0: exit(1); michael@0: } michael@0: cvar = PR_NewCondVar(lock); michael@0: if (cvar == NULL) { michael@0: fprintf(stderr, "PR_NewCondVar failed\n"); michael@0: exit(1); michael@0: } michael@0: /* test all four combinations */ michael@0: Test(PR_LOCAL_THREAD, PR_LOCAL_THREAD); michael@0: Test(PR_LOCAL_THREAD, PR_GLOBAL_THREAD); michael@0: Test(PR_GLOBAL_THREAD, PR_LOCAL_THREAD); michael@0: Test(PR_GLOBAL_THREAD, PR_GLOBAL_THREAD); michael@0: printf("PASSED\n"); michael@0: return 0; michael@0: } /* main */