Changeset 8902 for trunk/src


Ignore:
Timestamp:
Jul 22, 2002, 7:58:02 AM (23 years ago)
Author:
achimha
Message:

documentation

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/odincrt/critsect.cpp

    r8895 r8902  
    1 /* $Id: critsect.cpp,v 1.4 2002-07-19 11:06:45 sandervl Exp $ */
     1/* $Id: critsect.cpp,v 1.5 2002-07-22 05:58:02 achimha Exp $ */
    22/*
    3  * Critical sections
     3 * Critical sections in the Win32 sense
    44 *
    5  * Copyright 2002 Sander van Leeuwen (sandervl@xs4all.nl)
    6  * OS/2 port
    7  *
    8  * Based on Wine code
    9  *
    10  * Copyright 1998 Alexandre Julliard (991031 Port)
    11  *
     5 * Copyright 2002 Sander van Leeuwen <sandervl@innotek.de>
    126 *
    137 */
     
    3024#endif
    3125
     26//******************************************************************************
     27// This is an OS/2 implementation of what Win32 treats as "critical sections"
     28// It is an implementation that is highly optimized for the case where there is
     29// only one thread trying to access the critical section, i.e. it is available
     30// most of the time. Therefore we can use these critical sections for all our
     31// serialization and not lose any performance when concurrent access is unlikely.
     32//
     33// In case there is multiple access, we use the OS/2 kernel even semaphores.
     34//******************************************************************************
     35
     36
     37// encode PID and TID into one 32bit value
    3238#define MAKE_THREADID(processid, threadid)      ((processid << 16) | threadid)
    3339
     
    7076    APIRET rc;
    7177
     78    // initialize lock count with special value -1, meaning noone posesses it
    7279    crit->LockCount      = -1;
    7380    crit->RecursionCount = 0;
     
    130137    DWORD threadid = GetCurrentThreadId();
    131138
     139    // create crit sect just in time...
    132140    if (!crit->hmtxLock)
    133141    {
    134142        DosInitializeCriticalSection(crit, NULL);
    135143    }
    136     if (DosInterlockedIncrement( &crit->LockCount ))
     144    // do an atomic increase of the lockcounter and see if it is > 0
     145    // (i.e. it is already posessed)
     146    if (DosInterlockedIncrement(&crit->LockCount))
    137147    {
    138148testenter:
     149        // if the same thread is requesting it again, memorize it
    139150        if (crit->OwningThread == threadid)
    140151        {
     
    142153            return;
    143154        }
    144 
    145         if(DosInterlockedCompareExchange((PLONG)&crit->OwningThread, threadid, 0))
    146         {
     155        // do an atomic operation where we compare the owning thread id with 0
     156        // and if this is true, exchange it with the id of the current thread.
     157        if(DosInterlockedCompareExchange((PLONG)&crit->OwningThread, threadid, 0))
     158        {
     159            // the compare did not return equal, i.e. the crit sect is in use
     160
    147161            ULONG ulnrposts;
    148162
     
    154168            }
    155169            DosResetEventSem(crit->hmtxLock, &ulnrposts);
     170            // multiple waiters could be running now. Repeat the logic so that
     171            // only one actually can get the critical section
    156172            goto testenter;
    157173        }
     
    202218    }
    203219}
    204 
Note: See TracChangeset for help on using the changeset viewer.