1/* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13 * express or implied.
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
17 */
18/*! \addtogroup osclproc OSCL Proc
19 *
20 * @{
21 */
22
23
24
25/** \file oscl_scheduler_readyq.h
26    \brief ready q types for oscl scheduler
27*/
28
29
30#ifndef OSCL_SCHEDULER_READYQ_H_INCLUDED
31#define OSCL_SCHEDULER_READYQ_H_INCLUDED
32
33#ifndef OSCL_SCHEDULER_TYPES_H_INCLUDED
34#include "oscl_scheduler_types.h"
35#endif
36#ifndef OSCL_SCHEDULER_TUNEABLES_H_INCLUDED
37#include "oscl_scheduler_tuneables.h"
38#endif
39
40
41#ifndef OSCL_PRIQUEUE_H_INCLUDED
42#include "oscl_priqueue.h"
43#endif
44#ifndef OSCL_BASE_ALLOC_H_INCLUDED
45#include "oscl_base_alloc.h"
46#endif
47#ifndef OSCL_SEMAPHORE_H_INCLUDED
48#include "oscl_semaphore.h"
49#endif
50#ifndef OSCL_MEM_H_INCLUDED
51#include "oscl_mem.h"
52#endif
53#ifndef OSCL_MUTEX_H_INCLUDED
54#include "oscl_mutex.h"
55#endif
56#ifndef OSCL_STRING_CONTAINERS_H_INCLUDED
57#include "oscl_string_containers.h"
58#endif
59
60class PVActiveBase;
61
62class OsclReadyAlloc: public Oscl_DefAlloc
63{
64    public:
65        OsclAny* allocate(const uint32 size) ;
66        OsclAny* allocate_fl(const uint32 size, const char * file_name, const int line_num);
67        void deallocate(OsclAny* p) ;
68    private:
69        OsclMemAllocator iBasicAlloc;
70};
71
72typedef PVActiveBase* TOsclReady;
73
74class OsclReadyCompare
75{
76    public:
77        static int compare(TOsclReady& a, TOsclReady& b) ;
78};
79class OsclTimerCompare
80{
81    public:
82        static int compare(TOsclReady& a, TOsclReady& b) ;
83};
84
85/** This is a thread-safe priority queue for holding the
86    active objects that are ready to run.
87    This queue also contains the request semaphore and the
88    queue observer callback logic.
89*/
90class PVLogger;
91class OsclSchedulerObserver;
92class OsclReadyQ
93        : public OsclPriorityQueue<TOsclReady, OsclReadyAlloc, Oscl_Vector<TOsclReady, OsclReadyAlloc>, OsclReadyCompare>
94{
95    public:
96        void Construct(int);
97        void ThreadLogon();
98        void ThreadLogoff();
99
100        void Remove(TOsclReady);
101
102        bool IsIn(TOsclReady);
103
104        uint32 Depth()
105        {
106            return size();
107        }
108
109        TOsclReady PopTop();
110        TOsclReady Top();
111
112        TOsclReady WaitAndPopTop();
113        TOsclReady WaitAndPopTop(uint32);
114
115        int32 PendComplete(PVActiveBase *pvbase, int32 aReason);
116        int32 WaitForRequestComplete(PVActiveBase*);
117
118        //For non-blocking scheduler observer support
119        void RegisterForCallback(OsclSchedulerObserver* aCallback, OsclAny* aCallbackContext);
120        void TimerCallback(uint32 aDelayMicrosec);
121        OsclSchedulerObserver* Callback()
122        {
123            return iCallback;
124        }
125
126    private:
127        TOsclReady PopTopAfterWait();
128
129        //mutex for thread protection
130        OsclNoYieldMutex iCrit;
131
132        //this semaphore tracks the queue size.  it is used to
133        //regulate the scheduling loop when running in blocking mode.
134        OsclSemaphore iSem;
135
136        //a sequence number needed to maintain FIFO sorting order in oscl pri queue.
137        uint32 iSeqNumCounter;
138
139        //For non-blocking scheduler observer support
140        OsclSchedulerObserver* iCallback;
141        OsclAny* iCallbackContext;
142};
143
144/*
145** A non-thread-safe queue for holding pending timers.
146*/
147class OsclTimerQ
148        : public OsclPriorityQueue<TOsclReady, OsclReadyAlloc, Oscl_Vector<TOsclReady, OsclReadyAlloc>, OsclTimerCompare>
149{
150    public:
151        void Construct(int);
152        void Add(TOsclReady);
153        void Remove(TOsclReady);
154        TOsclReady PopTop();
155        TOsclReady Top();
156        void Pop(TOsclReady);
157        bool IsIn(TOsclReady);
158    private:
159        //a sequence number needed to maintain FIFO sorting order in oscl pri queue.
160        uint32 iSeqNumCounter;
161};
162
163/** This class defines the queue link, which is common to both ready Q and timer Q.
164    Each AO contains its own queue link object.
165*/
166class TReadyQueLink
167{
168    public:
169        TReadyQueLink()
170        {
171            iAOPriority = 0;
172            iTimeToRunTicks = 0;
173            iSeqNum = 0;
174            iIsIn = NULL;
175        }
176
177        int32 iAOPriority;//scheduling priority
178        uint32 iTimeToRunTicks;//for timers, this is the time to run in ticks.
179        uint32 iTimeQueuedTicks;//the time when the AO was queued, in ticks.
180        uint32 iSeqNum;//sequence number for oscl pri queue.
181        OsclAny* iIsIn;//pointer to the queue we're in, cast as a void*
182
183};
184
185#endif
186
187
188/*! @} */
189