コード例 #1
0
static void ConnectingThread(void *arg)
{
    PRInt32 nbytes;
#ifdef SYMBIAN
    char buf[256];
#else
    char buf[1024];
#endif
    PRFileDesc *sock;
    PRNetAddr peer_addr, *addr;

    addr = (PRNetAddr*)arg;

    sock = PR_NewTCPSocket();
    if (sock == NULL)
    {
        PL_FPrintError(err_out, "PR_NewTCPSocket (client) failed");
        PR_ProcessExit(1);
    }

    if (PR_Connect(sock, addr, PR_INTERVAL_NO_TIMEOUT) == PR_FAILURE)
    {
        PL_FPrintError(err_out, "PR_Connect (client) failed");
        PR_ProcessExit(1);
    }
    if (PR_GetPeerName(sock, &peer_addr) == PR_FAILURE)
    {
        PL_FPrintError(err_out, "PR_GetPeerName (client) failed");
        PR_ProcessExit(1);
    }

    /*
    ** Then wait between the connection coming up and sending the expected
    ** data. At some point in time, the server should fail due to a timeou
    ** on the AcceptRead() operation, which according to the document is
    ** only due to the read() portion.
    */
    PR_Sleep(write_dally);

    nbytes = PR_Send(sock, GET, sizeof(GET), 0, PR_INTERVAL_NO_TIMEOUT);
    if (nbytes == -1) PL_FPrintError(err_out, "PR_Send (client) failed");

    nbytes = PR_Recv(sock, buf, sizeof(buf), 0, PR_INTERVAL_NO_TIMEOUT);
    if (nbytes == -1) PL_FPrintError(err_out, "PR_Recv (client) failed");
    else
    {
        PR_fprintf(std_out, "PR_Recv (client) succeeded: %d bytes\n", nbytes);
        buf[sizeof(buf) - 1] = '\0';
        PR_fprintf(std_out, "%s\n", buf);
    }

    if (PR_FAILURE == PR_Shutdown(sock, PR_SHUTDOWN_BOTH))
        PL_FPrintError(err_out, "PR_Shutdown (client) failed");

    if (PR_FAILURE == PR_Close(sock))
        PL_FPrintError(err_out, "PR_Close (client) failed");

    return;
}  /* ConnectingThread */
コード例 #2
0
int main(int argc, char **argv)
{
    PRHostEnt he;
    PRStatus status;
    PRIntn next_index;
    PRUint16 port_number;
    char netdb_buf[PR_NETDB_BUF_SIZE];
    PRNetAddr client_addr, server_addr;
    PRThread *client_thread, *server_thread;
    PRIntervalTime delta = PR_MillisecondsToInterval(500);

    err_out = PR_STDERR;
    std_out = PR_STDOUT;
    accept_timeout = PR_SecondsToInterval(2);
    emu_layer_ident = PR_GetUniqueIdentity("Emulated AcceptRead");
    emu_layer_methods = *PR_GetDefaultIOMethods();
    emu_layer_methods.acceptread = emu_AcceptRead;

    if (argc != 2 && argc != 3) port_number = DEFAULT_PORT;
    else port_number = (PRUint16)atoi(argv[(argc == 2) ? 1 : 2]);

    status = PR_InitializeNetAddr(PR_IpAddrAny, port_number, &server_addr);
    if (PR_SUCCESS != status)
    {
        PL_FPrintError(err_out, "PR_InitializeNetAddr failed");
        PR_ProcessExit(1);
    }
    if (argc < 3)
    {
        status = PR_InitializeNetAddr(
            PR_IpAddrLoopback, port_number, &client_addr);
        if (PR_SUCCESS != status)
        {
            PL_FPrintError(err_out, "PR_InitializeNetAddr failed");
            PR_ProcessExit(1);
        }
    }
    else
    {
        status = PR_GetHostByName(
            argv[1], netdb_buf, sizeof(netdb_buf), &he);
        if (status == PR_FAILURE)
        {
            PL_FPrintError(err_out, "PR_GetHostByName failed");
            PR_ProcessExit(1);
        }
        next_index = PR_EnumerateHostEnt(0, &he, port_number, &client_addr);
        if (next_index == -1)
        {
            PL_FPrintError(err_out, "PR_EnumerateHostEnt failed");
            PR_ProcessExit(1);
        }
    }

    for (
        write_dally = 0;
        write_dally < accept_timeout + (2 * delta);
        write_dally += delta)
    {
        PR_fprintf(
            std_out, "Testing w/ write_dally = %d msec\n",
            PR_IntervalToMilliseconds(write_dally));
        server_thread = PR_CreateThread(
            PR_USER_THREAD, AcceptingThread, &server_addr,
            PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
        if (server_thread == NULL)
        {
            PL_FPrintError(err_out, "PR_CreateThread (server) failed");
            PR_ProcessExit(1);
        }

        PR_Sleep(delta);  /* let the server pot thicken */

        client_thread = PR_CreateThread(
            PR_USER_THREAD, ConnectingThread, &client_addr,
            PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
        if (client_thread == NULL)
        {
            PL_FPrintError(err_out, "PR_CreateThread (client) failed");
            PR_ProcessExit(1);
        }

        if (PR_JoinThread(client_thread) == PR_FAILURE)
            PL_FPrintError(err_out, "PR_JoinThread (client) failed");

        if (PR_JoinThread(server_thread) == PR_FAILURE)
            PL_FPrintError(err_out, "PR_JoinThread (server) failed");
    }

    return 0;
}
コード例 #3
0
static void AcceptingThread(void *arg)
{
    PRStatus rv;
    PRInt32 bytes;
    PRSize buf_size = BUF_SIZE;
    PRUint8 buf[BUF_SIZE + (2 * sizeof(PRNetAddr)) + 32];
    PRNetAddr *accept_addr, *listen_addr = (PRNetAddr*)arg;
    PRFileDesc *accept_sock, *listen_sock = PR_NewTCPSocket();
    PRFileDesc *layer;
    PRSocketOptionData sock_opt;

    if (NULL == listen_sock)
    {
        PL_FPrintError(err_out, "PR_NewTCPSocket (server) failed");
        PR_ProcessExit(1);        
    }
    layer = PR_CreateIOLayerStub(emu_layer_ident, &emu_layer_methods);
    if (NULL == layer)
    {
        PL_FPrintError(err_out, "PR_CreateIOLayerStub (server) failed");
        PR_ProcessExit(1);        
    }
    if (PR_PushIOLayer(listen_sock, PR_TOP_IO_LAYER, layer) == PR_FAILURE)
    {
        PL_FPrintError(err_out, "PR_PushIOLayer (server) failed");
        PR_ProcessExit(1);        
    }
    sock_opt.option = PR_SockOpt_Reuseaddr;
    sock_opt.value.reuse_addr = PR_TRUE;
    rv = PR_SetSocketOption(listen_sock, &sock_opt);
    if (PR_FAILURE == rv)
    {
        PL_FPrintError(err_out, "PR_SetSocketOption (server) failed");
        PR_ProcessExit(1);        
    }
    rv = PR_Bind(listen_sock, listen_addr);
    if (PR_FAILURE == rv)
    {
        PL_FPrintError(err_out, "PR_Bind (server) failed");
        PR_ProcessExit(1);        
    }
    rv = PR_Listen(listen_sock, 10);
    if (PR_FAILURE == rv)
    {
        PL_FPrintError(err_out, "PR_Listen (server) failed");
        PR_ProcessExit(1);        
    }
    bytes = PR_AcceptRead(
        listen_sock, &accept_sock, &accept_addr, buf, buf_size, accept_timeout);

    if (-1 == bytes) PL_FPrintError(err_out, "PR_AcceptRead (server) failed");
    else
    {
        PrintAddress(accept_addr);
        PR_fprintf(
            std_out, "(Server) read [0x%p..0x%p) %s\n",
            buf, &buf[BUF_SIZE], buf);
        bytes = PR_Write(accept_sock, buf, bytes);
        rv = PR_Shutdown(accept_sock, PR_SHUTDOWN_BOTH);
        if (PR_FAILURE == rv)
            PL_FPrintError(err_out, "PR_Shutdown (server) failed");
    }

    if (-1 != bytes)
    {
        rv = PR_Close(accept_sock);
        if (PR_FAILURE == rv)
            PL_FPrintError(err_out, "PR_Close (server) failed");
    }

    rv = PR_Close(listen_sock);
    if (PR_FAILURE == rv)
        PL_FPrintError(err_out, "PR_Close (server) failed");
}  /* AcceptingThread */
コード例 #4
0
ファイル: priotest.c プロジェクト: biddyweb/switch-oss
int main(int argc, char **argv)
#endif
{
    PLOptStatus os;
    PRIntn duration = DEFAULT_DURATION;
    PRUint32 totalCount, highCount = 0, lowCount = 0;
	PLOptState *opt = PL_CreateOptState(argc, argv, "hdc:");

    debug_out = PR_STDOUT;
    oneSecond = PR_SecondsToInterval(1);

	while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
    {
		if (PL_OPT_BAD == os) continue;
        switch (opt->option)
        {
        case 'd':  /* debug mode */
			debug_mode = PR_TRUE;
            break;
        case 'c':  /* test duration */
			duration = atoi(opt->value);
            break;
        case 'h':  /* help message */
         default:
			Help();
			return 2;
        }
    }
	PL_DestroyOptState(opt);
    PR_STDIO_INIT();

    if (duration == 0) duration = DEFAULT_DURATION;

    RudimentaryTests();

    printf("Priority test: running for %d seconds\n\n", duration);

    (void)PerSecond(PR_IntervalNow());
    totalCount = PerSecond(PR_IntervalNow());

    PR_SetThreadPriority(PR_GetCurrentThread(), PR_PRIORITY_URGENT);

    if (debug_mode)
    {
        PR_fprintf(debug_out,
		    "The high priority thread should get approximately three\n");
        PR_fprintf( debug_out,
		    "times what the low priority thread manages. A maximum of \n");
        PR_fprintf( debug_out, "%d cycles are available.\n\n", totalCount);
    }

    duration = (duration + 4) / 5;
    CreateThreads(&lowCount, &highCount);
    while (duration--)
    {
        PRIntn loop = 5;
        while (loop--) PR_Sleep(oneSecond);
        if (debug_mode)
            PR_fprintf(debug_out, "high : low :: %d : %d\n", highCount, lowCount);
    }


    PR_ProcessExit((failed) ? 1 : 0);

	PR_NOT_REACHED("You can't get here -- but you did!");
	return 1;  /* or here */

}  /* main */
コード例 #5
0
ファイル: stack.c プロジェクト: Akin-Net/mozilla-central
int main(int argc, char **argv)
{
#if !(defined(SYMBIAN) && defined(__WINS__))
    PRInt32 rv, cnt, sum;
	DataRecord	*Item;
	PRStack		*list1, *list2;
	PRStackElem	*node;
	PRStatus rc;

	PRInt32 thread_cnt = DEFAULT_THREAD_CNT;
	PRInt32 data_cnt = DEFAULT_DATA_CNT;
	PRInt32 loops = DEFAULT_LOOP_CNT;
	PRThread **threads;
	stack_data *thread_args;

	PLOptStatus os;
	PLOptState *opt = PL_CreateOptState(argc, argv, "dt:c:l:");

	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;
        case 'c':  /* data count */
            data_cnt = atoi(opt->value);
            break;
        case 'l':  /* loop count */
            loops = atoi(opt->value);
            break;
         default:
            break;
        }
    }
	PL_DestroyOptState(opt);

	PR_SetConcurrency(4);

    output = PR_GetSpecialFD(PR_StandardOutput);
    errhandle = PR_GetSpecialFD(PR_StandardError);
	list1 = PR_CreateStack("Stack_1");
	if (list1 == NULL) {
		PR_fprintf(errhandle, "PR_CreateStack failed - error %d\n",
								PR_GetError());
		return 1;
	}

	list2 = PR_CreateStack("Stack_2");
	if (list2 == NULL) {
		PR_fprintf(errhandle, "PR_CreateStack failed - error %d\n",
								PR_GetError());
		return 1;
	}


	threads = (PRThread**) PR_CALLOC(sizeof(PRThread*) * thread_cnt);
	thread_args = (stack_data *) PR_CALLOC(sizeof(stack_data) * thread_cnt);

	if (_debug_on)
		PR_fprintf(output,"%s: thread_cnt = %d data_cnt = %d\n", argv[0],
							thread_cnt, data_cnt);
	for(cnt = 0; cnt < thread_cnt; cnt++) {
		PRThreadScope scope;

		thread_args[cnt].list1 = list1;
		thread_args[cnt].list2 = list2;
		thread_args[cnt].loops = loops;
		thread_args[cnt].data_cnt = data_cnt;	
		thread_args[cnt].initial_data_value = 1 + cnt * data_cnt;

		if (cnt & 1)
			scope = PR_GLOBAL_THREAD;
		else
			scope = PR_LOCAL_THREAD;


		threads[cnt] = PR_CreateThread(PR_USER_THREAD,
						  stackop, &thread_args[cnt],
						  PR_PRIORITY_NORMAL,
						  scope,
						  PR_JOINABLE_THREAD,
						  0);
		if (threads[cnt] == NULL) {
			PR_fprintf(errhandle, "PR_CreateThread failed - error %d\n",
								PR_GetError());
			PR_ProcessExit(2);
		}
		if (_debug_on)
			PR_fprintf(output,"%s: created thread = 0x%x\n", argv[0],
										threads[cnt]);
	}

	for(cnt = 0; cnt < thread_cnt; cnt++) {
    	rc = PR_JoinThread(threads[cnt]);
		PR_ASSERT(rc == PR_SUCCESS);
	}

	node = PR_StackPop(list1);
	/*
	 * list1 should be empty
	 */
	if (node != NULL) {
		PR_fprintf(errhandle, "Error - Stack 1 not empty\n");
		PR_ASSERT(node == NULL);
		PR_ProcessExit(4);
	}

	cnt = data_cnt * thread_cnt;
	sum = 0;
	while (cnt-- > 0) {
		node = PR_StackPop(list2);
		/*
		 * There should be at least 'cnt' number of records
		 */
		if (node == NULL) {
			PR_fprintf(errhandle, "Error - PR_StackPop returned NULL\n");
			PR_ProcessExit(3);
		}
		Item = RECORD_LINK_PTR(node);
		sum += Item->data;
	}
	node = PR_StackPop(list2);
	/*
	 * there should be exactly 'cnt' number of records
	 */
	if (node != NULL) {
		PR_fprintf(errhandle, "Error - Stack 2 not empty\n");
		PR_ASSERT(node == NULL);
		PR_ProcessExit(4);
	}
	PR_DELETE(threads);
	PR_DELETE(thread_args);

	PR_DestroyStack(list1);
	PR_DestroyStack(list2);

	if (sum == SUM_OF_NUMBERS(data_cnt * thread_cnt)) {
		PR_fprintf(output, "%s successful\n", argv[0]);
		PR_fprintf(output, "\t\tsum = 0x%x, expected = 0x%x\n", sum,
							SUM_OF_NUMBERS(thread_cnt * data_cnt));
		return 0;
	} else {
		PR_fprintf(output, "%s failed: sum = 0x%x, expected = 0x%x\n",
							argv[0], sum,
								SUM_OF_NUMBERS(data_cnt * thread_cnt));
		return 2;
	}
#endif
}
コード例 #6
0
ファイル: stack.c プロジェクト: Akin-Net/mozilla-central
static void stackop(void *thread_arg)
{
    PRInt32 val, cnt, index, loops;
	DataRecord	*Items, *Item;
	PRStack		*list1, *list2;
	PRStackElem	*node;
	stack_data *arg = (stack_data *) thread_arg;

	val = arg->initial_data_value;
	cnt = arg->data_cnt;
	loops = arg->loops;
	list1 = arg->list1;
	list2 = arg->list2;

	/*
	 * allocate memory for the data records
	 */
	Items = (DataRecord *) PR_CALLOC(sizeof(DataRecord) * cnt);
	PR_ASSERT(Items != NULL);
	index = 0;

	if (_debug_on)
		PR_fprintf(output,
		"Thread[0x%x] init_val = %d cnt = %d data1 = 0x%x datan = 0x%x\n",
				PR_GetCurrentThread(), val, cnt, &Items[0], &Items[cnt-1]);


	/*
	 * add the data records to list1
	 */
	while (cnt-- > 0) {
		Items[index].data = val++;
		PR_StackPush(list1, &Items[index].link);
		index++;
	}

	/*
	 * pop data records from list1 and add them back to list1
	 * generates contention for the stack accesses
	 */
	while (loops-- > 0) {
		cnt = arg->data_cnt;
		while (cnt-- > 0) {
			node = PR_StackPop(list1);
			if (node == NULL) {
				PR_fprintf(errhandle, "Error - PR_StackPop returned NULL\n");
				PR_ASSERT(node != NULL);
				PR_ProcessExit(3);
			}
			PR_StackPush(list1, node);
		}
	}
	/*
	 * remove the data records from list1 and add them to list2
	 */
	cnt = arg->data_cnt;
	while (cnt-- > 0) {
		node = PR_StackPop(list1);
		if (node == NULL) {
			PR_fprintf(errhandle, "Error - PR_StackPop returned NULL\n");
			PR_ASSERT(node != NULL);
			PR_ProcessExit(3);
		}
		PR_StackPush(list2, node);
	}
	if (_debug_on)
		PR_fprintf(output,
		"Thread[0x%x] init_val = %d cnt = %d exiting\n",
				PR_GetCurrentThread(), val, cnt);

}