-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathosal.c
More file actions
805 lines (665 loc) · 17.8 KB
/
osal.c
File metadata and controls
805 lines (665 loc) · 17.8 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
/*
# _____ ___ ____ ___ ____
# ____| | ____| | | |____|
# | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
#-----------------------------------------------------------------------
# Copyright 2001-2004, ps2dev - http://www.ps2dev.org
# Licenced under Academic Free License version 2.0
# Review ps2sdk README & LICENSE files for further details.
*/
#include <ps2_osal.h>
#include <time.h>
#define MAX_PS2_UID 2048 // SWAG
#define DEFAULT_STACK_SIZE_BYTES 4096
#define PS2_MAX_TLS 32
#define MAX_THREADS 256
#if 0
#define PS2_DEBUG(x) printf(x)
#else
#define PS2_DEBUG(x)
#endif
#define POLLING_DELAY_IN_us 100
#ifdef F___threadInfo
struct OsalThreadInfo __threadInfo[MAX_THREADS];
#endif
/* TLS key used to access ps2ThreadData struct for reach thread. */
#ifdef F___threadDataKey
unsigned int __threadDataKey;
#else
extern unsigned int __threadDataKey;
#endif
extern void *__globalTls;
/*
* Data stored on a per-thread basis - allocated in pte_osThreadCreate
* and freed in pte_osThreadDelete.
*/
typedef struct ps2ThreadData
{
/* Entry point and parameters to thread's main function */
pte_osThreadEntryPoint entryPoint;
void * argv;
/* Semaphore used for cancellation. Posted to by pte_osThreadCancel,
polled in pte_osSemaphoreCancellablePend */
s32 cancelSem;
} ps2ThreadData;
/****************************************************************************
*
* Helper functions
*
***************************************************************************/
#ifdef F___getThreadData
ps2ThreadData *__getThreadData(s32 threadHandle)
{
ps2ThreadData *pThreadData;
void *pTls;
pTls = __getTlsStructFromThread(threadHandle);
pThreadData = (ps2ThreadData *) pteTlsGetValue(pTls, __threadDataKey);
return pThreadData;
}
#else
ps2ThreadData *__getThreadData(s32 threadHandle);
#endif
static inline void DelayThread(uint32_t usecs) {
struct timespec tv = {0};
tv.tv_sec = usecs / 1000000;
tv.tv_nsec = (usecs % 1000000) * 1000;
nanosleep(&tv, NULL);
}
static inline int SemWaitTimeout(s32 semHandle, uint32_t timeout)
{
int ret;
u64 timeoutUsec;
u64 *timeoutPtr;
if (timeout == 0) {
if (PollSema(semHandle) < 0) {
return -1;
}
return 0;
}
timeoutPtr = NULL;
if (timeout > 0 && timeout != UINT32_MAX) {
timeoutUsec = timeout * 1000;
timeoutPtr = &timeoutUsec;
}
ret = WaitSemaEx(semHandle, 1, timeoutPtr);
if (ret < 0)
return -2;
return 0; //Wait condition satisfied.
}
/* A new thread's stub entry point. It retrieves the real entry point from the per thread control
* data as well as any parameters to this function, and then calls the entry point.
*/
#ifdef F___ps2StubThreadEntry
int __ps2StubThreadEntry(void *argv)
{
int result;
ps2ThreadData *pThreadData;
pThreadData = __getThreadData(GetThreadId());
result = (*(pThreadData->entryPoint))(pThreadData->argv);
return result;
}
#else
extern int __ps2StubThreadEntry(void *argv);
#endif
/****************************************************************************
*
* Initialization
*
***************************************************************************/
#ifdef F_pte_osInit
pte_osResult pte_osInit(void)
{
ee_sema_t sema;
pte_osResult result;
ps2ThreadData *pThreadData;
/* Allocate and initialize TLS support */
result = pteTlsGlobalInit(PS2_MAX_TLS);
if (result == PTE_OS_OK) {
/* Allocate a key that we use to store control information (e.g. cancellation semaphore) per thread */
result = __pteTlsAlloc(&__threadDataKey);
if (result == PTE_OS_OK) {
/* Initialize the structure used to emulate TLS for
* non-POSIX threads
*/
__globalTls = pteTlsThreadInit();
/* Also create a "thread data" structure for a single non-POSIX thread. */
/* Allocate some memory for our per-thread control data. We use this for:
* 1. Entry point and parameters for the user thread's main function.
* 2. Semaphore used for thread cancellation.
*/
pThreadData = (ps2ThreadData *) malloc(sizeof(ps2ThreadData));
if (pThreadData == NULL) {
result = PTE_OS_NO_RESOURCES;
} else {
/* Save a pointer to our per-thread control data as a TLS value */
__pteTlsSetValue(__globalTls, __threadDataKey, pThreadData);
sema.init_count = 0;
sema.max_count = 255;
sema.option = 0;
pThreadData->cancelSem = CreateSema(&sema);
result = PTE_OS_OK;
}
}
}
return result;
}
#endif
#ifdef F_pte_osTerminate
pte_osResult pte_osTerminate(void) {
pteTlsGlobalDestroy();
return PTE_OS_OK;
}
#endif
/****************************************************************************
*
* Threads
*
***************************************************************************/
#ifdef F_pte_osThreadCreate
pte_osResult pte_osThreadCreate(pte_osThreadEntryPoint entryPoint,
int stackSize,
int initialPriority,
void *argv,
pte_osThreadHandle* ppte_osThreadHandle)
{
ee_thread_t eethread;
ee_sema_t sema;
struct OsalThreadInfo *threadInfo;
void *stack;
static int threadNum = 1;
void *pTls;
s32 threadId;
pte_osResult result;
ps2ThreadData *pThreadData;
if (threadNum++ > MAX_PS2_UID) {
threadNum = 0;
}
/* Make sure that the stack we're going to allocate is big enough */
if (stackSize < DEFAULT_STACK_SIZE_BYTES) {
stackSize = DEFAULT_STACK_SIZE_BYTES;
}
/* Allocate TLS structure for this thread. */
pTls = pteTlsThreadInit();
if (pTls == NULL) {
PS2_DEBUG("pteTlsThreadInit: PTE_OS_NO_RESOURCES\n");
result = PTE_OS_NO_RESOURCES;
goto FAIL0;
}
/* Allocate some memory for our per-thread control data. We use this for:
* 1. Entry point and parameters for the user thread's main function.
* 2. Semaphore used for thread cancellation.
*/
pThreadData = (ps2ThreadData *) malloc(sizeof(ps2ThreadData));
if (pThreadData == NULL) {
pteTlsThreadDestroy(pTls);
PS2_DEBUG("malloc(ps2ThreadData): PTE_OS_NO_RESOURCES\n");
result = PTE_OS_NO_RESOURCES;
goto FAIL0;
}
/* Save a pointer to our per-thread control data as a TLS value */
__pteTlsSetValue(pTls, __threadDataKey, pThreadData);
pThreadData->entryPoint = entryPoint;
pThreadData->argv = argv;
sema.init_count = 0;
sema.max_count = 255;
sema.option = 0;
pThreadData->cancelSem = CreateSema(&sema);
/* Create EE Thread */
stack = malloc(stackSize);
eethread.attr = 0;
eethread.option = 0;
eethread.func = &__ps2StubThreadEntry;
eethread.stack = stack;
eethread.stack_size = stackSize;
eethread.gp_reg = &_gp;
eethread.initial_priority = initialPriority;
threadId = CreateThread(&eethread);
/* In order to emulate TLS functionality, we append the address of the TLS structure that we
* allocated above to an additional struct. To set or get TLS values for this thread, the user
* needs to get the threadId from the OS and then extract
* a pointer to the TLS structure.
*/
threadInfo = &__threadInfo[threadId];
threadInfo->threadNumber = threadNum;
threadInfo->tlsPtr = pTls;
if (!stack) {
free(pThreadData);
pteTlsThreadDestroy(pTls);
PS2_DEBUG("CreateThread: PTE_OS_NO_RESOURCES\n");
result = PTE_OS_NO_RESOURCES;
} else if (threadId < 0) {
free(pThreadData);
pteTlsThreadDestroy(pTls);
PS2_DEBUG("CreateThread: PTE_OS_GENERAL_FAILURE\n");
result = PTE_OS_GENERAL_FAILURE;
} else {
*ppte_osThreadHandle = threadId;
result = PTE_OS_OK;
}
FAIL0:
return result;
}
#endif
#ifdef F_pte_osThreadStart
pte_osResult pte_osThreadStart(pte_osThreadHandle osThreadHandle)
{
StartThread(osThreadHandle, 0);
return PTE_OS_OK;
}
#endif
#ifdef F_pte_osThreadDelete
pte_osResult pte_osThreadDelete(pte_osThreadHandle handle)
{
ps2ThreadData *pThreadData;
void *pTls;
ee_thread_status_t info;
struct OsalThreadInfo *threadInfo;
int res;
res = ReferThreadStatus(handle, &info);
pTls = __getTlsStructFromThread(handle);
pThreadData = __getThreadData(handle);
DeleteSema(pThreadData->cancelSem);
free(pThreadData);
pteTlsThreadDestroy(pTls);
TerminateThread(handle);
DeleteThread(handle);
if (res > 0 && info.stack) {
free(info.stack);
}
threadInfo = &__threadInfo[handle];
threadInfo->threadNumber = 0;
threadInfo->tlsPtr = NULL;
return PTE_OS_OK;
}
#endif
#ifdef F_pte_osThreadExitAndDelete
pte_osResult pte_osThreadExitAndDelete(pte_osThreadHandle handle)
{
pte_osThreadDelete(handle);
ExitDeleteThread();
return PTE_OS_OK;
}
#endif
#ifdef F_pte_osThreadExit
void pte_osThreadExit()
{
ExitThread();
}
#endif
/*
* This has to be cancellable, so we can't just call sceKernelWaitThreadEnd.
* Instead, poll on this in a loop, like we do for a cancellable semaphore.
*/
#ifdef F_pte_osThreadWaitForEnd
pte_osResult pte_osThreadWaitForEnd(pte_osThreadHandle threadHandle)
{
pte_osResult result;
ps2ThreadData *pThreadData;
pThreadData = __getThreadData(GetThreadId());
while (1) {
ee_thread_status_t info;
/* Poll task to see if it has ended */
ReferThreadStatus(threadHandle, &info);
if (info.status == THS_DORMANT) {
/* Thread has ended */
result = PTE_OS_OK;
break;
} else {
ee_sema_t semInfo;
if (pThreadData != NULL) {
s32 osResult;
osResult = ReferSemaStatus(pThreadData->cancelSem, &semInfo);
if (osResult == pThreadData->cancelSem) {
if (semInfo.count > 0) {
result = PTE_OS_INTERRUPTED;
break;
} else {
/* Nothing found and not timed out yet; let's yield so we're not
* in busy loop.
*/
DelayThread(POLLING_DELAY_IN_us);
}
} else {
result = PTE_OS_GENERAL_FAILURE;
break;
}
}
}
}
return result;
}
#endif
#ifdef F_pte_osThreadGetHandle
pte_osThreadHandle pte_osThreadGetHandle(void)
{
return GetThreadId();
}
#endif
#ifdef F_pte_osThreadGetPriority
int pte_osThreadGetPriority(pte_osThreadHandle threadHandle)
{
ee_thread_status_t thinfo;
ReferThreadStatus(threadHandle, &thinfo);
return thinfo.current_priority;
}
#endif
#ifdef F_pte_osThreadSetPriority
pte_osResult pte_osThreadSetPriority(pte_osThreadHandle threadHandle, int newPriority)
{
ChangeThreadPriority(threadHandle, newPriority);
return PTE_OS_OK;
}
#endif
#ifdef F_pte_osThreadCancel
pte_osResult pte_osThreadCancel(pte_osThreadHandle threadHandle)
{
s32 osResult;
pte_osResult result;
ps2ThreadData *pThreadData;
pThreadData = __getThreadData(threadHandle);
osResult = SignalSema(pThreadData->cancelSem);
if (osResult == pThreadData->cancelSem) {
result = PTE_OS_OK;
} else {
result = PTE_OS_GENERAL_FAILURE;
}
return result;
}
#endif
#ifdef F_pte_osThreadCheckCancel
pte_osResult pte_osThreadCheckCancel(pte_osThreadHandle threadHandle)
{
ps2ThreadData *pThreadData;
ee_sema_t semInfo;
s32 osResult;
pte_osResult result;
pThreadData = __getThreadData(threadHandle);
if (pThreadData != NULL) {
osResult = ReferSemaStatus(pThreadData->cancelSem, &semInfo);
if (osResult == pThreadData->cancelSem) {
if (semInfo.count > 0) {
result = PTE_OS_INTERRUPTED;
} else {
result = PTE_OS_OK;
}
} else {
/* sceKernelReferSemaStatus returned an error */
result = PTE_OS_GENERAL_FAILURE;
}
} else {
/* For some reason, we couldn't get thread data */
result = PTE_OS_GENERAL_FAILURE;
}
return result;
}
#endif
#ifdef F_pte_osThreadSleep
void pte_osThreadSleep(unsigned int msecs)
{
DelayThread(msecs * 1000);
}
#endif
#ifdef F_pte_osThreadGetMinPriority
int pte_osThreadGetMinPriority()
{
return 17;
}
#endif
#ifdef F_pte_osThreadGetMaxPriority
int pte_osThreadGetMaxPriority()
{
return 30;
}
#endif
#ifdef F_pte_osThreadGetDefaultPriority
int pte_osThreadGetDefaultPriority()
{
return 18;
}
#endif
#ifdef F_pthread_num_processors_np
int pthread_num_processors_np(void)
{
return 1;
}
#endif
/****************************************************************************
*
* Mutexes
*
****************************************************************************/
#ifdef F_pte_osMutexCreate
pte_osResult pte_osMutexCreate(pte_osMutexHandle *pHandle)
{
static int mutexCtr = 0;
ee_sema_t sema;
pte_osMutexHandle handle;
if (mutexCtr++ > MAX_PS2_UID) {
mutexCtr = 0;
}
sema.init_count = 1;
sema.max_count = 1;
sema.option = 0;
handle = CreateSema(&sema);
*pHandle = handle;
return PTE_OS_OK;
}
#endif
#ifdef F_pte_osMutexDelete
pte_osResult pte_osMutexDelete(pte_osMutexHandle handle)
{
DeleteSema(handle);
return PTE_OS_OK;
}
#endif
#ifdef F_pte_osMutexLock
pte_osResult pte_osMutexLock(pte_osMutexHandle handle)
{
SemWaitTimeout(handle, UINT32_MAX);
return PTE_OS_OK;
}
#endif
#ifdef F_pte_osMutexTimedLock
pte_osResult pte_osMutexTimedLock(pte_osMutexHandle handle, unsigned int timeoutMsecs)
{
pte_osResult result;
int status = SemWaitTimeout(handle, timeoutMsecs);
if (status < 0) {
// Assume that any error from SemWaitTimeout was due to a timeout
result = PTE_OS_TIMEOUT;
} else {
result = PTE_OS_OK;
}
return result;
}
#endif
#ifdef F_pte_osMutexUnlock
pte_osResult pte_osMutexUnlock(pte_osMutexHandle handle)
{
SignalSema(handle);
return PTE_OS_OK;
}
#endif
/****************************************************************************
*
* Semaphores
*
***************************************************************************/
#ifdef F_pte_osSemaphoreCreate
pte_osResult pte_osSemaphoreCreate(int initialValue, pte_osSemaphoreHandle *pHandle)
{
pte_osSemaphoreHandle handle;
ee_sema_t sema;
static int semCtr = 0;
if (semCtr++ > MAX_PS2_UID) {
semCtr = 0;
}
sema.init_count = initialValue;
sema.max_count = 32767;
sema.option = 0;
handle = CreateSema(&sema);
*pHandle = handle;
return PTE_OS_OK;
}
#endif
#ifdef F_pte_osSemaphoreDelete
pte_osResult pte_osSemaphoreDelete(pte_osSemaphoreHandle handle)
{
DeleteSema(handle);
return PTE_OS_OK;
}
#endif
#ifdef F_pte_osSemaphorePost
pte_osResult pte_osSemaphorePost(pte_osSemaphoreHandle handle, int count)
{
int i;
for (i = 0; i < count; i++)
SignalSema(handle);
return PTE_OS_OK;
}
#endif
#ifdef F_pte_osSemaphorePend
pte_osResult pte_osSemaphorePend(pte_osSemaphoreHandle handle, unsigned int *pTimeoutMsecs)
{
uint32_t timeout;
uint32_t result;
pte_osResult osResult;
if (pTimeoutMsecs == NULL) {
timeout = UINT32_MAX;
} else {
timeout = *pTimeoutMsecs;
}
result = SemWaitTimeout(handle, timeout);
if (result == 0) {
osResult = PTE_OS_OK;
} else if (result == -2) {
osResult = PTE_OS_TIMEOUT;
} else {
osResult = PTE_OS_GENERAL_FAILURE;
}
return osResult;
}
#endif
/*
* Pend on a semaphore- and allow the pend to be cancelled.
*
* PS2 OS provides no functionality to asynchronously interrupt a blocked call. We simulte
* this by polling on the main semaphore and the cancellation semaphore and sleeping in a loop.
*/
#ifdef F_pte_osSemaphoreCancellablePend
pte_osResult pte_osSemaphoreCancellablePend(pte_osSemaphoreHandle semHandle, unsigned int *pTimeout)
{
ps2ThreadData *pThreadData;
pThreadData = __getThreadData(GetThreadId());
clock_t start_time;
pte_osResult result = PTE_OS_OK;
unsigned int timeout;
unsigned char timeoutEnabled;
start_time = clock();
// clock() is in microseconds, timeout as passed in was in milliseconds
if (pTimeout == NULL) {
timeout = 0;
timeoutEnabled = 0;
} else {
timeout = *pTimeout * 1000;
timeoutEnabled = 1;
}
while (1) {
int status;
/* Poll semaphore */
status = SemWaitTimeout(semHandle, UINT32_MAX);
if (status == 0) {
/* User semaphore posted to */
result = PTE_OS_OK;
break;
} else if ((timeoutEnabled) && ((clock() - start_time) > timeout)) {
/* Timeout expired */
result = PTE_OS_TIMEOUT;
break;
} else {
ee_sema_t semInfo;
if (pThreadData != NULL) {
s32 osResult;
osResult = ReferSemaStatus(pThreadData->cancelSem, &semInfo);
if (osResult == pThreadData->cancelSem) {
if (semInfo.count > 0) {
result = PTE_OS_INTERRUPTED;
break;
} else {
/* Nothing found and not timed out yet; let's yield so we're not
* in busy loop.
*/
DelayThread(POLLING_DELAY_IN_us);
}
} else {
result = PTE_OS_GENERAL_FAILURE;
break;
}
}
}
}
return result;
}
#endif
/****************************************************************************
*
* Atomic Operations
*
***************************************************************************/
#ifdef F_pte_osAtomicExchange
int pte_osAtomicExchange(int *ptarg, int val)
{
int intc = DIntr();
int origVal;
origVal = *ptarg;
*ptarg = val;
if(intc) { EIntr(); }
return origVal;
}
#endif
#ifdef F_pte_osAtomicCompareExchange
int pte_osAtomicCompareExchange(int *pdest, int exchange, int comp)
{
int intc = DIntr();
int origVal;
origVal = *pdest;
if (*pdest == comp){
*pdest = exchange;
}
if(intc) { EIntr(); }
return origVal;
}
#endif
#ifdef F_pte_osAtomicExchangeAdd
int pte_osAtomicExchangeAdd(int volatile* pAddend, int value)
{
int origVal;
int intc = DIntr();
origVal = *pAddend;
*pAddend += value;
if(intc) { EIntr(); }
return origVal;
}
#endif
#ifdef F_pte_osAtomicDecrement
int pte_osAtomicDecrement(int *pdest)
{
int val;
int intc = DIntr();
(*pdest)--;
val = *pdest;
if(intc) { EIntr(); }
return val;
}
#endif
#ifdef F_pte_osAtomicIncrement
int pte_osAtomicIncrement(int *pdest)
{
int val;
int intc = DIntr();
(*pdest)++;
val = *pdest;
if(intc) { EIntr(); }
return val;
}
#endif