dotgnu-pnet-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Dotgnu-pnet-commits] CVS: pnet/support wait_event.c,NONE,1.1 wait.c,1.


From: Gopal.V <address@hidden>
Subject: [Dotgnu-pnet-commits] CVS: pnet/support wait_event.c,NONE,1.1 wait.c,1.2,1.3 Makefile.am,1.39,1.40 thr_defs.h,1.5,1.6
Date: Sat, 07 Dec 2002 05:46:57 -0500

Update of /cvsroot/dotgnu-pnet/pnet/support
In directory subversions:/tmp/cvs-serv7459/support

Modified Files:
        wait.c Makefile.am thr_defs.h 
Added Files:
        wait_event.c 
Log Message:
Patch ID: 781 from Tum committed by t3rmin4tor



--- NEW FILE ---
/*
 * wait_event.c - Wait event objects for the threading sub-system.
 *
 * Copyright (C) 2002 Free Software Foundation
 *
 * Authors: Thong Nguyen
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "thr_defs.h"

#ifdef  __cplusplus
extern  "C" {
#endif

/*
 * Handler for cleaning up the an event.with.
 */
static int EventClose(ILWaitEvent *event)
{
        /* Clean up the event */        
        _ILWakeupQueueDestroy(&(event->queue));
        _ILMutexDestroy(&(event->parent.lock));

        return IL_WAITCLOSE_FREE;
}

/*
 * Handler for registering a wakeup listener an with an event.
 */
static int EventRegister(ILWaitEvent *event, _ILWakeup *wakeup)
{
        int result;

        /* Lock down the event */
        _ILMutexLock(&(event->parent.lock));

        /* Determine what to do based on the event's current state */
        if((event->data & EVENT_SET_MASK))
        {
                /* Event is set, aquire is successful */

                /* check if we should auto reset the event */
                if (!(event->data & EVENT_MANUALRESET_MASK))
                {
                        /* event is an auto-reset event so reset it */
                        event->data &= ~(EVENT_SET_MASK);
                }

                result = IL_WAITREG_ACQUIRED;
        }
        else
        {
                /* Event isn't set, so add the wakup to the queue */

                if(_ILWakeupQueueAdd(&(event->queue), wakeup, event))
                {
                        result = IL_WAITREG_OK;
                }
                else
                {
                        result = IL_WAITREG_FAILED;
                }
        }

        /* Unlock the event and return */
        _ILMutexUnlock(&(event->parent.lock));

        return result;
}

/*
 * Handler for unregistering a wakeup listener an with an event.
 */
static void EventUnregister(ILWaitEvent *event, _ILWakeup *wakeup, int release)
{
        /* Lock down the event */
        _ILMutexLock(&(event->parent.lock));

        /* Remove ourselves from the wait queue if we are currently on it */
        _ILWakeupQueueRemove(&(event->queue), wakeup);
        
        /* Unlock the event and return */
        _ILMutexUnlock(&(event->parent.lock));
}

/*
 * Creates and returns a new wait event.
 *
 * @param manualReset  If false (0), the event automatically resets itself
 *                     after a single thread has been released.
 *
 * @param initialState The initial state of the event.  If true (1) The initial
 *                     state of the event is signalled.
 */
ILWaitHandle *ILWaitEventCreate(int manualReset, int initialState)
{
        ILWaitEvent *event;

        /* Allocate memory for the event */
        if((event = (ILWaitEvent *)ILMalloc(sizeof(ILWaitEvent))) == 0)
        {
                return 0;
        }

        _ILMutexCreate(&(event->parent.lock));

        /* setup event callbacks */
        event->parent.kind = IL_WAIT_EVENT;
        event->parent.closeFunc = (ILWaitCloseFunc)EventClose;  
        event->parent.registerFunc = (ILWaitRegisterFunc)EventRegister;
        event->parent.unregisterFunc = (ILWaitUnregisterFunc)EventUnregister;

        _ILWakeupQueueCreate(&(event->queue));

        /* Set the event initialstate/manualreset flags */
        event->data = (initialState) ? 1 : 0;
        event->data |= (manualReset) ? 2 : 0;

        return &(event->parent);
}

/*
 * Sets the state of an event to signalled.
 *
 * @param handle  The pointer to the wait event.
 * @returns 1 if successful.
 */
int ILWaitEventSet(ILWaitHandle *handle)
{
        ILWaitEvent *event = (ILWaitEvent *)handle;

        /* Lock down the event */
        _ILMutexLock(&(event->parent.lock));

        if (!(event->data & (EVENT_SET_MASK)))
        {               
                if (event->queue.first == 0)
                {
                        /* No threads are waiting so just set the event */

                        event->data |= (EVENT_SET_MASK);
                }
                else
                {
                        /* One or more threads must be waiting */
                        
                        /* Check if this event is manually reset */
                        if (event->data & (EVENT_MANUALRESET_MASK))
                        {
                                /* Manual reset events should be set to 
signalled here
                                 * Auto reset events shouldn't be set since a 
thread is
                                 * about to be released. */

                                event->data |= (EVENT_SET_MASK);
                        }

                        /* Wakeup the next thread */
                        _ILWakeupQueueWake(&event->queue);
                }
        }
        
        /* Unlock the event and return */
        _ILMutexUnlock(&(event->parent.lock));

        return 1;
}

/*
 * Sets the state of the mutex to unsignalled.
 *
 * @param handle  The pointer to the wait event.
 * @returns 1 if successful.
 */
int ILWaitEventReset(ILWaitHandle *handle)
{
        ILWaitEvent *event = (ILWaitEvent *)handle;

        /* Lock down the event */
        _ILMutexLock(&(event->parent.lock));

        /* Reset the event */
        event->data &= ~(EVENT_SET_MASK);
        
        /* Unlock the event and return */
        _ILMutexUnlock(&(event->parent.lock));

        return 1;
}

#ifdef  __cplusplus
};
#endif

Index: wait.c
===================================================================
RCS file: /cvsroot/dotgnu-pnet/pnet/support/wait.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -r1.2 -r1.3
*** wait.c      7 Jun 2002 03:48:48 -0000       1.2
--- wait.c      7 Dec 2002 10:46:55 -0000       1.3
***************
*** 193,197 ****
                                (*(handle->unregisterFunc))(handle, wakeup, 1);
                        }
!                       return LeaveWaitHandle(thread, handles[index], 0);
                }
                else if(result == IL_WAITREG_FAILED)
--- 193,197 ----
                                (*(handle->unregisterFunc))(handle, wakeup, 1);
                        }
!                       return LeaveWaitHandle(thread, handles[index], index);
                }
                else if(result == IL_WAITREG_FAILED)
***************
*** 299,305 ****
        /* Adjust the wait limit to reflect handles we already acquired */
        _ILWakeupAdjustLimit(wakeup, limit);
! 
!       /* Wait until we are signalled, timed out, or interrupted */
!       result = _ILWakeupWait(wakeup, timeout, 0);
  
        /* Unregister the thread from the wait handles */
--- 299,314 ----
        /* Adjust the wait limit to reflect handles we already acquired */
        _ILWakeupAdjustLimit(wakeup, limit);
!       
!       if (limit == 0)
!       {
!               /* No need to wait since we managed to aquire every handle 
immediately. */
!               
!               result = 1;
!       }
!       else
!       {
!               /* Wait until we are signalled, timed out, or interrupted */
!               result = _ILWakeupWait(wakeup, timeout, 0);
!       }
  
        /* Unregister the thread from the wait handles */

Index: Makefile.am
===================================================================
RCS file: /cvsroot/dotgnu-pnet/pnet/support/Makefile.am,v
retrieving revision 1.39
retrieving revision 1.40
diff -C2 -r1.39 -r1.40
*** Makefile.am 21 Nov 2002 21:24:56 -0000      1.39
--- Makefile.am 7 Dec 2002 10:46:55 -0000       1.40
***************
*** 56,59 ****
--- 56,60 ----
                                                 wait.c \
                                                 wait_mutex.c \
+                                                wait_event.c \
                                                 wakeup.c \
                                                 write_float.c \

Index: thr_defs.h
===================================================================
RCS file: /cvsroot/dotgnu-pnet/pnet/support/thr_defs.h,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -r1.5 -r1.6
*** thr_defs.h  6 Jun 2002 23:54:39 -0000       1.5
--- thr_defs.h  7 Dec 2002 10:46:55 -0000       1.6
***************
*** 118,121 ****
--- 118,122 ----
  #define       IL_WAIT_NAMED_MUTEX             0x1001
  #define       IL_WAIT_MONITOR                 0x1002
+ #define       IL_WAIT_EVENT                   0x1003
  
  /*
***************
*** 165,168 ****
--- 166,185 ----
  
  };
+ 
+ /*
+  * Internal structure of a wait event handle.
+  */
+ typedef struct
+ {
+       ILWaitHandle                    parent; 
+       /* Bit 0 = Set Flag */
+       /* Bit 1 = ManualReset Flag */
+       int volatile                    data;
+       _ILWakeupQueue                  queue;
+ 
+ } ILWaitEvent;
+ 
+ #define EVENT_SET_MASK (1)
+ #define EVENT_MANUALRESET_MASK (2)
  
  /*




reply via email to

[Prev in Thread] Current Thread [Next in Thread]