File: ps2.c

package info (click to toggle)
retroarch 1.20.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 75,736 kB
  • sloc: ansic: 1,207,646; cpp: 104,166; objc: 8,567; asm: 6,624; python: 3,776; makefile: 2,838; sh: 2,786; xml: 1,408; perl: 393; javascript: 10
file content (59 lines) | stat: -rw-r--r-- 1,686 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
#define LIBCO_C
#include "libco.h"

#include <stdlib.h>
#include <stdint.h>
#include <kernel.h>

/* Since cothread_t is a void pointer it must contain an address. We can't return a reference to a local variable
 * because it would go out of scope, so we create a static variable instead so we can return a reference to it.
 */
static int32_t active_thread_id = -1;
extern void *_gp;

cothread_t co_active()
{
  active_thread_id = GetThreadId();
  return &active_thread_id;
}

cothread_t co_create(unsigned int size, void (*entrypoint)(void))
{
   /* Similar scenario as with active_thread_id except there will only be one active_thread_id while there could be many
    * new threads each with their own handle, so we create them on the heap instead and delete them manually when they're
    * no longer needed in co_delete().
    */
   ee_thread_t thread;
   int32_t new_thread_id;
   cothread_t handle       = malloc(sizeof(cothread_t));
   void *threadStack       = (void *)malloc(size);

   if (!threadStack)
      return -1;

   thread.stack_size		   = size;
   thread.gp_reg			   = &_gp;
   thread.func				   = (void *)entrypoint;
   thread.stack			   = threadStack;
   thread.option			   = 0;
   thread.initial_priority = 1;

   new_thread_id           = CreateThread(&thread);
   StartThread(new_thread_id, NULL);
   *(uint32_t *)handle     = new_thread_id;
   return handle;
}

void co_delete(cothread_t handle)
{
   TerminateThread(*(uint32_t *)handle);
   DeleteThread(*(uint32_t *)handle);
   free(handle);
}

void co_switch(cothread_t handle)
{
   WakeupThread(*(uint32_t *)handle);
   /* Sleep the currently active thread so the new thread can start */
   SleepThread();
}