worker.h 3.68 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/**
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef MOCK_RIL_WORKER_H_
#define MOCK_RIL_WORKER_H_

#include <queue>
21 22
#include <list>
#include <vector>
23 24
#include <pthread.h>
#include <cutils/atomic.h>
25
#include <utils/SystemClock.h>
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106

/**
 * A Thread class.
 *
 * 0) Extend WorkerThread creating a Worker method which
 *    monitors isRunning(). For example:
 *
 *   void * Worker(void *param) {
 *       while (isRunning() == 0) {
 *           pthread_mutex_lock(&mutex_);
 *           while (isRunning() && !SOME-CONDITION) {
 *               pthread_cond_wait(&cond_, &mutex_);
 *           }
 *           if (isRunning()) {
 *               DO-WORK
 *           } else {
 *               pthread_mutex_unlock(&mutex_);
 *           }
 *       }
 *       return NULL;
 *   }
 *
 * 1) Create the WorkerThread.
 * 2) Execute Run passing a param which will be passed to Worker.
 * 3) Call Stop() or destroy the thread to stop processing.
 *
 */
class WorkerThread {
  protected:
    pthread_attr_t attr_;
    pthread_mutex_t mutex_;
    pthread_cond_t cond_;
    pthread_t tid_;
    void *workerParam_;

    #define STATE_INITIALIZED   1
    #define STATE_RUNNING       2
    #define STATE_STOPPING      3
    #define STATE_STOPPED       4
    int32_t state_;

    static void * Work(void *param);

    virtual bool isRunning();

  public:
    WorkerThread();

    virtual ~WorkerThread();

    // Return true if changed from STATE_RUNNING to STATE_STOPPING
    virtual bool BeginStopping();

    // Wait until state is not STATE_STOPPING
    virtual void WaitUntilStopped();

    virtual void Stop();

    virtual int Run(void *workerParam);

    /**
     * Method called to do work, see example above.
     * While running isRunning() must be monitored.
     */
    virtual void *Worker(void *) = 0;
};


/**
 * A WorkerQueue.
 *
 * 0) Extend overriding Process
 * 1) Create an instance
 * 2) Call Run.
 * 3) Call Add, passing a pointer which is added to a queue
 * 4) Process will be called with a pointer as work can be done.
 */
class WorkerQueue {
  private:
    friend class WorkerQueueThread;

107 108 109 110 111 112 113 114 115 116 117 118 119
    struct Record {
        int64_t time;
        void *p;
    };

    class record_compare {
      public:
        // To get ascending order return true if lhs > rhs.
        bool operator() (const struct Record* lhs, const struct Record* rhs) const {
            return lhs->time > rhs->time;
        }
    };

120 121
    std::list<struct Record *> q_;                // list of records to be processed
    std::list<struct Record *> free_list_;        // list of records that have been released
122
    std::priority_queue<struct Record *, std::vector<struct Record *>, record_compare> delayed_q_;
123
                                                  // list of records that are delayed
124 125
    class WorkerQueueThread *wqt_;

126 127 128 129 130
  protected:
    struct Record *obtain_record(void *p, int delay_in_ms);

    void release_record(struct Record *r);

131 132 133 134 135 136 137 138 139 140 141
  public:
    WorkerQueue();

    virtual ~WorkerQueue();

    int Run();

    void Stop();

    void Add(void *p);

142 143
    void AddDelayed(void *p, int delay_in_ms);

144 145 146 147 148 149
    virtual void Process(void *) = 0;
};

extern void testWorker();

#endif // MOCK_RIL_WORKER_H_