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

« back to all changes in this revision

Viewing changes to src/thread_os2.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_os2.cpp 15764 2009-03-18 19:47:05Z rubidium $ */
 
2
 
 
3
/** @file thread_os2.cpp OS/2 implementation of Threads. */
 
4
 
 
5
#include "stdafx.h"
 
6
#include "thread.h"
 
7
 
 
8
#define INCL_DOS
 
9
#include <os2.h>
 
10
#include <process.h>
 
11
 
 
12
/**
 
13
 * OS/2 version for ThreadObject.
 
14
 */
 
15
class ThreadObject_OS2 : public ThreadObject {
 
16
private:
 
17
        TID thread;          ///< System thread identifier.
 
18
        OTTDThreadFunc proc; ///< External thread procedure.
 
19
        void *param;         ///< Parameter for the external thread procedure.
 
20
        bool self_destruct;  ///< Free ourselves when done?
 
21
 
 
22
public:
 
23
        /**
 
24
         * Create a thread and start it, calling proc(param).
 
25
         */
 
26
        ThreadObject_OS2(OTTDThreadFunc proc, void *param, bool self_destruct) :
 
27
                thread(0),
 
28
                proc(proc),
 
29
                param(param),
 
30
                self_destruct(self_destruct)
 
31
        {
 
32
                thread = _beginthread(stThreadProc, NULL, 32768, this);
 
33
        }
 
34
 
 
35
        /* virtual */ bool Exit()
 
36
        {
 
37
                _endthread();
 
38
                return true;
 
39
        }
 
40
 
 
41
        /* virtual */ void Join()
 
42
        {
 
43
                DosWaitThread(&this->thread, DCWW_WAIT);
 
44
                this->thread = 0;
 
45
        }
 
46
private:
 
47
        /**
 
48
         * On thread creation, this function is called, which calls the real startup
 
49
         *  function. This to get back into the correct instance again.
 
50
         */
 
51
        static void stThreadProc(void *thr)
 
52
        {
 
53
                ((ThreadObject_OS2 *)thr)->ThreadProc();
 
54
        }
 
55
 
 
56
        /**
 
57
         * A new thread is created, and this function is called. Call the custom
 
58
         *  function of the creator of the thread.
 
59
         */
 
60
        void ThreadProc()
 
61
        {
 
62
                /* Call the proc of the creator to continue this thread */
 
63
                try {
 
64
                        this->proc(this->param);
 
65
                } catch (OTTDThreadExitSignal e) {
 
66
                } catch (...) {
 
67
                        NOT_REACHED();
 
68
                }
 
69
 
 
70
                if (self_destruct) {
 
71
                        this->Exit();
 
72
                        delete this;
 
73
                }
 
74
        }
 
75
};
 
76
 
 
77
/* static */ bool ThreadObject::New(OTTDThreadFunc proc, void *param, ThreadObject **thread)
 
78
{
 
79
        ThreadObject *to = new ThreadObject_OS2(proc, param, thread == NULL);
 
80
        if (thread != NULL) *thread = to;
 
81
        return true;
 
82
}
 
83
 
 
84
/**
 
85
 * OS/2 version of ThreadMutex.
 
86
 */
 
87
class ThreadMutex_OS2 : public ThreadMutex {
 
88
private:
 
89
        HMTX mutex;
 
90
 
 
91
public:
 
92
        ThreadMutex_OS2()
 
93
        {
 
94
                DosCreateMutexSem(NULL, &mutex, 0, FALSE);
 
95
        }
 
96
 
 
97
        /* virtual */ ~ThreadMutex_OS2()
 
98
        {
 
99
                DosCloseMutexSem(mutex);
 
100
        }
 
101
 
 
102
        /* virtual */ void BeginCritical()
 
103
        {
 
104
                DosRequestMutexSem(mutex, (unsigned long) SEM_INDEFINITE_WAIT);
 
105
        }
 
106
 
 
107
        /* virtual */ void EndCritical()
 
108
        {
 
109
                DosReleaseMutexSem(mutex);
 
110
        }
 
111
};
 
112
 
 
113
/* static */ ThreadMutex *ThreadMutex::New()
 
114
{
 
115
        return new ThreadMutex_OS2();
 
116
}