Annotation of sbbs/src/sbbs3/js_rtpool.c, revision 1.1

1.1     ! root        1: /* $Id: js_rtpool.c,v 1.18 2010/04/03 00:16:53 deuce Exp $ */
        !             2: 
        !             3: #include "js_rtpool.h"
        !             4: #include <threadwrap.h>                /* Must be included after jsapi.h */
        !             5: 
        !             6: #ifdef DLLCALL
        !             7: #undef DLLCALL
        !             8: #endif
        !             9: #ifdef _WIN32
        !            10:        #ifdef __BORLANDC__
        !            11:                #define DLLCALL __stdcall
        !            12:        #else
        !            13:                #define DLLCALL
        !            14:        #endif
        !            15: #else  /* !_WIN32 */
        !            16:        #define DLLCALL
        !            17: #endif
        !            18: 
        !            19: #define SHARED_RUNTIMES
        !            20: 
        !            21: struct jsrt_queue {
        !            22:        JSRuntime       *rt;
        !            23:        int                     created;
        !            24: #ifdef SHARED_RUNTIMES
        !            25:        const char*     file;
        !            26:        long            line;
        !            27: #else
        !            28:        int                     maxbytes;
        !            29:        int                     used;
        !            30: #endif
        !            31: };
        !            32: 
        !            33: #define JSRT_QUEUE_SIZE                128
        !            34: struct jsrt_queue jsrt_queue[JSRT_QUEUE_SIZE];
        !            35: static pthread_mutex_t         jsrt_mutex;
        !            36: static int                     initialized=0;
        !            37: #ifndef SHARED_RUNTIMES
        !            38: static sem_t                   jsrt_sem;
        !            39: #endif
        !            40: 
        !            41: JSRuntime * DLLCALL jsrt_GetNew(int maxbytes, unsigned long timeout, const char *filename, long line)
        !            42: {
        !            43: #ifdef SHARED_RUNTIMES
        !            44:        int     i;
        !            45: 
        !            46:        if(!initialized) {
        !            47:                pthread_mutex_init(&jsrt_mutex, NULL);
        !            48:                initialized=TRUE;
        !            49:        }
        !            50: 
        !            51:        pthread_mutex_lock(&jsrt_mutex);
        !            52:        for(i=0; i<JSRT_QUEUE_SIZE; i++) {
        !            53:                if(!jsrt_queue[i].created) {
        !            54:                        jsrt_queue[i].rt=JS_NewRuntime(maxbytes);
        !            55:                        if(jsrt_queue[i].rt != NULL) {
        !            56:                                jsrt_queue[i].file=filename;
        !            57:                                jsrt_queue[i].line=line;
        !            58:                                jsrt_queue[i].created=1;
        !            59:                        }
        !            60:                }
        !            61:                if(jsrt_queue[i].created && jsrt_queue[i].file == filename && jsrt_queue[i].line == line) {
        !            62:                        pthread_mutex_unlock(&jsrt_mutex);
        !            63:                        return(jsrt_queue[i].rt);
        !            64:                }
        !            65:        }
        !            66:        pthread_mutex_unlock(&jsrt_mutex);
        !            67: 
        !            68:        return(NULL);
        !            69: #else
        !            70:        int     i;
        !            71:        int     last_unused=-1;
        !            72: 
        !            73:        if(!initialized) {
        !            74:                pthread_mutex_init(&jsrt_mutex, NULL);
        !            75:                sem_init(&jsrt_sem, 0, JSRT_QUEUE_SIZE);
        !            76:                initialized=TRUE;
        !            77:        }
        !            78: 
        !            79:        if(sem_trywait_block(&jsrt_sem, timeout)==0) {
        !            80:                pthread_mutex_lock(&jsrt_mutex);
        !            81:                for(i=0; i<JSRT_QUEUE_SIZE; i++) {
        !            82:                        if(!jsrt_queue[i].created) {
        !            83:                                jsrt_queue[i].rt=JS_NewRuntime(maxbytes);
        !            84:                                if(jsrt_queue[i].rt != NULL) {
        !            85:                                        jsrt_queue[i].maxbytes=maxbytes;
        !            86:                                        jsrt_queue[i].created=1;
        !            87:                                        jsrt_queue[i].used=0;
        !            88:                                }
        !            89:                        }
        !            90:                        if(!jsrt_queue[i].used) {
        !            91:                                last_unused=i;
        !            92:                                if(jsrt_queue[i].created && jsrt_queue[i].maxbytes == maxbytes) {
        !            93:                                        jsrt_queue[i].used=1;
        !            94:                                        pthread_mutex_unlock(&jsrt_mutex);
        !            95:                                        return(jsrt_queue[i].rt);
        !            96:                                }
        !            97:                        }
        !            98:                }
        !            99: 
        !           100:                if(last_unused != -1) {
        !           101:                        jsrt_queue[last_unused].used=1;
        !           102:                        pthread_mutex_unlock(&jsrt_mutex);
        !           103:                        return(jsrt_queue[last_unused].rt);
        !           104:                }
        !           105:                pthread_mutex_unlock(&jsrt_mutex);
        !           106:        }
        !           107: 
        !           108:        return(NULL);
        !           109: #endif
        !           110: }
        !           111: 
        !           112: void DLLCALL jsrt_Release(JSRuntime *rt)
        !           113: {
        !           114: #ifdef SHARED_RUNTIMES
        !           115: #else
        !           116:        int     i;
        !           117: 
        !           118:        for(i=0; i<JSRT_QUEUE_SIZE; i++) {
        !           119:                if(rt==jsrt_queue[i].rt) {
        !           120:                        pthread_mutex_lock(&jsrt_mutex);
        !           121:                        jsrt_queue[i].used=0;
        !           122:                        /* TODO: Clear "stuff"? */
        !           123:                        pthread_mutex_unlock(&jsrt_mutex);
        !           124:                        sem_post(&jsrt_sem);
        !           125:                }
        !           126:        }
        !           127: #endif
        !           128: }
        !           129: 
        !           130: void DLLCALL jsrt_TriggerAll(void)
        !           131: {
        !           132: #ifdef USE_JS_OPERATION_CALLBACK
        !           133:        int     i;
        !           134:        int j;
        !           135:        JSContext       *iterp,*cx;
        !           136: 
        !           137:        if(!initialized)
        !           138:                return;
        !           139:        for(i=0; i<JSRT_QUEUE_SIZE; i++) {
        !           140:                pthread_mutex_lock(&jsrt_mutex);
        !           141: #ifdef SHARED_RUNTIMES
        !           142:                if(jsrt_queue[i].created) {
        !           143: #else
        !           144:                if(jsrt_queue[i].used) {
        !           145: #endif
        !           146:                        iterp=NULL;
        !           147:                        while((cx = JS_ContextIterator(jsrt_queue[i].rt, &iterp)) != NULL)
        !           148:                                JS_TriggerOperationCallback(cx);
        !           149:                }
        !           150:                pthread_mutex_unlock(&jsrt_mutex);
        !           151:        }
        !           152: #endif
        !           153: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.