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: * This is a test for the io continuation thread machinery michael@0: * in pthreads. michael@0: */ michael@0: michael@0: #include "nspr.h" michael@0: #include michael@0: michael@0: int num_threads = 10; /* must be an even number */ michael@0: PRThreadScope thread_scope = PR_GLOBAL_THREAD; michael@0: michael@0: void ThreadFunc(void *arg) michael@0: { michael@0: PRFileDesc *fd = (PRFileDesc *) arg; michael@0: char buf[1024]; michael@0: PRInt32 nbytes; michael@0: PRErrorCode err; michael@0: michael@0: nbytes = PR_Recv(fd, buf, sizeof(buf), 0, PR_SecondsToInterval(20)); michael@0: if (nbytes == -1) { michael@0: err = PR_GetError(); michael@0: if (err != PR_PENDING_INTERRUPT_ERROR) { michael@0: fprintf(stderr, "PR_Recv failed: (%d, %d)\n", michael@0: err, PR_GetOSError()); michael@0: PR_ProcessExit(1); michael@0: } michael@0: /* michael@0: * After getting an I/O interrupt, this thread must michael@0: * close the fd before it exits due to a limitation michael@0: * of our NT implementation. michael@0: */ michael@0: if (PR_Close(fd) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_Close failed\n"); michael@0: PR_ProcessExit(1); michael@0: } michael@0: } else { michael@0: fprintf(stderr, "PR_Recv received %d bytes!?\n", nbytes); michael@0: PR_ProcessExit(1); michael@0: } michael@0: } michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: PRFileDesc **fds; michael@0: PRThread **threads; michael@0: PRIntervalTime start, elapsed; michael@0: int index; michael@0: michael@0: fds = (PRFileDesc **) PR_MALLOC(2 * num_threads * sizeof(PRFileDesc *)); michael@0: PR_ASSERT(fds != NULL); michael@0: threads = (PRThread **) PR_MALLOC(num_threads * sizeof(PRThread *)); michael@0: PR_ASSERT(threads != NULL); michael@0: michael@0: for (index = 0; index < num_threads; index++) { michael@0: if (PR_NewTCPSocketPair(&fds[2 * index]) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_NewTCPSocket failed\n"); michael@0: PR_ProcessExit(1); michael@0: } michael@0: threads[index] = PR_CreateThread( michael@0: PR_USER_THREAD, ThreadFunc, fds[2 * index], michael@0: PR_PRIORITY_NORMAL, thread_scope, PR_JOINABLE_THREAD, 0); michael@0: if (NULL == threads[index]) { michael@0: fprintf(stderr, "PR_CreateThread failed\n"); michael@0: PR_ProcessExit(1); michael@0: } michael@0: } michael@0: michael@0: /* Let the threads block in PR_Recv */ michael@0: PR_Sleep(PR_SecondsToInterval(2)); michael@0: michael@0: printf("Interrupting the threads\n"); michael@0: fflush(stdout); michael@0: start = PR_IntervalNow(); michael@0: for (index = 0; index < num_threads; index++) { michael@0: if (PR_Interrupt(threads[index]) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_Interrupt failed\n"); michael@0: PR_ProcessExit(1); michael@0: } michael@0: } michael@0: for (index = 0; index < num_threads; index++) { michael@0: if (PR_JoinThread(threads[index]) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_JoinThread failed\n"); michael@0: PR_ProcessExit(1); michael@0: } michael@0: } michael@0: elapsed = (PRIntervalTime)(PR_IntervalNow() - start); michael@0: printf("Threads terminated in %d milliseconds\n", michael@0: PR_IntervalToMilliseconds(elapsed)); michael@0: fflush(stdout); michael@0: michael@0: /* We are being very generous and allow 10 seconds. */ michael@0: if (elapsed >= PR_SecondsToInterval(10)) { michael@0: fprintf(stderr, "Interrupting threads took longer than 10 seconds!!\n"); michael@0: PR_ProcessExit(1); michael@0: } michael@0: michael@0: for (index = 0; index < num_threads; index++) { michael@0: /* fds[2 * index] was passed to and closed by threads[index]. */ michael@0: if (PR_Close(fds[2 * index + 1]) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_Close failed\n"); michael@0: PR_ProcessExit(1); michael@0: } michael@0: } michael@0: PR_DELETE(threads); michael@0: PR_DELETE(fds); michael@0: printf("PASS\n"); michael@0: PR_Cleanup(); michael@0: return 0; michael@0: }