Skip to content

Commit 071345f

Browse files
Maschellfincs
authored andcommitted
Implement wut_set_thread_specific/wut_get_thread_specific as weak functions
1 parent 5f42c2c commit 071345f

5 files changed

Lines changed: 46 additions & 12 deletions

File tree

libraries/wutnewlib/wut_reent.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#include "wut_newlib.h"
2+
#include "wut_thread_specific.h"
23
#include <stdlib.h>
34

45
#include <coreinit/thread.h>
56

6-
#define __WUT_CONTEXT_THREAD_SPECIFIC_ID OS_THREAD_SPECIFIC_WUT_RESERVED_1
7+
#define __WUT_CONTEXT_THREAD_SPECIFIC_ID WUT_THREAD_SPECIFIC_1
78

89
struct __wut_thread_context
910
{
@@ -17,7 +18,7 @@ __wut_thread_cleanup(OSThread *thread,
1718
{
1819
struct __wut_thread_context *context;
1920

20-
context = (struct __wut_thread_context *)OSGetThreadSpecific(__WUT_CONTEXT_THREAD_SPECIFIC_ID);
21+
context = (struct __wut_thread_context *) wut_get_thread_specific(__WUT_CONTEXT_THREAD_SPECIFIC_ID);
2122
if (!context || &context->reent == _GLOBAL_REENT) {
2223
abort();
2324
}
@@ -29,33 +30,33 @@ __wut_thread_cleanup(OSThread *thread,
2930
_reclaim_reent(&context->reent);
3031

3132
// Use global reent during free since the current reent is getting freed
32-
OSSetThreadSpecific(__WUT_CONTEXT_THREAD_SPECIFIC_ID, _GLOBAL_REENT);
33+
wut_set_thread_specific(__WUT_CONTEXT_THREAD_SPECIFIC_ID, _GLOBAL_REENT);
3334

3435
free(context);
3536

36-
OSSetThreadSpecific(__WUT_CONTEXT_THREAD_SPECIFIC_ID, NULL);
37+
wut_set_thread_specific(__WUT_CONTEXT_THREAD_SPECIFIC_ID, NULL);
3738
}
3839

3940
struct _reent *
4041
__wut_getreent(void)
4142
{
4243
struct __wut_thread_context *context;
4344

44-
context = (struct __wut_thread_context *)OSGetThreadSpecific(__WUT_CONTEXT_THREAD_SPECIFIC_ID);
45+
context = (struct __wut_thread_context *) wut_get_thread_specific(__WUT_CONTEXT_THREAD_SPECIFIC_ID);
4546
if (!context) {
4647
// Temporarily use global reent during context allocation
47-
OSSetThreadSpecific(__WUT_CONTEXT_THREAD_SPECIFIC_ID, _GLOBAL_REENT);
48+
wut_set_thread_specific(__WUT_CONTEXT_THREAD_SPECIFIC_ID, _GLOBAL_REENT);
4849

4950
context = (struct __wut_thread_context *)malloc(sizeof(*context));
5051
if (!context) {
51-
OSSetThreadSpecific(__WUT_CONTEXT_THREAD_SPECIFIC_ID, NULL);
52+
wut_set_thread_specific(__WUT_CONTEXT_THREAD_SPECIFIC_ID, NULL);
5253
return NULL;
5354
}
5455

5556
_REENT_INIT_PTR(&context->reent);
5657
context->savedCleanup = OSSetThreadCleanupCallback(OSGetCurrentThread(), &__wut_thread_cleanup);
5758

58-
OSSetThreadSpecific(__WUT_CONTEXT_THREAD_SPECIFIC_ID, context);
59+
wut_set_thread_specific(__WUT_CONTEXT_THREAD_SPECIFIC_ID, context);
5960
}
6061

6162
return &context->reent;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include "wut_thread_specific.h"
2+
#include <coreinit/thread.h>
3+
#include <wut.h>
4+
5+
void __attribute__((weak))
6+
wut_set_thread_specific(__wut_thread_specific_id id, void *value) {
7+
OSSetThreadSpecific(OS_THREAD_SPECIFIC_WUT_RESERVED_0 + id - WUT_THREAD_SPECIFIC_0, value);
8+
}
9+
10+
void *__attribute__((weak))
11+
wut_get_thread_specific(__wut_thread_specific_id id) {
12+
return OSGetThreadSpecific(OS_THREAD_SPECIFIC_WUT_RESERVED_0 + id - WUT_THREAD_SPECIFIC_0);;
13+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#pragma once
2+
3+
typedef enum __wut_thread_specific_id {
4+
WUT_THREAD_SPECIFIC_0 = 0,
5+
WUT_THREAD_SPECIFIC_1 = 1,
6+
} __wut_thread_specific_id;
7+
8+
#ifdef __cplusplus
9+
extern "C" {
10+
#endif
11+
12+
void wut_set_thread_specific(__wut_thread_specific_id id, void *value);
13+
14+
void *wut_get_thread_specific(__wut_thread_specific_id id);
15+
16+
#ifdef __cplusplus
17+
}
18+
#endif

libraries/wutstdc++/wut_gthread.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@
66
#include <coreinit/thread.h>
77
#include <coreinit/mutex.h>
88

9+
#include "../wutnewlib/wut_thread_specific.h"
10+
911
#define __WUT_MAX_KEYS (128)
1012
#define __WUT_STACK_SIZE (128*1024)
1113

1214
#define __WUT_ONCE_VALUE_INIT (0)
1315
#define __WUT_ONCE_VALUE_STARTED (1)
1416
#define __WUT_ONCE_VALUE_DONE (2)
1517

16-
#define __WUT_KEY_THREAD_SPECIFIC_ID OS_THREAD_SPECIFIC_WUT_RESERVED_0
18+
#define __WUT_KEY_THREAD_SPECIFIC_ID WUT_THREAD_SPECIFIC_0
1719

1820
typedef volatile uint32_t __wut_once_t;
1921
typedef struct {

libraries/wutstdc++/wut_gthread_keys.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,15 @@ __wut_key_delete(__wut_key_t key)
5959
static const void **
6060
__wut_get_thread_keys()
6161
{
62-
const void **keys = (const void **)OSGetThreadSpecific(__WUT_KEY_THREAD_SPECIFIC_ID);
62+
const void **keys = (const void **)wut_get_thread_specific(__WUT_KEY_THREAD_SPECIFIC_ID);
6363
if (!keys) {
6464
keys = (const void **)malloc(sizeof(void *) * sizeof(__WUT_MAX_KEYS));
6565
if (!keys) {
6666
return NULL;
6767
}
6868

6969
memset(keys, 0, sizeof(void *) * sizeof(__WUT_MAX_KEYS));
70-
OSSetThreadSpecific(__WUT_KEY_THREAD_SPECIFIC_ID, keys);
70+
wut_set_thread_specific(__WUT_KEY_THREAD_SPECIFIC_ID, keys);
7171
}
7272

7373
return keys;
@@ -100,7 +100,7 @@ __wut_setspecific(__wut_key_t key,
100100
void
101101
__wut_key_cleanup(OSThread *thread)
102102
{
103-
void **keys = (void **)OSGetThreadSpecific(__WUT_KEY_THREAD_SPECIFIC_ID);
103+
void **keys = (void **)wut_get_thread_specific(__WUT_KEY_THREAD_SPECIFIC_ID);
104104
if (!keys) {
105105
return;
106106
}

0 commit comments

Comments
 (0)