src/server/util/systhr.c

Tue, 12 Sep 2023 18:08:11 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 12 Sep 2023 18:08:11 +0200
changeset 511
a5a142fea2ae
parent 162
b169992137a8
permissions
-rw-r--r--

update uwproj

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
 *
 * THE BSD LICENSE
 *
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer. 
 * Redistributions in binary form must reproduce the above copyright notice, 
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution. 
 *
 * Neither the name of the  nor the names of its contributors may be
 * used to endorse or promote products derived from this software without 
 * specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * systhr.c: Abstracted threading mechanisms
 * 
 * Rob McCool
 */


#include "systhr.h"
//include "ereport.h"

//include "prinit.h"
//include "prthread.h"
//include "private/pprthred.h"

#include "systems.h"

#ifdef XP_UNIX
#include <poll.h>
#endif

#define DEFAULT_STACKSIZE (64*1024)

static unsigned long _systhr_stacksize = DEFAULT_STACKSIZE;

NSAPI_PUBLIC 
void systhread_set_default_stacksize(unsigned long size)
{
	_systhr_stacksize = size;
}



SYS_THREAD systhread_start(int prio, int stksz, thrstartfunc fn, void *arg) {
    pthread_t      thr = 0;
    pthread_attr_t attr;
    
    pthread_attr_init(&attr);
    if(stksz) {
        pthread_attr_setstacksize(&attr, stksz);
    }
    
    if(pthread_create(&thr, &attr, (posix_thrstartfunc)fn, arg) != 0) {
        /* error */
    }
    
    return thr;
}

SYS_THREAD systhread_current(void) {
    return pthread_self();
}

void systhread_yield(void) {
    sched_yield();
}


void systhread_timerset(int usec) {
   
}

SYS_THREAD systhread_attach(void) {
    /* TODO: what to do? */
    return 0;
}

void systhread_detach(SYS_THREAD thr) {
    pthread_detach(thr);
}

void systhread_terminate(SYS_THREAD thr) {
    //PR_Interrupt((PRThread *)thr);
}

void systhread_sleep(int msec) {
    if(msec > 0) {
        poll(NULL, 0, msec);
    } else {
        sched_yield();
    }
}

NSAPI_PUBLIC int systhread_newkey() {
    pthread_key_t key;
    pthread_key_create(&key, NULL);
    
    return (int)key; // TODO: don't use int
}

NSAPI_PUBLIC void* systhread_getdata(int key) {
    return pthread_getspecific((pthread_key_t)key);
}

NSAPI_PUBLIC void systhread_setdata(int key, void *data) {
    pthread_setspecific((pthread_key_t)key, data);
}

NSAPI_PUBLIC void systhread_init(char *name)
{
    //if (!PR_Initialized()) {
    //    PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 256);
    //}
    // XXX: ruslan - this bug can potentially exist on all plafroms due to
    //	possible priority inversion on NSPR spin locks. This code will
    //	need to be remove as we get new NSPR drop
    // <WORKAROUND>
    /* VB: This is to fix bug# 364813 coupled with NSPR not wanting to patch
           their problems. The fix is to prevent NSPR itself from
           using atomic stacks.
    */
    // ruslan: this problem exists on DEC also. We will roll it back when
    //	we have the right fix from NSPR group. It has smth. to do with
    //	atomic operations on DEC, it's an assembly code which is different
    //	for every platform. NSPR_FD_CACHE_SIZE_HIGH env var will cause the
    //	same effect as this fix. Debug version of NSPR always works as it doesn't
    //  have FD stack.

    //int maxPRFdCache = 8192;
    //PR_SetFDCacheSize(0, maxPRFdCache);
    // </WORKAROUND>
}

NSAPI_PUBLIC void systhread_dummy(void) {
}

mercurial