File: gsThreadLinux.c

package info (click to toggle)
openmohaa 0.81.1%2Bdfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: trixie
  • size: 29,124 kB
  • sloc: ansic: 270,865; cpp: 250,173; sh: 234; asm: 141; xml: 64; makefile: 7
file content (212 lines) | stat: -rw-r--r-- 5,769 bytes parent folder | download | duplicates (2)
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//
// Linux Threading Support (pthreads)
// 
// NOTE: when implementing this make sure the "-lpthread" compiler option is used
//
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
#include "../gsPlatformUtil.h"
#include "../gsPlatformThread.h"
#include "../gsAssert.h"
#include "../gsDebug.h"
#include <pthread.h>

#define _REENTRANT

#define PTHREAD_NO_ERROR 0


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// These functions are unsupported in the current version of the SDK

gsi_u32 gsiInterlockedIncrement(gsi_u32 * value)
{
	GS_ASSERT_STR(gsi_false, "gsiInterlockIncrement is unsupported for LINUX in the current version of the SDK\n");
	return 1;
}

gsi_u32 gsiInterlockedDecrement(gsi_u32 * value)
{
	GS_ASSERT_STR(gsi_false, "gsiInterlockIncrement is unsupported for LINUX in the current version of the SDK\n");
	return 1;
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
int gsiStartThread(GSThreadFunc func, gsi_u32 theStackSize, void *arg, GSIThreadID * id)
{
	pthread_attr_init(&id->attr);
	pthread_attr_setstacksize(&id->attr, theStackSize);

	if (pthread_create(&id->thread, &id->attr, (void *)func, arg) != PTHREAD_NO_ERROR)
	{
		gsDebugFormat(GSIDebugCat_Common, GSIDebugType_Misc, GSIDebugLevel_WarmError,
			"Failed to create thread\r\n");
		return -1;
	}

	return 0;
}

void gsiCancelThread(GSIThreadID id)
{
	//should i destroy the attributes here?
	pthread_attr_destroy(&id.attr);

	if (pthread_cancel(id.thread) != PTHREAD_NO_ERROR) {
		//there was an error - how should we handle these? or should we?
		gsDebugFormat(GSIDebugCat_Common, GSIDebugType_Misc, GSIDebugLevel_WarmError,
			"Failed to cancel thread\r\n");
	}
	//free up memory and set to NULL

	gsifree(&id.thread);
}

// This must be called from INSIDE the thread you wish to exit
void gsiExitThread(GSIThreadID id)
{
	// detach the thread so that it knows to free resources upon exit
	pthread_detach(id.thread);
	
	// exit thread to free up resources
	pthread_exit(NULL);
}

void gsiCleanupThread(GSIThreadID id)
{
	// destroy any leftover attributes associated with the thread
	pthread_attr_destroy(&id.attr);
}

gsi_u32 gsiHasThreadShutdown(GSIThreadID id) 
{ 
	// pthreads lacks detection mechanism for this
	GSI_UNUSED(id);
	return 1;
}

void gsiInitializeCriticalSection(GSICriticalSection *theCrit)
{
	if (pthread_mutex_init(theCrit, NULL) != PTHREAD_NO_ERROR)
	{
		gsDebugFormat(GSIDebugCat_Common, GSIDebugType_Misc, GSIDebugLevel_WarmError,
			"Failed to initialize critical section\r\n");
	}
}

void gsiEnterCriticalSection(GSICriticalSection *theCrit)
{
	if (pthread_mutex_lock(theCrit) != PTHREAD_NO_ERROR)
	{
		gsDebugFormat(GSIDebugCat_Common, GSIDebugType_Misc, GSIDebugLevel_WarmError,
			"Failed to lock mutex for entering critical section\r\n");
	}
}

void gsiLeaveCriticalSection(GSICriticalSection *theCrit)
{
	if (pthread_mutex_unlock(theCrit) != PTHREAD_NO_ERROR)
	{
		gsDebugFormat(GSIDebugCat_Common, GSIDebugType_Misc, GSIDebugLevel_WarmError,
			"Failed to unlock mutex for leaving critical section\r\n");
	}
}

void gsiDeleteCriticalSection(GSICriticalSection *theCrit)
{
	if (pthread_mutex_destroy(theCrit) != PTHREAD_NO_ERROR)
	{
		gsDebugFormat(GSIDebugCat_Common, GSIDebugType_Misc, GSIDebugLevel_WarmError,
			"Failed to destroy mutex\r\n");
	}
	theCrit = NULL;
}

GSISemaphoreID gsiCreateSemaphore(gsi_i32 theInitialCount, gsi_i32 theMaxCount, char* theName)
{
	int result;
	GSISemaphoreID semaphore;

	//we can use the default attributes for the mutex by passing NULL
	result = pthread_mutex_init(&semaphore.mLock, NULL);

	if (result != PTHREAD_NO_ERROR)
	{
		gsDebugFormat(GSIDebugCat_Common, GSIDebugType_Misc, GSIDebugLevel_WarmError,
			"Failed to create semaphore\r\n");
	}

	semaphore.mValue = theInitialCount;
	semaphore.mMax = theMaxCount;

	GSI_UNUSED(theName);
	return semaphore;
}

// Waits for -- and signals -- the semaphore
gsi_u32 gsiWaitForSemaphore(GSISemaphoreID theSemaphore, gsi_u32 theTimeoutMs)
{
	gsi_time startTime = current_time();
	gsi_bool infinite = (theTimeoutMs == GSI_INFINITE)?gsi_true:gsi_false;

	do
	{
		//try to lock, if it doesn't then its busy
		if(pthread_mutex_trylock(&theSemaphore.mLock) == PTHREAD_NO_ERROR)
		{
			if(theSemaphore.mValue > 0)
			{
				theSemaphore.mValue--;
				pthread_mutex_unlock(&theSemaphore.mLock);
				return 1;
			}

			pthread_mutex_unlock(&theSemaphore.mLock);
		}

		if(theTimeoutMs != 0)
			msleep(2);

	} while(gsi_is_true(infinite) || ((current_time() - startTime) < theTimeoutMs));

	return 0;
}

// Allow other objects to access the semaphore
void gsiReleaseSemaphore(GSISemaphoreID theSemaphore, gsi_i32 theReleaseCount)
{
	if(pthread_mutex_trylock(&theSemaphore.mLock) != PTHREAD_NO_ERROR)
	{
		gsDebugFormat(GSIDebugCat_Common, GSIDebugType_Misc, GSIDebugLevel_WarmError,
			"Failed to lock semaphore\r\n");

		GSI_UNUSED(theReleaseCount);
	}
	else
	{
		theSemaphore.mValue += theReleaseCount;
		if(theSemaphore.mValue > theSemaphore.mMax)
			theSemaphore.mValue = theSemaphore.mMax;

		pthread_mutex_unlock(&theSemaphore.mLock);
	}
}

void gsiCloseSemaphore(GSISemaphoreID theSemaphore)
{
	if (pthread_mutex_destroy(&theSemaphore.mLock) != PTHREAD_NO_ERROR)
	{
		gsDebugFormat(GSIDebugCat_Common, GSIDebugType_Misc, GSIDebugLevel_WarmError,
			"Failed to destroy semaphore\r\n");
		GSI_UNUSED(theSemaphore);
	}

	//need to free up memory
	gsifree(&theSemaphore.mValue);
	gsifree(&theSemaphore.mMax);
	gsifree(&theSemaphore);
}