From 8b91eae32e6bcd6c1bc31f74346d766d87e49394 Mon Sep 17 00:00:00 2001 From: Andrea Palmate Date: Fri, 27 Oct 2023 11:50:09 +0200 Subject: [PATCH 01/12] Wrapped stdin, stdout and stderr into a functions so we can avoid to add into and this will avoid problems on some ports --- libc.gmk | 1 + library/include/stdio.h | 11 ++++++----- library/include/sys/times.h | 2 ++ library/include/unistd.h | 4 ---- library/shared_library/clib4_vectors.h | 4 ++++ library/shared_library/interface.h | 5 +++++ library/shared_library/stubs.c | 4 ++++ library/stdio/__std.c | 21 +++++++++++++++++++++ 8 files changed, 43 insertions(+), 9 deletions(-) create mode 100644 library/stdio/__std.c diff --git a/libc.gmk b/libc.gmk index 5f36967c..4bb02eb0 100755 --- a/libc.gmk +++ b/libc.gmk @@ -350,6 +350,7 @@ C_STDIO := \ prng/srand.o \ prng/srand48.o \ prng/srandom.o \ + stdio/__std.o \ stdio/asprintf.o \ stdio/change_fd_action.o \ stdio/change_fd_user_data.o \ diff --git a/library/include/stdio.h b/library/include/stdio.h index 933715fa..a21fd56c 100755 --- a/library/include/stdio.h +++ b/library/include/stdio.h @@ -13,8 +13,6 @@ #include #include -#include - __BEGIN_DECLS /* 'End of file' indicator returned by, for example, fgetc() */ @@ -116,9 +114,12 @@ typedef struct __sFILE FILE; /****************************************************************************/ /* The three standard I/O streams */ -#define stdin ((FILE *) __CLIB4->__iob[0]) -#define stdout ((FILE *) __CLIB4->__iob[1]) -#define stderr ((FILE *) __CLIB4->__iob[2]) +extern FILE *__stdin(); +extern FILE *__stdout(); +extern FILE *__stderr(); +#define stdin (__stdin()) +#define stdout ( __stdout()) +#define stderr (__stderr()) /****************************************************************************/ diff --git a/library/include/sys/times.h b/library/include/sys/times.h index f1bcccb1..71325019 100755 --- a/library/include/sys/times.h +++ b/library/include/sys/times.h @@ -5,6 +5,8 @@ __BEGIN_DECLS +#include + /* Structure describing CPU time used by a process and its children. */ struct tms { diff --git a/library/include/unistd.h b/library/include/unistd.h index ea05c887..88fd1670 100755 --- a/library/include/unistd.h +++ b/library/include/unistd.h @@ -11,10 +11,6 @@ #include #endif /* _FCNTL_H */ -#ifndef _STDIO_H -#include -#endif /* _STDIO_H */ - #ifndef _STDINT_H #include #endif /* _STDINT_H */ diff --git a/library/shared_library/clib4_vectors.h b/library/shared_library/clib4_vectors.h index b52e6ac2..5757ecc3 100644 --- a/library/shared_library/clib4_vectors.h +++ b/library/shared_library/clib4_vectors.h @@ -1149,4 +1149,8 @@ static void *clib4Vectors[] = { (void *) (fts_open), /* 4316 */ (void *) (fts_read), /* 4320 */ (void *) (fts_set), /* 4324 */ + + (void *) (__stdin), /* 4328 */ + (void *) (__stdout), /* 4332 */ + (void *) (__stderr), /* 4336 */ }; \ No newline at end of file diff --git a/library/shared_library/interface.h b/library/shared_library/interface.h index 937f5706..d43abc84 100644 --- a/library/shared_library/interface.h +++ b/library/shared_library/interface.h @@ -1319,6 +1319,11 @@ struct Clib4IFace { FTS * (* fts_open) (char * const *, int, int (*)(const FTSENT **, const FTSENT **)); /* 4316 */ FTSENT * (* fts_read) (FTS *); /* 4320 */ int (* fts_set) (FTS *, FTSENT *, int); /* 4324 */ + + FILE * (* __stdin) (void); /* 4328 */ + FILE * (* __stdout) (void); /* 4332 */ + FILE * (* __stderr) (void); /* 4336 */ + }; #ifdef __PIC__ diff --git a/library/shared_library/stubs.c b/library/shared_library/stubs.c index 19428e1b..04027538 100644 --- a/library/shared_library/stubs.c +++ b/library/shared_library/stubs.c @@ -1088,3 +1088,7 @@ Clib4Call(fts_close, 4312); Clib4Call(fts_open, 4316); Clib4Call(fts_read, 4320); Clib4Call(fts_set, 4324); + +Clib4Call(__stdin, 4328); +Clib4Call(__stdout, 4332); +Clib4Call(__stderr, 4336); diff --git a/library/stdio/__std.c b/library/stdio/__std.c new file mode 100644 index 00000000..024e341a --- /dev/null +++ b/library/stdio/__std.c @@ -0,0 +1,21 @@ +/* + * $Id: __std.c,v 1.0 2023-10-27 12:04:22 clib4devs Exp $ +*/ + +#include +#include + +FILE * +__stdin () { + return (FILE *) __CLIB4->__iob[0]; +} + +FILE * +__stdout () { + return (FILE *) __CLIB4->__iob[1]; +} + +FILE * +__stderr () { + return (FILE *) __CLIB4->__iob[2]; +} \ No newline at end of file From 8d663539c28dbdf7488fc2505b257e55d2636bdb Mon Sep 17 00:00:00 2001 From: Andrea Palmate Date: Fri, 3 Nov 2023 12:02:21 +0100 Subject: [PATCH 02/12] Removed dependency from --- library/include/ctype.h | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/library/include/ctype.h b/library/include/ctype.h index 338ddfbb..ee676e4e 100755 --- a/library/include/ctype.h +++ b/library/include/ctype.h @@ -6,7 +6,6 @@ #define _CTYPE_H #include -#include __BEGIN_DECLS @@ -48,18 +47,17 @@ extern int toascii(int c); /****************************************************************************/ -#define isalnum(c) ((__CLIB4->__ctype_table[(c) & 255] & (__CTYPE_DIGIT|__CTYPE_LOWER_CASE|__CTYPE_UPPER_CASE)) != 0) -#define isalpha(c) ((__CLIB4->__ctype_table[(c) & 255] & (__CTYPE_LOWER_CASE|__CTYPE_UPPER_CASE)) != 0) -#define iscntrl(c) ((__CLIB4->__ctype_table[(c) & 255] & __CTYPE_CONTROL) != 0) -#define isdigit(c) ((__CLIB4->__ctype_table[(c) & 255] & __CTYPE_DIGIT) != 0) -#define isxdigit(c) ((__CLIB4->__ctype_table[(c) & 255] & __CTYPE_HEX_DIGIT) != 0) -#define isgraph(c) ((__CLIB4->__ctype_table[(c) & 255] & (__CTYPE_DIGIT|__CTYPE_PUNCTUATION|__CTYPE_LOWER_CASE|__CTYPE_UPPER_CASE)) != 0) -#define ispunct(c) ((__CLIB4->__ctype_table[(c) & 255] & __CTYPE_PUNCTUATION) != 0) -#define isprint(c) ((__CLIB4->__ctype_table[(c) & 255] & __CTYPE_PRINTABLE) != 0) -#define islower(c) ((__CLIB4->__ctype_table[(c) & 255] & __CTYPE_LOWER_CASE) != 0) -#define isupper(c) ((__CLIB4->__ctype_table[(c) & 255] & __CTYPE_UPPER_CASE) != 0) -#define isspace(c) ((__CLIB4->__ctype_table[(c) & 255] & __CTYPE_WHITE_SPACE) != 0) -#define isblank(c) ((c) == ' ' || (c) == '\t') +static __inline int __isspace(int _c) { + return _c == ' ' || (unsigned)_c-'\t' < 5; +} + +#define isalpha(a) (0 ? isalpha(a) : (((unsigned)(a)|32)-'a') < 26) +#define isdigit(a) (0 ? isdigit(a) : ((unsigned)(a)-'0') < 10) +#define islower(a) (0 ? islower(a) : ((unsigned)(a)-'a') < 26) +#define isupper(a) (0 ? isupper(a) : ((unsigned)(a)-'A') < 26) +#define isprint(a) (0 ? isprint(a) : ((unsigned)(a)-0x20) < 0x5f) +#define isgraph(a) (0 ? isgraph(a) : ((unsigned)(a)-0x21) < 0x5e) +#define isspace(a) __isspace(a) #endif /* __cplusplus */ From db96a4f881549ffc498713652a8b94bce9aeeacb Mon Sep 17 00:00:00 2001 From: Andrea Palmate Date: Mon, 6 Nov 2023 09:43:01 +0100 Subject: [PATCH 03/12] More include fixes --- library/include/math.h | 9 +++++++-- library/include/unistd.h | 2 -- library/profile/gmon.c | 1 + library/socket/socket_headers.h | 4 ++++ 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/library/include/math.h b/library/include/math.h index d148ac7c..d95ec351 100755 --- a/library/include/math.h +++ b/library/include/math.h @@ -88,8 +88,13 @@ extern float ynf(int n, float x); /* HUGE_VALL is a 'long double' Infinity. */ #define HUGE_VALL (1.0L / 0.0L) -#define INFINITY ((const float) __CLIB4->__infinity) -#define NAN ((const float) __CLIB4->__nan) +# ifndef INFINITY +# define INFINITY (__builtin_inff()) +# endif + +# ifndef NAN +# define NAN (__builtin_nanf("")) +# endif #define FP_INFINITE 0x01 /* -/+ infinity */ #define FP_NAN 0x02 /* not a number */ diff --git a/library/include/unistd.h b/library/include/unistd.h index 88fd1670..08f7c217 100755 --- a/library/include/unistd.h +++ b/library/include/unistd.h @@ -15,8 +15,6 @@ #include #endif /* _STDINT_H */ -#include - __BEGIN_DECLS #define STDIN_FILENO 0 diff --git a/library/profile/gmon.c b/library/profile/gmon.c index db8e9137..19844b6b 100644 --- a/library/profile/gmon.c +++ b/library/profile/gmon.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include diff --git a/library/socket/socket_headers.h b/library/socket/socket_headers.h index 5eed3d57..26f180c4 100755 --- a/library/socket/socket_headers.h +++ b/library/socket/socket_headers.h @@ -53,6 +53,10 @@ #include #endif +#ifndef _SYS_SELECT_H +#include +#endif + #ifndef _CLIB4_H #include #endif From 5444d691484ce6f39b84d43a872462b211bce238 Mon Sep 17 00:00:00 2001 From: Andrea Palmate Date: Mon, 6 Nov 2023 09:58:22 +0100 Subject: [PATCH 04/12] Removed dos.h from math.h --- library/include/math.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/library/include/math.h b/library/include/math.h index d95ec351..887b7c8f 100755 --- a/library/include/math.h +++ b/library/include/math.h @@ -13,8 +13,6 @@ __BEGIN_DECLS #include #endif /* _STDLIB_H */ -#include - /* Natural log of 2 */ #define _M_LN2 (double) 0.693147180559945309417 From 82e61d2faad530c5d3f76d8a65c86cbb80206fdb Mon Sep 17 00:00:00 2001 From: Andrea Palmate Date: Tue, 7 Nov 2023 11:00:00 +0100 Subject: [PATCH 05/12] Removed broken pthread_attr_t in signal.h --- library/include/signal.h | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/library/include/signal.h b/library/include/signal.h index 6520ccf2..f2e62e5f 100755 --- a/library/include/signal.h +++ b/library/include/signal.h @@ -6,12 +6,7 @@ #define _SIGNAL_H #include - -#ifdef __cplusplus -#ifdef __USE_AMIGAOS_NAMESPACE__ -#define pthread_attr_t AmigaOS::pthread_attr_t -#endif -#endif +#include __BEGIN_DECLS From d708d1ab3acc14bf2eb3eb464060526db416f985 Mon Sep 17 00:00:00 2001 From: Andrea Palmate Date: Wed, 6 Dec 2023 10:33:06 +0100 Subject: [PATCH 06/12] Some minor include changes --- library/include/pthread.h | 14 ++------------ library/include/signal.h | 1 - library/include/sys/cdefs.h | 20 ++++++++++++++------ 3 files changed, 16 insertions(+), 19 deletions(-) diff --git a/library/include/pthread.h b/library/include/pthread.h index 33b1465a..f4c4f1a3 100644 --- a/library/include/pthread.h +++ b/library/include/pthread.h @@ -31,12 +31,7 @@ #include #include -#ifdef __cplusplus -#ifdef __USE_AMIGAOS_NAMESPACE__ -namespace AmigaOS { -#endif -extern "C" { -#endif +__BEGIN_DECLS // // POSIX options @@ -414,11 +409,6 @@ extern void pthread_cleanup_pop(int execute); extern int pthread_kill(pthread_t thread, int sig); -#ifdef __cplusplus -} -#ifdef __USE_AMIGAOS_NAMESPACE__ -} -#endif -#endif +__END_DECLS #endif diff --git a/library/include/signal.h b/library/include/signal.h index f2e62e5f..277f0423 100755 --- a/library/include/signal.h +++ b/library/include/signal.h @@ -6,7 +6,6 @@ #define _SIGNAL_H #include -#include __BEGIN_DECLS diff --git a/library/include/sys/cdefs.h b/library/include/sys/cdefs.h index d57d3f53..ffbc4a3a 100755 --- a/library/include/sys/cdefs.h +++ b/library/include/sys/cdefs.h @@ -23,13 +23,21 @@ #define __flexarr [0] #ifdef __cplusplus -#define __BEGIN_DECLS \ - extern "C" \ - { -#define __END_DECLS } + #ifdef __USE_AMIGAOS_NAMESPACE__ + #define __BEGIN_DECLS \ + namespace AmigaOS { \ + extern "C" \ + { + #define __END_DECLS } } + #else + #define __BEGIN_DECLS \ + extern "C" \ + { + #define __END_DECLS } + #endif #else -#define __BEGIN_DECLS -#define __END_DECLS + #define __BEGIN_DECLS + #define __END_DECLS #endif #ifndef __BOUNDED_POINTERS__ From 1ed0e09c9805c0c558593f361eb471da0d441135 Mon Sep 17 00:00:00 2001 From: MigthyMax Date: Sun, 25 Feb 2024 11:38:22 +0100 Subject: [PATCH 07/12] Removed from pthread.h implementation details --- library/include/dos.h | 3 +++ library/include/pthread.h | 26 ++++---------------------- library/include/semaphore.h | 9 +-------- library/pthread/common.h | 32 ++++++++++++++++++++++++++++++++ library/rt/aio_misc.h | 1 + 5 files changed, 41 insertions(+), 30 deletions(-) diff --git a/library/include/dos.h b/library/include/dos.h index 605548a2..e439be4c 100644 --- a/library/include/dos.h +++ b/library/include/dos.h @@ -14,6 +14,9 @@ #include #include #include +#include +#include +#include /* Category name handling variables. */ #define NUM_LOCALES (LC_MAX + 1) diff --git a/library/include/pthread.h b/library/include/pthread.h index f4c4f1a3..3988f601 100644 --- a/library/include/pthread.h +++ b/library/include/pthread.h @@ -26,8 +26,6 @@ #include #include #include -#include -#include #include #include @@ -144,12 +142,7 @@ struct pthread_mutexattr { typedef struct pthread_mutexattr pthread_mutexattr_t; -struct pthread_mutex { - APTR mutex; - int kind; - int incond; - struct Task *owner; -}; +struct pthread_mutex; typedef struct pthread_mutex pthread_mutex_t; @@ -173,11 +166,7 @@ struct pthread_condattr { typedef struct pthread_condattr pthread_condattr_t; -struct pthread_cond { - int pad1; - struct SignalSemaphore semaphore; - struct MinList waiters; -}; +struct pthread_cond; typedef struct pthread_cond pthread_cond_t; @@ -195,12 +184,7 @@ struct pthread_barrierattr { typedef struct pthread_barrierattr pthread_barrierattr_t; -struct pthread_barrier { - unsigned int curr_height; - unsigned int total_height; - pthread_cond_t breeched; - pthread_mutex_t lock; -}; +struct pthread_barrier; typedef struct pthread_barrier pthread_barrier_t; @@ -214,9 +198,7 @@ struct pthread_rwlockattr { typedef struct pthread_rwlockattr pthread_rwlockattr_t; -struct pthread_rwlock { - struct SignalSemaphore semaphore; -}; +struct pthread_rwlock; typedef struct pthread_rwlock pthread_rwlock_t; diff --git a/library/include/semaphore.h b/library/include/semaphore.h index 8ccbc43e..91b92932 100755 --- a/library/include/semaphore.h +++ b/library/include/semaphore.h @@ -40,14 +40,7 @@ #undef SEM_FAILED #define SEM_FAILED ((sem_t *)(-1)) -struct sema -{ - struct Node node; - int value; - int waiters_count; - pthread_mutex_t lock; - pthread_cond_t count_nonzero; -}; +struct sema; typedef struct sema sem_t; diff --git a/library/pthread/common.h b/library/pthread/common.h index 4cd3b92b..15fd3cc0 100644 --- a/library/pthread/common.h +++ b/library/pthread/common.h @@ -6,6 +6,38 @@ #include "pthread.h" #include +struct pthread_mutex { + APTR mutex; + int kind; + int incond; + struct Task *owner; +}; + +struct pthread_cond { + int pad1; + struct SignalSemaphore semaphore; + struct MinList waiters; +}; + +struct pthread_barrier { + unsigned int curr_height; + unsigned int total_height; + pthread_cond_t breeched; + pthread_mutex_t lock; +}; + +struct pthread_rwlock { + struct SignalSemaphore semaphore; +}; + +struct sema { + struct Node node; + int value; + int waiters_count; + pthread_mutex_t lock; + pthread_cond_t count_nonzero; +}; + #undef NEWLIST #define NEWLIST(_l) \ do \ diff --git a/library/rt/aio_misc.h b/library/rt/aio_misc.h index 339a49a6..7a521435 100644 --- a/library/rt/aio_misc.h +++ b/library/rt/aio_misc.h @@ -22,6 +22,7 @@ #include #include +#include "../pthread/common.h" #include "clist.h" /* Used by aio functions */ From 4d99f8a1253c9f91accfa5f26e911a4afec22c8f Mon Sep 17 00:00:00 2001 From: MigthyMax Date: Sun, 25 Feb 2024 11:38:42 +0100 Subject: [PATCH 08/12] Even removed SDK include form malloc.h header --- library/include/malloc.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/library/include/malloc.h b/library/include/malloc.h index f6f3a079..ca0dceae 100755 --- a/library/include/malloc.h +++ b/library/include/malloc.h @@ -9,8 +9,6 @@ #include #include -#include -#include #define SIZE_ALIGN (4 * sizeof(size_t)) #define SIZE_MASK (-SIZE_ALIGN) From eaa0d5d09a3bfa942cdfa19284391773e1fdf122 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrea=20Palmat=C3=A8?= Date: Wed, 20 Mar 2024 20:01:42 +0100 Subject: [PATCH 09/12] Reverted back libprofile that was causing profiling to start always even when not compiled with -pg --- GNUmakefile.os4 | 1 + libc.gmk | 4 --- libcrypt.gmk | 3 --- libprofile.gmk | 57 +++++++++++++++++++++++++++++++++++++++ library/termios/tcdrain.c | 1 - 5 files changed, 58 insertions(+), 8 deletions(-) create mode 100644 libprofile.gmk diff --git a/GNUmakefile.os4 b/GNUmakefile.os4 index 206cdfed..0fa5b008 100644 --- a/GNUmakefile.os4 +++ b/GNUmakefile.os4 @@ -153,6 +153,7 @@ include libm.gmk include libamiga.gmk include libdebug.gmk include libpthread.gmk +include libprofile.gmk include libcrypt.gmk include librt.gmk include shared.gmk diff --git a/libc.gmk b/libc.gmk index 4bb02eb0..b5c986a6 100755 --- a/libc.gmk +++ b/libc.gmk @@ -953,10 +953,6 @@ C_LIBRARY := \ C_LIB := \ c.lib_rev.o \ - profile/_mcount.o \ - profile/profil.o \ - profile/gmon.o \ - profile/mcount.o \ shared_library/stubs.o \ unistd/getopt.o \ unistd/getopt_long.o \ diff --git a/libcrypt.gmk b/libcrypt.gmk index 1bbb1c2d..4749bebf 100644 --- a/libcrypt.gmk +++ b/libcrypt.gmk @@ -1,9 +1,6 @@ # # $Id: libcrypt.gmk,v 1.0 2022-07-09 10:09:48 clib4devs Exp $ # -# :ts=8 -# -# -*- mode: makefile; -*- OUT_STATIC := $(BUILD_DIR)/obj/libcrypt OUT_SHARED := $(BUILD_DIR)/obj.shared/libcrypt diff --git a/libprofile.gmk b/libprofile.gmk new file mode 100644 index 00000000..663a4825 --- /dev/null +++ b/libprofile.gmk @@ -0,0 +1,57 @@ +# +# $Id: libprofile.gmk,v 1.1 2006-09-17 17:37:27 clib2devs Exp $ +# + +OUT_STATIC := $(BUILD_DIR)/obj/libprofile +OUT_SHARED := $(BUILD_DIR)/obj.shared/libprofile + +ifeq ($(SHARED),yes) + LIBS += $(OUTPUT_LIB)/libprofile.so +endif +ifeq ($(STATIC),yes) + LIBS += $(OUTPUT_LIB)/libprofile.a +endif + +PROFILE_LIB = \ + profile/_mcount.o \ + profile/profil.o \ + profile/gmon.o \ + profile/mcount.o + +SOURCES_SHARED = $(addprefix $(OUT_SHARED)/, $(PROFILE_LIB)) +SOURCES_STATIC = $(addprefix $(OUT_STATIC)/, $(PROFILE_LIB)) + +# Dependencies to rebuild if the library version changes + +$(OUT_STATIC)/profile.lib_rev.o : $(LIB_DIR)/profile/profile.lib_rev.c $(LIB_DIR)/profile/profile.lib_rev.h +$(OUT_SHARED)/profile.lib_rev.o : $(LIB_DIR)/profile/profile.lib_rev.c $(LIB_DIR)/profile/profile.lib_rev.h + +$(OUT_STATIC)/%.o : AFLAGS += $(LARGEDATA) +$(OUT_SHARED)/%.o : AFLAGS += $(PIC) $(LARGEDATA) + +$(OUT_STATIC)/%.o : $(LIB_DIR)/%.S + $(VERBOSE)$(ASSEMBLE) + +$(OUT_SHARED)/%.o : $(LIB_DIR)/%.S + $(VERBOSE)$(ASSEMBLE) + +$(OUT_STATIC)/%.o : CFLAGS += $(LARGEDATA) +$(OUT_SHARED)/%.o : CFLAGS += $(PIC) $(LARGEDATA) + +ifdef SPE +$(OUT_STATIC)/%.o : $(LIB_DIR)/%.c + $(VERBOSE)$(COMPILE_SPE) +$(OUT_SHARED)/%.o : $(LIB_DIR)/%.c + $(VERBOSE)$(COMPILE_SHARED_SPE) +else +$(OUT_STATIC)/%.o : $(LIB_DIR)/%.c + $(VERBOSE)$(COMPILE) +$(OUT_SHARED)/%.o : $(LIB_DIR)/%.c + $(VERBOSE)$(COMPILE_SHARED) +endif + +$(OUTPUT_LIB)/libprofile.a : $(SOURCES_STATIC) + $(VERBOSE)$(MAKELIB) + +$(OUTPUT_LIB)/libprofile.so : $(OUTPUT_LIB)/libc.so $(SOURCES_SHARED) + $(VERBOSE)$(MAKESHARED) \ No newline at end of file diff --git a/library/termios/tcdrain.c b/library/termios/tcdrain.c index ceec20f0..d6ebff51 100644 --- a/library/termios/tcdrain.c +++ b/library/termios/tcdrain.c @@ -55,7 +55,6 @@ tcdrain(int file_descriptor) { break; default: /* TODO: Serial port support. */ - printf("cicero1b\n"); __fd_unlock(fd); __set_errno(ENXIO); goto out; From 0e4b52970c5d23fa83faf57a8973b734be85078c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrea=20Palmat=C3=A8?= Date: Wed, 20 Mar 2024 20:05:23 +0100 Subject: [PATCH 10/12] Bumped control file to the latest repository version --- misc/control | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/misc/control b/misc/control index b7be7c2b..a7574687 100644 --- a/misc/control +++ b/misc/control @@ -1,5 +1,5 @@ Package: amigaos4-clib4 -Version: 1.0.0 +Version: 1.0.2 Maintainer: Andrea Palmatè Architecture: amd64 Section: libdevel From ea377395e56104b414991cd3ae5d817cbcad4f36 Mon Sep 17 00:00:00 2001 From: MigthyMax Date: Thu, 21 Mar 2024 10:27:20 +0100 Subject: [PATCH 11/12] Made pthread_mutex, pthread_cond, pthread_rwlock public again --- library/include/pthread.h | 19 +++++++++++++++---- library/pthread/common.h | 17 ----------------- library/pthread/pthread.c | 20 ++++++++++---------- library/pthread/pthread_cond_destroy.c | 12 +++++++----- library/pthread/pthread_cond_init.c | 16 +++++++++++++--- library/pthread/pthread_rwlock_destroy.c | 7 ++++--- library/pthread/pthread_rwlock_init.c | 7 +++++-- library/pthread/pthread_rwlock_rdlock.c | 4 ++-- library/pthread/pthread_rwlock_timedrdlock.c | 2 +- library/pthread/pthread_rwlock_timedwrlock.c | 2 +- library/pthread/pthread_rwlock_tryrdlock.c | 4 ++-- library/pthread/pthread_rwlock_trywrlock.c | 4 ++-- library/pthread/pthread_rwlock_unlock.c | 6 +++--- library/pthread/pthread_rwlock_wrlock.c | 6 +++--- 14 files changed, 68 insertions(+), 58 deletions(-) diff --git a/library/include/pthread.h b/library/include/pthread.h index 3988f601..0a65906b 100644 --- a/library/include/pthread.h +++ b/library/include/pthread.h @@ -142,7 +142,12 @@ struct pthread_mutexattr { typedef struct pthread_mutexattr pthread_mutexattr_t; -struct pthread_mutex; +struct pthread_mutex { + void* mutex; // APTR + int kind; + int incond; + struct Task *owner; +}; typedef struct pthread_mutex pthread_mutex_t; @@ -166,11 +171,15 @@ struct pthread_condattr { typedef struct pthread_condattr pthread_condattr_t; -struct pthread_cond; +struct pthread_cond { + int pad1; + void* semaphore; // SignalSemaphore + void* waiters; // MinList +}; typedef struct pthread_cond pthread_cond_t; -#define PTHREAD_COND_INITIALIZER {0, NULL_SEMAPHORE, NULL_MINLIST} +#define PTHREAD_COND_INITIALIZER {0, 0, 0} // // Barriers @@ -198,7 +207,9 @@ struct pthread_rwlockattr { typedef struct pthread_rwlockattr pthread_rwlockattr_t; -struct pthread_rwlock; +struct pthread_rwlock { + void* semaphore; // SignalSemaphore +}; typedef struct pthread_rwlock pthread_rwlock_t; diff --git a/library/pthread/common.h b/library/pthread/common.h index 15fd3cc0..2c8f7716 100644 --- a/library/pthread/common.h +++ b/library/pthread/common.h @@ -6,19 +6,6 @@ #include "pthread.h" #include -struct pthread_mutex { - APTR mutex; - int kind; - int incond; - struct Task *owner; -}; - -struct pthread_cond { - int pad1; - struct SignalSemaphore semaphore; - struct MinList waiters; -}; - struct pthread_barrier { unsigned int curr_height; unsigned int total_height; @@ -26,10 +13,6 @@ struct pthread_barrier { pthread_mutex_t lock; }; -struct pthread_rwlock { - struct SignalSemaphore semaphore; -}; - struct sema { struct Node node; int value; diff --git a/library/pthread/pthread.c b/library/pthread/pthread.c index 5003b312..721ebbb4 100644 --- a/library/pthread/pthread.c +++ b/library/pthread/pthread.c @@ -152,7 +152,7 @@ _pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const stru return EINVAL; // initialize static conditions - if (SemaphoreIsInvalid(&cond->semaphore)) + if (SemaphoreIsInvalid(cond->semaphore)) pthread_cond_init(cond, NULL); task = FindTask(NULL); @@ -194,9 +194,9 @@ _pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const stru sigs |= 1 << waiter.sigbit; // add it to the end of the list - ObtainSemaphore(&cond->semaphore); - AddTail((struct List *) &cond->waiters, (struct Node *) &waiter); - ReleaseSemaphore(&cond->semaphore); + ObtainSemaphore(cond->semaphore); + AddTail((struct List *) cond->waiters, (struct Node *) &waiter); + ReleaseSemaphore(cond->semaphore); // wait for the condition to be signalled or the timeout mutex->incond++; @@ -206,9 +206,9 @@ _pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const stru mutex->incond--; // remove the node from the list - ObtainSemaphore(&cond->semaphore); + ObtainSemaphore(cond->semaphore); Remove((struct Node *) &waiter); - ReleaseSemaphore(&cond->semaphore); + ReleaseSemaphore(cond->semaphore); if (waiter.sigbit != SIGB_COND_FALLBACK) FreeSignal(waiter.sigbit); @@ -238,16 +238,16 @@ _pthread_cond_broadcast(pthread_cond_t *cond, BOOL onlyfirst) { return EINVAL; // initialize static conditions - if (SemaphoreIsInvalid(&cond->semaphore)) + if (SemaphoreIsInvalid(cond->semaphore)) pthread_cond_init(cond, NULL); // signal the waiting threads - ObtainSemaphore(&cond->semaphore); - ForeachNode(&cond->waiters, waiter) { + ObtainSemaphore(cond->semaphore); + ForeachNode(cond->waiters, waiter) { Signal(waiter->task, 1 << waiter->sigbit); if (onlyfirst) break; } - ReleaseSemaphore(&cond->semaphore); + ReleaseSemaphore(cond->semaphore); return 0; } diff --git a/library/pthread/pthread_cond_destroy.c b/library/pthread/pthread_cond_destroy.c index a4a90745..80ba6883 100644 --- a/library/pthread/pthread_cond_destroy.c +++ b/library/pthread/pthread_cond_destroy.c @@ -43,18 +43,20 @@ pthread_cond_destroy(pthread_cond_t *cond) { return EINVAL; // probably a statically allocated condition - if (SemaphoreIsInvalid(&cond->semaphore)) + if (SemaphoreIsInvalid(cond->semaphore)) return 0; - if (AttemptSemaphore(&cond->semaphore) == FALSE) + if (AttemptSemaphore(cond->semaphore) == FALSE) return EBUSY; - if (!IsMinListEmpty(&cond->waiters)) { - ReleaseSemaphore(&cond->semaphore); + if (!IsMinListEmpty((struct MinList *)cond->waiters)) { + ReleaseSemaphore(cond->semaphore); return EBUSY; } - ReleaseSemaphore(&cond->semaphore); + ReleaseSemaphore(cond->semaphore); + FreeSysObject(ASOT_SEMAPHORE,cond->semaphore); + free(cond->waiters); memset(cond, 0, sizeof(pthread_cond_t)); return 0; diff --git a/library/pthread/pthread_cond_init.c b/library/pthread/pthread_cond_init.c index b57e0cd2..b1023da3 100644 --- a/library/pthread/pthread_cond_init.c +++ b/library/pthread/pthread_cond_init.c @@ -40,11 +40,21 @@ int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr) { (void) attr; - if (cond == NULL) + if (cond == NULL) { return EINVAL; + } - InitSemaphore(&cond->semaphore); - NewMinList(&cond->waiters); + cond->semaphore = AllocSysObjectTags(ASOT_SEMAPHORE,TAG_END); + if (!cond->semaphore) + return ENOMEM; + + cond->waiters = malloc(sizeof(struct MinList)); + if(!cond->waiters) { + FreeSysObject(ASOT_SEMAPHORE,cond->semaphore); + + return ENOMEM; + } + NewMinList(cond->waiters); return 0; } \ No newline at end of file diff --git a/library/pthread/pthread_rwlock_destroy.c b/library/pthread/pthread_rwlock_destroy.c index dfe76ffc..5355a7c0 100644 --- a/library/pthread/pthread_rwlock_destroy.c +++ b/library/pthread/pthread_rwlock_destroy.c @@ -43,13 +43,14 @@ pthread_rwlock_destroy(pthread_rwlock_t *lock) { return EINVAL; // probably a statically allocated rwlock - if (SemaphoreIsInvalid(&lock->semaphore)) + if (SemaphoreIsInvalid(lock->semaphore)) return 0; - if (AttemptSemaphore(&lock->semaphore) == FALSE) + if (AttemptSemaphore(lock->semaphore) == FALSE) return EBUSY; - ReleaseSemaphore(&lock->semaphore); + ReleaseSemaphore(lock->semaphore); + FreeSysObject(ASOT_SEMAPHORE,lock->semaphore); memset(lock, 0, sizeof(pthread_rwlock_t)); return 0; diff --git a/library/pthread/pthread_rwlock_init.c b/library/pthread/pthread_rwlock_init.c index 7598bf11..e6f6184e 100644 --- a/library/pthread/pthread_rwlock_init.c +++ b/library/pthread/pthread_rwlock_init.c @@ -41,10 +41,13 @@ int pthread_rwlock_init(pthread_rwlock_t *lock, const pthread_rwlockattr_t *attr) { (void) (attr); - if (lock == NULL) + if (lock == NULL) { return EINVAL; + } - InitSemaphore(&lock->semaphore); + lock->semaphore = AllocSysObjectTags(ASOT_SEMAPHORE,TAG_END); + if (!lock->semaphore) + return ENOMEM; return 0; } \ No newline at end of file diff --git a/library/pthread/pthread_rwlock_rdlock.c b/library/pthread/pthread_rwlock_rdlock.c index 5cfa0f05..f8f4d9f2 100644 --- a/library/pthread/pthread_rwlock_rdlock.c +++ b/library/pthread/pthread_rwlock_rdlock.c @@ -45,7 +45,7 @@ pthread_rwlock_rdlock(pthread_rwlock_t *lock) { pthread_testcancel(); // initialize static rwlocks - if (SemaphoreIsInvalid(&lock->semaphore)) + if (SemaphoreIsInvalid(lock->semaphore)) pthread_rwlock_init(lock, NULL); // "Results are undefined if the calling thread holds a write lock on rwlock at the time the call is made." @@ -55,7 +55,7 @@ pthread_rwlock_rdlock(pthread_rwlock_t *lock) { return EDEADLK; */ - ObtainSemaphoreShared(&lock->semaphore); + ObtainSemaphoreShared(lock->semaphore); return 0; } \ No newline at end of file diff --git a/library/pthread/pthread_rwlock_timedrdlock.c b/library/pthread/pthread_rwlock_timedrdlock.c index 8e0f31e3..26c143fb 100644 --- a/library/pthread/pthread_rwlock_timedrdlock.c +++ b/library/pthread/pthread_rwlock_timedrdlock.c @@ -55,5 +55,5 @@ pthread_rwlock_timedrdlock(pthread_rwlock_t *lock, const struct timespec *abstim if (result != EBUSY) return result; - return _pthread_obtain_sema_timed(&lock->semaphore, abstime, SM_SHARED); + return _pthread_obtain_sema_timed((struct SignalSemaphore *)lock->semaphore, abstime, SM_SHARED); } \ No newline at end of file diff --git a/library/pthread/pthread_rwlock_timedwrlock.c b/library/pthread/pthread_rwlock_timedwrlock.c index 72737536..47f8e3dd 100644 --- a/library/pthread/pthread_rwlock_timedwrlock.c +++ b/library/pthread/pthread_rwlock_timedwrlock.c @@ -55,5 +55,5 @@ pthread_rwlock_timedwrlock(pthread_rwlock_t *lock, const struct timespec *abstim if (result != EBUSY) return result; - return _pthread_obtain_sema_timed(&lock->semaphore, abstime, SM_EXCLUSIVE); + return _pthread_obtain_sema_timed((struct SignalSemaphore *)lock->semaphore, abstime, SM_EXCLUSIVE); } \ No newline at end of file diff --git a/library/pthread/pthread_rwlock_tryrdlock.c b/library/pthread/pthread_rwlock_tryrdlock.c index b4b79039..1e051e60 100644 --- a/library/pthread/pthread_rwlock_tryrdlock.c +++ b/library/pthread/pthread_rwlock_tryrdlock.c @@ -45,10 +45,10 @@ pthread_rwlock_tryrdlock(pthread_rwlock_t *lock) { return EINVAL; // initialize static rwlocks - if (SemaphoreIsInvalid(&lock->semaphore)) + if (SemaphoreIsInvalid(lock->semaphore)) pthread_rwlock_init(lock, NULL); - ret = AttemptSemaphoreShared(&lock->semaphore); + ret = AttemptSemaphoreShared(lock->semaphore); return (ret == TRUE) ? 0 : EBUSY; } \ No newline at end of file diff --git a/library/pthread/pthread_rwlock_trywrlock.c b/library/pthread/pthread_rwlock_trywrlock.c index c5a2fb34..cc7388cf 100644 --- a/library/pthread/pthread_rwlock_trywrlock.c +++ b/library/pthread/pthread_rwlock_trywrlock.c @@ -45,10 +45,10 @@ pthread_rwlock_trywrlock(pthread_rwlock_t *lock) { return EINVAL; // initialize static rwlocks - if (SemaphoreIsInvalid(&lock->semaphore)) + if (SemaphoreIsInvalid(lock->semaphore)) pthread_rwlock_init(lock, NULL); - ret = AttemptSemaphore(&lock->semaphore); + ret = AttemptSemaphore(lock->semaphore); return (ret == TRUE) ? 0 : EBUSY; } \ No newline at end of file diff --git a/library/pthread/pthread_rwlock_unlock.c b/library/pthread/pthread_rwlock_unlock.c index 903f1fdf..a8ebebed 100644 --- a/library/pthread/pthread_rwlock_unlock.c +++ b/library/pthread/pthread_rwlock_unlock.c @@ -43,16 +43,16 @@ pthread_rwlock_unlock(pthread_rwlock_t *lock) { return EINVAL; // initialize static rwlocks - if (SemaphoreIsInvalid(&lock->semaphore)) + if (SemaphoreIsInvalid(lock->semaphore)) pthread_rwlock_init(lock, NULL); //if (!SemaphoreIsMine(&lock->semaphore)) // if no one has obtained the semaphore don't unlock the rwlock // this can be a leap of faith because we don't maintain a separate list of readers - if (lock->semaphore.ss_NestCount < 1) + if (((struct SignalSemaphore *)lock->semaphore)->ss_NestCount < 1) return EPERM; - ReleaseSemaphore(&lock->semaphore); + ReleaseSemaphore(lock->semaphore); return 0; } \ No newline at end of file diff --git a/library/pthread/pthread_rwlock_wrlock.c b/library/pthread/pthread_rwlock_wrlock.c index dd527e7e..8eb925eb 100644 --- a/library/pthread/pthread_rwlock_wrlock.c +++ b/library/pthread/pthread_rwlock_wrlock.c @@ -45,13 +45,13 @@ pthread_rwlock_wrlock(pthread_rwlock_t *lock) { pthread_testcancel(); // initialize static rwlocks - if (SemaphoreIsInvalid(&lock->semaphore)) + if (SemaphoreIsInvalid(lock->semaphore)) pthread_rwlock_init(lock, NULL); - if (SemaphoreIsMine(&lock->semaphore)) + if (SemaphoreIsMine(lock->semaphore)) return EDEADLK; - ObtainSemaphore(&lock->semaphore); + ObtainSemaphore(lock->semaphore); return 0; } \ No newline at end of file From 66a8072bd3fdaf71fd836048f56e681aa3b9de0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrea=20Palmat=C3=A8?= Date: Thu, 21 Mar 2024 11:19:30 +0100 Subject: [PATCH 12/12] Added pthread_cond test --- library/c.lib_rev.h | 6 +-- test_programs/threads/cond.c | 80 ++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 test_programs/threads/cond.c diff --git a/library/c.lib_rev.h b/library/c.lib_rev.h index f8519237..28f0f301 100755 --- a/library/c.lib_rev.h +++ b/library/c.lib_rev.h @@ -2,7 +2,7 @@ #define REVISION 0 #define SUBREVISION 0 -#define DATE "27.09.2023" +#define DATE "21.03.2024" #define VERS "clib4.library 1.0.0" -#define VSTRING "clib4.library 1.0.0 (27.09.2023)\r\n" -#define VERSTAG "\0$VER: clib4.library 1.0.0 (27.09.2023)" \ No newline at end of file +#define VSTRING "clib4.library 1.0.0 (21.03.2024)\r\n" +#define VERSTAG "\0$VER: clib4.library 1.0.0 (21.03.2024)" \ No newline at end of file diff --git a/test_programs/threads/cond.c b/test_programs/threads/cond.c new file mode 100644 index 00000000..d30dfd7b --- /dev/null +++ b/test_programs/threads/cond.c @@ -0,0 +1,80 @@ +#include +#include +#include + +/* Compile like this: + ppc-amigaos-gcc -mcrt=clib4 --std=c99 cond.c -o cond -lpthread +*/ + +const size_t NUMTHREADS = 20; + +/* a global count of the number of threads finished working. It will + be protected by mutex and changes to it will be signalled to the + main thread via cond */ + +int done = 0; +pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; +pthread_cond_t cond = PTHREAD_COND_INITIALIZER; + +/* Note: error checking on pthread_X calls ommitted for clarity - you + should always check the return values in real code. */ + +/* Note: passing the thread id via a void pointer is cheap and easy, + * but the code assumes pointers and long ints are the same size + * (probably 64bits), which is a little hacky. */ + +void *ThreadEntry(void *id) { + const int myid = (long) id; // force the pointer to be a 64bit integer + + const int workloops = 5; + for (int i = 0; i < workloops; i++) { + printf("[thread %d] working (%d/%d)\n", myid, i, workloops); + sleep(1); // simulate doing some costly work + } + + // we're going to manipulate done and use the cond, so we need the mutex + pthread_mutex_lock(&mutex); + + // increase the count of threads that have finished their work. + done++; + printf("[thread %d] done is now %d. Signalling cond.\n", myid, done); + + // wait up the main thread (if it is sleeping) to test the value of done + pthread_cond_signal(&cond); + pthread_mutex_unlock(&mutex); + + return NULL; +} + +int main(int argc, char **argv) { + puts("[thread main] starting"); + + pthread_t threads[NUMTHREADS]; + + for (int t = 0; t < NUMTHREADS; t++) + pthread_create(&threads[t], NULL, ThreadEntry, (void *) (long) t); + + // we're going to test "done" so we need the mutex for safety + pthread_mutex_lock(&mutex); + + // are the other threads still busy? + while (done < NUMTHREADS) { + printf("[thread main] done is %d which is < %d so waiting on cond\n", + done, (int) NUMTHREADS); + + /* block this thread until another thread signals cond. While + blocked, the mutex is released, then re-aquired before this + thread is woken up and the call returns. */ + pthread_cond_wait(&cond, &mutex); + + puts("[thread main] wake - cond was signalled."); + + /* we go around the loop with the lock held */ + } + + printf("[thread main] done == %d so everyone is done\n", (int) NUMTHREADS); + + pthread_mutex_unlock(&mutex); + + return 0; +} \ No newline at end of file