os/graphics/graphicscomposition/openwfcompositionengine/adaptation/src/Platform/OS/symbian/owfmessagequeue.cpp
Update contrib.
1 // Copyright (c) 2009-2010 Nokia Corporation and/or its subsidiary(-ies).
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and/or associated documentation files (the
5 // "Materials"), to deal in the Materials without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Materials, and to
8 // permit persons to whom the Materials are furnished to do so, subject to
9 // the following conditions:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Materials.
14 // THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20 // MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
27 #define KInitialIndexValue (-1)
29 #include "owfmessagequeue.h"
33 class CMessageQueue: public CBase
36 virtual ~CMessageQueue();
37 static CMessageQueue* NewL();
38 static CMessageQueue* NewLC();
39 void Send(TInt msg, void* data);
40 TInt Fetch(OWF_MESSAGE* msg, TInt timeout);
41 TInt Fetch(OWF_MESSAGE* msg);
48 const static TInt iQueueSize = 16;
49 OWF_MESSAGE iMessageArray[iQueueSize];
50 RSemaphore iFetchSeamaphore;
51 RSemaphore iSendSemaphore;
52 RFastLock iProtectionMutex;
57 CMessageQueue::CMessageQueue():
58 iSendIndex(KInitialIndexValue),
59 iFetchIndex(KInitialIndexValue)
64 CMessageQueue::~CMessageQueue()
66 iFetchSeamaphore.Close();
67 iSendSemaphore.Close();
68 iProtectionMutex.Close();
71 void CMessageQueue::ConstructL()
73 User::LeaveIfError(iFetchSeamaphore.CreateLocal(0));
74 User::LeaveIfError(iSendSemaphore.CreateLocal(iQueueSize));
75 User::LeaveIfError(iProtectionMutex.CreateLocal());
78 CMessageQueue* CMessageQueue::NewLC()
80 CMessageQueue* self = new(ELeave) CMessageQueue();
81 CleanupStack::PushL(self);
86 CMessageQueue* CMessageQueue::NewL()
88 CMessageQueue* self = CMessageQueue::NewLC();
89 CleanupStack::Pop(self);
93 void CMessageQueue::Send(TInt msg, void* data)
95 iSendSemaphore.Wait();
96 iProtectionMutex.Wait();
97 iSendIndex = (iSendIndex + 1) % iQueueSize;
98 iMessageArray[iSendIndex].id = msg;
99 iMessageArray[iSendIndex].data = data;
100 iProtectionMutex.Signal();
101 iFetchSeamaphore.Signal();
104 TInt CMessageQueue::Fetch(OWF_MESSAGE* msg, TInt timeout)
112 if ((ret = iFetchSeamaphore.Wait(timeout)) == KErrNone)
114 iProtectionMutex.Wait();
115 if (iSendIndex == iFetchIndex)
117 iProtectionMutex.Signal();
122 iFetchIndex = (iFetchIndex + 1) % iQueueSize;
123 *msg = iMessageArray[iFetchIndex];
124 iProtectionMutex.Signal();
125 iSendSemaphore.Signal();
132 TInt CMessageQueue::Fetch(OWF_MESSAGE* msg)
139 iFetchSeamaphore.Wait();
140 iProtectionMutex.Wait();
141 iFetchIndex = (iFetchIndex + 1) % iQueueSize;
142 *msg = iMessageArray[iFetchIndex];
143 iProtectionMutex.Signal();
144 iSendSemaphore.Signal();
149 OWF_MessageQueue_Destroy(OWF_MESSAGE_QUEUE* queue)
155 delete (CMessageQueue*)queue->queuePtr;
156 queue->queuePtr = NULL;
160 OWF_MessageQueue_Init(OWF_MESSAGE_QUEUE* queue)
164 return MSG_QUEUE_INIT_ERR;
167 TRAPD(err, queue->queuePtr = CMessageQueue::NewL());
168 return (err || !queue->queuePtr) ? MSG_QUEUE_INIT_ERR : MSG_QUEUE_INIT_OK;
172 OWF_Message_Send(OWF_MESSAGE_QUEUE* queue,
177 OWF_ASSERT(queue->queuePtr);
179 ((CMessageQueue*)(queue->queuePtr))->Send(msg, data);
183 OWF_Message_Wait(OWF_MESSAGE_QUEUE* queue,
188 OWF_ASSERT(queue->queuePtr);
193 if (WAIT_FOREVER == timeout)
195 ret = ((CMessageQueue*)(queue->queuePtr))->Fetch(msg);
199 ret = ((CMessageQueue*)(queue->queuePtr))->Fetch(msg, timeout);
204 return MSG_QUEUE_WAIT_MSG_FETCHED;
206 return MSG_QUEUE_WAIT_MSG_TIMEDOUT;
208 return MSG_QUEUE_WAIT_MSG_ERR;