os/kernelhwsrv/kernel/eka/euser/us_mqueue.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) 2002-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_mqueue.cpp
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#include "us_std.h"
sl@0
    19
#include <e32kpan.h>
sl@0
    20
#include <e32msgqueue.h>
sl@0
    21
sl@0
    22
sl@0
    23
sl@0
    24
sl@0
    25
EXPORT_C TInt RMsgQueueBase::CreateLocal(TInt aSize, TInt aMsgLength, TOwnerType aType)
sl@0
    26
/**
sl@0
    27
Creates a message queue that is private to the current process,
sl@0
    28
and opens a handle to that message queue.
sl@0
    29
sl@0
    30
The Kernel side object representing the message queue is
sl@0
    31
unnamed. This means that it is not possible to search for the message queue,
sl@0
    32
and this makes it local to the current process.
sl@0
    33
sl@0
    34
By default, any thread in the process can use this handle to
sl@0
    35
access the message queue. However, specifying EOwnerThread as the
sl@0
    36
third parameter to this function means that only the creating thread can use
sl@0
    37
the handle to access the message queue; any other thread in this process that
sl@0
    38
wants to access the message queue must duplicate this handle.
sl@0
    39
		
sl@0
    40
@param aSize      The number of message 'slots' in the queue.
sl@0
    41
                  This must be a positive value, i.e. greater than zero.
sl@0
    42
@param aMsgLength The size of each message for the queue, this cannot exceed
sl@0
    43
                  KMaxLength.
sl@0
    44
@param aType      The type of handle to be created.
sl@0
    45
                  EOwnerProcess is the default value, if not explicitly specified.
sl@0
    46
sl@0
    47
@return KErrNone if the queue is created sucessfully, otherwise one of
sl@0
    48
        the other system wide error codes.
sl@0
    49
		
sl@0
    50
@panic KERN-EXEC 49 if aSize is less than or equal to zero.
sl@0
    51
@panic KERN-EXEC 48 if aMsgLength is not a multiple of 4 bytes, 
sl@0
    52
                    is less than 4, or is greater than KMaxLength. 
sl@0
    53
                    
sl@0
    54
@see KMaxLength                    
sl@0
    55
*/
sl@0
    56
	{
sl@0
    57
	return SetReturnedHandle(Exec::MsgQueueCreate(NULL, aSize, aMsgLength, aType));
sl@0
    58
	}
sl@0
    59
sl@0
    60
sl@0
    61
sl@0
    62
sl@0
    63
EXPORT_C TInt RMsgQueueBase::CreateGlobal(const TDesC &aName,TInt aSize, TInt aMsgLength,TOwnerType aType)
sl@0
    64
/**
sl@0
    65
Creates a global message queue, and opens a handle to that
sl@0
    66
message queue.
sl@0
    67
sl@0
    68
The kernel side object representing the message queue is given
sl@0
    69
the name contained in the specified descriptor, which makes it global,
sl@0
    70
i.e. it is visible to all processes. This means that any thread in any
sl@0
    71
process can search for the message queue, and open a handle to it.
sl@0
    72
If the specified name is empty the kernel side object representing the
sl@0
    73
message queue is unnamed and so cannot be opened by name or searched
sl@0
    74
for. It can however be passed to another process as a process parameter
sl@0
    75
or via IPC.
sl@0
    76
sl@0
    77
By default, any thread in the process can use this handle to
sl@0
    78
access the message queue. However, specifying EOwnerThread as the
sl@0
    79
fourth parameter to this function, means that only the creating thread can use
sl@0
    80
this handle to access the message queue; any other thread in this process that
sl@0
    81
wants to access the message queue must either duplicate this handle or use 
sl@0
    82
OpenGlobal().
sl@0
    83
sl@0
    84
@param aName      The name to be assigned to the message queue
sl@0
    85
@param aSize      The number of message 'slots' in the queue.
sl@0
    86
                  This must be a positive value, i.e. greater than zero.
sl@0
    87
@param aMsgLength The size of each message for the queue, this cannot exceed
sl@0
    88
                  KMaxLength.
sl@0
    89
@param aType      The type of handle to be created.
sl@0
    90
                  EOwnerProcess is the default value, if not explicitly specified.
sl@0
    91
sl@0
    92
@return KErrNone if the queue is created sucessfully, otherwise one of
sl@0
    93
        the other system wide error codes.
sl@0
    94
		
sl@0
    95
@panic KERN-EXEC 49 if aSize is less than or equal to zero.
sl@0
    96
@panic KERN-EXEC 48 if aMsgLength is not a multiple of 4 bytes, 
sl@0
    97
                    is less than 4, or is greater than KMaxLength. 
sl@0
    98
                    
sl@0
    99
@see KMaxLength                    
sl@0
   100
@see RMsgQueueBase::OpenGlobal
sl@0
   101
*/
sl@0
   102
	{
sl@0
   103
	TInt r = User::ValidateName(aName);
sl@0
   104
	if(KErrNone!=r)
sl@0
   105
		return r;
sl@0
   106
	TBuf8<KMaxKernelName> name8;
sl@0
   107
	name8.Copy(aName);
sl@0
   108
	return SetReturnedHandle(Exec::MsgQueueCreate(&name8, aSize, aMsgLength, aType));
sl@0
   109
	}
sl@0
   110
sl@0
   111
sl@0
   112
sl@0
   113
sl@0
   114
EXPORT_C TInt RMsgQueueBase::OpenGlobal(const TDesC &aName, TOwnerType aType)
sl@0
   115
/**
sl@0
   116
Opens a global message queue.
sl@0
   117
sl@0
   118
Global message queues are identified by name.
sl@0
   119
sl@0
   120
By default, any thread in the process can use this handle to
sl@0
   121
access the message queue. However, specifying EOwnerThread as the
sl@0
   122
second parameter to this function, means that only the opening thread can use
sl@0
   123
this handle to access the message queue; any other thread in this process that
sl@0
   124
wants to access the message queue must either duplicate this handle or use
sl@0
   125
OpenGlobal() again.
sl@0
   126
sl@0
   127
@param aName The name of the message queue.
sl@0
   128
@param aType The type of handle to be created.
sl@0
   129
             EOwnerProcess is the default value, if not explicitly specified.
sl@0
   130
sl@0
   131
@return KErrNone if queue opened sucessfully, otherwise one of
sl@0
   132
        the other system wide error codes.
sl@0
   133
sl@0
   134
@see RMsgQueueBase::OpenGlobal
sl@0
   135
*/
sl@0
   136
	{
sl@0
   137
	return OpenByName(aName,aType,EMsgQueue);
sl@0
   138
	}
sl@0
   139
sl@0
   140
sl@0
   141
sl@0
   142
//realtime
sl@0
   143
EXPORT_C TInt RMsgQueueBase::Send(const TAny* aPtr, TInt aLength)
sl@0
   144
/**
sl@0
   145
sl@0
   146
Sends a message through this queue.
sl@0
   147
sl@0
   148
The function does not wait (i.e. block), if the queue is full.
sl@0
   149
sl@0
   150
Note that, once on the queue, the content of the message cannot
sl@0
   151
be accessed other than through a call to Receive() or ReceiveBlocking().
sl@0
   152
		 
sl@0
   153
@param aPtr    A pointer to the message data
sl@0
   154
@param aLength The length of the message data, this must not exceed
sl@0
   155
               the queue's message size.
sl@0
   156
				
sl@0
   157
@return  KErrNone, if successful;
sl@0
   158
         KErrOverflow, if queue is full,
sl@0
   159
sl@0
   160
@panic KERN-EXEC 48 if aLength is greater than the message length specified
sl@0
   161
       when the queue was created, or if aLength is less than or equal to zero.
sl@0
   162
sl@0
   163
@see RMsgQueueBase::Receive
sl@0
   164
@see RMsgQueueBase::ReceiveBlocking
sl@0
   165
*/
sl@0
   166
	{
sl@0
   167
	return Exec::MsgQueueSend(iHandle, aPtr, aLength);
sl@0
   168
	}
sl@0
   169
sl@0
   170
sl@0
   171
sl@0
   172
sl@0
   173
EXPORT_C void RMsgQueueBase::SendBlocking(const TAny* aPtr, TInt aLength)
sl@0
   174
/**
sl@0
   175
Sends a message through this queue, and waits for space to become available 
sl@0
   176
if the queue is full.
sl@0
   177
sl@0
   178
The function uses NotifySpaceAvailable() to provide the blocking operation. 
sl@0
   179
Note that it is not possible to cancel a call to SendBlocking().
sl@0
   180
sl@0
   181
@param aPtr    A pointer to the message data.
sl@0
   182
@param aLength The length of the message data, this must not exceed
sl@0
   183
               the queue's message size.
sl@0
   184
sl@0
   185
@panic KERN-EXEC 48 if aLength is greater than the message length specified
sl@0
   186
       when the queue was created, or if aLength is less than or equal to zero.
sl@0
   187
       
sl@0
   188
@see RMsgQueueBase::NotifySpaceAvailable
sl@0
   189
*/
sl@0
   190
	{
sl@0
   191
	TRequestStatus stat;
sl@0
   192
	while (Exec::MsgQueueSend(iHandle, aPtr, aLength) == KErrOverflow)
sl@0
   193
		{
sl@0
   194
		stat = KRequestPending;
sl@0
   195
		Exec::MsgQueueNotifySpaceAvailable(iHandle, stat);
sl@0
   196
		User::WaitForRequest(stat);
sl@0
   197
		}
sl@0
   198
	}
sl@0
   199
sl@0
   200
sl@0
   201
sl@0
   202
//realtime
sl@0
   203
EXPORT_C TInt RMsgQueueBase::Receive(TAny* aPtr, TInt aLength)
sl@0
   204
/**
sl@0
   205
sl@0
   206
Retrieves the first message in the queue.
sl@0
   207
sl@0
   208
The function does not wait (i.e. block), if the queue is empty.
sl@0
   209
sl@0
   210
@param aPtr    A pointer to a buffer to receive the message data.
sl@0
   211
@param aLength The length of the buffer for the message, this must match
sl@0
   212
               the queue's message size.
sl@0
   213
sl@0
   214
@return KErrNone, ifsuccessful;
sl@0
   215
        KErrUnderflow, if the queue is empty.
sl@0
   216
        
sl@0
   217
@panic KERN-EXEC 48 if aLength is not equal to the message length
sl@0
   218
       specified when the queue was created.
sl@0
   219
*/
sl@0
   220
	{
sl@0
   221
	return Exec::MsgQueueReceive(iHandle, aPtr, aLength);
sl@0
   222
	}
sl@0
   223
sl@0
   224
sl@0
   225
sl@0
   226
sl@0
   227
EXPORT_C void RMsgQueueBase::ReceiveBlocking(TAny* aPtr, TInt aLength)
sl@0
   228
/**
sl@0
   229
Retrieves the first message in the queue, and waits if the queue is empty.
sl@0
   230
			 
sl@0
   231
The function uses NotifyDataAvailable() to provide the blocking operation.
sl@0
   232
Note it is not possible to cancel a call to ReceiveBlocking().
sl@0
   233
sl@0
   234
@param aPtr    A pointer to a buffer to receive the message data.
sl@0
   235
@param aLength The length of the buffer for the message, this must match
sl@0
   236
               the queue's message size.
sl@0
   237
               
sl@0
   238
@panic KERN-EXEC 48 if aLength is not equal to the message length
sl@0
   239
       specified when the queue was created.
sl@0
   240
       
sl@0
   241
@see RMsgQueueBase::NotifyDataAvailable
sl@0
   242
*/
sl@0
   243
	{
sl@0
   244
	TRequestStatus stat;
sl@0
   245
	while (Exec::MsgQueueReceive(iHandle, aPtr, aLength) == KErrUnderflow)
sl@0
   246
		{
sl@0
   247
		stat = KRequestPending;
sl@0
   248
		Exec::MsgQueueNotifyDataAvailable(iHandle, stat);
sl@0
   249
		User::WaitForRequest(stat);
sl@0
   250
		}
sl@0
   251
	}
sl@0
   252
sl@0
   253
sl@0
   254
sl@0
   255
sl@0
   256
EXPORT_C void RMsgQueueBase::NotifySpaceAvailable(TRequestStatus& aStatus)
sl@0
   257
/**
sl@0
   258
Requests notification when space becomes available in the queue.
sl@0
   259
	
sl@0
   260
This is an asynchronous request that completes when there is at least
sl@0
   261
one 'slot'available in the queue.
sl@0
   262
sl@0
   263
A thread can have only one space available notification request	outstanding on
sl@0
   264
this message queue. If a second request is made before
sl@0
   265
the first request completes, then the calling thread is panicked.
sl@0
   266
sl@0
   267
@param aStatus The request status object to be completed when space
sl@0
   268
               becomes available.
sl@0
   269
sl@0
   270
@panic KERN-EXEC 47 if a second request is made
sl@0
   271
       while the first request remains outstanding.
sl@0
   272
*/
sl@0
   273
	{
sl@0
   274
	aStatus = KRequestPending;
sl@0
   275
	Exec::MsgQueueNotifySpaceAvailable(iHandle, aStatus);
sl@0
   276
	}
sl@0
   277
sl@0
   278
sl@0
   279
sl@0
   280
sl@0
   281
EXPORT_C void RMsgQueueBase::CancelSpaceAvailable()
sl@0
   282
/**
sl@0
   283
Cancels an outstanding space available notification	request.
sl@0
   284
	
sl@0
   285
If the request is not already complete, then it now completes with KErrCancel.
sl@0
   286
	
sl@0
   287
@panic KERN-EXEC 50 if attempting to cancel an outstanding request made by
sl@0
   288
       a thread in a different process.
sl@0
   289
			
sl@0
   290
@see RMsgQueueBase::NotifySpaceAvailable
sl@0
   291
*/
sl@0
   292
	{
sl@0
   293
	Exec::MsgQueueCancelSpaceAvailable(iHandle);
sl@0
   294
	}
sl@0
   295
sl@0
   296
sl@0
   297
sl@0
   298
sl@0
   299
EXPORT_C void RMsgQueueBase::NotifyDataAvailable(TRequestStatus& aStatus)
sl@0
   300
/**
sl@0
   301
Requests notification when there is at least one message in the queue.
sl@0
   302
sl@0
   303
A thread can have only one data available notification request	outstanding on
sl@0
   304
this message queue. If a second request is made before
sl@0
   305
the first request completes, then the calling thread is panicked.
sl@0
   306
sl@0
   307
@param aStatus The request status object to be completed when
sl@0
   308
               a message becomes available.
sl@0
   309
sl@0
   310
@panic KERN-EXEC 47 if a second request is made
sl@0
   311
       while the first request remains outstanding.
sl@0
   312
*/
sl@0
   313
	{
sl@0
   314
	aStatus = KRequestPending;
sl@0
   315
	Exec::MsgQueueNotifyDataAvailable(iHandle, aStatus);
sl@0
   316
	}
sl@0
   317
sl@0
   318
sl@0
   319
sl@0
   320
sl@0
   321
EXPORT_C void RMsgQueueBase::CancelDataAvailable()
sl@0
   322
/**
sl@0
   323
Cancels an outstanding data available notification request.
sl@0
   324
sl@0
   325
If the request is not already complete, then it now completes with KErrCancel.
sl@0
   326
sl@0
   327
@panic KERN-EXEC 50 if attempting to cancel an outstanding request made by
sl@0
   328
       a thread in a different process.
sl@0
   329
       
sl@0
   330
@see RMsgQueueBase::NotifyDataAvailable
sl@0
   331
*/
sl@0
   332
	{
sl@0
   333
	Exec::MsgQueueCancelDataAvailable(iHandle);
sl@0
   334
	}
sl@0
   335
sl@0
   336
sl@0
   337
sl@0
   338
sl@0
   339
EXPORT_C TInt RMsgQueueBase::MessageSize()
sl@0
   340
/**
sl@0
   341
Gets the size of message slots in the queue.
sl@0
   342
sl@0
   343
@return The size of a message slot in the queue.
sl@0
   344
*/
sl@0
   345
	{
sl@0
   346
	return Exec::MsgQueueSize(iHandle);
sl@0
   347
	}
sl@0
   348
sl@0
   349
sl@0
   350
sl@0
   351
sl@0
   352
EXPORT_C TInt RMsgQueueBase::Open(RMessagePtr2 aMessage, TInt aParam, TOwnerType aType)
sl@0
   353
/**
sl@0
   354
Opens a global message queue using a handle passed in a server message.
sl@0
   355
sl@0
   356
By default, any thread in the process can use this handle to
sl@0
   357
access the message queue. However, specifying EOwnerThread as the
sl@0
   358
third parameter to this function, means that only the opening thread can use
sl@0
   359
this handle to access the message queue.
sl@0
   360
sl@0
   361
@param aMessage The server message.
sl@0
   362
@param aParam   The number of the message parameter which holds the handle.
sl@0
   363
@param aType    The type of handle to be created.
sl@0
   364
		        EOwnerProcess is the default value, if not explicitly specified.
sl@0
   365
*/
sl@0
   366
	{
sl@0
   367
	return SetReturnedHandle(Exec::MessageOpenObject(aMessage.Handle(),EMsgQueue,aParam,aType));
sl@0
   368
	}
sl@0
   369
sl@0
   370
sl@0
   371
sl@0
   372
sl@0
   373
EXPORT_C TInt RMsgQueueBase::Open(TInt aArgumentIndex, TOwnerType aType)
sl@0
   374
/**
sl@0
   375
Opens a message queue using the handle passed in during process creation.
sl@0
   376
sl@0
   377
@param aArgumentIndex The number on the parameter which holds the handle.
sl@0
   378
@param aType          The type of handle to be created.
sl@0
   379
                      EOwnerProcess is the default value, if not explicitly
sl@0
   380
                      specified.
sl@0
   381
sl@0
   382
@return KErrNone, ifsuccessful;
sl@0
   383
		KErrArgument, if aArgumentIndex doesn't contain a message queue handle;          
sl@0
   384
		KErrNotFound, if aArgumentIndex is empty. 
sl@0
   385
*/
sl@0
   386
	{
sl@0
   387
	return SetReturnedHandle(Exec::ProcessGetHandleParameter(aArgumentIndex, EMsgQueue, aType));
sl@0
   388
	}