From ec3d921f7b716e7d0b56652649e680c9194072f3 Mon Sep 17 00:00:00 2001 From: Olaf Barthel Date: Thu, 3 Mar 2005 14:20:55 +0000 Subject: [PATCH] - Moved the signal semaphore allocation/initialization/deallocation into a dedicated module. This also has the advantage that it's harder to break code by accidentally forgetting to call InitSemaphore() after having allocated the memory for it. git-svn-id: file:///Users/olsen/Code/migration-svn-zu-git/logical-line-staging/clib2/trunk@14853 87f5fb63-7c3d-0410-a384-fd976d0f7a62 --- library/GNUmakefile.68k | 3 +- library/GNUmakefile.os4 | 3 +- library/changes | 5 +++ library/dirent_data.c | 8 ++-- library/fcntl_open.c | 6 +-- library/locale_init_exit.c | 8 ++-- library/smakefile | 7 +-- library/socket_accept.c | 8 ++-- library/socket_hook_entry.c | 4 +- library/socket_init_exit.c | 8 ++-- library/socket_socket.c | 8 ++-- library/stdio_fclose.c | 6 +-- library/stdio_fdhookentry.c | 4 +- library/stdio_init_exit.c | 13 +++--- library/stdio_lock.c | 15 ++----- library/stdio_openiob.c | 6 +-- library/stdlib_malloc.c | 8 ++-- library/stdlib_protos.h | 16 +++++-- library/stdlib_semaphore.c | 86 +++++++++++++++++++++++++++++++++++++ 19 files changed, 148 insertions(+), 74 deletions(-) create mode 100644 library/stdlib_semaphore.c diff --git a/library/GNUmakefile.68k b/library/GNUmakefile.68k index e17234a..a7c5f15 100644 --- a/library/GNUmakefile.68k +++ b/library/GNUmakefile.68k @@ -1,5 +1,5 @@ # -# $Id: GNUmakefile.68k,v 1.33 2005-03-02 12:57:53 obarthel Exp $ +# $Id: GNUmakefile.68k,v 1.34 2005-03-03 14:20:55 obarthel Exp $ # # :ts=8 # @@ -317,6 +317,7 @@ C_LIB = \ stdlib_rand_r.o \ stdlib_realloc.o \ stdlib_red_black.o \ + stdlib_semaphore.o \ stdlib_setenv.o \ stdlib_setjmp.o \ stdlib_set_errno.o \ diff --git a/library/GNUmakefile.os4 b/library/GNUmakefile.os4 index 8ecce5a..573ef65 100644 --- a/library/GNUmakefile.os4 +++ b/library/GNUmakefile.os4 @@ -1,5 +1,5 @@ # -# $Id: GNUmakefile.os4,v 1.33 2005-03-02 12:57:53 obarthel Exp $ +# $Id: GNUmakefile.os4,v 1.34 2005-03-03 14:20:55 obarthel Exp $ # # :ts=8 # @@ -321,6 +321,7 @@ C_LIB = \ stdlib_rand_r.o \ stdlib_realloc.o \ stdlib_red_black.o \ + stdlib_semaphore.o \ stdlib_setenv.o \ stdlib_setjmp.o \ stdlib_set_errno.o \ diff --git a/library/changes b/library/changes index 82544bf..f8cf1b5 100644 --- a/library/changes +++ b/library/changes @@ -91,6 +91,11 @@ - The shell command parameter parser now considers the non-breaking space character (ISO code 160) to be a blank space character, too. +- Moved the signal semaphore allocation/initialization/deallocation + into a dedicated module. This also has the advantage that it's + harder to break code by accidentally forgetting to call + InitSemaphore() after having allocated the memory for it. + c.lib 1.188 (7.2.2005) diff --git a/library/dirent_data.c b/library/dirent_data.c index 733f2c3..561f832 100644 --- a/library/dirent_data.c +++ b/library/dirent_data.c @@ -1,5 +1,5 @@ /* - * $Id: dirent_data.c,v 1.7 2005-02-28 10:07:30 obarthel Exp $ + * $Id: dirent_data.c,v 1.8 2005-03-03 14:20:55 obarthel Exp $ * * :ts=4 * @@ -82,11 +82,9 @@ CLIB_CONSTRUCTOR(__dirent_init) #if defined(__THREAD_SAFE) { - dirent_lock = AllocVec(sizeof(*dirent_lock),MEMF_ANY|MEMF_PUBLIC); + dirent_lock = __create_semaphore(); if(dirent_lock == NULL) goto out; - - InitSemaphore(dirent_lock); } #endif /* __THREAD_SAFE */ @@ -116,7 +114,7 @@ CLIB_DESTRUCTOR(__dirent_exit) #if defined(__THREAD_SAFE) { - FreeVec(dirent_lock); + __delete_semaphore(dirent_lock); dirent_lock = NULL; } #endif /* __THREAD_SAFE */ diff --git a/library/fcntl_open.c b/library/fcntl_open.c index 020daea..da1e18c 100644 --- a/library/fcntl_open.c +++ b/library/fcntl_open.c @@ -1,5 +1,5 @@ /* - * $Id: fcntl_open.c,v 1.12 2005-02-28 13:22:53 obarthel Exp $ + * $Id: fcntl_open.c,v 1.13 2005-03-03 14:20:55 obarthel Exp $ * * :ts=4 * @@ -331,14 +331,12 @@ open(const char *path_name, int open_flag, ... /* mode_t mode */ ) #if defined(__THREAD_SAFE) { - fd_lock = AllocVec(sizeof(*fd_lock),MEMF_ANY|MEMF_PUBLIC); + fd_lock = __create_semaphore(); if(fd_lock == NULL) { __set_errno(ENOMEM); goto out; } - - InitSemaphore(fd_lock); } #else { diff --git a/library/locale_init_exit.c b/library/locale_init_exit.c index 78ce026..56e2b03 100644 --- a/library/locale_init_exit.c +++ b/library/locale_init_exit.c @@ -1,5 +1,5 @@ /* - * $Id: locale_init_exit.c,v 1.7 2005-02-28 10:07:30 obarthel Exp $ + * $Id: locale_init_exit.c,v 1.8 2005-03-03 14:20:55 obarthel Exp $ * * :ts=4 * @@ -222,7 +222,7 @@ CLIB_DESTRUCTOR(__locale_exit_destructor) #if defined(__THREAD_SAFE) { - FreeVec(locale_lock); + __delete_semaphore(locale_lock); locale_lock = NULL; } #endif /* __THREAD_SAFE */ @@ -241,11 +241,9 @@ CLIB_CONSTRUCTOR(__locale_init_constructor) #if defined(__THREAD_SAFE) { - locale_lock = AllocVec(sizeof(*locale_lock),MEMF_ANY|MEMF_PUBLIC); + locale_lock = __create_semaphore(); if(locale_lock == NULL) goto out; - - InitSemaphore(locale_lock); } #endif /* __THREAD_SAFE */ diff --git a/library/smakefile b/library/smakefile index 1663281..ece54be 100644 --- a/library/smakefile +++ b/library/smakefile @@ -1,5 +1,5 @@ -_# -# $Id: smakefile,v 1.27 2005-03-02 12:57:53 obarthel Exp $ +# +# $Id: smakefile,v 1.28 2005-03-03 14:20:55 obarthel Exp $ # # :ts=8 # @@ -36,7 +36,7 @@ INCLUDE_DIR = V:include CPU = cpu=060 MATH = define=IEEE_FLOATING_POINT_SUPPORT math=IEEE SUPPORT = define=UNIX_PATH_SEMANTICS define=SOCKET_SUPPORT define=USERGROUP_SUPPORT \ - define=__C_MACROS__ + define=__C_MACROS__ define=__THREAD_SAFE ############################################################################## @@ -395,6 +395,7 @@ STDLIB_OBJ = \ stdlib_dosbase.o \ stdlib_get_errno.o \ stdlib_set_errno.o \ + stdlib_semaphore.o \ stdlib_sysbase.o \ stdlib_termination_message.o \ stdlib_threshold.o \ diff --git a/library/socket_accept.c b/library/socket_accept.c index 90221ad..b6342e9 100644 --- a/library/socket_accept.c +++ b/library/socket_accept.c @@ -1,5 +1,5 @@ /* - * $Id: socket_accept.c,v 1.7 2005-02-28 13:22:53 obarthel Exp $ + * $Id: socket_accept.c,v 1.8 2005-03-03 14:20:55 obarthel Exp $ * * :ts=4 * @@ -106,14 +106,12 @@ accept(int sockfd,struct sockaddr *cliaddr,int *addrlen) #if defined(__THREAD_SAFE) { - lock = AllocVec(sizeof(*lock),MEMF_ANY|MEMF_PUBLIC); + lock = __create_semaphore(); if(lock == NULL) { __set_errno(ENOMEM); goto out; } - - InitSemaphore(lock); } #endif /* __THREAD_SAFE */ @@ -141,7 +139,7 @@ accept(int sockfd,struct sockaddr *cliaddr,int *addrlen) __stdio_unlock(); - FreeVec(lock); + __delete_semaphore(lock); if(__check_abort_enabled) __check_abort(); diff --git a/library/socket_hook_entry.c b/library/socket_hook_entry.c index cd2ae84..7e729b1 100644 --- a/library/socket_hook_entry.c +++ b/library/socket_hook_entry.c @@ -1,5 +1,5 @@ /* - * $Id: socket_hook_entry.c,v 1.10 2005-02-28 13:22:53 obarthel Exp $ + * $Id: socket_hook_entry.c,v 1.11 2005-03-03 14:20:55 obarthel Exp $ * * :ts=4 * @@ -125,7 +125,7 @@ __socket_hook_entry( __fd_unlock(fd); /* Free the lock semaphore now. */ - FreeVec(fd->fd_Lock); + __delete_semaphore(fd->fd_Lock); /* And that's the last for this file descriptor. */ memset(fd,0,sizeof(*fd)); diff --git a/library/socket_init_exit.c b/library/socket_init_exit.c index 67d0e93..d063672 100644 --- a/library/socket_init_exit.c +++ b/library/socket_init_exit.c @@ -1,5 +1,5 @@ /* - * $Id: socket_init_exit.c,v 1.10 2005-02-28 13:22:53 obarthel Exp $ + * $Id: socket_init_exit.c,v 1.11 2005-03-03 14:20:55 obarthel Exp $ * * :ts=4 * @@ -257,11 +257,9 @@ __socket_init(void) { #if defined(__THREAD_SAFE) { - lock = AllocVec(sizeof(*lock),MEMF_ANY|MEMF_PUBLIC); + lock = __create_semaphore(); if(lock == NULL) goto out; - - InitSemaphore(lock); } #else { @@ -287,7 +285,7 @@ __socket_init(void) { SHOWMSG("could not duplicate daemon socket"); - FreeVec(lock); + __delete_semaphore(lock); __show_error("Network server streams could not be initialized."); goto out; diff --git a/library/socket_socket.c b/library/socket_socket.c index 9a94867..78d81a3 100644 --- a/library/socket_socket.c +++ b/library/socket_socket.c @@ -1,5 +1,5 @@ /* - * $Id: socket_socket.c,v 1.5 2005-02-28 13:22:53 obarthel Exp $ + * $Id: socket_socket.c,v 1.6 2005-03-03 14:20:55 obarthel Exp $ * * :ts=4 * @@ -73,14 +73,12 @@ socket(int domain,int type,int protocol) #if defined(__THREAD_SAFE) { - lock = AllocVec(sizeof(*lock),MEMF_ANY|MEMF_PUBLIC); + lock = __create_semaphore(); if(lock == NULL) { __set_errno(ENOMEM); goto out; } - - InitSemaphore(lock); } #endif /* __THREAD_SAFE */ @@ -106,7 +104,7 @@ socket(int domain,int type,int protocol) __stdio_unlock(); - FreeVec(lock); + __delete_semaphore(lock); if(__check_abort_enabled) __check_abort(); diff --git a/library/stdio_fclose.c b/library/stdio_fclose.c index f8f546a..1111bf8 100644 --- a/library/stdio_fclose.c +++ b/library/stdio_fclose.c @@ -1,5 +1,5 @@ /* - * $Id: stdio_fclose.c,v 1.7 2005-02-28 10:07:30 obarthel Exp $ + * $Id: stdio_fclose.c,v 1.8 2005-03-03 14:20:55 obarthel Exp $ * * :ts=4 * @@ -62,8 +62,6 @@ fclose(FILE *stream) assert( stream != NULL ); - assert( file->iob_Lock == NULL || file->iob_Lock->ss_Owner == NULL ); - if(__check_abort_enabled) __check_abort(); @@ -142,7 +140,7 @@ fclose(FILE *stream) free(file->iob_CustomBuffer); /* Free the lock semaphore now. */ - FreeVec(file->iob_Lock); + __delete_semaphore(file->iob_Lock); memset(file,0,sizeof(*file)); diff --git a/library/stdio_fdhookentry.c b/library/stdio_fdhookentry.c index ac46acd..39f995c 100644 --- a/library/stdio_fdhookentry.c +++ b/library/stdio_fdhookentry.c @@ -1,5 +1,5 @@ /* - * $Id: stdio_fdhookentry.c,v 1.15 2005-02-28 13:22:53 obarthel Exp $ + * $Id: stdio_fdhookentry.c,v 1.16 2005-03-03 14:20:55 obarthel Exp $ * * :ts=4 * @@ -356,7 +356,7 @@ __fd_hook_entry( __fd_unlock(fd); /* Free the lock semaphore now. */ - FreeVec(fd->fd_Lock); + __delete_semaphore(fd->fd_Lock); /* And that's the last for this file descriptor. */ memset(fd,0,sizeof(*fd)); diff --git a/library/stdio_init_exit.c b/library/stdio_init_exit.c index 84694ad..96d8251 100644 --- a/library/stdio_init_exit.c +++ b/library/stdio_init_exit.c @@ -1,5 +1,5 @@ /* - * $Id: stdio_init_exit.c,v 1.21 2005-02-28 13:22:53 obarthel Exp $ + * $Id: stdio_init_exit.c,v 1.22 2005-03-03 14:20:55 obarthel Exp $ * * :ts=4 * @@ -202,19 +202,16 @@ __stdio_init(void) { /* Allocate memory for an arbitration mechanism, then initialize it. */ - stdio_lock = AllocVec(sizeof(*stdio_lock),MEMF_ANY|MEMF_PUBLIC); - fd_lock = AllocVec(sizeof(*fd_lock),MEMF_ANY|MEMF_PUBLIC); + stdio_lock = __create_semaphore(); + fd_lock = __create_semaphore(); if(stdio_lock == NULL || fd_lock == NULL) { - FreeVec(stdio_lock); - FreeVec(fd_lock); + __delete_semaphore(stdio_lock); + __delete_semaphore(fd_lock); goto out; } - - InitSemaphore(stdio_lock); - InitSemaphore(fd_lock); } #else { diff --git a/library/stdio_lock.c b/library/stdio_lock.c index 9ca851f..9481d09 100644 --- a/library/stdio_lock.c +++ b/library/stdio_lock.c @@ -1,5 +1,5 @@ /* - * $Id: stdio_lock.c,v 1.2 2005-02-28 10:07:31 obarthel Exp $ + * $Id: stdio_lock.c,v 1.3 2005-03-03 14:20:55 obarthel Exp $ * * :ts=4 * @@ -70,13 +70,8 @@ __stdio_unlock(void) void __stdio_lock_exit(void) { - assert( stdio_lock == NULL || stdio_lock->ss_Owner == NULL ); - - if(stdio_lock != NULL) - { - FreeVec(stdio_lock); - stdio_lock = NULL; - } + __delete_semaphore(stdio_lock); + stdio_lock = NULL; } /****************************************************************************/ @@ -86,12 +81,10 @@ __stdio_lock_init(void) { int result = -1; - stdio_lock = AllocVec(sizeof(*stdio_lock),MEMF_ANY|MEMF_PUBLIC); + stdio_lock = __create_semaphore(); if(stdio_lock == NULL) goto out; - InitSemaphore(stdio_lock); - result = 0; out: diff --git a/library/stdio_openiob.c b/library/stdio_openiob.c index e98c527..90f8488 100644 --- a/library/stdio_openiob.c +++ b/library/stdio_openiob.c @@ -1,5 +1,5 @@ /* - * $Id: stdio_openiob.c,v 1.10 2005-02-28 10:07:31 obarthel Exp $ + * $Id: stdio_openiob.c,v 1.11 2005-03-03 14:20:55 obarthel Exp $ * * :ts=4 * @@ -169,11 +169,9 @@ __open_iob(const char *filename, const char *mode, int file_descriptor, int slot { /* Allocate memory for an arbitration mechanism, then initialize it. */ - lock = AllocVec(sizeof(*lock),MEMF_ANY|MEMF_PUBLIC); + lock = __create_semaphore(); if(lock == NULL) goto out; - - InitSemaphore(lock); } #else { diff --git a/library/stdlib_malloc.c b/library/stdlib_malloc.c index 43e6e69..9511997 100644 --- a/library/stdlib_malloc.c +++ b/library/stdlib_malloc.c @@ -1,5 +1,5 @@ /* - * $Id: stdlib_malloc.c,v 1.9 2005-02-28 10:07:32 obarthel Exp $ + * $Id: stdlib_malloc.c,v 1.10 2005-03-03 14:20:55 obarthel Exp $ * * :ts=4 * @@ -358,7 +358,7 @@ __memory_exit(void) #if defined(__THREAD_SAFE) { - FreeVec(memory_semaphore); + __delete_semaphore(memory_semaphore); memory_semaphore = NULL; } #endif /* __THREAD_SAFE */ @@ -377,11 +377,9 @@ __memory_init(void) #if defined(__THREAD_SAFE) { - memory_semaphore = AllocVec(sizeof(*memory_semaphore),MEMF_ANY|MEMF_PUBLIC); + memory_semaphore = __create_semaphore(); if(memory_semaphore == NULL) goto out; - - InitSemaphore(memory_semaphore); } #endif /* __THREAD_SAFE */ diff --git a/library/stdlib_protos.h b/library/stdlib_protos.h index d96a3da..4c872b2 100644 --- a/library/stdlib_protos.h +++ b/library/stdlib_protos.h @@ -1,5 +1,5 @@ /* - * $Id: stdlib_protos.h,v 1.10 2005-03-03 09:32:09 obarthel Exp $ + * $Id: stdlib_protos.h,v 1.11 2005-03-03 14:20:55 obarthel Exp $ * * :ts=4 * @@ -185,18 +185,26 @@ extern void __stkovf(void); /* stdlib_termination_message.c */ extern void __print_termination_message(const char * termination_message); +/****************************************************************************/ + /* stdlib_set_process_window.c */ -APTR __set_process_window(APTR new_window_pointer); +extern APTR __set_process_window(APTR new_window_pointer); /****************************************************************************/ /* stdlib_set_errno.c */ -void __set_errno(int new_errno); +extern void __set_errno(int new_errno); /****************************************************************************/ /* stdlib_get_errno.c */ -int __get_errno(void); +extern int __get_errno(void); + +/****************************************************************************/ + +/* stdlib_semaphore.c */ +extern struct SignalSemaphore * __create_semaphore(void); +extern void __delete_semaphore(struct SignalSemaphore * semaphore); /****************************************************************************/ diff --git a/library/stdlib_semaphore.c b/library/stdlib_semaphore.c new file mode 100644 index 0000000..c56485d --- /dev/null +++ b/library/stdlib_semaphore.c @@ -0,0 +1,86 @@ +/* + * $Id: stdlib_semaphore.c,v 1.1 2005-03-03 14:20:55 obarthel Exp $ + * + * :ts=4 + * + * Portable ISO 'C' (1994) runtime library for the Amiga computer + * Copyright (c) 2002-2005 by Olaf Barthel + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Neither the name of Olaf Barthel nor the names of contributors + * may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "stdlib_headers.h" + +/****************************************************************************/ + +struct SignalSemaphore * +__create_semaphore(void) +{ + struct SignalSemaphore * semaphore; + + #if defined(__amigaos4__) + { + semaphore = AllocSysObject(ASOT_SEMAPHORE,NULL); + } + #else + { + semaphore = AllocVec(sizeof(*semaphore),MEMF_ANY|MEMF_PUBLIC); + if(semaphore != NULL) + InitSemaphore(semaphore); + } + #endif /* __amigaos4 */ + + return(semaphore); +} + +/****************************************************************************/ + +void +__delete_semaphore(struct SignalSemaphore * semaphore) +{ + if(semaphore != NULL) + { + #if defined(__amigaos4__) + { + FreeSysObject(ASOT_SEMAPHORE,semaphore); + } + #else + { + assert( semaphore->ss_Owner == NULL ); + + #if defined(DEBUG) + { + /* Just in case somebody tries to reuse this data + structure; this should produce an alert if + attempted. */ + memset(semaphore,0,sizeof(*semaphore)); + } + #endif /* DEBUG */ + + FreeVec(semaphore); + } + #endif /* __amigaos4 */ + } +}