Skip to content

Commit 254389f

Browse files
authored
Merge pull request torvalds#261 from liuyuan10/tls
lkl: Change tls key to struct lkl_tls_key
2 parents 487de9a + c329658 commit 254389f

File tree

4 files changed

+52
-30
lines changed

4 files changed

+52
-30
lines changed

arch/lkl/include/uapi/asm/host_ops.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
/* Defined in {posix,nt}-host.c */
55
struct lkl_mutex;
66
struct lkl_sem;
7+
struct lkl_tls_key;
78
typedef unsigned long lkl_thread_t;
89
struct lkl_jmp_buf {
910
unsigned long buf[32];
@@ -93,10 +94,10 @@ struct lkl_host_operations {
9394
lkl_thread_t (*thread_self)(void);
9495
int (*thread_equal)(lkl_thread_t a, lkl_thread_t b);
9596

96-
int (*tls_alloc)(unsigned int *key, void (*destructor)(void *));
97-
int (*tls_free)(unsigned int key);
98-
int (*tls_set)(unsigned int key, void *data);
99-
void *(*tls_get)(unsigned int key);
97+
struct lkl_tls_key *(*tls_alloc)(void (*destructor)(void *));
98+
void (*tls_free)(struct lkl_tls_key *key);
99+
int (*tls_set)(struct lkl_tls_key *key, void *data);
100+
void *(*tls_get)(struct lkl_tls_key *key);
100101

101102
void* (*mem_alloc)(unsigned long);
102103
void (*mem_free)(void *);

arch/lkl/kernel/syscalls.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ static void del_host_task(void *arg)
8888
do_exit(0);
8989
}
9090

91-
static unsigned int task_key;
91+
static struct lkl_tls_key *task_key;
9292

9393
long lkl_syscall(long no, long *params)
9494
{
@@ -121,19 +121,17 @@ long lkl_syscall(long no, long *params)
121121

122122
int syscalls_init(void)
123123
{
124-
int ret = 0;
125-
126124
snprintf(current->comm, sizeof(current->comm), "host0");
127125
set_thread_flag(TIF_HOST_THREAD);
128126
host0 = current;
129127

130128
if (lkl_ops->tls_alloc) {
131-
ret = lkl_ops->tls_alloc(&task_key, del_host_task);
132-
if (ret)
133-
return ret;
129+
task_key = lkl_ops->tls_alloc(del_host_task);
130+
if (!task_key)
131+
return -1;
134132
}
135133

136-
return ret;
134+
return 0;
137135
}
138136

139137
void syscalls_cleanup(void)

tools/lkl/lib/nt-host.c

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ struct lkl_sem {
1717
HANDLE sem;
1818
};
1919

20+
struct lkl_tls_key {
21+
DWORD key;
22+
};
23+
2024
static struct lkl_sem *sem_alloc(int count)
2125
{
2226
struct lkl_sem *sem = malloc(sizeof(struct lkl_sem));
@@ -122,28 +126,34 @@ static int thread_equal(lkl_thread_t a, lkl_thread_t b)
122126
return a == b;
123127
}
124128

125-
static int tls_alloc(unsigned int *key, void (*destructor)(void *))
129+
static struct lkl_tls_key *tls_alloc(void (*destructor)(void *))
126130
{
127-
*key = FlsAlloc((PFLS_CALLBACK_FUNCTION)destructor);
128-
return *key == TLS_OUT_OF_INDEXES ? -1 : 0;
131+
struct lkl_tls_key *ret = malloc(sizeof(struct lkl_tls_key));
132+
133+
ret->key = FlsAlloc((PFLS_CALLBACK_FUNCTION)destructor);
134+
if (ret->key == TLS_OUT_OF_INDEXES) {
135+
free(ret);
136+
return -1;
137+
}
138+
return 0;
129139
}
130140

131-
static int tls_free(unsigned int key)
141+
static void tls_free(struct lkl_tls_key *key)
132142
{
133143
/* setting to NULL first to prevent the callback from being called */
134-
if (!FlsSetValue(key, NULL))
135-
return -1;
136-
return FlsFree(key) ? 0 : -1;
144+
FlsSetValue(key->key, NULL);
145+
FlsFree(key->key);
146+
free(key);
137147
}
138148

139-
static int tls_set(unsigned int key, void *data)
149+
static int tls_set(struct lkl_tls_key *key, void *data)
140150
{
141-
return FlsSetValue(key, data) ? 0 : -1;
151+
return FlsSetValue(key->key, data) ? 0 : -1;
142152
}
143153

144-
static void *tls_get(unsigned int key)
154+
static void *tls_get(struct lkl_tls_key *key)
145155
{
146-
return FlsGetValue(key);
156+
return FlsGetValue(key->key);
147157
}
148158

149159

tools/lkl/lib/posix-host.c

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ struct lkl_sem {
4949
#endif /* _POSIX_SEMAPHORES */
5050
};
5151

52+
struct lkl_tls_key {
53+
pthread_key_t key;
54+
};
55+
5256
#define WARN_UNLESS(exp) do { \
5357
if (exp < 0) \
5458
lkl_printf("%s: %s\n", #exp, strerror(errno)); \
@@ -216,24 +220,33 @@ static int thread_equal(lkl_thread_t a, lkl_thread_t b)
216220
return pthread_equal(a, b);
217221
}
218222

219-
static int tls_alloc(unsigned int *key, void (*destructor)(void *))
223+
static struct lkl_tls_key *tls_alloc(void (*destructor)(void *))
220224
{
221-
return pthread_key_create((pthread_key_t *)key, destructor);
225+
struct lkl_tls_key *ret = malloc(sizeof(struct lkl_tls_key));
226+
227+
if (WARN_PTHREAD(pthread_key_create(&ret->key, destructor))) {
228+
free(ret);
229+
return NULL;
230+
}
231+
return ret;
222232
}
223233

224-
static int tls_free(unsigned int key)
234+
static void tls_free(struct lkl_tls_key *key)
225235
{
226-
return pthread_key_delete(key);
236+
WARN_PTHREAD(pthread_key_delete(key->key));
237+
free(key);
227238
}
228239

229-
static int tls_set(unsigned int key, void *data)
240+
static int tls_set(struct lkl_tls_key *key, void *data)
230241
{
231-
return pthread_setspecific(key, data);
242+
if (WARN_PTHREAD(pthread_setspecific(key->key, data)))
243+
return -1;
244+
return 0;
232245
}
233246

234-
static void *tls_get(unsigned int key)
247+
static void *tls_get(struct lkl_tls_key *key)
235248
{
236-
return pthread_getspecific(key);
249+
return pthread_getspecific(key->key);
237250
}
238251

239252
static unsigned long long time_ns(void)

0 commit comments

Comments
 (0)