1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: NULL; c-basic-offset: 2 -*- */
2
/* ***** BEGIN LICENSE BLOCK *****
3
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Mozilla Public License Version
6
* 1.1 (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/MPL/
10
* Software distributed under the License is distributed on an "AS IS" basis,
11
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
* for the specific language governing rights and limitations under the
15
* The Original Code is the Netscape Portable Runtime (NSPR).
17
* The Initial Developer of the Original Code is
18
* Netscape Communications Corporation.
19
* Portions created by the Initial Developer are Copyright (C) 1998-2000
20
* the Initial Developer. All Rights Reserved.
23
* George Warner, Apple Computer Inc.
24
* Simon Fraser <sfraser@netscape.com>
26
* Alternatively, the contents of this file may be used under the terms of
27
* either the GNU General Public License Version 2 or later (the "GPL"), or
28
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29
* in which case the provisions of the GPL or the LGPL are applicable instead
30
* of those above. If you wish to allow use of your version of this file only
31
* under the terms of either the GPL or the LGPL, and not to allow others to
32
* use your version of this file under the terms of the MPL, indicate your
33
* decision by deleting the provisions above and replace them with the notice
34
* and other provisions required by the GPL or the LGPL. If you do not delete
35
* the provisions above, a recipient may use your version of this file under
36
* the terms of any one of the MPL, the GPL or the LGPL.
38
* ***** END LICENSE BLOCK ***** */
40
#include "mdcriticalregion.h"
41
#include <MacErrors.h>
44
This code is a replacement for MPEnterCriticalRegion/MPLeaveCriticalRegion,
45
which is broken on Mac OS 10.0.x builds, but fixed in 10.1. This code works
50
typedef struct MDCriticalRegionData_struct {
51
MPTaskID mMPTaskID; /* Who's in the critical region? */
52
UInt32 mDepthCount; /* How deep? */
53
MPSemaphoreID mMPSemaphoreID; /* ready semaphore */
54
} MDCriticalRegionData, *MDCriticalRegionDataPtr;
58
MD_CriticalRegionCreate(MDCriticalRegionID * outCriticalRegionID)
60
MDCriticalRegionDataPtr newCriticalRegionPtr;
61
MPSemaphoreID mpSemaphoreID;
64
if (outCriticalRegionID == NULL)
67
*outCriticalRegionID = NULL;
69
newCriticalRegionPtr = (MDCriticalRegionDataPtr)MPAllocateAligned(sizeof(MDCriticalRegionData),
70
kMPAllocateDefaultAligned, kMPAllocateClearMask);
71
if (newCriticalRegionPtr == NULL)
74
// Note: this semaphore is pre-fired (ready!)
75
err = MPCreateBinarySemaphore(&mpSemaphoreID);
78
newCriticalRegionPtr->mMPTaskID = kInvalidID;
79
newCriticalRegionPtr->mDepthCount = 0;
80
newCriticalRegionPtr->mMPSemaphoreID = mpSemaphoreID;
82
*outCriticalRegionID = (MDCriticalRegionID)newCriticalRegionPtr;
86
MPFree((LogicalAddress)newCriticalRegionPtr);
93
MD_CriticalRegionDelete(MDCriticalRegionID inCriticalRegionID)
95
MDCriticalRegionDataPtr criticalRegion = (MDCriticalRegionDataPtr)inCriticalRegionID;
98
if (criticalRegion == NULL)
101
if ((criticalRegion->mMPTaskID != kInvalidID) && (criticalRegion->mDepthCount > 0))
102
return kMPInsufficientResourcesErr;
104
if (criticalRegion->mMPSemaphoreID != kInvalidID)
105
err = MPDeleteSemaphore(criticalRegion->mMPSemaphoreID);
106
if (noErr != err) return err;
108
criticalRegion->mMPSemaphoreID = kInvalidID;
109
MPFree((LogicalAddress) criticalRegion);
115
MD_CriticalRegionEnter(MDCriticalRegionID inCriticalRegionID, Duration inTimeout)
117
MDCriticalRegionDataPtr criticalRegion = (MDCriticalRegionDataPtr)inCriticalRegionID;
118
MPTaskID currentTaskID = MPCurrentTaskID();
119
OSStatus err = noErr;
121
if (criticalRegion == NULL)
124
// if I'm inside the critical region...
125
if (currentTaskID == criticalRegion->mMPTaskID)
128
criticalRegion->mDepthCount++;
133
// wait for the ready semaphore
134
err = MPWaitOnSemaphore(criticalRegion->mMPSemaphoreID, inTimeout);
135
// we didn't get it. return the error
136
if (noErr != err) return err;
139
criticalRegion->mMPTaskID = currentTaskID;
140
criticalRegion->mDepthCount = 1;
146
MD_CriticalRegionExit(MDCriticalRegionID inCriticalRegionID)
148
MDCriticalRegionDataPtr criticalRegion = (MDCriticalRegionDataPtr)inCriticalRegionID;
149
MPTaskID currentTaskID = MPCurrentTaskID();
150
OSStatus err = noErr;
152
// if we don't own the critical region...
153
if (currentTaskID != criticalRegion->mMPTaskID)
154
return kMPInsufficientResourcesErr;
156
// if we aren't at a depth...
157
if (criticalRegion->mDepthCount == 0)
158
return kMPInsufficientResourcesErr;
161
criticalRegion->mDepthCount--;
163
// if we just bottomed out...
164
if (criticalRegion->mDepthCount == 0)
166
// release ownership of the structure
167
criticalRegion->mMPTaskID = kInvalidID;
168
// and signal the ready semaphore
169
err = MPSignalSemaphore(criticalRegion->mMPSemaphoreID);