File: gsThreadPs2.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 (204 lines) | stat: -rw-r--r-- 4,661 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
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
#include "../gscommon.h"


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
gsi_u32 gsiInterlockedIncrement(gsi_u32 * value)
{
	int interrupt = DI();
	int ret = ++(*value);
	if (interrupt)
		EI();

	// return "ret" rather than "value" here b/c
	// value may be modified by another thread 
	// before we can return it
	return ret;
}

gsi_u32 gsiInterlockedDecrement(gsi_u32 * value)
{
	int interrupt = DI();
	int ret = --(*value);
	if (interrupt)
		EI();

	// return "ret" rather than "value" here b/c
	// value may be modified by another thread 
	// before we can return it
	return ret;
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
void gsiInitializeCriticalSection(GSICriticalSection *theCrit) 
{
	theCrit->mSemaphore = gsiCreateSemaphore(1, 1, NULL); 
	theCrit->mOwnerThread = 0;
	theCrit->mEntryCount = 0;
}
void gsiEnterCriticalSection(GSICriticalSection *theCrit) 
{ 
	// If we're not already in it, wait for it
	if (GetThreadId() != theCrit->mOwnerThread)
	{
		gsiWaitForSemaphore(theCrit->mSemaphore, 0);
		theCrit->mOwnerThread = GetThreadId();
	}

	// Increment entry count
	theCrit->mEntryCount++;
}
void gsiLeaveCriticalSection(GSICriticalSection *theCrit)
{ 
	// We must be the owner? (assert?)
	if (GetThreadId() != theCrit->mOwnerThread)
	{
		assert(GetThreadId() == theCrit->mOwnerThread);
		return;
	}

	// Release semaphore
	theCrit->mEntryCount--;
	if (theCrit->mEntryCount == 0)
	{
		theCrit->mOwnerThread = 0;
		gsiReleaseSemaphore(theCrit->mSemaphore, 1);     
	}
}

void gsiDeleteCriticalSection(GSICriticalSection *theCrit) 
{ 
	gsiCloseSemaphore(theCrit->mSemaphore);       
}

gsi_u32 gsiHasThreadShutdown(GSIThreadID theThreadID) 
{ 
	struct ThreadParam aStatus;
	ReferThreadStatus(theThreadID, &aStatus);
	if (aStatus.status == THS_DORMANT)
		return 1; // dead
	else
		return 0; // still kicking;
}

GSISemaphoreID gsiCreateSemaphore(gsi_i32 theInitialCount, gsi_i32 theMaxCount, char* theName)
{
	struct SemaParam aParam;
	int aSemaphore = 0;

	aParam.initCount = theInitialCount;
	aParam.maxCount = theMaxCount;
	
	aSemaphore = CreateSema(&aParam);
	if (aSemaphore < 0)
	{
		gsDebugFormat(GSIDebugCat_Common, GSIDebugType_Misc, GSIDebugLevel_WarmError,
			"Failed to create semaphore\r\n");
	}
	
	GSI_UNUSED(theName);
	
	return aSemaphore;
}

gsi_u32 gsiWaitForSemaphore(GSISemaphoreID theSemaphore, gsi_u32 theTimeoutMs)
{
	int result = WaitSema(theSemaphore);
	return (gsi_u32)result;

	GSI_UNUSED(theTimeoutMs);
}

void gsiReleaseSemaphore(GSISemaphoreID theSemaphore, gsi_i32 theReleaseCount)
{
	while (theReleaseCount-- > 0)
		SignalSema(theSemaphore);
	//ReleaseSemaphore(theSemaphore, theReleaseCount, NULL);
}

void gsiCloseSemaphore(GSISemaphoreID theSemaphore)
{
	DeleteSema(theSemaphore);
}

int gsiStartThread(GSThreadFunc func,  gsi_u32 theStackSize, void *arg, GSIThreadID *id)
{
	const unsigned int stackSize = theStackSize;
	const int threadPriority = 3;
	struct ThreadParam param;
	void * stack;
	int threadID;

	// allocate a stack
	stack = gsimemalign(16, stackSize);
	if(!stack)
		return -1;

	// setup the thread parameters
	param.entry = func;
	param.stack = stack;
	param.stackSize = (int)stackSize;
	param.gpReg = &_gp;
	param.initPriority = threadPriority;

	// create the thread
	threadID = CreateThread(&param);
	if(threadID == -1)
	{
		gsifree(stack);
		return -1;
	}

	// start the thread
	if(StartThread(threadID, arg) == -1)
	{
		DeleteThread(threadID);
		gsifree(stack);
		return -1;
	}

	// store the id
	*id = threadID;

	// Note:  This was added to prevent PS2 lockups when starting multiple threads
	//        The PS2 would block for approx 5 seconds
	msleep(1);

	return 0;
}

void gsiCancelThread(GSIThreadID id)
{
	void* aStack = NULL;

	// get the stack ptr
	struct ThreadParam aThreadParam;
	ReferThreadStatus(id, &aThreadParam);
	aStack = (void*)aThreadParam.stack;

	// terminate the thread
	TerminateThread(id);

	// delete the thread
	DeleteThread(id);

	//free the stack
	gsifree(aStack);
}

// This must be called from INSIDE the thread you wish to exit
void gsiExitThread(GSIThreadID id)
{
    // TODO: does PS2 need to explicitly EXIT a thread like win32/linux?
	GSI_UNUSED(id);
}

void gsiCleanupThread(GSIThreadID id)
{
	// same as cancel (terminates just to be sure)
	gsiCancelThread(id);
}