1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/libsystypes/include/systypes/thread.h Tue Feb 27 17:20:22 2024 +0100
1.3 @@ -0,0 +1,30 @@
1.4 +/*
1.5 + * Thread-related utilities.
1.6 + *
1.7 + * Copyright (C) 2024 Paul Boddie <paul@boddie.org.uk>
1.8 + *
1.9 + * This program is free software; you can redistribute it and/or
1.10 + * modify it under the terms of the GNU General Public License as
1.11 + * published by the Free Software Foundation; either version 2 of
1.12 + * the License, or (at your option) any later version.
1.13 + *
1.14 + * This program is distributed in the hope that it will be useful,
1.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.17 + * GNU General Public License for more details.
1.18 + *
1.19 + * You should have received a copy of the GNU General Public License
1.20 + * along with this program; if not, write to the Free Software
1.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
1.22 + * Boston, MA 02110-1301, USA
1.23 + */
1.24 +
1.25 +#pragma once
1.26 +
1.27 +#include <l4/sys/types.h>
1.28 +
1.29 +EXTERN_C_BEGIN
1.30 +
1.31 +l4_cap_idx_t get_current_thread();
1.32 +
1.33 +EXTERN_C_END
2.1 --- a/libsystypes/lib/src/Makefile Fri Feb 23 01:33:47 2024 +0100
2.2 +++ b/libsystypes/lib/src/Makefile Tue Feb 27 17:20:22 2024 +0100
2.3 @@ -4,7 +4,7 @@
2.4 TARGET = libsystypes.so libsystypes.a
2.5 PC_FILENAME = libsystypes
2.6
2.7 -SRC_C = base.c stat.c
2.8 +SRC_C = base.c stat.c thread.c
2.9
2.10 REQUIRES_LIBS = l4re_c-util
2.11
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/libsystypes/lib/src/thread.c Tue Feb 27 17:20:22 2024 +0100
3.3 @@ -0,0 +1,42 @@
3.4 +/*
3.5 + * Thread-related utilities.
3.6 + *
3.7 + * Copyright (C) 2024 Paul Boddie <paul@boddie.org.uk>
3.8 + *
3.9 + * This program is free software; you can redistribute it and/or
3.10 + * modify it under the terms of the GNU General Public License as
3.11 + * published by the Free Software Foundation; either version 2 of
3.12 + * the License, or (at your option) any later version.
3.13 + *
3.14 + * This program is distributed in the hope that it will be useful,
3.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3.17 + * GNU General Public License for more details.
3.18 + *
3.19 + * You should have received a copy of the GNU General Public License
3.20 + * along with this program; if not, write to the Free Software
3.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
3.22 + * Boston, MA 02110-1301, USA
3.23 + */
3.24 +
3.25 +#include <l4/re/env.h>
3.26 +
3.27 +#include <pthread-l4.h>
3.28 +#include <pthread.h>
3.29 +
3.30 +#include "thread.h"
3.31 +
3.32 +
3.33 +
3.34 +l4_cap_idx_t get_current_thread()
3.35 +{
3.36 + l4_cap_idx_t thread = pthread_l4_cap(pthread_self());
3.37 +
3.38 + /* NOTE: On MIPS32, at least, in a payload started by libexec, the main thread
3.39 + is not necessarily returned correctly. */
3.40 +
3.41 + if (l4_is_invalid_cap(thread))
3.42 + return l4re_env()->main_thread;
3.43 +
3.44 + return thread;
3.45 +}