LogTimes.h revision fa3716b2501ccddc8e0cd30f6343692b8deb7639
1/*
2 * Copyright (C) 2012-2013 The Android Open Source Project
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 express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _LOGD_LOG_TIMES_H__
18#define _LOGD_LOG_TIMES_H__
19
20#include <pthread.h>
21#include <time.h>
22#include <sys/types.h>
23#include <sysutils/SocketClient.h>
24#include <utils/List.h>
25
26class LogReader;
27
28class LogTimeEntry {
29    static pthread_mutex_t timesLock;
30    unsigned int mRefCount;
31    bool mRelease;
32    bool mError;
33    bool threadRunning;
34    bool threadTriggered;
35    pthread_t mThread;
36    LogReader &mReader;
37    static void *threadStart(void *me);
38    static void threadStop(void *me);
39    const unsigned int mLogMask;
40    const pid_t mPid;
41    unsigned int skipAhead;
42    unsigned long mCount;
43    unsigned long mTail;
44    unsigned long mIndex;
45
46public:
47    LogTimeEntry(LogReader &reader, SocketClient *client, bool nonBlock,
48                 unsigned long tail, unsigned int logMask, pid_t pid,
49                 log_time start);
50
51    SocketClient *mClient;
52    static const struct timespec EPOCH;
53    log_time mStart;
54    const bool mNonBlock;
55    const log_time mEnd; // only relevant if mNonBlock
56
57    // Protect List manipulations
58    static void lock(void) { pthread_mutex_lock(&timesLock); }
59    static void unlock(void) { pthread_mutex_unlock(&timesLock); }
60
61    void startReader_Locked(void);
62
63    bool runningReader_Locked(void) const {
64        return threadRunning || mRelease || mError || mNonBlock;
65    }
66    void triggerReader_Locked(void) { threadTriggered = true; }
67    void triggerSkip_Locked(unsigned int skip) { skipAhead = skip; }
68
69    // Called after LogTimeEntry removed from list, lock implicitly held
70    void release_Locked(void) {
71        mRelease = true;
72        if (mRefCount || threadRunning) {
73            return;
74        }
75        // No one else is holding a reference to this
76        delete this;
77    }
78
79    // Called to mark socket in jeopardy
80    void error_Locked(void) { mError = true; }
81    void error(void) { lock(); mError = true; unlock(); }
82
83    bool isError_Locked(void) const { return mRelease || mError; }
84
85    // Mark Used
86    //  Locking implied, grabbed when protection around loop iteration
87    void incRef_Locked(void) { ++mRefCount; }
88
89    bool owned_Locked(void) const { return mRefCount != 0; }
90
91    void decRef_Locked(void) {
92        if ((mRefCount && --mRefCount) || !mRelease || threadRunning) {
93            return;
94        }
95        // No one else is holding a reference to this
96        delete this;
97    }
98
99    // flushTo filter callbacks
100    static bool FilterFirstPass(const LogBufferElement *element, void *me);
101    static bool FilterSecondPass(const LogBufferElement *element, void *me);
102};
103
104typedef android::List<LogTimeEntry *> LastLogTimes;
105
106#endif
107