Back to index

lightning-sunbird  0.9+nobinonly
primblok.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*
00039  * File:        primblok.c
00040  * Purpose:     testing whether the primordial thread can block in a
00041  *              native blocking function without affecting the correct
00042  *              functioning of NSPR I/O functions (Bugzilla bug #30746)
00043  */
00044 
00045 #if !defined(WINNT)
00046 
00047 #include <stdio.h>
00048 
00049 int main()
00050 {
00051     printf("This test is not relevant on this platform\n");
00052     return 0;
00053 }
00054 
00055 #else /* WINNT */
00056 
00057 #include "nspr.h"
00058 
00059 #include <windows.h>
00060 #include <stdio.h>
00061 #include <stdlib.h>
00062 #include <string.h>
00063 
00064 #define TEST_FILE_NAME "primblok.dat"
00065 
00066 /* use InterlockedExchange to update this variable */
00067 static LONG iothread_done;
00068 
00069 static void PR_CALLBACK IOThread(void *arg)
00070 {
00071     PRFileDesc *fd;
00072     char buf[32];
00073     PRInt32 nbytes;
00074 
00075     /* Give the primordial thread one second to block */
00076     Sleep(1000);
00077 
00078     /*
00079      * See if our PR_Write call will hang when the primordial
00080      * thread is blocking in a native blocking function.
00081      */
00082     fd = PR_Open(TEST_FILE_NAME, PR_WRONLY|PR_CREATE_FILE, 0666);
00083     if (NULL == fd) {
00084         fprintf(stderr, "PR_Open failed\n");
00085         exit(1);
00086     }
00087     memset(buf, 0xaf, sizeof(buf));
00088     fprintf(stderr, "iothread: calling PR_Write\n");
00089     nbytes = PR_Write(fd, buf, sizeof(buf));
00090     fprintf(stderr, "iothread: PR_Write returned\n");
00091     if (nbytes != sizeof(buf)) {
00092         fprintf(stderr, "PR_Write returned %d\n", nbytes);
00093         exit(1);
00094     }
00095     if (PR_Close(fd) == PR_FAILURE) {
00096         fprintf(stderr, "PR_Close failed\n");
00097         exit(1);
00098     }
00099     if (PR_Delete(TEST_FILE_NAME) == PR_FAILURE) {
00100         fprintf(stderr, "PR_Delete failed\n");
00101         exit(1);
00102     }
00103 
00104     /* Tell the main thread that we are done */
00105     InterlockedExchange(&iothread_done, 1);
00106 }
00107 
00108 int main()
00109 {
00110     PRThread *iothread;
00111 
00112     /* Must be a global thread */
00113     iothread = PR_CreateThread(
00114         PR_USER_THREAD, IOThread, NULL, PR_PRIORITY_NORMAL,
00115         PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
00116     if (iothread == NULL) {
00117         fprintf(stderr, "cannot create thread\n");
00118         exit(1);
00119     }
00120 
00121     /*
00122      * Block in a native blocking function.
00123      * Give iothread 5 seconds to finish its task.
00124      */
00125     Sleep(5000);
00126 
00127     /*
00128      * Is iothread done or is it hung?
00129      *
00130      * I'm actually only interested in reading the value
00131      * of iothread_done.  I'm using InterlockedExchange as
00132      * a thread-safe way to read iothread_done.
00133      */
00134     if (InterlockedExchange(&iothread_done, 1) == 0) {
00135         fprintf(stderr, "iothread is hung\n");
00136         fprintf(stderr, "FAILED\n");
00137         exit(1);
00138     }
00139 
00140     if (PR_JoinThread(iothread) == PR_FAILURE) {
00141         fprintf(stderr, "PR_JoinThread failed\n");
00142         exit(1);
00143     }
00144     printf("PASSED\n");
00145     return 0;
00146 }  /* main */
00147 
00148 #endif /* WINNT */