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

« back to all changes in this revision

Viewing changes to src/thread_pthread.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_pthread.cpp 15663 2009-03-10 11:12:03Z rubidium $ */
 
2
 
 
3
/** @file thread_pthread.cpp POSIX pthread implementation of Threads. */
 
4
 
 
5
#include "stdafx.h"
 
6
#include "thread.h"
 
7
#include <pthread.h>
 
8
 
 
9
/**
 
10
 * POSIX pthread version for ThreadObject.
 
11
 */
 
12
class ThreadObject_pthread : public ThreadObject {
 
13
private:
 
14
        pthread_t thread;    ///< System thread identifier.
 
15
        OTTDThreadFunc proc; ///< External thread procedure.
 
16
        void *param;         ///< Parameter for the external thread procedure.
 
17
        bool self_destruct;  ///< Free ourselves when done?
 
18
 
 
19
public:
 
20
        /**
 
21
         * Create a pthread and start it, calling proc(param).
 
22
         */
 
23
        ThreadObject_pthread(OTTDThreadFunc proc, void *param, bool self_destruct) :
 
24
                thread(0),
 
25
                proc(proc),
 
26
                param(param),
 
27
                self_destruct(self_destruct)
 
28
        {
 
29
                pthread_create(&this->thread, NULL, &stThreadProc, this);
 
30
        }
 
31
 
 
32
        /* virtual */ bool Exit()
 
33
        {
 
34
                assert(pthread_self() == this->thread);
 
35
                /* For now we terminate by throwing an error, gives much cleaner cleanup */
 
36
                throw OTTDThreadExitSignal();
 
37
        }
 
38
 
 
39
        /* virtual */ void Join()
 
40
        {
 
41
                /* You cannot join yourself */
 
42
                assert(pthread_self() != this->thread);
 
43
                pthread_join(this->thread, NULL);
 
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_pthread *)thr)->ThreadProc();
 
54
                pthread_exit(NULL);
 
55
        }
 
56
 
 
57
        /**
 
58
         * A new thread is created, and this function is called. Call the custom
 
59
         *  function of the creator of the thread.
 
60
         */
 
61
        void ThreadProc()
 
62
        {
 
63
                /* Call the proc of the creator to continue this thread */
 
64
                try {
 
65
                        this->proc(this->param);
 
66
                } catch (OTTDThreadExitSignal e) {
 
67
                } catch (...) {
 
68
                        NOT_REACHED();
 
69
                }
 
70
 
 
71
                if (self_destruct) {
 
72
                        pthread_detach(pthread_self());
 
73
                        delete this;
 
74
                }
 
75
        }
 
76
};
 
77
 
 
78
/* static */ bool ThreadObject::New(OTTDThreadFunc proc, void *param, ThreadObject **thread)
 
79
{
 
80
        ThreadObject *to = new ThreadObject_pthread(proc, param, thread == NULL);
 
81
        if (thread != NULL) *thread = to;
 
82
        return true;
 
83
}
 
84
 
 
85
/**
 
86
 * POSIX pthread version of ThreadMutex.
 
87
 */
 
88
class ThreadMutex_pthread : public ThreadMutex {
 
89
private:
 
90
        pthread_mutex_t mutex;
 
91
 
 
92
public:
 
93
        ThreadMutex_pthread()
 
94
        {
 
95
                pthread_mutex_init(&this->mutex, NULL);
 
96
        }
 
97
 
 
98
        /* virtual */ ~ThreadMutex_pthread()
 
99
        {
 
100
                pthread_mutex_destroy(&this->mutex);
 
101
        }
 
102
 
 
103
        /* virtual */ void BeginCritical()
 
104
        {
 
105
                pthread_mutex_lock(&this->mutex);
 
106
        }
 
107
 
 
108
        /* virtual */ void EndCritical()
 
109
        {
 
110
                pthread_mutex_unlock(&this->mutex);
 
111
        }
 
112
};
 
113
 
 
114
/* static */ ThreadMutex *ThreadMutex::New()
 
115
{
 
116
        return new ThreadMutex_pthread();
 
117
}