os/kernelhwsrv/kernel/eka/euser/us_rwlock.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// e32\euser\us_rwlock.cpp
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
sl@0
    19
#include "us_std.h"
sl@0
    20
#include <e32atomics.h>
sl@0
    21
sl@0
    22
const TInt KReadersIndex				= 0;
sl@0
    23
const TInt KWriterIndex					= 1;
sl@0
    24
const TInt KReadersPendingIndex			= 2;
sl@0
    25
const TInt KWritersPendingIndex			= 3;
sl@0
    26
const TUint64 KReaderValue				= UI64LIT(0x0000000000000001);
sl@0
    27
const TUint64 KWriterValue				= UI64LIT(0x0000000000010000);
sl@0
    28
const TUint64 KReaderPendingValue		= UI64LIT(0x0000000100000000);
sl@0
    29
const TUint64 KWriterPendingValue		= UI64LIT(0x0001000000000000);
sl@0
    30
const TUint64 KReadersMask				= UI64LIT(0x000000000000ffff);
sl@0
    31
const TUint64 KWriterMask				= KWriterValue;
sl@0
    32
const TUint64 KReadersOrWritersMask		= KReadersMask | KWriterMask;
sl@0
    33
const TUint64 KReadersPendingClearMask	= UI64LIT(0xffff0000ffffffff);
sl@0
    34
sl@0
    35
/**
sl@0
    36
Initialise a read-write lock object.
sl@0
    37
@param		aPriority		Type of priority to use - see RReadWriteLockPriority::TReadWriteLockPriority
sl@0
    38
@return		KErrNone		Instance successfully created
sl@0
    39
			Otherwise an error returned by RSemaphore::CreateLocal
sl@0
    40
@panic		EReadWriteLockInvalidPriority if aPriority is not valid.
sl@0
    41
*/
sl@0
    42
EXPORT_C TInt RReadWriteLock::CreateLocal(TReadWriteLockPriority aPriority)
sl@0
    43
	{
sl@0
    44
	__ASSERT_ALWAYS(aPriority >= EWriterPriority && aPriority <= EReaderPriority, Panic(EReadWriteLockInvalidPriority));
sl@0
    45
sl@0
    46
	iPriority = aPriority;
sl@0
    47
	iValues = 0;
sl@0
    48
#ifdef _DEBUG
sl@0
    49
		iSpare[0] = 0; // Keep a rough track of writer starvation
sl@0
    50
#endif
sl@0
    51
sl@0
    52
	TInt ret = iReaderSem.CreateLocal(0, EOwnerProcess);
sl@0
    53
	if (ret == KErrNone)
sl@0
    54
		ret = iWriterSem.CreateLocal(0, EOwnerProcess);
sl@0
    55
	if (ret != KErrNone)
sl@0
    56
		iReaderSem.Close();
sl@0
    57
sl@0
    58
	return ret;
sl@0
    59
	}
sl@0
    60
sl@0
    61
/**
sl@0
    62
Close a read-write lock object, releasing the associated semaphores.
sl@0
    63
@panic		EReadWriteLockStillPending if there are any outstanding clients or pending clients
sl@0
    64
*/
sl@0
    65
EXPORT_C void RReadWriteLock::Close()
sl@0
    66
	{
sl@0
    67
	__ASSERT_ALWAYS(iValues == 0, Panic(EReadWriteLockStillPending));
sl@0
    68
sl@0
    69
	iReaderSem.Close();
sl@0
    70
	iWriterSem.Close();
sl@0
    71
	}
sl@0
    72
sl@0
    73
/**
sl@0
    74
Ask for a read lock. Will be granted if:
sl@0
    75
	1) No-one else currently holds the lock or
sl@0
    76
	2) Only readers hold the lock and:
sl@0
    77
		a) There are no pending writers or
sl@0
    78
		b) The priority is for readers.
sl@0
    79
Otherwise this function blocks until the lock becomes available to it.
sl@0
    80
Please note that ReadLock() is not re-entrant - calling it a second time without releasing the first lock
sl@0
    81
runs the risk of being blocked and risking a deadlock situation.
sl@0
    82
@panic		EReadWriteLockTooManyClients if the resulting number of readers or pending readers exceeds EReadWriteLockClientCategoryLimit
sl@0
    83
*/
sl@0
    84
EXPORT_C void RReadWriteLock::ReadLock()
sl@0
    85
	{
sl@0
    86
	TBool blocked;
sl@0
    87
	TUint64 initialValues;
sl@0
    88
	TUint16* indexedValues = (TUint16*)&initialValues;
sl@0
    89
sl@0
    90
	do	{
sl@0
    91
		initialValues = iValues;
sl@0
    92
sl@0
    93
		if (indexedValues[KWriterIndex] > 0 ||
sl@0
    94
			(iPriority != EReaderPriority && indexedValues[KWritersPendingIndex] > 0))
sl@0
    95
			{
sl@0
    96
			__ASSERT_ALWAYS(indexedValues[KReadersPendingIndex] < KMaxTUint16, Panic(EReadWriteLockTooManyClients));
sl@0
    97
			blocked = ETrue;
sl@0
    98
			}
sl@0
    99
		else
sl@0
   100
			{
sl@0
   101
			__ASSERT_ALWAYS(indexedValues[KReadersIndex] < KMaxTUint16, Panic(EReadWriteLockTooManyClients));
sl@0
   102
			blocked = EFalse;
sl@0
   103
			}
sl@0
   104
		}
sl@0
   105
	while (!__e32_atomic_cas_rel64(&iValues, &initialValues, initialValues + (blocked ? KReaderPendingValue : KReaderValue)));
sl@0
   106
sl@0
   107
	if (blocked)
sl@0
   108
		iReaderSem.Wait();
sl@0
   109
	}
sl@0
   110
sl@0
   111
/**
sl@0
   112
Ask for a write lock. Will be granted if no-one else currently holds the lock.
sl@0
   113
Otherwise this function blocks until the lock becomes available to it.
sl@0
   114
Only one writer can hold the lock at one time. No readers can hold the lock while a writer has it.
sl@0
   115
Please note that WriteLock() is not re-entrant - calling it a second time without releasing the first lock
sl@0
   116
will block and cause a deadlock situation.
sl@0
   117
@panic		EReadWriteLockTooManyClients if the resulting number of pending writers exceeds EReadWriteLockClientCategoryLimit
sl@0
   118
*/
sl@0
   119
EXPORT_C void RReadWriteLock::WriteLock()
sl@0
   120
	{
sl@0
   121
	TBool blocked;
sl@0
   122
	TUint64 initialValues;
sl@0
   123
	TUint16* indexedValues = (TUint16*)&initialValues;
sl@0
   124
sl@0
   125
	do	{
sl@0
   126
		initialValues = iValues;
sl@0
   127
sl@0
   128
		if (initialValues & KReadersOrWritersMask)
sl@0
   129
			{
sl@0
   130
			__ASSERT_ALWAYS(indexedValues[KWritersPendingIndex] < KMaxTUint16, Panic(EReadWriteLockTooManyClients));
sl@0
   131
			blocked = ETrue;
sl@0
   132
			}
sl@0
   133
		else
sl@0
   134
			{
sl@0
   135
			blocked = EFalse;
sl@0
   136
			}
sl@0
   137
		}
sl@0
   138
	while (!__e32_atomic_cas_rel64(&iValues, &initialValues, initialValues + (blocked ? KWriterPendingValue : KWriterValue)));
sl@0
   139
sl@0
   140
	if (blocked)
sl@0
   141
		iWriterSem.Wait();
sl@0
   142
	}
sl@0
   143
sl@0
   144
/**
sl@0
   145
Ask for a read lock without blocking.
sl@0
   146
@return		ETrue - lock granted
sl@0
   147
			EFalse - failed to obtain the lock
sl@0
   148
@panic		EReadWriteLockTooManyClients if the resulting number of readers exceeds EReadWriteLockClientCategoryLimit
sl@0
   149
@see		ReadLock()
sl@0
   150
*/
sl@0
   151
EXPORT_C TBool RReadWriteLock::TryReadLock()
sl@0
   152
	{
sl@0
   153
	TUint64 initialValues;
sl@0
   154
	TUint16* indexedValues = (TUint16*)&initialValues;
sl@0
   155
sl@0
   156
	do	{
sl@0
   157
		initialValues = iValues;
sl@0
   158
sl@0
   159
		if (indexedValues[KWriterIndex] > 0 ||
sl@0
   160
			(iPriority != EReaderPriority && indexedValues[KWritersPendingIndex] > 0))
sl@0
   161
			return EFalse;
sl@0
   162
sl@0
   163
		__ASSERT_ALWAYS(indexedValues[KReadersIndex] < KMaxTUint16, Panic(EReadWriteLockTooManyClients));
sl@0
   164
		}
sl@0
   165
	while (!__e32_atomic_cas_rel64(&iValues, &initialValues, initialValues + KReaderValue));
sl@0
   166
sl@0
   167
	return ETrue;
sl@0
   168
	}
sl@0
   169
sl@0
   170
/**
sl@0
   171
Ask for a write lock without blocking.
sl@0
   172
@return		ETrue - lock granted
sl@0
   173
			EFalse - failed to obtain the lock
sl@0
   174
@see		WriteLock()
sl@0
   175
*/
sl@0
   176
EXPORT_C TBool RReadWriteLock::TryWriteLock()
sl@0
   177
	{
sl@0
   178
	TUint64 initialValues;
sl@0
   179
sl@0
   180
	do	{
sl@0
   181
		initialValues = iValues;
sl@0
   182
sl@0
   183
		if (initialValues & KReadersOrWritersMask)
sl@0
   184
			return EFalse;
sl@0
   185
		}
sl@0
   186
	while (!__e32_atomic_cas_rel64(&iValues, &initialValues, initialValues + KWriterValue));
sl@0
   187
sl@0
   188
	return ETrue;
sl@0
   189
	}
sl@0
   190
sl@0
   191
/**
sl@0
   192
Tries to atomically release a read lock and gain a write lock.
sl@0
   193
This function will succeed if:
sl@0
   194
	- This is the only reader and
sl@0
   195
		- There are no pending writers or
sl@0
   196
		- The priority is reader
sl@0
   197
@return		ETrue - write lock granted
sl@0
   198
			EFalse - failed to obtain a write lock, read lock retained
sl@0
   199
@panic		EReadWriteLockBadLockState if the read lock is not currently held
sl@0
   200
*/
sl@0
   201
EXPORT_C TBool RReadWriteLock::TryUpgradeReadLock()
sl@0
   202
	{
sl@0
   203
	__ASSERT_ALWAYS((iValues & KReadersMask) != 0, Panic(EReadWriteLockBadLockState)); // Check we actually hold a read lock
sl@0
   204
	__ASSERT_DEBUG((iValues & KWriterMask) == 0, Panic(EReadWriteLockBadLockState)); // Check we don't hold a write lock - shouldn't be possible
sl@0
   205
sl@0
   206
	TUint64 initialValues;
sl@0
   207
	TUint16* indexedValues = (TUint16*)&initialValues;
sl@0
   208
sl@0
   209
	do	{
sl@0
   210
		initialValues = iValues;
sl@0
   211
sl@0
   212
		if (indexedValues[KReadersIndex] > 1 ||
sl@0
   213
			(iPriority != EReaderPriority && indexedValues[KWritersPendingIndex] > 0))
sl@0
   214
              return EFalse;
sl@0
   215
		}
sl@0
   216
	while (!__e32_atomic_cas_acq64(&iValues, &initialValues, initialValues - KReaderValue + KWriterValue));
sl@0
   217
sl@0
   218
	return ETrue;
sl@0
   219
	}
sl@0
   220
sl@0
   221
/**
sl@0
   222
Atomically releases a held write lock and gains a read lock. Also unblocks any
sl@0
   223
pending readers if:
sl@0
   224
	- Priority is EPriorityReader or
sl@0
   225
	- There are no pending writers
sl@0
   226
This function can not fail, so it does not return anything.
sl@0
   227
@panic		EReadWriteLockBadLockState if the lock is not currently held
sl@0
   228
*/
sl@0
   229
EXPORT_C void RReadWriteLock::DowngradeWriteLock()
sl@0
   230
	{
sl@0
   231
	__ASSERT_ALWAYS((iValues & KWriterMask) == KWriterValue, Panic(EReadWriteLockBadLockState)); // Check we actually hold a write lock
sl@0
   232
	__ASSERT_DEBUG((iValues & KReadersMask) == 0, Panic(EReadWriteLockBadLockState)); // Check we don't hold a read lock - shouldn't be possible
sl@0
   233
sl@0
   234
	TUint unlockReaders;
sl@0
   235
	TUint64 initialValues;
sl@0
   236
	TUint16* indexedValues = (TUint16*)&initialValues;
sl@0
   237
	TUint64 newValues;
sl@0
   238
sl@0
   239
	do	{
sl@0
   240
		unlockReaders = 0;
sl@0
   241
		initialValues = iValues;
sl@0
   242
		newValues = initialValues - KWriterValue + KReaderValue; // Clear current write lock flag and add a read lock
sl@0
   243
sl@0
   244
		if (indexedValues[KReadersPendingIndex] > 0 &&
sl@0
   245
			(indexedValues[KWritersPendingIndex] == 0 || iPriority == EReaderPriority)) // Release any other pending readers
sl@0
   246
			{
sl@0
   247
			unlockReaders = indexedValues[KReadersPendingIndex];
sl@0
   248
			newValues &= KReadersPendingClearMask; // Clear pending readers
sl@0
   249
sl@0
   250
			if (unlockReaders == KMaxTUint16) // Put a pending reader back to avoid overflow in the readers field
sl@0
   251
				{
sl@0
   252
				unlockReaders--;
sl@0
   253
				newValues += KReaderPendingValue;
sl@0
   254
				}
sl@0
   255
sl@0
   256
			newValues += unlockReaders;
sl@0
   257
			}
sl@0
   258
		}
sl@0
   259
	while (!__e32_atomic_cas_acq64(&iValues, &initialValues, newValues));
sl@0
   260
sl@0
   261
	if (unlockReaders > 0)
sl@0
   262
		iReaderSem.Signal(unlockReaders);
sl@0
   263
	}
sl@0
   264
sl@0
   265
/**
sl@0
   266
Releases a held read or write lock. If no-one else holds this lock (ie other
sl@0
   267
readers) then this will unblock one or more pending clients based on the priority:
sl@0
   268
	EAlternatePriority	- If a read lock is being released then:
sl@0
   269
							- Give the lock to the first pending writer, if there is one
sl@0
   270
							- Else give the lock to all pending readers, if there are any
sl@0
   271
						- If a write lock is being released then:
sl@0
   272
							- If there are pending readers:
sl@0
   273
								- If there are pending writers then unblock one pending reader
sl@0
   274
								- Else if there are no pending writers then unblock all pending readers
sl@0
   275
							- Else unblock one pending writer, if there is one
sl@0
   276
	EReaderPriority		- Unblock all pending readers. If none then unblock one pending writer, if there is one
sl@0
   277
	EWriterPriority		- Unblock one pending writer, if there is one. If none then unblock any and all pending readers
sl@0
   278
@panic		EReadWriteLockBadLockState if the lock is not currently held
sl@0
   279
*/
sl@0
   280
EXPORT_C void RReadWriteLock::Unlock()
sl@0
   281
	{
sl@0
   282
	__ASSERT_ALWAYS((iValues & KReadersOrWritersMask) != 0, Panic(EReadWriteLockBadLockState)); // Check we actually hold a lock
sl@0
   283
	__ASSERT_DEBUG((iValues & KReadersOrWritersMask) <= KWriterValue, Panic(EReadWriteLockBadLockState)); // Check we don't hold a read lock and a write lock at the same time - shouldn't be possible
sl@0
   284
sl@0
   285
	TInt unlockClients = 0;
sl@0
   286
sl@0
   287
	switch (iPriority)
sl@0
   288
		{
sl@0
   289
	case EWriterPriority:
sl@0
   290
		unlockClients = UnlockWriter(); break;
sl@0
   291
	case EAlternatePriority:
sl@0
   292
		unlockClients = UnlockAlternate(); break;
sl@0
   293
	default: // EReaderPriority:
sl@0
   294
		unlockClients = UnlockReader(); break;
sl@0
   295
		};
sl@0
   296
sl@0
   297
	if (unlockClients == -1)
sl@0
   298
		{
sl@0
   299
#ifdef _DEBUG
sl@0
   300
		iSpare[0] = 0; // Keep a rough track of writer starvation
sl@0
   301
#endif
sl@0
   302
		iWriterSem.Signal();
sl@0
   303
		}
sl@0
   304
	else if (unlockClients > 0)
sl@0
   305
		{
sl@0
   306
#ifdef _DEBUG
sl@0
   307
		const TUint64 KWritersPendingMask = UI64LIT(0xffff000000000000);
sl@0
   308
		if (iValues & KWritersPendingMask)
sl@0
   309
			iSpare[0]++; // Keep a rough track of writer starvation
sl@0
   310
		if (iSpare[0] > 1000)
sl@0
   311
			Panic(EReadWriteLockWriterStarvation);
sl@0
   312
#endif
sl@0
   313
		iReaderSem.Signal(unlockClients);
sl@0
   314
		}
sl@0
   315
	}
sl@0
   316
sl@0
   317
TInt RReadWriteLock::UnlockWriter()
sl@0
   318
	{
sl@0
   319
	TUint64 initialValues;
sl@0
   320
	TUint16* indexedValues = (TUint16*)&initialValues;
sl@0
   321
	TUint64 newValues;
sl@0
   322
	TInt unlockClients;
sl@0
   323
sl@0
   324
	do	{
sl@0
   325
		unlockClients = 0;
sl@0
   326
		initialValues = iValues;
sl@0
   327
		newValues = initialValues - (indexedValues[KReadersIndex] > 0 ? KReaderValue : KWriterValue); // Clear current lock flag
sl@0
   328
sl@0
   329
		if ((newValues & KReadersOrWritersMask) == 0) // No longer locked - release someone else
sl@0
   330
			{
sl@0
   331
			if (indexedValues[KWritersPendingIndex] > 0) // Release a writer
sl@0
   332
				{
sl@0
   333
				unlockClients = -1;
sl@0
   334
				newValues -= KWriterPendingValue;
sl@0
   335
				newValues += KWriterValue;
sl@0
   336
				}
sl@0
   337
			else if (indexedValues[KReadersPendingIndex] > 0) // Release all pending readers
sl@0
   338
				{
sl@0
   339
				unlockClients = indexedValues[KReadersPendingIndex];
sl@0
   340
				newValues &= KReadersPendingClearMask; // Clear pending readers
sl@0
   341
				newValues += unlockClients;
sl@0
   342
				}
sl@0
   343
			}
sl@0
   344
		}
sl@0
   345
	while (!__e32_atomic_cas_acq64(&iValues, &initialValues, newValues));
sl@0
   346
sl@0
   347
	return unlockClients;
sl@0
   348
	}
sl@0
   349
sl@0
   350
TInt RReadWriteLock::UnlockAlternate()
sl@0
   351
	{
sl@0
   352
	TUint64 initialValues;
sl@0
   353
	TUint16* indexedValues = (TUint16*)&initialValues;
sl@0
   354
	TUint64 newValues;
sl@0
   355
	TInt unlockClients;
sl@0
   356
sl@0
   357
	do	{
sl@0
   358
		unlockClients = 0;
sl@0
   359
		initialValues = iValues;
sl@0
   360
		newValues = initialValues - (indexedValues[KReadersIndex] > 0 ? KReaderValue : KWriterValue); // Clear current lock flag
sl@0
   361
sl@0
   362
		if ((newValues & KReadersOrWritersMask) == 0) // No longer locked - release someone else
sl@0
   363
			{
sl@0
   364
			if (indexedValues[KWritersPendingIndex] > 0 &&
sl@0
   365
				(indexedValues[KReadersIndex] > 0 || indexedValues[KReadersPendingIndex] == 0)) // Release a writer if there is one and either this is a read unlock or there are no readers pending
sl@0
   366
				{
sl@0
   367
				unlockClients = -1;
sl@0
   368
				newValues -= KWriterPendingValue;
sl@0
   369
				newValues += KWriterValue;
sl@0
   370
				}
sl@0
   371
			else if (indexedValues[KReadersPendingIndex] > 0) // Release one or more readers
sl@0
   372
				{
sl@0
   373
				if (indexedValues[KWritersPendingIndex] > 0) // Just one because there are pending writers
sl@0
   374
					{
sl@0
   375
					unlockClients = 1;
sl@0
   376
					newValues -= KReaderPendingValue;
sl@0
   377
					newValues += KReaderValue;
sl@0
   378
					}
sl@0
   379
				else // All of them
sl@0
   380
					{
sl@0
   381
					unlockClients = indexedValues[KReadersPendingIndex];
sl@0
   382
					newValues &= KReadersPendingClearMask; // Clear pending readers
sl@0
   383
					newValues += unlockClients;
sl@0
   384
					}
sl@0
   385
				}
sl@0
   386
sl@0
   387
			}
sl@0
   388
		}
sl@0
   389
	while (!__e32_atomic_cas_acq64(&iValues, &initialValues, newValues));
sl@0
   390
sl@0
   391
	return unlockClients;
sl@0
   392
	}
sl@0
   393
sl@0
   394
TInt RReadWriteLock::UnlockReader()
sl@0
   395
	{
sl@0
   396
	TUint64 initialValues;
sl@0
   397
	TUint16* indexedValues = (TUint16*)&initialValues;
sl@0
   398
	TUint64 newValues;
sl@0
   399
	TInt unlockClients;
sl@0
   400
sl@0
   401
	do	{
sl@0
   402
		unlockClients = 0;
sl@0
   403
		initialValues = iValues;
sl@0
   404
		newValues = initialValues - (indexedValues[KReadersIndex] > 0 ? KReaderValue : KWriterValue); // Clear current lock flag
sl@0
   405
sl@0
   406
		if ((newValues & KReadersOrWritersMask) == 0) // No longer locked - release someone else
sl@0
   407
			{
sl@0
   408
			if (indexedValues[KReadersPendingIndex] > 0) // Release all pending readers
sl@0
   409
				{
sl@0
   410
				unlockClients = indexedValues[KReadersPendingIndex];
sl@0
   411
				newValues &= KReadersPendingClearMask; // Clear pending readers
sl@0
   412
				newValues += unlockClients;
sl@0
   413
				}
sl@0
   414
			else if (indexedValues[KWritersPendingIndex] > 0) // Release a writer
sl@0
   415
				{
sl@0
   416
				unlockClients = -1;
sl@0
   417
				newValues -= KWriterPendingValue;
sl@0
   418
				newValues += KWriterValue;
sl@0
   419
				}
sl@0
   420
			}
sl@0
   421
		}
sl@0
   422
	while (!__e32_atomic_cas_acq64(&iValues, &initialValues, newValues));
sl@0
   423
sl@0
   424
	return unlockClients;
sl@0
   425
	}
sl@0
   426