aboutsummaryrefslogtreecommitdiffstats
path: root/win/rfb_win32/Threading.cxx
blob: 79607662dc743d63d8d70a5348caa0d444ca8452 (plain)
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
/* Copyright (C) 2002-2005 RealVNC Ltd.  All Rights Reserved.
 * 
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this software; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
 * USA.
 */

// -=- Threading.cxx
// Win32 Threading interface implementation

#include <malloc.h>

#include <rdr/Exception.h>
#include <rfb/LogWriter.h>
#include <rfb/util.h>
#include <rfb_win32/Threading.h>

using namespace rfb;

static LogWriter vlog("Threading");

static DWORD threadStorage = TlsAlloc();


inline void logAction(Thread* t, const char* action) {
  vlog.debug("%-16.16s %s(%lx)", action, t->getName(), t);
}

inline void logError(Thread* t, const char* err) {
  vlog.error("%-16.16s %s(%lx):%s", "failed", t->getName(), t, err);
}


DWORD WINAPI
Thread::threadProc(LPVOID lpParameter) {
  Thread* thread = (Thread*) lpParameter;
  TlsSetValue(threadStorage, thread);
  logAction(thread, "started");
  try {
    thread->run();
    logAction(thread, "stopped");
  } catch (rdr::Exception& e) {
    logError(thread, e.str());
  }
  bool deleteThread = false;
  {
    Lock l(thread->mutex);
    thread->state = ThreadStopped;
    thread->sig->signal();
    deleteThread = thread->deleteAfterRun;
  }
  if (deleteThread)
    delete thread;
  return 0;
}

Thread::Thread(const char* name_) : name(strDup(name_ ? name_ : "Unnamed")), sig(0), deleteAfterRun(false) {
  sig = new Condition(mutex);
  cond_event.h = CreateEvent(NULL, TRUE, FALSE, NULL);
  thread.h = CreateThread(NULL, 0, threadProc, this, CREATE_SUSPENDED, &thread_id);
  state = ThreadCreated;
  logAction(this, "created");
}

Thread::Thread(HANDLE thread_, DWORD thread_id_) : 
  thread(thread_), thread_id(thread_id_), name(strDup("Native")), sig(0), deleteAfterRun(false) {
  cond_event.h = CreateEvent(NULL, TRUE, FALSE, NULL);
  state = ThreadNative;
  logAction(this, "created");
}

Thread::~Thread() {
  logAction(this, "destroying");
  if (!deleteAfterRun && state != ThreadNative)
    this->join();
  if (sig)
    delete sig;
  logAction(this, "destroyed");
}

void
Thread::run() {
}

void
Thread::start() {
  Lock l(mutex);
  if (state == ThreadCreated) {
    state = ThreadStarted;
    sig->signal();
    ResumeThread(thread);
  }
}

Thread*
Thread::join() {
  if (deleteAfterRun)
    throw rdr::Exception("attempt to join() with deleteAfterRun thread");
  Lock l(mutex);
  if (state == ThreadJoined) {
    logAction(this, "already joined");
  } else {
    logAction(this, "joining");
    while (state == ThreadStarted) {
      sig->wait();
      logAction(this, "checking");
    }
    state = ThreadJoined;
    logAction(this, "joined");
  }
  return this;
}

const char*
Thread::getName() const {
  return name.buf;
}

ThreadState
Thread::getState() const {
  return state;
}

unsigned long
Thread::getThreadId() const {
  return thread_id;
}


Thread*
Thread::self() {
  Thread* thread = (Thread*) TlsGetValue(threadStorage);
  if (!thread) {
    // *** memory leak - could use GetExitCodeThread to lazily detect when
    //     to clean up native thread objects
    thread = new Thread(GetCurrentThread(), GetCurrentThreadId());
    TlsSetValue(threadStorage, thread);
  }
  return thread;
}