blob: 0bfa7a22fecadb1fc87cec40a1660f62e2e28b46 [file] [log] [blame]
Mark Salyzyn0175b072014-02-26 09:50:16 -08001/*
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;
Mark Salyzync113c582014-08-07 08:16:52 -070034 pthread_cond_t threadTriggeredCondition;
Mark Salyzyn0175b072014-02-26 09:50:16 -080035 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,
Mark Salyzynfa3716b2014-02-14 16:05:05 -080048 unsigned long tail, unsigned int logMask, pid_t pid,
49 log_time start);
Mark Salyzyn0175b072014-02-26 09:50:16 -080050
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
Mark Salyzync03e72c2014-02-18 11:23:53 -080063 bool runningReader_Locked(void) const {
Mark Salyzyn0175b072014-02-26 09:50:16 -080064 return threadRunning || mRelease || mError || mNonBlock;
65 }
Mark Salyzync113c582014-08-07 08:16:52 -070066 void triggerReader_Locked(void) {
67 pthread_cond_signal(&threadTriggeredCondition);
68 }
69
Mark Salyzyn0175b072014-02-26 09:50:16 -080070 void triggerSkip_Locked(unsigned int skip) { skipAhead = skip; }
71
72 // Called after LogTimeEntry removed from list, lock implicitly held
Mark Salyzync03e72c2014-02-18 11:23:53 -080073 void release_Locked(void) {
Mark Salyzyn0175b072014-02-26 09:50:16 -080074 mRelease = true;
Mark Salyzync113c582014-08-07 08:16:52 -070075 pthread_cond_signal(&threadTriggeredCondition);
Mark Salyzyn0175b072014-02-26 09:50:16 -080076 if (mRefCount || threadRunning) {
77 return;
78 }
79 // No one else is holding a reference to this
80 delete this;
81 }
82
83 // Called to mark socket in jeopardy
84 void error_Locked(void) { mError = true; }
Mark Salyzync113c582014-08-07 08:16:52 -070085 void error(void) { lock(); error_Locked(); unlock(); }
Mark Salyzyn0175b072014-02-26 09:50:16 -080086
87 bool isError_Locked(void) const { return mRelease || mError; }
88
89 // Mark Used
90 // Locking implied, grabbed when protection around loop iteration
91 void incRef_Locked(void) { ++mRefCount; }
92
93 bool owned_Locked(void) const { return mRefCount != 0; }
94
Mark Salyzync03e72c2014-02-18 11:23:53 -080095 void decRef_Locked(void) {
Mark Salyzyn0175b072014-02-26 09:50:16 -080096 if ((mRefCount && --mRefCount) || !mRelease || threadRunning) {
97 return;
98 }
99 // No one else is holding a reference to this
100 delete this;
101 }
102
103 // flushTo filter callbacks
104 static bool FilterFirstPass(const LogBufferElement *element, void *me);
105 static bool FilterSecondPass(const LogBufferElement *element, void *me);
106};
107
108typedef android::List<LogTimeEntry *> LastLogTimes;
109
110#endif