From 6e069229083006f6e340aebe3a9b548d99b50180 Mon Sep 17 00:00:00 2001 From: Ingo Weinhold Date: Sun, 29 Nov 2009 10:47:01 +0000 Subject: [PATCH] Replace semaphores/benaphores in the env, fork, and user/group code by lazy mutexes. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@34340 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/system/libroot/posix/Jamfile | 2 +- src/system/libroot/posix/stdlib/Jamfile | 2 +- src/system/libroot/posix/stdlib/env.cpp | 21 +++++++------------ src/system/libroot/posix/unistd/fork.c | 20 +++++++----------- .../libroot/posix/user_group_common.cpp | 15 ++++++------- 5 files changed, 25 insertions(+), 35 deletions(-) diff --git a/src/system/libroot/posix/Jamfile b/src/system/libroot/posix/Jamfile index dcacbf8465..f9488b8bcb 100644 --- a/src/system/libroot/posix/Jamfile +++ b/src/system/libroot/posix/Jamfile @@ -4,7 +4,7 @@ UsePrivateHeaders app shared [ FDirName syslog_daemon ] ; UsePrivateSystemHeaders ; UsePrivateHeaders kernel ; # For util/KMessage.h -UsePrivateHeaders libroot runtime_loader ; +UsePrivateHeaders libroot runtime_loader shared ; if $(HAIKU_MULTIUSER_QUERY) = 1 { PWD_BACKEND = pwd_query.c ; diff --git a/src/system/libroot/posix/stdlib/Jamfile b/src/system/libroot/posix/stdlib/Jamfile index 847b266bfb..008b461aac 100644 --- a/src/system/libroot/posix/stdlib/Jamfile +++ b/src/system/libroot/posix/stdlib/Jamfile @@ -1,6 +1,6 @@ SubDir HAIKU_TOP src system libroot posix stdlib ; -UsePrivateHeaders drivers libroot runtime_loader ; +UsePrivateHeaders drivers libroot runtime_loader shared ; UsePrivateSystemHeaders ; MergeObject posix_stdlib.o : diff --git a/src/system/libroot/posix/stdlib/env.cpp b/src/system/libroot/posix/stdlib/env.cpp index e4cca9fac7..9f4403ddee 100644 --- a/src/system/libroot/posix/stdlib/env.cpp +++ b/src/system/libroot/posix/stdlib/env.cpp @@ -11,21 +11,14 @@ #include -#include #include +#include #include +#include #include -#define RETURN_AND_SET_ERRNO(err) \ - if (err < 0) { \ - errno = err; \ - return -1; \ - } \ - return err; - - -static benaphore sEnvLock; +static lazy_mutex sEnvLock; static char **sManagedEnviron; char **environ = NULL; @@ -34,14 +27,14 @@ char **environ = NULL; static inline void lock_variables(void) { - benaphore_lock(&sEnvLock); + lazy_mutex_lock(&sEnvLock); } static inline void unlock_variables(void) { - benaphore_unlock(&sEnvLock); + lazy_mutex_unlock(&sEnvLock); } @@ -193,7 +186,7 @@ update_variable(const char *name, int32 length, const char *value, static void environ_fork_hook(void) { - benaphore_init(&sEnvLock, "env lock"); + lazy_mutex_init(&sEnvLock, "env lock"); } @@ -206,7 +199,7 @@ __init_env(const struct user_space_program_args *args) // Following POSIX, there is no need to make any of the environment // functions thread-safe - but we do it anyway as much as possible to // protect our implementation - benaphore_init(&sEnvLock, "env lock"); + lazy_mutex_init(&sEnvLock, "env lock"); environ = args->env; sManagedEnviron = NULL; diff --git a/src/system/libroot/posix/unistd/fork.c b/src/system/libroot/posix/unistd/fork.c index 375f3bb4e6..9f28d3632a 100644 --- a/src/system/libroot/posix/unistd/fork.c +++ b/src/system/libroot/posix/unistd/fork.c @@ -10,6 +10,7 @@ #include #include +#include #include #include #include @@ -22,7 +23,7 @@ typedef struct fork_hook { static fork_hook *sPrepareHooks, *sParentHooks, *sChildHooks; static fork_hook *sLastParentHook, *sLastChildHook; -static sem_id sForkLock; +static lazy_mutex sForkLock; extern thread_id __main_thread_id; @@ -95,10 +96,7 @@ call_fork_hooks(fork_hook *hook) status_t __init_fork(void) { - sForkLock = create_sem(1, "fork lock"); - if (sForkLock < B_OK) - return sForkLock; - + lazy_mutex_init(&sForkLock, "fork lock"); return B_OK; } @@ -111,9 +109,7 @@ __init_fork(void) status_t __register_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void)) { - status_t status; - - while ((status = acquire_sem(sForkLock)) == B_INTERRUPTED); + status_t status = lazy_mutex_lock(&sForkLock); if (status != B_OK) return status; @@ -126,7 +122,7 @@ __register_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(voi if (status == B_OK && child) status = add_fork_hook(&sChildHooks, &sLastChildHook, child); - release_sem(sForkLock); + lazy_mutex_unlock(&sForkLock); return status; } @@ -137,7 +133,7 @@ fork(void) thread_id thread; status_t status; - while ((status = acquire_sem(sForkLock)) == B_INTERRUPTED); + status = lazy_mutex_lock(&sForkLock); if (status != B_OK) return status; @@ -147,7 +143,7 @@ fork(void) thread = _kern_fork(); if (thread < 0) { // something went wrong - release_sem(sForkLock); + lazy_mutex_unlock(&sForkLock); errno = thread; return -1; } @@ -164,7 +160,7 @@ fork(void) } else { // we are the parent call_fork_hooks(sParentHooks); - release_sem(sForkLock); + lazy_mutex_unlock(&sForkLock); } return thread; diff --git a/src/system/libroot/posix/user_group_common.cpp b/src/system/libroot/posix/user_group_common.cpp index 39f75452c5..3dc4f83f7d 100644 --- a/src/system/libroot/posix/user_group_common.cpp +++ b/src/system/libroot/posix/user_group_common.cpp @@ -14,8 +14,8 @@ #include -#include #include +#include #include #include @@ -28,21 +28,22 @@ const char* BPrivate::kPasswdFile = "/etc/passwd"; const char* BPrivate::kGroupFile = "/etc/group"; const char* BPrivate::kShadowPwdFile = "/etc/shadow"; -static benaphore sUserGroupLock; +static lazy_mutex sUserGroupLock; static port_id sRegistrarPort = -1; status_t BPrivate::user_group_lock() { - return benaphore_lock(&sUserGroupLock); + return lazy_mutex_lock(&sUserGroupLock); } status_t BPrivate::user_group_unlock() { - return benaphore_unlock(&sUserGroupLock); + lazy_mutex_unlock(&sUserGroupLock); + return B_OK; } @@ -86,7 +87,7 @@ public: if (fString != NULL) { *fString = '\0'; fString++; - } + } return token; } @@ -384,12 +385,12 @@ BPrivate::parse_shadow_pwd_line(char* line, char*& name, char*& password, void __init_pwd_backend(void) { - benaphore_init(&sUserGroupLock, "user group"); + lazy_mutex_init(&sUserGroupLock, "user group"); } void __reinit_pwd_backend_after_fork(void) { - benaphore_init(&sUserGroupLock, "user group"); + lazy_mutex_init(&sUserGroupLock, "user group"); }