DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (b6057e17f856)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/*
** File:        foreign.c
** Description: Testing various functions w/ foreign threads
**
**      We create a thread and get it to call exactly one runtime function.
**      The thread is allowed to be created by some other environment that
**      NSPR, but it does not announce itself to the runtime prior to calling
**      in.
**
**      The goal: try to survive.
**      
*/

#include "prcvar.h"
#include "prenv.h"
#include "prerror.h"
#include "prinit.h"
#include "prinrval.h"
#include "prio.h"
#include "prlock.h"
#include "prlog.h"
#include "prmem.h"
#include "prthread.h"
#include "prtypes.h"
#include "prprf.h"
#include "plgetopt.h"

#include <stdio.h>
#include <stdlib.h>

static enum {
    thread_nspr, thread_pthread, thread_sproc, thread_win32
} thread_provider;

typedef void (*StartFn)(void*);
typedef struct StartObject
{
    StartFn start;
    void *arg;
} StartObject;

static PRFileDesc *output;

static int _debug_on = 0;

#define DEFAULT_THREAD_COUNT	10

#define DPRINTF(arg)	if (_debug_on) PR_fprintf arg

#if defined(_PR_PTHREADS) && !defined(_PR_DCETHREADS)
#include <pthread.h>
#include "md/_pth.h"
static void *pthread_start(void *arg)
{
    StartFn start = ((StartObject*)arg)->start;
    void *data = ((StartObject*)arg)->arg;
    PR_Free(arg);
    start(data);
    return NULL;
}  /* pthread_start */
#endif /* defined(_PR_PTHREADS) && !defined(_PR_DCETHREADS) */

#if defined(IRIX) && !defined(_PR_PTHREADS)
#include <sys/types.h>
#include <sys/prctl.h>
static void sproc_start(void *arg, PRSize size)
{
    StartObject *so = (StartObject*)arg;
    StartFn start = so->start;
    void *data = so->arg;
    PR_Free(so);
    start(data);
}  /* sproc_start */
#endif  /* defined(IRIX) && !defined(_PR_PTHREADS) */

#if defined(WIN32)
#include <windows.h>
#include <process.h>  /* for _beginthreadex() */

static PRUintn __stdcall windows_start(void *arg)
{
    StartObject *so = (StartObject*)arg;
    StartFn start = so->start;
    void *data = so->arg;
    PR_Free(so);
    start(data);
    return 0;
}  /* windows_start */
#endif /* defined(WIN32) */

static PRStatus NSPRPUB_TESTS_CreateThread(StartFn start, void *arg)
{
    PRStatus rv;

    switch (thread_provider)
    {
    case thread_nspr:
        {
            PRThread *thread = PR_CreateThread(
                PR_USER_THREAD, start, arg,
                PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
                PR_UNJOINABLE_THREAD, 0);
            rv = (NULL == thread) ? PR_FAILURE : PR_SUCCESS;
        }
        break;
    case thread_pthread:
#if defined(_PR_PTHREADS) && !defined(_PR_DCETHREADS)
        {
            int rv;
            pthread_t id;
            pthread_attr_t tattr;
            StartObject *start_object;
            start_object = PR_NEW(StartObject);
            PR_ASSERT(NULL != start_object);
            start_object->start = start;
            start_object->arg = arg;

            rv = _PT_PTHREAD_ATTR_INIT(&tattr);
            PR_ASSERT(0 == rv);

            rv = pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
            PR_ASSERT(0 == rv);

            rv = pthread_attr_setstacksize(&tattr, 64 * 1024);
            PR_ASSERT(0 == rv);

            rv = _PT_PTHREAD_CREATE(&id, tattr, pthread_start, start_object);
            (void)_PT_PTHREAD_ATTR_DESTROY(&tattr);
            return (0 == rv) ? PR_SUCCESS : PR_FAILURE;
        }
#else
        PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
        rv = PR_FAILURE;
        break;
#endif /* defined(_PR_PTHREADS) && !defined(_PR_DCETHREADS) */

    case thread_sproc:
#if defined(IRIX) && !defined(_PR_PTHREADS)
        {
            PRInt32 pid;
            StartObject *start_object;
            start_object = PR_NEW(StartObject);
            PR_ASSERT(NULL != start_object);
            start_object->start = start;
            start_object->arg = arg;
            pid = sprocsp(
                sproc_start, PR_SALL, start_object, NULL, 64 * 1024);
            rv = (0 < pid) ? PR_SUCCESS : PR_FAILURE;
        }
#else
        PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
        rv = PR_FAILURE;
#endif  /* defined(IRIX) && !defined(_PR_PTHREADS) */
        break;
    case thread_win32:
#if defined(WIN32)
        {
            void *th;
            PRUintn id;       
            StartObject *start_object;
            start_object = PR_NEW(StartObject);
            PR_ASSERT(NULL != start_object);
            start_object->start = start;
            start_object->arg = arg;
            th = (void*)_beginthreadex(
                NULL, /* LPSECURITY_ATTRIBUTES - pointer to thread security attributes */  
                0U, /* DWORD - initial thread stack size, in bytes */
                windows_start, /* LPTHREAD_START_ROUTINE - pointer to thread function */
                start_object, /* LPVOID - argument for new thread */
                STACK_SIZE_PARAM_IS_A_RESERVATION, /*DWORD dwCreationFlags - creation flags */
                &id /* LPDWORD - pointer to returned thread identifier */ );

            rv = (NULL == th) ? PR_FAILURE : PR_SUCCESS;
        }
#else
        PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
        rv = PR_FAILURE;
#endif
        break;
    default:
        PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
        rv = PR_FAILURE;
    }
    return rv;
}  /* NSPRPUB_TESTS_CreateThread */

static void PR_CALLBACK lazyEntry(void *arg)
{
    PR_ASSERT(NULL == arg);
}  /* lazyEntry */


static void OneShot(void *arg)
{
    PRUintn pdkey;
    PRLock *lock;
    PRFileDesc *fd;
    PRDir *dir;
    PRFileDesc *pair[2];
    PRIntn test = (PRIntn)arg;

	for (test = 0; test < 12; ++test) {

    switch (test)
    {
        case 0:
            lock = PR_NewLock(); 
			DPRINTF((output,"Thread[0x%x] called PR_NewLock\n",
			PR_GetCurrentThread()));
            PR_DestroyLock(lock);
            break;
            
        case 1:
            (void)PR_SecondsToInterval(1);
			DPRINTF((output,"Thread[0x%x] called PR_SecondsToInterval\n",
			PR_GetCurrentThread()));
            break;
            
        case 2: (void)PR_CreateThread(
            PR_USER_THREAD, lazyEntry, NULL, PR_PRIORITY_NORMAL,
            PR_LOCAL_THREAD, PR_UNJOINABLE_THREAD, 0); 
			DPRINTF((output,"Thread[0x%x] called PR_CreateThread\n",
			PR_GetCurrentThread()));
            break;
            
        case 3:
            fd = PR_Open("foreign.tmp", PR_CREATE_FILE | PR_RDWR, 0666); 
			DPRINTF((output,"Thread[0x%x] called PR_Open\n",
			PR_GetCurrentThread()));
            PR_Close(fd);
            break;
            
        case 4:
            fd = PR_NewUDPSocket(); 
			DPRINTF((output,"Thread[0x%x] called PR_NewUDPSocket\n",
			PR_GetCurrentThread()));
            PR_Close(fd);
            break;
            
        case 5:
            fd = PR_NewTCPSocket(); 
			DPRINTF((output,"Thread[0x%x] called PR_NewTCPSocket\n",
			PR_GetCurrentThread()));
            PR_Close(fd);
            break;
            
        case 6:
#ifdef SYMBIAN
#define TEMP_DIR "c:\\data\\"
#else
#define TEMP_DIR "/tmp/"
#endif
            dir = PR_OpenDir(TEMP_DIR);
			DPRINTF((output,"Thread[0x%x] called PR_OpenDir\n",
			PR_GetCurrentThread()));
            PR_CloseDir(dir);
            break;
            
        case 7:
            (void)PR_NewThreadPrivateIndex(&pdkey, NULL);
			DPRINTF((output,"Thread[0x%x] called PR_NewThreadPrivateIndex\n",
			PR_GetCurrentThread()));
            break;
        
        case 8:
            (void)PR_GetEnv("PATH");
			DPRINTF((output,"Thread[0x%x] called PR_GetEnv\n",
			PR_GetCurrentThread()));
            break;
            
        case 9:
            (void)PR_NewTCPSocketPair(pair);
			DPRINTF((output,"Thread[0x%x] called PR_NewTCPSocketPair\n",
			PR_GetCurrentThread()));
            PR_Close(pair[0]);
            PR_Close(pair[1]);
            break;
            
        case 10:
            PR_SetConcurrency(2);
			DPRINTF((output,"Thread[0x%x] called PR_SetConcurrency\n",
			PR_GetCurrentThread()));
            break;

        case 11:
            PR_SetThreadPriority(PR_GetCurrentThread(), PR_PRIORITY_HIGH);
			DPRINTF((output,"Thread[0x%x] called PR_SetThreadPriority\n",
			PR_GetCurrentThread()));
            break;
            
        default: 
            break;
    } /* switch() */
	}
}  /* OneShot */

int main(int argc, char **argv)
{
    PRStatus rv;
	PRInt32	thread_cnt = DEFAULT_THREAD_COUNT;
	PLOptStatus os;
	PLOptState *opt = PL_CreateOptState(argc, argv, "dt:");

#if defined(WIN32)
	thread_provider = thread_win32;
#elif defined(_PR_PTHREADS)
	thread_provider = thread_pthread;
#elif defined(IRIX)
	thread_provider = thread_sproc;
#else
    thread_provider = thread_nspr;
#endif


	while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
    {
		if (PL_OPT_BAD == os) continue;
        switch (opt->option)
        {
        case 'd':  /* debug mode */
			_debug_on = 1;
            break;
        case 't':  /* thread count */
            thread_cnt = atoi(opt->value);
            break;
         default:
            break;
        }
    }
	PL_DestroyOptState(opt);

	PR_SetConcurrency(2);

	output = PR_GetSpecialFD(PR_StandardOutput);

    while (thread_cnt-- > 0)
    {
        rv = NSPRPUB_TESTS_CreateThread(OneShot, (void*)thread_cnt);
        PR_ASSERT(PR_SUCCESS == rv);
        PR_Sleep(PR_MillisecondsToInterval(5));
    }
    PR_Sleep(PR_SecondsToInterval(3));
    return (PR_SUCCESS == PR_Cleanup()) ? 0 : 1;
}  /* main */

/* foreign.c */