Fix for NPE after disposing an experiment (Bug 381412)
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.kernel.core / src / org / eclipse / linuxtools / internal / lttng2 / kernel / core / stateprovider / CtfKernelStateInput.java
CommitLineData
efc403bb
AM
1/*******************************************************************************
2 * Copyright (c) 2012 Ericsson
3 * Copyright (c) 2010, 2011 École Polytechnique de Montréal
4 * Copyright (c) 2010, 2011 Alexandre Montplaisir <alexandre.montplaisir@gmail.com>
5 *
6 * All rights reserved. This program and the accompanying materials are
7 * made available under the terms of the Eclipse Public License v1.0 which
8 * accompanies this distribution, and is available at
9 * http://www.eclipse.org/legal/epl-v10.html
10 *
11 *******************************************************************************/
12
13package org.eclipse.linuxtools.internal.lttng2.kernel.core.stateprovider;
14
15import java.io.IOException;
16import java.util.concurrent.ArrayBlockingQueue;
17import java.util.concurrent.BlockingQueue;
18
efc403bb
AM
19import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfEvent;
20import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfTrace;
2c2f900e 21import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
18ab1d18
AM
22import org.eclipse.linuxtools.tmf.core.statesystem.IStateChangeInput;
23import org.eclipse.linuxtools.tmf.core.statesystem.IStateSystemBuilder;
efc403bb
AM
24
25/**
26 * This is the state change input plugin for TMF's state system which handles
27 * the LTTng 2.0 kernel traces in CTF format.
28 *
29 * It uses the reference handler defined in CTFKernelHandler.java.
30 *
31 * @author alexmont
32 *
33 */
dc0f7bfe 34public class CtfKernelStateInput implements IStateChangeInput {
efc403bb 35
2c2f900e 36 private static final int EVENTS_QUEUE_SIZE = 10000;
efc403bb
AM
37
38 private final BlockingQueue<CtfTmfEvent> eventsQueue;
39
2c2f900e 40 private final CtfTmfTrace trace;
dc0f7bfe 41 private final CtfKernelHandler eventHandler;
efc403bb
AM
42
43 private final Thread eventHandlerThread;
44
45 private boolean ssAssigned;
46
47 /**
48 * Instantiate a new state provider plugin.
49 *
50 * @param traceFile
51 * The LTTng 2.0 kernel trace directory
52 * @throws IOException
53 * If the directory was not found, or not recognized as a CTF
54 * trace.
55 */
dc0f7bfe 56 public CtfKernelStateInput(CtfTmfTrace trace) {
efc403bb 57 eventsQueue = new ArrayBlockingQueue<CtfTmfEvent>(EVENTS_QUEUE_SIZE);
2c2f900e 58 this.trace = trace;
dc0f7bfe 59 eventHandler = new CtfKernelHandler(eventsQueue);
efc403bb
AM
60 ssAssigned = false;
61
62 eventHandlerThread = new Thread(eventHandler,
63 "CTF Kernel Event Handler"); //$NON-NLS-1$
64 }
65
66 @Override
2c2f900e
AM
67 public CtfTmfTrace getTrace() {
68 return trace;
69 }
efc403bb 70
2c2f900e
AM
71 @Override
72 public long getStartTime() {
73 return trace.getStartTime().getValue();
74 }
efc403bb 75
2c2f900e
AM
76 @Override
77 public CtfTmfEvent getExpectedEventType() {
78 return CtfTmfEvent.getNullEvent();
efc403bb
AM
79 }
80
81 @Override
d26f90fd
AM
82 public void assignTargetStateSystem(IStateSystemBuilder ssb) {
83 eventHandler.assignStateSystem(ssb);
efc403bb 84 ssAssigned = true;
2c2f900e 85 eventHandlerThread.start();
efc403bb
AM
86 }
87
efc403bb 88 @Override
2c2f900e
AM
89 public void processEvent(ITmfEvent event) {
90 /* Make sure the target state system has been assigned */
91 if (!ssAssigned) {
92 System.err.println("Cannot process event without a target state system"); //$NON-NLS-1$
93 return;
94 }
95
96 /* Insert the event we're received into the events queue */
97 CtfTmfEvent currentEvent = (CtfTmfEvent) event;
98 try {
99 eventsQueue.put(currentEvent);
100 } catch (InterruptedException e) {
101 e.printStackTrace();
102 }
103 }
104
105 @Override
106 public void dispose() {
107 /* Insert a null event in the queue to stop the event handler's thread. */
108 try {
109 eventsQueue.put(CtfTmfEvent.getNullEvent());
110 eventHandlerThread.join();
111 } catch (InterruptedException e) {
112 e.printStackTrace();
113 }
114 ssAssigned = false;
115 eventHandler.assignStateSystem(null);
efc403bb
AM
116 }
117}
This page took 0.033297 seconds and 5 git commands to generate.