File: SingleThreadedApplication.C

package info (click to toggle)
witty 3.3.3%2Bdfsg-4.1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 28,228 kB
  • ctags: 26,694
  • sloc: cpp: 147,809; ansic: 77,999; xml: 16,331; sh: 1,303; makefile: 198; java: 86; sql: 14
file content (162 lines) | stat: -rw-r--r-- 3,671 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
 * Copyright (C) 2014 Emweb bvba, Heverlee, Belgium.
 *
 * See the LICENSE file for terms of use.
 */

#include "SingleThreadedApplication.h"

SingleThreadedApplication::
SingleThreadedApplication(const Wt::WEnvironment& env)
  : Wt::WApplication(env),
    finalized_(false),
    exception_(false),
    event_(0),
    done_(false),
    newEvent_(false)
{ }

void SingleThreadedApplication::initialize()
{
  log("debug") << "STA" << ": initialize()";
  Wt::WApplication::initialize();

  create();
}

void SingleThreadedApplication::finalize()
{
  log("debug") << "STA" << ": finalize()";
  Wt::WApplication::finalize();

  destroy();

  finalized_ = true;
}

void SingleThreadedApplication::notify(const Wt::WEvent& event)
{
  if (appThread_.get_id() == boost::thread::id()) { // not-a-thread
    done_ = false;
    log("debug") << "STA" << ": starting thread";
    appThread_ = boost::thread(boost::bind(&SingleThreadedApplication::run,
					   this));
    waitDone();
  }

  if (boost::this_thread::get_id() == appThread_.get_id()) {
    /* This could be from within a recursive event loop */
    log("debug") << "STA" << ": notify() called within app thread";
    threadNotify(event);
    return;
  }

  if (event.eventType() == Wt::ResourceEvent) {
    /*
     * We do not relay resource events since these will not unlock
     * a recursive event loop and thus we cannot communicate with the
     * private thread when it's blocked in a recursive event loop
     */
    log("debug") << "STA" << ": notify() for resource, handling in thread pool.";
    threadNotify(event);
    return;
  }

  event_ = &event;

  done_ = false;
  {
    log("debug") << "STA" << ": notifying thread";
    boost::mutex::scoped_lock lock(newEventMutex_);
    newEvent_ = true;
    newEventCondition_.notify_one();
  }

  waitDone();

  if (exception_) {
    exception_ = false;
    throw std::runtime_error("STA: rethrowing exception");
  }

  if (finalized_) {
    log("debug") << "STA" << ": joining thread";
    appThread_.join();
    appThread_ = boost::thread();
  }
}

void SingleThreadedApplication::waitDone()
{
  log("debug") << "STA" << ": waiting for event done";
  boost::mutex::scoped_lock lock(doneMutex_);

  while (!done_)
    doneCondition_.wait(lock);
}

void SingleThreadedApplication::run()
{
  signalDone();

  boost::mutex::scoped_lock lock(newEventMutex_);
  eventLock_ = &lock;

  for (;;) {
    if (!newEvent_) {
      log("debug") << "STA" << ": [thread] waiting for event";
      newEventCondition_.wait(lock);
    }

    log("debug") << "STA" << ": [thread] handling event";
    attachThread(true);
    try {
      threadNotify(*event_);
    } catch (std::exception& e) {
      log("error") << "STA" << ": [thread] Caught exception: " << e.what();
      exception_ = true;
    } catch (...) {
      log("error") << "STA" << ": [thread] Caught exception";
      exception_ = true;
    }
    attachThread(false);
    signalDone();

    if (finalized_)
      break;

    newEvent_ = false;
  }

  signalDone();
}

void SingleThreadedApplication::threadNotify(const Wt::WEvent& event)
{
  Wt::WApplication::notify(event);
}

void SingleThreadedApplication::waitForEvent()
{
  log("debug") << "STA" << ": [thread] waitForEvent()";

  eventLock_->unlock();
  try {
    Wt::WApplication::waitForEvent();
  } catch (...) {
    eventLock_->lock();
    throw;
  }

  eventLock_->lock();

  log("debug") << "STA" << ": [thread] returning from waitForEvent()";
}

void SingleThreadedApplication::signalDone()
{
  log("debug") << "STA" << ": [thread] signaling event done";
  boost::mutex::scoped_lock lock(doneMutex_);
  done_ = true;
  doneCondition_.notify_one();
}