Annotation of sbbs/src/xpdev/semwrap.c, revision 1.1

1.1     ! root        1: /* semwrap.c */
        !             2: 
        !             3: /* Semaphore-related cross-platform development wrappers */
        !             4: 
        !             5: /* $Id: semwrap.c,v 1.14 2005/04/29 22:41:55 deuce Exp $ */
        !             6: 
        !             7: /****************************************************************************
        !             8:  * @format.tab-size 4          (Plain Text/Source Code File Header)                    *
        !             9:  * @format.use-tabs true       (see http://www.synchro.net/ptsc_hdr.html)              *
        !            10:  *                                                                                                                                                     *
        !            11:  * Copyright 2003 Rob Swindell - http://www.synchro.net/copyright.html         *
        !            12:  *                                                                                                                                                     *
        !            13:  * This library is free software; you can redistribute it and/or                       *
        !            14:  * modify it under the terms of the GNU Lesser General Public License          *
        !            15:  * as published by the Free Software Foundation; either version 2                      *
        !            16:  * of the License, or (at your option) any later version.                                      *
        !            17:  * See the GNU Lesser General Public License for more details: lgpl.txt or     *
        !            18:  * http://www.fsf.org/copyleft/lesser.html                                                                     *
        !            19:  *                                                                                                                                                     *
        !            20:  * Anonymous FTP access to the most recent released source is available at     *
        !            21:  * ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net     *
        !            22:  *                                                                                                                                                     *
        !            23:  * Anonymous CVS access to the development source and modification history     *
        !            24:  * is available at cvs.synchro.net:/cvsroot/sbbs, example:                                     *
        !            25:  * cvs -d :pserver:[email protected]:/cvsroot/sbbs login                       *
        !            26:  *     (just hit return, no password is necessary)                                                     *
        !            27:  * cvs -d :pserver:[email protected]:/cvsroot/sbbs checkout src                *
        !            28:  *                                                                                                                                                     *
        !            29:  * For Synchronet coding style and modification guidelines, see                                *
        !            30:  * http://www.synchro.net/source.html                                                                          *
        !            31:  *                                                                                                                                                     *
        !            32:  * You are encouraged to submit any modifications (preferably in Unix diff     *
        !            33:  * format) via e-mail to [email protected]                                                                      *
        !            34:  *                                                                                                                                                     *
        !            35:  * Note: If this box doesn't appear square, then you need to fix your tabs.    *
        !            36:  ****************************************************************************/
        !            37: 
        !            38: #include <errno.h>
        !            39: #include "semwrap.h"
        !            40: 
        !            41: #if defined(__unix__)
        !            42: 
        !            43: #include <sys/time.h>  /* timespec */
        !            44: #include <stdlib.h>    /* NULL */
        !            45: 
        !            46: int DLLCALL
        !            47: sem_trywait_block(sem_t *sem, unsigned long timeout)
        !            48: {
        !            49:        int     retval;
        !            50:        struct timespec abstime;
        !            51:        struct timeval currtime;
        !            52:        
        !            53:        gettimeofday(&currtime,NULL);
        !            54:        abstime.tv_sec=currtime.tv_sec + (currtime.tv_usec/1000 + timeout)/1000;
        !            55:        abstime.tv_nsec=(currtime.tv_usec*1000 + timeout*1000000)%1000000000;
        !            56: 
        !            57:        retval=sem_timedwait(sem, &abstime);
        !            58:        if(retval && errno==ETIMEDOUT)
        !            59:                errno=EAGAIN;
        !            60:        return retval;
        !            61: }
        !            62: 
        !            63: #elif defined(_WIN32)
        !            64: 
        !            65: #include <limits.h>            /* INT_MAX */
        !            66: 
        !            67: #if defined(__BORLANDC__)
        !            68:        #pragma argsused
        !            69: #endif
        !            70: int DLLCALL sem_init(sem_t* psem, int pshared, unsigned int value)
        !            71: {
        !            72: 
        !            73:        if((*(psem)=CreateSemaphore(NULL,value,INT_MAX,NULL))==NULL)
        !            74:                return -1;
        !            75:                
        !            76:        return 0;
        !            77: }
        !            78: 
        !            79: int DLLCALL sem_trywait_block(sem_t* psem, unsigned long timeout)
        !            80: {
        !            81:        if(WaitForSingleObject(*(psem),timeout)!=WAIT_OBJECT_0) {
        !            82:                errno=EAGAIN;
        !            83:                return -1;
        !            84:        }
        !            85: 
        !            86:        return 0;
        !            87: }
        !            88: 
        !            89: int DLLCALL sem_post(sem_t* psem)
        !            90: {
        !            91:        if(ReleaseSemaphore(*(psem),1,NULL)==TRUE)
        !            92:                return 0;
        !            93: 
        !            94:        return -1;
        !            95: }
        !            96: 
        !            97: int DLLCALL sem_getvalue(sem_t* psem, int* vp)
        !            98: {
        !            99: #if 0          /* This only works on 9x *sniff* */
        !           100:        ReleaseSemaphore(*(psem),0,(LPLONG)vp);
        !           101:        return 0;
        !           102: #else
        !           103:        /* Note, this should REALLY be in a critical section... */
        !           104:        int     retval=0;
        !           105: 
        !           106:        if(WaitForSingleObject(*(psem),0)!=WAIT_OBJECT_0)
        !           107:                *vp=0;
        !           108:        else {
        !           109:                if(ReleaseSemaphore(*(psem),1,(LPLONG)vp))
        !           110:                        (*vp)++;
        !           111:                else
        !           112:                        retval=-1;
        !           113:        }
        !           114:        return(retval);
        !           115: #endif
        !           116: }
        !           117: 
        !           118: int DLLCALL sem_destroy(sem_t* psem)
        !           119: {
        !           120:        if(CloseHandle(*(psem))==TRUE)
        !           121:                return 0;
        !           122:        return -1;
        !           123: }
        !           124: 
        !           125: #endif /* _WIN32 */

unix.superglobalmegacorp.com

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