Skip to content

Commit a791be9

Browse files
committed
unix: Add basic thread support using pthreads.
Has the ability to create new threads.
1 parent 27cc077 commit a791be9

5 files changed

Lines changed: 101 additions & 0 deletions

File tree

unix/Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ ifeq ($(MICROPY_PY_SOCKET),1)
9393
CFLAGS_MOD += -DMICROPY_PY_SOCKET=1
9494
SRC_MOD += modsocket.c
9595
endif
96+
ifeq ($(MICROPY_PY_THREAD),1)
97+
CFLAGS_MOD += -DMICROPY_PY_THREAD=1
98+
LDFLAGS_MOD += -lpthread
99+
endif
96100

97101
ifeq ($(MICROPY_PY_FFI),1)
98102

@@ -128,6 +132,7 @@ SRC_C = \
128132
main.c \
129133
gccollect.c \
130134
unix_mphal.c \
135+
mpthreadport.c \
131136
input.c \
132137
file.c \
133138
modmachine.c \

unix/main.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include "py/gc.h"
4646
#include "py/stackctrl.h"
4747
#include "py/mphal.h"
48+
#include "py/mpthread.h"
4849
#include "extmod/misc.h"
4950
#include "genhdr/mpversion.h"
5051
#include "input.h"
@@ -379,6 +380,9 @@ STATIC void set_sys_argv(char *argv[], int argc, int start_arg) {
379380
MP_NOINLINE int main_(int argc, char **argv);
380381

381382
int main(int argc, char **argv) {
383+
#if MICROPY_PY_THREAD
384+
mp_thread_init();
385+
#endif
382386
// We should capture stack top ASAP after start, and it should be
383387
// captured guaranteedly before any other stack variables are allocated.
384388
// For this, actual main (renamed main_) should not be inlined into

unix/mpconfigport.mk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ MICROPY_USE_READLINE = 1
1111
# Whether to enable FatFs VFS
1212
MICROPY_FATFS = 1
1313

14+
# _thread module using pthreads
15+
MICROPY_PY_THREAD = 1
16+
1417
# Subset of CPython termios module
1518
MICROPY_PY_TERMIOS = 1
1619

unix/mpthreadport.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include <errno.h>
28+
29+
#include "py/mpstate.h"
30+
#include "py/mpthread.h"
31+
32+
#if MICROPY_PY_THREAD
33+
34+
STATIC pthread_key_t tls_key;
35+
36+
void mp_thread_init(void) {
37+
pthread_key_create(&tls_key, NULL);
38+
pthread_setspecific(tls_key, &mp_state_ctx.thread);
39+
}
40+
41+
mp_state_thread_t *mp_thread_get_state(void) {
42+
return (mp_state_thread_t*)pthread_getspecific(tls_key);
43+
}
44+
45+
void mp_thread_set_state(void *state) {
46+
pthread_setspecific(tls_key, state);
47+
}
48+
49+
void mp_thread_create(void *(*entry)(void*), void *arg) {
50+
pthread_t id;
51+
pthread_create(&id, NULL, entry, arg);
52+
}
53+
54+
#endif // MICROPY_PY_THREAD

unix/mpthreadport.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
#ifndef __MICROPY_INCLUDED_UNIX_MPTHREADPORT_H__
27+
#define __MICROPY_INCLUDED_UNIX_MPTHREADPORT_H__
28+
29+
#include <pthread.h>
30+
31+
typedef pthread_mutex_t mp_thread_mutex_t;
32+
33+
void mp_thread_init(void);
34+
35+
#endif // __MICROPY_INCLUDED_UNIX_MPTHREADPORT_H__

0 commit comments

Comments
 (0)