Exemplo n.º 1
0
PRIntn main(PRIntn argc, char **argv)
{
    PRInt32 rv, test, result = 0;
    PRFileDesc *output = PR_GetSpecialFD(PR_StandardOutput);

    test = -2;
    rv = PR_AtomicIncrement(&test);
    result = result | ((rv < 0) ? 0 : 1);
    PR_fprintf(
        output, "PR_AtomicIncrement(%d) == %d: %s\n",
        test, rv, (rv < 0) ? "PASSED" : "FAILED");
    rv = PR_AtomicIncrement(&test);
    result = result | ((rv == 0) ? 0 : 1);
    PR_fprintf(
        output, "PR_AtomicIncrement(%d) == %d: %s\n",
        test, rv, (rv == 0) ? "PASSED" : "FAILED");
    rv = PR_AtomicIncrement(&test);
    result = result | ((rv > 0) ? 0 : 1);
    PR_fprintf(
        output, "PR_AtomicIncrement(%d) == %d: %s\n",
        test, rv, (rv > 0) ? "PASSED" : "FAILED");

    test = 2;
    rv = PR_AtomicDecrement(&test);
    result = result | ((rv > 0) ? 0 : 1);
    PR_fprintf(
        output, "PR_AtomicDecrement(%d) == %d: %s\n",
        test, rv, (rv > 0) ? "PASSED" : "FAILED");
    rv = PR_AtomicDecrement(&test);
    result = result | ((rv == 0) ? 0 : 1);
    PR_fprintf(
        output, "PR_AtomicDecrement(%d) == %d: %s\n",
        test, rv, (rv == 0) ? "PASSED" : "FAILED");
    rv = PR_AtomicDecrement(&test);
    result = result | ((rv < 0) ? 0 : 1);
    PR_fprintf(
        output, "PR_AtomicDecrement(%d) == %d: %s\n",
        test, rv, (rv < 0) ? "PASSED" : "FAILED");

    test = -2;
    rv = PR_AtomicSet(&test, 2);
    result = result | (((rv == -2) && (test == 2)) ? 0 : 1);
    PR_fprintf(
        output, "PR_AtomicSet(%d) == %d: %s\n",
        test, rv, ((rv == -2) && (test == 2)) ? "PASSED" : "FAILED");

    PR_fprintf(
        output, "Atomic operations test %s\n",
        (result == 0) ? "PASSED" : "FAILED");
    return result;
}  /* main */
Exemplo n.º 2
0
nsPlatformCharset::nsPlatformCharset()
{
  PR_AtomicIncrement(&gCnt);
  static PRCallOnceType once;
  PR_CallOnce(&once, InitLock);
  NS_ASSERTION(gLock, "Can't allocate a lock?!");
}
void PyXPCOM_DLLAddRef(void)
{
	// Must be thread-safe, although cant have the Python lock!
	CEnterLeaveXPCOMFramework _celf;
	PRInt32 cnt = PR_AtomicIncrement(&g_cLockCount);
	if (cnt==1) { // First call
		if (!Py_IsInitialized()) {
			Py_Initialize();
			// Make sure our Windows framework is all setup.
			PyXPCOM_Globals_Ensure();
			// Make sure we have _something_ as sys.argv.
			if (PySys_GetObject((char*)"argv")==NULL) {
				PyObject *path = PyList_New(0);
#if PY_MAJOR_VERSION <= 2
				PyObject *str = PyString_FromString("");
#else
				PyObject *str = PyUnicode_FromString("");
#endif
				PyList_Append(path, str);
				PySys_SetObject((char*)"argv", path);
				Py_XDECREF(path);
				Py_XDECREF(str);
			}

			// Must force Python to start using thread locks, as
			// we are free-threaded (maybe, I think, sometimes :-)
			PyEval_InitThreads();
#ifndef PYXPCOM_USE_PYGILSTATE
			// Release Python lock, as first thing we do is re-get it.
			ptsGlobal = PyEval_SaveThread();
#endif
			// NOTE: We never finalize Python!!
		}
	}
}
Exemplo n.º 4
0
nsSystemPrincipal::AddRef()
{
  NS_PRECONDITION(PRInt32(mJSPrincipals.refcount) >= 0, "illegal refcnt");
  nsrefcnt count = PR_AtomicIncrement((PRInt32 *)&mJSPrincipals.refcount);
  NS_LOG_ADDREF(this, count, "nsSystemPrincipal", sizeof(*this));
  return count;
}
Exemplo n.º 5
0
nsresult nsTimerImpl::InitCommon(PRUint32 aType, PRUint32 aDelay)
{
  nsresult rv;

  NS_ENSURE_TRUE(gThread, NS_ERROR_NOT_INITIALIZED);

  rv = gThread->Init();
  NS_ENSURE_SUCCESS(rv, rv);

  /**
   * In case of re-Init, both with and without a preceding Cancel, clear the
   * mCanceled flag and assign a new mGeneration.  But first, remove any armed
   * timer from the timer thread's list.
   *
   * If we are racing with the timer thread to remove this timer and we lose,
   * the RemoveTimer call made here will fail to find this timer in the timer
   * thread's list, and will return false harmlessly.  We test mArmed here to
   * avoid the small overhead in RemoveTimer of locking the timer thread and
   * checking its list for this timer.  It's safe to test mArmed even though
   * it might be cleared on another thread in the next cycle (or even already
   * be cleared by another CPU whose store hasn't reached our CPU's cache),
   * because RemoveTimer is idempotent.
   */
  if (mArmed)
    gThread->RemoveTimer(this);
  mCanceled = PR_FALSE;
  mGeneration = PR_AtomicIncrement(&gGenerator);

  mType = (PRUint8)aType;
  SetDelayInternal(aDelay);

  return gThread->AddTimer(this);
}
Exemplo n.º 6
0
SECStatus
sslMutex_Lock(sslMutex *pMutex)
{
    PRInt32 newValue;
    if (PR_FALSE == pMutex->isMultiProcess) {
        return single_process_sslMutex_Lock(pMutex);
    }

    if (pMutex->u.pipeStr.mPipes[2] != SSL_MUTEX_MAGIC) {
        PORT_SetError(PR_INVALID_ARGUMENT_ERROR);
        return SECFailure;
    }
    newValue = PR_AtomicIncrement(&pMutex->u.pipeStr.nWaiters);
    /* Do Memory Barrier here. */
    if (newValue > 1) {
        int   cc;
        char  c;
        do {
            cc = read(pMutex->u.pipeStr.mPipes[0], &c, 1);
        } while (cc < 0 && errno == EINTR);
        if (cc != 1) {
            if (cc < 0)
                nss_MD_unix_map_default_error(errno);
            else
                PORT_SetError(PR_UNKNOWN_ERROR);
            return SECFailure;
        }
    }
    return SECSuccess;
}
Exemplo n.º 7
0
PRFileDesc *
ServerSetup(void)
{
    PRFileDesc *listenSocket;
    PRNetAddr serverAddr;
    PRThread *WorkerThread;

    if ( (listenSocket = PR_NewTCPSocket()) == NULL) {
        if (debug_mode) printf("\tServer error creating listen socket\n");
		else failed_already=1;
        return NULL;
    }

    memset(&serverAddr, 0, sizeof(PRNetAddr));
    serverAddr.inet.family = AF_INET;
    serverAddr.inet.port = PR_htons(PORT);
    serverAddr.inet.ip = PR_htonl(INADDR_ANY);

    if ( PR_Bind(listenSocket, &serverAddr) == PR_FAILURE) {
        if (debug_mode) printf("\tServer error binding to server address: OS error %d\n",
                PR_GetOSError());
		else failed_already=1;
        PR_Close(listenSocket);
        return NULL;
    }

    if ( PR_Listen(listenSocket, 128) == PR_FAILURE) {
        if (debug_mode) printf("\tServer error listening to server socket\n");
		else failed_already=1;
        PR_Close(listenSocket);

        return NULL;
    }

    /* Create Clients */
    workerThreads = 0;
    workerThreadsBusy = 0;

    workerThreadsLock = PR_NewLock();

    WorkerThread = PR_CreateThread(
                      PR_SYSTEM_THREAD,
                      WorkerThreadFunc,
                      listenSocket,
                      PR_PRIORITY_NORMAL,
                      ServerScope,
                      PR_UNJOINABLE_THREAD,
                      STACKSIZE);

    if (!WorkerThread) {
        if (debug_mode) printf("error creating working thread\n");
        PR_Close(listenSocket);
        return NULL;
    }
    PR_AtomicIncrement(&workerThreads);
    if (debug_mode) DPRINTF("\tServer created primordial worker thread\n");

    return listenSocket;
}
//
// Request a reservation in the keep-alive system
//
PRBool
PollManager::RequestReservation(void)
{
    PR_ASSERT(numKeepAlives_ >= 0);

    PRInt32 numKeepAlives = PR_AtomicIncrement(&numKeepAlives_);
    if (numKeepAlives > maxKeepAlives_) {
        PR_AtomicDecrement(&numKeepAlives_);

        if (PR_AtomicIncrement(&numKeepAliveRefusals_) == 1)
            ereport(LOG_VERBOSE, "PollManager::RequestReservation() keep-alive subsystem full");

        return PR_FALSE;
    }

    return PR_TRUE;
}
Exemplo n.º 9
0
NSS_IMPLEMENT NSSSlot *
nssSlot_AddRef (
  NSSSlot *slot
)
{
    PR_AtomicIncrement(&slot->base.refCount);
    return slot;
}
NS_IMETHODIMP_(nsrefcnt) nsPrintProgress::AddRef(void)
{
  NS_PRECONDITION(PRInt32(mRefCnt) >= 0, "illegal refcnt");
  nsrefcnt count;
  count = PR_AtomicIncrement((PRInt32*)&mRefCnt);
  //NS_LOG_ADDREF(this, count, "nsPrintProgress", sizeof(*this));
  return count;
}
Exemplo n.º 11
0
/*
 * Function: prldap_new_tpdindex()
 * Description: allocate a thread-private data index.
 * Returns: the new index.
 */
static PRUintn
prldap_new_tpdindex( void )
{
    PRUintn	tpdindex;

    tpdindex = (PRUintn)PR_AtomicIncrement( &prldap_tpd_maxindex );
    return( tpdindex );
}
Exemplo n.º 12
0
Py_nsISupports::Py_nsISupports(nsISupports *punk, const nsIID &iid, PyTypeObject *this_type)
{
	ob_type = this_type;
	m_obj = punk;
	m_iid = iid;
	// refcnt of object managed by caller.
	PR_AtomicIncrement(&cInterfaces);
	_Py_NewReference(this);
}
Exemplo n.º 13
0
nsExceptionManager::nsExceptionManager(nsExceptionService *svc) :
  mNextThread(nsnull),
  mService(svc)
{
  /* member initializers and constructor code */
#ifdef NS_DEBUG
  PR_AtomicIncrement(&totalInstances);
#endif
}
	Py_DOMnsISupports(PyObject *pycontext, nsISupports *p, const nsIID &iid) :
		Py_nsISupports(p, iid, type),
		m_pycontext(pycontext) {
		/* The IID _must_ be the IID of the interface we are wrapping! */
		//NS_ABORT_IF_FALSE(iid.Equals(NS_GET_IID(InterfaceName)), "Bad IID");
		Py_INCREF(pycontext);
		PR_AtomicIncrement(&cPyDOMISupportsObjects);

	}
Exemplo n.º 15
0
NS_IMETHODIMP 
PlugletEngine::LockFactory(PRBool aLock) 
{
    if(aLock) {
        PR_AtomicIncrement(&lockCount); 
    } else {
        PR_AtomicDecrement(&lockCount);
    }
    return NS_OK;
}
nsrefcnt
nsNodeInfoManager::AddRef()
{
  NS_PRECONDITION(PRInt32(mRefCnt) >= 0, "illegal refcnt");

  nsrefcnt count = PR_AtomicIncrement((PRInt32*)&mRefCnt);
  NS_LOG_ADDREF(this, count, "nsNodeInfoManager", sizeof(*this));

  return count;
}
Exemplo n.º 17
0
NSFC_GetFileInfo(const char *filename, NSFCFileInfo *finfo,
                 NSFCCache cache)
{
    NSFCEntry entry = NSFCENTRY_INIT;
    NSFCEntryImpl *nep = entry;
    NSFCStatus rfc;
    NSFCStatusInfo statusInfo; 
    NSFCFileInfo myfinfo;
    NSFCFileInfo *f_info = (finfo) ? finfo : &myfinfo;
    PRBool hit = PR_FALSE;

    PRStatus rv = PR_SUCCESS;

    NSFCSTATUSINFO_INIT(&statusInfo);

    rfc = NSFC_AccessFilename(filename, &nep, f_info, cache, &statusInfo);
    if (rfc == NSFC_OK) {
        PR_ASSERT(nep->flags & NSFCENTRY_HASINFO);
        if (finfo)
            memcpy(finfo, &nep->finfo, sizeof(*finfo));
        if (nep->flags & NSFCENTRY_ERRORINFO) {
            rv = PR_FAILURE;
        }
        if (statusInfo != NSFC_STATUSINFO_CREATE)
            hit = PR_TRUE;
        NSFC_RecordEntryHit(cache, nep);
        NSFC_ReleaseEntry(cache, &nep);
    }
    else if (rfc == NSFC_STATFAIL) {
            rv = PR_FAILURE;
    }
    else {
        rv = NSFC_GetNonCacheFileInfo(filename, f_info);
    }

    if (hit)
        PR_AtomicIncrement((PRInt32 *)&cache->infoHits);
    else
        PR_AtomicIncrement((PRInt32 *)&cache->infoMiss);

    return rv;
}
Exemplo n.º 18
0
nsrefcnt
PyG_Base::AddRef(void)
{
	nsrefcnt cnt = (nsrefcnt) PR_AtomicIncrement((PRInt32*)&mRefCnt);
#ifdef NS_BUILD_REFCNT_LOGGING
	// If we have no pBaseObject, then we need to ignore them
	if (m_pBaseObject == NULL)
		NS_LOG_ADDREF(this, cnt, refcntLogRepr, sizeof(*this));
#endif
	return cnt;
}
Exemplo n.º 19
0
/*
 * Close all outstanding persistent searches.
 * To be used when the server is shutting down.
 */
void
ps_stop_psearch_system()
{
    PSearch	*ps;

	if ( PS_IS_INITIALIZED()) {
		PSL_LOCK_WRITE();
		for ( ps = psearch_list->pl_head; NULL != ps; ps = ps->ps_next ) {
			PR_AtomicIncrement( &ps->ps_complete );
		}
		PSL_UNLOCK_WRITE();
		ps_wakeup_all();
	}
}
Exemplo n.º 20
0
nsresult 
nsPlatformCharset::InitInfo()
{  
  PR_AtomicIncrement(&gCnt); // count for gInfo

  if (gInfo == nsnull) {
    nsGREResProperties *info =
        new nsGREResProperties(NS_LITERAL_CSTRING("os2charset.properties"));

    NS_ASSERTION(info , "cannot open properties file");
    NS_ENSURE_TRUE(info, NS_ERROR_FAILURE);
    gInfo = info;
  }
  return NS_OK;
}
Exemplo n.º 21
0
static JSBool
Suspend(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
  nsJSSh* shell;
  if (!GetJSShGlobal(cx, obj, &shell)) return JS_FALSE;

  nsCOMPtr<nsIThread> thread = do_GetCurrentThread();

  PR_AtomicIncrement(&shell->mSuspendCount);
  
  while (shell->mSuspendCount) {
    LOG(("|"));
    NS_ProcessNextEvent(thread);
  }
           
  return JS_TRUE;
}
Exemplo n.º 22
0
/*
 * Notifies just get posted to the protecting mutex. The
 * actual notification is done when the lock is released so that
 * MP systems don't contend for a lock that they can't have.
 */
static void pt_PostNotifyToCvar(PRCondVar *cvar, PRBool broadcast)
{
    PRIntn index = 0;
    _PT_Notified *notified = &cvar->lock->notified;

    PR_ASSERT(PR_TRUE == cvar->lock->locked);
    PR_ASSERT(pthread_equal(cvar->lock->owner, pthread_self()));
    PR_ASSERT(_PT_PTHREAD_MUTEX_IS_LOCKED(cvar->lock->mutex));

    while (1)
    {
        for (index = 0; index < notified->length; ++index)
        {
            if (notified->cv[index].cv == cvar)
            {
                if (broadcast)
                    notified->cv[index].times = -1;
                else if (-1 != notified->cv[index].times)
                    notified->cv[index].times += 1;
                goto finished;  /* we're finished */
            }
        }
        /* if not full, enter new CV in this array */
        if (notified->length < PT_CV_NOTIFIED_LENGTH) break;

        /* if there's no link, create an empty array and link it */
        if (NULL == notified->link)
            notified->link = PR_NEWZAP(_PT_Notified);
        notified = notified->link;
    }

    /* A brand new entry in the array */
    (void)PR_AtomicIncrement(&cvar->notify_pending);
    notified->cv[index].times = (broadcast) ? -1 : 1;
    notified->cv[index].cv = cvar;
    notified->length += 1;

finished:
    PR_ASSERT(PR_TRUE == cvar->lock->locked);
    PR_ASSERT(pthread_equal(cvar->lock->owner, pthread_self()));
}  /* pt_PostNotifyToCvar */
Exemplo n.º 23
0
XPCWrappedNativeProto::XPCWrappedNativeProto(XPCWrappedNativeScope* Scope,
                                             nsIClassInfo* ClassInfo,
                                             PRUint32 ClassInfoFlags,
                                             XPCNativeSet* Set)
    : mScope(Scope),
      mJSProtoObject(nsnull),
      mClassInfo(ClassInfo),
      mClassInfoFlags(ClassInfoFlags),
      mSet(Set),
      mSecurityInfo(nsnull),
      mScriptableInfo(nsnull)
{
    // This native object lives as long as its associated JSObject - killed
    // by finalization of the JSObject (or explicitly if Init fails).

    MOZ_COUNT_CTOR(XPCWrappedNativeProto);

#ifdef DEBUG
    PR_AtomicIncrement(&gDEBUG_LiveProtoCount);
#endif
}
Exemplo n.º 24
0
static PRInt32 _udt_recv(PRFileDesc *fd, void *buf, PRInt32 amount,
        PRIntn flags, PRIntervalTime timeout) {
    uint32_t dummy_data;
    PRUdtSocketDesc* desc = (PRUdtSocketDesc*)(fd->secret);
    if (desc->sign_cnt != desc->rd_cnt) {
        PR_Recv(desc->sock_pair0, (char*)&dummy_data, sizeof(dummy_data), 0, 0);
        PR_AtomicIncrement(&desc->rd_cnt);
    }
	// peek is not supported in udt
	if(PR_MSG_PEEK & flags)
		return PR_WOULD_BLOCK_ERROR;
    int rc = udt_recv(desc->udtfd, (char*)buf, amount, 0);
    if(rc < 0) {
        int udterrcode = udt_getErrorCode();
        if ((UDT_ERRECONNLOST == udterrcode) ||
           (UDT_ERRENOCONN == udterrcode)) {
            PR_Close(desc->sock_pair1);
            //rc = 0;
        }
    }
	return rc;
}
Exemplo n.º 25
0
static PRInt32 _udt_read(PRFileDesc *fd, void *buf, PRInt32 amount) {
    UDTSOCKET s = get_socket_from_fd(fd);
    uint32_t dummy_data;
    int rc = 0;
    PRUdtSocketDesc* desc = (PRUdtSocketDesc*)(fd->secret);
    if (desc->sign_cnt != desc->rd_cnt) {
        PR_Recv(desc->sock_pair0, (char*)&dummy_data, sizeof(dummy_data), 0, 0);
        PR_AtomicIncrement(&desc->rd_cnt);
    }
    rc = udt_recv(s, (char*)buf, amount, 0);
	if(rc < 0) {
	    int udterrcode = udt_getErrorCode();
	    if (UDT_ERRECONNLOST == udterrcode) {
	        rc = 0;
	        PR_Close(desc->sock_pair0);
	        PR_Close(desc->sock_pair1);
	    } else if (UDT_ERRENOCONN == udterrcode) {
	        rc = 0;
            PR_Close(desc->sock_pair0);
            PR_Close(desc->sock_pair1);
	    }
	}
    return rc;
}
Exemplo n.º 26
0
nsExceptionService::nsExceptionService()
  : mProviders(4, PR_TRUE) /* small, thread-safe hashtable */
{
#ifdef NS_DEBUG
  if (PR_AtomicIncrement(&totalInstances)!=1) {
    NS_ERROR("The nsExceptionService is a singleton!");
  }
#endif
  /* member initializers and constructor code */
  if (tlsIndex == BAD_TLS_INDEX) {
    PRStatus status;
    status = PR_NewThreadPrivateIndex( &tlsIndex, ThreadDestruct );
    NS_ASSERTION(status==0, "ScriptErrorService could not allocate TLS storage.");
  }
  lock = PR_NewLock();
  NS_ASSERTION(lock, "Error allocating ExceptionService lock");

  // observe XPCOM shutdown.
  nsCOMPtr<nsIObserverService> observerService =
    mozilla::services::GetObserverService();
  NS_ASSERTION(observerService, "Could not get observer service!");
  if (observerService)
    observerService->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, PR_FALSE);
}
Exemplo n.º 27
0
STDMETHODIMP_(ULONG) CMapiFactory::AddRef()
{
    return (PR_AtomicIncrement(&m_cRef));
}
Exemplo n.º 28
0
STDMETHODIMP_(ULONG) CMapiImp::AddRef()
{
    return PR_AtomicIncrement(&m_cRef);
}
Exemplo n.º 29
0
 explicit nsStressRunner(int num) : mNum(num), mWasRun(false) {
     PR_AtomicIncrement(&gNum);
 }
Exemplo n.º 30
0
int
decrement_repl_active_threads()
{
    PR_AtomicIncrement(&repl_active_threads);
	return repl_active_threads;
}