summaryrefslogtreecommitdiffstats
path: root/mcop/thread.cc
blob: 847a0f0a1e24dfc92d5c1230b5124f299171b9fb (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
    /*

    Copyright (C) 2001 Stefan Westerfeld
                       [email protected]

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.
  
    This library 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
    Library General Public License for more details.
   
    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.

    */

#include "thread.h"
#include <string.h>

using namespace Arts;

// Thread:
Thread::~Thread()
{
	delete impl;
}

Thread_impl::~Thread_impl()
{
}

// Mutex:
Mutex::~Mutex()
{
	delete impl;
}

Mutex_impl::~Mutex_impl()
{
}

// ThreadCondition:

ThreadCondition::~ThreadCondition()
{
	delete impl;
}

ThreadCondition_impl::~ThreadCondition_impl()
{
}

// Semaphore
Semaphore::~Semaphore()
{
	delete impl;
}

Semaphore_impl::~Semaphore_impl()
{
}

// No threading:

namespace Arts {

class SystemThreadsNoMutex_impl : public Mutex_impl {
public:
	void lock() {};
	bool tryLock() { return true; };
	void unlock() {};
};

static Thread *systemThreadsNoneCurrent = 0;

class SystemThreadsNoThread_impl : public Thread_impl {
private:
	Thread *thread;
public:
	SystemThreadsNoThread_impl(Thread *thread) : thread(thread) {}
	void setPriority(int) {}
	void start() {
		Thread *oldCurrent = systemThreadsNoneCurrent;

		systemThreadsNoneCurrent = thread;
		thread->run();
		systemThreadsNoneCurrent = oldCurrent;
	}
	void waitDone() {}
};

class SystemThreadsNoThreadCondition_impl : public ThreadCondition_impl {
public:
	void wakeOne() {};
	void wakeAll() {};
	void wait(Mutex_impl *) {};
};

class SystemThreadsNoSemaphore_impl : public Semaphore_impl {
public:
	void wait() {}
	int tryWait() { return 0; }
	void post() {}
	int getValue() { return 0; }
};

class SystemThreadsNone : public SystemThreads {
public:
	bool isMainThread() {
		return (systemThreadsNoneCurrent == 0);
	}
	Mutex_impl *createMutex_impl() {
		return new SystemThreadsNoMutex_impl();
	}
	Mutex_impl *createRecMutex_impl() {
		return new SystemThreadsNoMutex_impl();
	}
	Thread_impl *createThread_impl(Thread *thread) {
		return new SystemThreadsNoThread_impl(thread);
	}
	ThreadCondition_impl *createThreadCondition_impl() {
		return new SystemThreadsNoThreadCondition_impl();
	}
	Thread *getCurrentThread() {
		return systemThreadsNoneCurrent;
	}
	Semaphore_impl *createSemaphore_impl(int, int) {
		return new SystemThreadsNoSemaphore_impl();
	}
};

}

static SystemThreadsNone systemThreadsNone;
static SystemThreads *SystemThreads_the = 0;

SystemThreads *SystemThreads::the()
{
	if(!SystemThreads_the) return &systemThreadsNone;
	return SystemThreads_the;
}

bool SystemThreads::init(SystemThreads *the)
{
	if(SystemThreads_the)
		return false;
	else
	{
		SystemThreads_the = the;
		return true;
	}
}

bool SystemThreads::supported()
{
	return (SystemThreads_the != 0);
}

SystemThreads::~SystemThreads()
{
}

namespace Arts { void ARTS_EXPORT *gslGlobalMutexTable = 0; }