# HG changeset patch # User Paul Boddie # Date 1709050822 -3600 # Node ID db3da45046d67a5cf476f2999bc0c1d6d5a7336b # Parent a597a0af78f367018538a611cdc2e628e993b2d1 Added a convenience method for getting the current thread. diff -r a597a0af78f3 -r db3da45046d6 libsystypes/include/systypes/thread.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libsystypes/include/systypes/thread.h Tue Feb 27 17:20:22 2024 +0100 @@ -0,0 +1,30 @@ +/* + * Thread-related utilities. + * + * Copyright (C) 2024 Paul Boddie + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA + */ + +#pragma once + +#include + +EXTERN_C_BEGIN + +l4_cap_idx_t get_current_thread(); + +EXTERN_C_END diff -r a597a0af78f3 -r db3da45046d6 libsystypes/lib/src/Makefile --- a/libsystypes/lib/src/Makefile Fri Feb 23 01:33:47 2024 +0100 +++ b/libsystypes/lib/src/Makefile Tue Feb 27 17:20:22 2024 +0100 @@ -4,7 +4,7 @@ TARGET = libsystypes.so libsystypes.a PC_FILENAME = libsystypes -SRC_C = base.c stat.c +SRC_C = base.c stat.c thread.c REQUIRES_LIBS = l4re_c-util diff -r a597a0af78f3 -r db3da45046d6 libsystypes/lib/src/thread.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libsystypes/lib/src/thread.c Tue Feb 27 17:20:22 2024 +0100 @@ -0,0 +1,42 @@ +/* + * Thread-related utilities. + * + * Copyright (C) 2024 Paul Boddie + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA + */ + +#include + +#include +#include + +#include "thread.h" + + + +l4_cap_idx_t get_current_thread() +{ + l4_cap_idx_t thread = pthread_l4_cap(pthread_self()); + + /* NOTE: On MIPS32, at least, in a payload started by libexec, the main thread + is not necessarily returned correctly. */ + + if (l4_is_invalid_cap(thread)) + return l4re_env()->main_thread; + + return thread; +}