~ubuntu-branches/debian/squeeze/openttd/squeeze

« back to all changes in this revision

Viewing changes to src/thread_win32.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jordi Mallach, Matthijs Kooijman, Jordi Mallach
  • Date: 2009-04-15 18:22:10 UTC
  • mfrom: (1.1.6 upstream) (2.1.3 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090415182210-22ktb8kdbp2tf3bm
[ Matthijs Kooijman ]
* New upstream release.
* Remove Debian specific desktop file, upstream provides one now. 
* Add debian/watch file.

[ Jordi Mallach ]
* Bump Standards-Version to 3.8.1, with no changes required.
* Move to debhelper compat 7. Bump Build-Depends accordingly.
* Use dh_prep.
* Add "set -e" to config script.
* Remove a few extra doc files that get installed by upstream Makefile.
* Add more complete copyright information.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: thread_win32.cpp 15159 2009-01-20 03:44:43Z rubidium $ */
 
2
 
 
3
/** @file thread_win32.cpp Win32 thread implementation of Threads. */
 
4
 
 
5
#include "stdafx.h"
 
6
#include "thread.h"
 
7
#include "debug.h"
 
8
#include "core/alloc_func.hpp"
 
9
#include <stdlib.h>
 
10
#include <windows.h>
 
11
#include <process.h>
 
12
 
 
13
/**
 
14
 * Win32 thread version for ThreadObject.
 
15
 */
 
16
class ThreadObject_Win32 : public ThreadObject {
 
17
private:
 
18
        HANDLE thread;       ///< System thread identifier.
 
19
        uint id;             ///< Thread identifier.
 
20
        OTTDThreadFunc proc; ///< External thread procedure.
 
21
        void *param;         ///< Parameter for the external thread procedure.
 
22
        bool self_destruct;  ///< Free ourselves when done?
 
23
 
 
24
public:
 
25
        /**
 
26
         * Create a win32 thread and start it, calling proc(param).
 
27
         */
 
28
        ThreadObject_Win32(OTTDThreadFunc proc, void *param, bool self_destruct) :
 
29
                thread(NULL),
 
30
                id(0),
 
31
                proc(proc),
 
32
                param(param),
 
33
                self_destruct(self_destruct)
 
34
        {
 
35
                this->thread = (HANDLE)_beginthreadex(NULL, 0, &stThreadProc, this, CREATE_SUSPENDED, &this->id);
 
36
                if (this->thread == NULL) return;
 
37
                ResumeThread(this->thread);
 
38
        }
 
39
 
 
40
        /* virtual */ ~ThreadObject_Win32()
 
41
        {
 
42
                if (this->thread != NULL) {
 
43
                        CloseHandle(this->thread);
 
44
                        this->thread = NULL;
 
45
                }
 
46
        }
 
47
 
 
48
        /* virtual */ bool Exit()
 
49
        {
 
50
                assert(GetCurrentThreadId() == this->id);
 
51
                /* For now we terminate by throwing an error, gives much cleaner cleanup */
 
52
                throw OTTDThreadExitSignal();
 
53
        }
 
54
 
 
55
        /* virtual */ void Join()
 
56
        {
 
57
                /* You cannot join yourself */
 
58
                assert(GetCurrentThreadId() != this->id);
 
59
                WaitForSingleObject(this->thread, INFINITE);
 
60
        }
 
61
 
 
62
private:
 
63
        /**
 
64
         * On thread creation, this function is called, which calls the real startup
 
65
         *  function. This to get back into the correct instance again.
 
66
         */
 
67
        static uint CALLBACK stThreadProc(void *thr)
 
68
        {
 
69
                ((ThreadObject_Win32 *)thr)->ThreadProc();
 
70
                return 0;
 
71
        }
 
72
 
 
73
        /**
 
74
         * A new thread is created, and this function is called. Call the custom
 
75
         *  function of the creator of the thread.
 
76
         */
 
77
        void ThreadProc()
 
78
        {
 
79
                try {
 
80
                        this->proc(this->param);
 
81
                } catch (OTTDThreadExitSignal) {
 
82
                } catch (...) {
 
83
                        NOT_REACHED();
 
84
                }
 
85
 
 
86
                if (self_destruct) delete this;
 
87
        }
 
88
};
 
89
 
 
90
/* static */ bool ThreadObject::New(OTTDThreadFunc proc, void *param, ThreadObject **thread)
 
91
{
 
92
        ThreadObject *to = new ThreadObject_Win32(proc, param, thread == NULL);
 
93
        if (thread != NULL) *thread = to;
 
94
        return true;
 
95
}
 
96
 
 
97
/**
 
98
 * Win32 thread version of ThreadMutex.
 
99
 */
 
100
class ThreadMutex_Win32 : public ThreadMutex {
 
101
private:
 
102
        CRITICAL_SECTION critical_section;
 
103
 
 
104
public:
 
105
        ThreadMutex_Win32()
 
106
        {
 
107
                InitializeCriticalSection(&this->critical_section);
 
108
        }
 
109
 
 
110
        /* virtual */ ~ThreadMutex_Win32()
 
111
        {
 
112
                DeleteCriticalSection(&this->critical_section);
 
113
        }
 
114
 
 
115
        /* virtual */ void BeginCritical()
 
116
        {
 
117
                EnterCriticalSection(&this->critical_section);
 
118
        }
 
119
 
 
120
        /* virtual */ void EndCritical()
 
121
        {
 
122
                LeaveCriticalSection(&this->critical_section);
 
123
        }
 
124
};
 
125
 
 
126
/* static */ ThreadMutex *ThreadMutex::New()
 
127
{
 
128
        return new ThreadMutex_Win32();
 
129
}