about summary refs log tree commit diff stats
path: root/src/wrapped/wrappedgthread2.c
blob: b992e0b40577b8b7ea786ac8aaba202239bd438c (plain) (blame)
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define _GNU_SOURCE         /* See feature_test_macros(7) */
#include <dlfcn.h>

#include "wrappedlibs.h"

#include "debug.h"
#include "wrapper.h"
#include "bridge.h"
#include "librarian/library_private.h"
#include "x64emu.h"
#include "emu/x64emu_private.h"
#include "callback.h"
#include "librarian.h"
#include "box64context.h"
#include "emu/x64emu_private.h"
#include "myalign.h"
#include "gtkclass.h"

const char* gthread2Name = "libgthread-2.0.so.0";
#define LIBNAME gthread2

typedef void  (*vFp_t)(void*);

#define SUPER() \
    GO(g_thread_init, vFp_t)                            \
    GO(g_thread_init_with_errorcheck_mutexes, vFp_t)

typedef struct gthread2_my_s {
    // functions
    #define GO(A, B)    B   A;
    SUPER()
    #undef GO
} gthread2_my_t;

void* getGthread2My(library_t* lib)
{
    gthread2_my_t* my = (gthread2_my_t*)calloc(1, sizeof(gthread2_my_t));
    #define GO(A, W) my->A = (W)dlsym(lib->priv.w.lib, #A);
    SUPER()
    #undef GO
    return my;
}
#undef SUPER

void freeGthread2My(void* lib)
{
    //gthread2_my_t *my = (gthread2_my_t *)lib;
}

EXPORT int g_threads_got_initialized;

typedef struct my_GThreadFunctions_s
{
  void*  (*mutex_new)               (void);
  void     (*mutex_lock)            (void *mutex);
  int (*mutex_trylock)              (void *mutex);
  void     (*mutex_unlock)          (void *mutex);
  void     (*mutex_free)            (void *mutex);
  void*   (*cond_new)               (void);
  void     (*cond_signal)           (void *cond);
  void     (*cond_broadcast)        (void *cond);
  void     (*cond_wait)             (void *cond, void *mutex);
  int (*cond_timed_wait)            (void *cond, void *mutex, void *end_time);
  void      (*cond_free)            (void *cond);
  void* (*private_new)              (void* destructor);            //GDestroyNotify
  void*  (*private_get)             (void *private_key);
  void      (*private_set)          (void *private_key, void* data);
  void      (*thread_create)        (void* func, void* data, unsigned long stack_size, int joinable, int bound, int priority, void* thread, void **error);  //GThreadFunc
  void      (*thread_yield)         (void);
  void      (*thread_join)          (void* thread);
  void      (*thread_exit)          (void);
  void      (*thread_set_priority)  (void* thread, int priority);
  void      (*thread_self)          (void* thread);
  int  (*thread_equal)              (void* thread1, void* thread2);
} my_GThreadFunctions_t;

EXPORT void my_g_thread_init(x64emu_t* emu, my_GThreadFunctions_t* vtable)
{
    if(g_threads_got_initialized) {
        // no need to do it twice
        my_setGlobalGThreadsInit();
        return;
    }

    library_t * lib = GetLibInternal(gthread2Name);
    gthread2_my_t *my = (gthread2_my_t*)lib->priv.w.p2;

    if(!vtable)
        return my->g_thread_init(NULL);

    printf_log(LOG_INFO, "Warning, vtable not NULL in g_thread_init not supported yet!\n");
    
    my->g_thread_init(vtable);

    if(g_threads_got_initialized)
        my_setGlobalGThreadsInit();
}

EXPORT void my_g_thread_init_with_errorcheck_mutexes(x64emu_t* emu, my_GThreadFunctions_t* vtable)
{
    if(g_threads_got_initialized) {
        // no need to do it twice
        my_setGlobalGThreadsInit();
        return;
    }

    library_t * lib = GetLibInternal(gthread2Name);
    gthread2_my_t *my = (gthread2_my_t*)lib->priv.w.p2;

    if(vtable)
        printf_log(LOG_NONE, "Warning, vtable is not NULL in g_thread_init_with_errorcheck_mutexes call!\n");
    
    my->g_thread_init_with_errorcheck_mutexes(vtable);  // will certainly crash here...

    if(g_threads_got_initialized)
        my_setGlobalGThreadsInit();
}

void** my_GetGthreadsGotInitialized()
{
    return (void**)&g_threads_got_initialized;
}



#define PRE_INIT    \
    if(box64_nogtk) \
        return -1;

#define CUSTOM_INIT \
    lib->priv.w.p2 = getGthread2My(lib);

#define CUSTOM_FINI \
    freeGthread2My(lib->priv.w.p2); \
    free(lib->priv.w.p2);

#include "wrappedlib_init.h"