andre@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public
andre@0:  * License, v. 2.0. If a copy of the MPL was not distributed with this
andre@0:  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
andre@0: 
andre@0: #include <string.h>
andre@0: #include "primpl.h"
andre@0: 
andre@0: /* Lock used to lock the environment */
andre@0: #if defined(_PR_NO_PREEMPT)
andre@0: #define _PR_NEW_LOCK_ENV()
andre@0: #define _PR_DELETE_LOCK_ENV()
andre@0: #define _PR_LOCK_ENV()
andre@0: #define _PR_UNLOCK_ENV()
andre@0: #elif defined(_PR_LOCAL_THREADS_ONLY)
andre@0: extern _PRCPU * _pr_primordialCPU;
andre@0: static PRIntn _is;
andre@0: #define _PR_NEW_LOCK_ENV()
andre@0: #define _PR_DELETE_LOCK_ENV()
andre@0: #define _PR_LOCK_ENV() if (_pr_primordialCPU) _PR_INTSOFF(_is);
andre@0: #define _PR_UNLOCK_ENV() if (_pr_primordialCPU) _PR_INTSON(_is);
andre@0: #else
andre@0: static PRLock *_pr_envLock = NULL;
andre@0: #define _PR_NEW_LOCK_ENV() {_pr_envLock = PR_NewLock();}
andre@0: #define _PR_DELETE_LOCK_ENV() \
andre@0:     { if (_pr_envLock) { PR_DestroyLock(_pr_envLock); _pr_envLock = NULL; } }
andre@0: #define _PR_LOCK_ENV() { if (_pr_envLock) PR_Lock(_pr_envLock); }
andre@0: #define _PR_UNLOCK_ENV() { if (_pr_envLock) PR_Unlock(_pr_envLock); }
andre@0: #endif
andre@0: 
andre@0: /************************************************************************/
andre@0: 
andre@0: void _PR_InitEnv(void)
andre@0: {
andre@0: 	_PR_NEW_LOCK_ENV();
andre@0: }
andre@0: 
andre@0: void _PR_CleanupEnv(void)
andre@0: {
andre@0:     _PR_DELETE_LOCK_ENV();
andre@0: }
andre@0: 
andre@0: PR_IMPLEMENT(char*) PR_GetEnv(const char *var)
andre@0: {
andre@0:     char *ev;
andre@0: 
andre@0:     if (!_pr_initialized) _PR_ImplicitInitialization();
andre@0: 
andre@0:     _PR_LOCK_ENV();
andre@0:     ev = _PR_MD_GET_ENV(var);
andre@0:     _PR_UNLOCK_ENV();
andre@0:     return ev;
andre@0: }
andre@0: 
andre@0: PR_IMPLEMENT(PRStatus) PR_SetEnv(const char *string)
andre@0: {
andre@0:     PRIntn result;
andre@0: 
andre@0:     if (!_pr_initialized) _PR_ImplicitInitialization();
andre@0: 
andre@0:     if ( !strchr(string, '=')) return(PR_FAILURE);
andre@0: 
andre@0:     _PR_LOCK_ENV();
andre@0:     result = _PR_MD_PUT_ENV(string);
andre@0:     _PR_UNLOCK_ENV();
andre@0:     return (result)? PR_FAILURE : PR_SUCCESS;
andre@0: }