Home | History | Annotate | Download | only in src
      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 #ifndef PV_THREADSAFE_CALLBACKAO_H_INCLUDED
     19 #define PV_THREADSAFE_CALLBACKAO_H_INCLUDED
     20 
     21 #ifndef OSCL_BASE_H_INCLUDED
     22 #include "oscl_base.h"
     23 #endif
     24 
     25 #ifndef OSCL_SCHEDULER_AO_H_INCLUDED
     26 #include "oscl_scheduler_ao.h"
     27 #endif
     28 
     29 #ifndef OSCL_ERROR_CODES_H_INCLUDED
     30 #include "oscl_error_codes.h"
     31 #endif
     32 
     33 #ifndef PVLOGGER_H_INCLUDED
     34 #include "pvlogger.h"
     35 #endif
     36 
     37 #ifndef OSCL_SEMAPHORE_H_INCLUDED
     38 #include "oscl_semaphore.h"
     39 #endif
     40 // definition of queue type and queue element
     41 
     42 #define DEFAULT_QUEUE_DEPTH 3
     43 typedef struct QElement QElement;
     44 typedef struct QueueT QueueT;
     45 
     46 
     47 
     48 const char thisAOname[] = "threadsafecallbackAO";
     49 
     50 class ThreadSafeCallbackAO: public OsclActiveObject
     51 {
     52     public:
     53 
     54         struct QElement
     55         {
     56             OsclAny *pData;
     57         };
     58 
     59         struct QueueT
     60         {
     61             QElement* pFirst; // queue head
     62 
     63             uint32 index_in;            // index of element to be queued next
     64             uint32 index_out;       // index of element to be de-queued next
     65             uint32 NumElem;         // Number of elements currently in the queue
     66             uint32 MaxNumElements;  // Maximum number of queue elements (i.e, queue depth)
     67         };
     68         // constructor
     69         OSCL_IMPORT_REF ThreadSafeCallbackAO(void *aObserver = NULL, uint32 aDepth = DEFAULT_QUEUE_DEPTH, const char *aAOname = thisAOname, int32 aPriority = OsclActiveObject::EPriorityNominal);
     70 
     71         // desctructor
     72         OSCL_IMPORT_REF virtual ~ThreadSafeCallbackAO();
     73 
     74 
     75         OSCL_IMPORT_REF void ThreadLogon();
     76         OSCL_IMPORT_REF void ThreadLogoff();
     77 
     78         OSCL_IMPORT_REF OsclReturnCode ReceiveEvent(OsclAny *EventData); // Generic callback API
     79         OSCL_IMPORT_REF virtual OsclReturnCode ProcessEvent(OsclAny *EventData); // Process data
     80 
     81     protected:
     82 
     83         OSCL_IMPORT_REF virtual void Run();
     84 
     85         PVLogger* iLogger;
     86         const char *iLoggerString;
     87 
     88         void *iObserver;
     89         OsclSemaphore RemoteThreadCtrlSema; // Semaphore that blocks the remote thread when the event queue is full
     90         OsclMutex Mutex; // Mutex that controls access to the event queue
     91         QueueT *Q; // queue of events
     92 
     93         // QUEUE OPERATIONS:
     94         OSCL_IMPORT_REF OsclReturnCode QueueInit(uint32 aMaxQueueDepth);
     95         OSCL_IMPORT_REF OsclReturnCode QueueDeInit();
     96         OSCL_IMPORT_REF OsclReturnCode Queue(OsclAny *); // queue an element
     97         OSCL_IMPORT_REF virtual OsclAny *DeQueue(OsclReturnCode &status); // dequeue element and return a pointer to the element. return the status
     98         OSCL_IMPORT_REF uint32 GetQueueNumElem(); // number of elements in the queue
     99 
    100 };
    101 
    102 #endif
    103