xrubythread.cpp
1 /***************************************************************************
2  Copyright (C) 2002-2015 Kentaro Kitagawa
3  kitagawa@phys.s.u-tokyo.ac.jp
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  You should have received a copy of the GNU Library General
11  Public License and a list of authors along with this program;
12  see the files COPYING and AUTHORS.
13 ***************************************************************************/
14 #include "xrubythread.h"
15 
16 XRubyThread::XRubyThread(const char *name, bool runtime, const XString &filename)
17  : XNode(name, runtime),
18  m_filename(create<XStringNode>("Filename", true)),
19  m_status(create<XStringNode>("Status", true)),
20  m_action(create<XStringNode>("Action", true)),
21  m_lineinput(create<XStringNode>("LineInput", true)),
22  m_threadID(create<XLongNode>("ThreadID", true)) {
23 
24  iterate_commit([=](Transaction &tr){
25  tr[ *m_threadID] = -1;
26  tr[ *m_filename] = filename;
27  tr[ *m_action] = RUBY_THREAD_ACTION_STARTING;
28  tr[ *m_status] = RUBY_THREAD_STATUS_STARTING;
29  tr[ *lineinput()].setUIEnabled(false);
30 
31  m_lsnOnLineChanged = tr[ *lineinput()].onValueChanged().connectWeakly(shared_from_this(),
32  &XRubyThread::onLineChanged);
33  });
34 }
35 
36 bool
37 XRubyThread::isRunning() const {
38  return (( **m_status)->to_str() == RUBY_THREAD_STATUS_RUN);
39 }
40 bool
41 XRubyThread::isAlive() const {
42  return (( **m_status)->to_str() != RUBY_THREAD_STATUS_N_A);
43 }
44 void
45 XRubyThread::kill() {
46  trans( *m_action) = RUBY_THREAD_ACTION_KILL;
47  trans( *lineinput()).setUIEnabled(false);
48 }
49 void
50 XRubyThread::resume() {
51  trans( *m_action) = RUBY_THREAD_ACTION_WAKEUP;
52 }
53 void
54 XRubyThread::onLineChanged(const Snapshot &shot, XValueNodeBase *) {
55  XString line = shot[ *lineinput()];
56  XScopedLock<XMutex> lock(m_lineBufferMutex);
57  m_lineBuffer.push_back(line);
58  iterate_commit([=](Transaction &tr){
59  tr[ *lineinput()] = "";
60  tr.unmark(m_lsnOnLineChanged);
61  });
62 }
63 
64 XString
66  XScopedLock<XMutex> lock(m_lineBufferMutex);
67  if( !m_lineBuffer.size()) {
68  lineinput()->setUIEnabled(true);
69  return XString();
70  }
71  XString line = m_lineBuffer.front();
72  m_lineBuffer.pop_front();
73 // lineinput()->setUIEnabled(false);
74  return line + "\n";
75 }

Generated for KAME4 by  doxygen 1.8.3