static PRIntervalTime ContentiousCMonitor(PRUint32 loops) { PRStatus status; PRThread *thread = NULL; MonitorContentious_t * contention; PRIntervalTime overhead, timein = PR_IntervalNow(); contention = (MonitorContentious_t *) PR_Calloc(1, sizeof(MonitorContentious_t)); contention->ml = NULL; contention->overhead = 0; contention->loops = loops; contention->interval = contention_interval; thread = PR_CreateThread( PR_USER_THREAD, Contender, contention, PR_PRIORITY_LOW, PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0); PR_ASSERT(thread != NULL); overhead = PR_IntervalNow() - timein; while (contention->loops > 0) { PR_CEnterMonitor(contention); contention->overhead += contention->interval; PR_Sleep(contention->interval); PR_CExitMonitor(contention); } timein = PR_IntervalNow(); status = PR_JoinThread(thread); overhead += (PR_IntervalNow() - timein); return overhead + contention->overhead; } /* ContentiousCMonitor */
nsresult CopyListener::OnStopCopy(nsresult aStatus) { if (NS_SUCCEEDED(aStatus)) { #ifdef NS_DEBUG printf("CopyListener: SUCCESSFUL ON THE COPY OPERATION!\n"); #endif } else { #ifdef NS_DEBUG printf("CopyListener: COPY OPERATION FAILED!\n"); #endif } if (mCopyInProgress) { PR_CEnterMonitor(this); PR_CNotifyAll(this); mCopyInProgress = false; PR_CExitMonitor(this); } if (mComposeAndSend) mComposeAndSend->NotifyListenerOnStopCopy(aStatus); return NS_OK; }
/* void OnStopSending (in string aMsgID, in nsresult aStatus, in wstring aMsg, in nsIFileSpec returnFileSpec); */ NS_IMETHOD OnStopSending(const char *aMsgID, nsresult aStatus, const PRUnichar *aMsg, nsIFile *returnFile) { PR_CEnterMonitor(this); PR_CNotifyAll(this); m_done = PR_TRUE; PR_CExitMonitor(this); return NS_OK ; }
/* ** CACHED MONITORS */ static PRIntervalTime NonContentiousCMonitor(PRUint32 loops) { MonitorContentious_t contention; while (loops-- > 0) { PR_CEnterMonitor(&contention); PR_CExitMonitor(&contention); } return 0; } /* NonContentiousCMonitor */
static void PR_CALLBACK Contender(void *arg) { MonitorContentious_t *contention = (MonitorContentious_t*)arg; while (contention->loops-- > 0) { PR_CEnterMonitor(contention); contention->overhead += contention->interval; PR_Sleep(contention->interval); PR_CExitMonitor(contention); } } /* Contender */
void nsAutoCMonitor::Enter() { #ifdef DEBUG nsAutoLockBase* stackTop = (nsAutoLockBase*) PR_GetThreadPrivate(LockStackTPI); NS_ASSERTION(stackTop == mDown, "non-LIFO nsAutoCMonitor::Enter"); mDown = stackTop; (void) PR_SetThreadPrivate(LockStackTPI, this); #endif PR_CEnterMonitor(mLockObject); mLockCount += 1; }
static void T1CMon(void) { PRStatus rv; PRThread *t2, *t3; PR_fprintf(err, "\n**********************************\n"); PR_fprintf(err, " CACHED MONITORS\n"); PR_fprintf(err, "**********************************\n"); base = PR_IntervalNow(); PR_CEnterMonitor(&sharedCM.o1); LogNow("T1 waiting 3 seconds on o1", PR_SUCCESS); rv = PR_CWait(&sharedCM.o1, PR_SecondsToInterval(3)); if (PR_SUCCESS == rv) LogNow("T1 resuming on o1", rv); else LogNow("T1 wait on o1 failed", rv); PR_CExitMonitor(&sharedCM.o1); LogNow("T1 creating T2", PR_SUCCESS); t2 = PR_CreateThread( PR_USER_THREAD, T2CMon, &sharedCM, PR_PRIORITY_NORMAL, PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0); LogNow("T1 creating T3", PR_SUCCESS); t3 = PR_CreateThread( PR_USER_THREAD, T3CMon, &sharedCM, PR_PRIORITY_NORMAL, PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0); PR_CEnterMonitor(&sharedCM.o2); LogNow("T1 waiting forever on o2", PR_SUCCESS); rv = PR_CWait(&sharedCM.o2, PR_INTERVAL_NO_TIMEOUT); if (PR_SUCCESS == rv) LogNow("T1 resuming on o2", rv); else LogNow("T1 wait on o2 failed", rv); PR_CExitMonitor(&sharedCM.o2); (void)PR_JoinThread(t2); (void)PR_JoinThread(t3); } /* T1CMon */
static void WaitCMonitorThread(void *arg) { PRIntervalTime timeout = (PRIntervalTime) arg; PRIntervalTime elapsed; #if defined(XP_UNIX) || defined(WIN32) PRInt32 timeout_msecs = PR_IntervalToMilliseconds(timeout); PRInt32 elapsed_msecs; #endif #if defined(XP_UNIX) struct timeval end_time_tv; #endif #if defined(WIN32) && !defined(WINCE) struct _timeb end_time_tb; #endif int dummy; PR_CEnterMonitor(&dummy); PR_CWait(&dummy, timeout); PR_CExitMonitor(&dummy); elapsed = (PRIntervalTime)(PR_IntervalNow() - start_time); if (elapsed + tolerance < timeout || elapsed > timeout + tolerance) { fprintf(stderr, "timeout wrong\n"); exit(1); } #if defined(XP_UNIX) gettimeofday(&end_time_tv, NULL); elapsed_msecs = 1000*(end_time_tv.tv_sec - start_time_tv.tv_sec) + (end_time_tv.tv_usec - start_time_tv.tv_usec)/1000; #endif #if defined(WIN32) #if defined(WINCE) elapsed_msecs = GetTickCount() - start_time_tick; #else _ftime(&end_time_tb); elapsed_msecs = 1000*(end_time_tb.time - start_time_tb.time) + (end_time_tb.millitm - start_time_tb.millitm); #endif #endif #if defined(XP_UNIX) || defined(WIN32) if (elapsed_msecs + tolerance_msecs < timeout_msecs || elapsed_msecs > timeout_msecs + tolerance_msecs) { fprintf(stderr, "timeout wrong\n"); exit(1); } #endif if (debug_mode) { fprintf(stderr, "wait cached monitor thread (scope %d) done\n", PR_GetThreadScope(PR_GetCurrentThread())); } }
static NSFCStatus _NSFC_ReleaseEntry(NSFCCache cache, NSFCEntryImpl *nep, PRBool *destroyed) { NSFCStatus rfc = NSFC_OK; PR_ASSERT(nep); if (destroyed) *destroyed = PR_FALSE; if (nep != NULL) { PR_ASSERT(nep->refcnt != 0); /* Lock nep's bucket */ PRUint32 bucket = nep->hash % cache->hsize; NSFC_ACQUIREBUCKET(cache, bucket); /* * If the entry is marked for delete, and the use * count is decremented to zero, the entry can be * deleted. */ if (--nep->refcnt == 0) { PR_ASSERT(nep->fWriting == 0); if (nep->fDelete) { NSFC_DestroyEntry(cache, nep); if (destroyed) *destroyed = PR_TRUE; } } else if ((nep->flags & NSFCENTRY_WAITING) && (nep->refcnt == 1)) { /* required to notify someone who is waiting to set private data */ PR_CEnterMonitor((void*)nep); PR_CNotifyAll((void*)nep); PR_CExitMonitor((void*)nep); } /* Unlock nep's bucket */ NSFC_RELEASEBUCKET(cache, bucket); rfc = NSFC_OK; } return rfc; }
JSJ_InitStandalone() { static int initialized = -1; if (initialized == 1) return; PR_CEnterMonitor(&initialized); switch (initialized) { case -1: /* we're first */ initialized = 0; /* in progress */ PR_CExitMonitor(&initialized); /* do it */ sajsj_InitLocked(); PR_CEnterMonitor(&initialized); initialized = 1; PR_CNotifyAll(&initialized); break; case 0: /* in progress */ PR_CWait(&initialized, WAIT_FOREVER); break; case 1: /* happened since the last check */ break; } PR_CExitMonitor(&initialized); if (!jsjiTask) return; /* FIXME fail loudly! */ return; }
static void PR_CALLBACK T3CMon(void *arg) { PRStatus rv; CMonShared *shared = (CMonShared*)arg; PR_CEnterMonitor(&shared->o2); LogNow("T3 waiting 5 seconds on o2", PR_SUCCESS); rv = PR_CWait(&shared->o2, PR_SecondsToInterval(5)); if (PR_SUCCESS == rv) LogNow("T3 resuming on o2", rv); else LogNow("T3 wait failed on o2", rv); rv = PR_CNotify(&shared->o2); LogNow("T3 notify on o2", rv); PR_CExitMonitor(&shared->o2); } /* T3CMon */
static PRIntervalTime ContentiousCMonitor(PRUint32 loops) { PRStatus status; PRThread *thread = NULL; MonitorContentious_t * contention; PRIntervalTime overhead, timein = PR_IntervalNow(); contention = PR_NEWZAP(MonitorContentious_t); contention->ml = NULL; contention->loops = loops; contention->interval = contention_interval; thread = PR_CreateThread( PR_USER_THREAD, Contender, contention, PR_PRIORITY_LOW, PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0); PR_ASSERT(thread != NULL); overhead = PR_IntervalNow() - timein; while (contention->loops-- > 0) { PR_CEnterMonitor(contention); contention->contentious+= 1; contention->overhead += contention->interval; PR_Sleep(contention->interval); PR_CExitMonitor(contention); } timein = PR_IntervalNow(); status = PR_JoinThread(thread); overhead += (PR_IntervalNow() - timein); overhead += overhead + contention->overhead; if (verbosity) PR_fprintf( std_err, "Access ratio: %u to %u\n", contention->contentious, contention->contender); PR_Free(contention); return overhead; } /* ContentiousCMonitor */
// this is used for Send without UI nsresult nsMapiHook::BlindSendMail (unsigned long aSession, nsIMsgCompFields * aCompFields) { nsresult rv = NS_OK ; if (!IsBlindSendAllowed()) return NS_ERROR_FAILURE; /** create nsIMsgComposeParams obj and other fields to populate it **/ nsCOMPtr<nsIDOMWindowInternal> hiddenWindow; // get parent window nsCOMPtr<nsIAppShellService> appService = do_GetService( "@mozilla.org/appshell/appShellService;1", &rv); if (NS_FAILED(rv)|| (!appService) ) return rv ; rv = appService->GetHiddenDOMWindow(getter_AddRefs(hiddenWindow)); if ( NS_FAILED(rv) ) return rv ; // smtp password and Logged in used IdKey from MapiConfig (session obj) nsMAPIConfiguration * pMapiConfig = nsMAPIConfiguration::GetMAPIConfiguration() ; if (!pMapiConfig) return NS_ERROR_FAILURE ; // get the singelton obj PRUnichar * password = pMapiConfig->GetPassword(aSession) ; // password nsCAutoString smtpPassword; LossyCopyUTF16toASCII(password, smtpPassword); // Id key nsCString MsgIdKey; pMapiConfig->GetIdKey(aSession, MsgIdKey); // get the MsgIdentity for the above key using AccountManager nsCOMPtr <nsIMsgAccountManager> accountManager = do_GetService (NS_MSGACCOUNTMANAGER_CONTRACTID) ; if (NS_FAILED(rv) || (!accountManager) ) return rv ; nsCOMPtr <nsIMsgIdentity> pMsgId ; rv = accountManager->GetIdentity (MsgIdKey, getter_AddRefs(pMsgId)) ; if (NS_FAILED(rv) ) return rv ; // create a send listener to get back the send status nsCOMPtr <nsIMsgSendListener> sendListener ; rv = nsMAPISendListener::CreateMAPISendListener(getter_AddRefs(sendListener)) ; if (NS_FAILED(rv) || (!sendListener) ) return rv; // create the compose params object nsCOMPtr<nsIMsgComposeParams> pMsgComposeParams (do_CreateInstance(NS_MSGCOMPOSEPARAMS_CONTRACTID, &rv)); if (NS_FAILED(rv) || (!pMsgComposeParams) ) return rv ; // populate the compose params PRBool forcePlainText; aCompFields->GetForcePlainText(&forcePlainText); pMsgComposeParams->SetType(nsIMsgCompType::New); pMsgComposeParams->SetFormat(forcePlainText ? nsIMsgCompFormat::PlainText : nsIMsgCompFormat::HTML); pMsgComposeParams->SetIdentity(pMsgId); pMsgComposeParams->SetComposeFields(aCompFields); pMsgComposeParams->SetSendListener(sendListener) ; pMsgComposeParams->SetSmtpPassword(smtpPassword.get()); // create the nsIMsgCompose object to send the object nsCOMPtr<nsIMsgCompose> pMsgCompose (do_CreateInstance(NS_MSGCOMPOSE_CONTRACTID, &rv)); if (NS_FAILED(rv) || (!pMsgCompose) ) return rv ; /** initialize nsIMsgCompose, Send the message, wait for send completion response **/ rv = pMsgCompose->Initialize(hiddenWindow, pMsgComposeParams) ; if (NS_FAILED(rv)) return rv ; return pMsgCompose->SendMsg(nsIMsgSend::nsMsgDeliverNow, pMsgId, nsnull, nsnull, nsnull) ; if (NS_FAILED(rv)) return rv ; // assign to interface pointer from nsCOMPtr to facilitate typecast below nsIMsgSendListener * pSendListener = sendListener ; // we need to wait here to make sure that we return only after send is completed // so we will have a event loop here which will process the events till the Send IsDone. nsIThread *thread = NS_GetCurrentThread(); while ( !((nsMAPISendListener *) pSendListener)->IsDone() ) { PR_CEnterMonitor(pSendListener); PR_CWait(pSendListener, PR_MicrosecondsToInterval(1000UL)); PR_CExitMonitor(pSendListener); NS_ProcessPendingEvents(thread); } return rv ; }
nsresult nsMsgCopy::DoCopy(nsIFile *aDiskFile, nsIMsgFolder *dstFolder, nsIMsgDBHdr *aMsgToReplace, bool aIsDraft, nsIMsgWindow *msgWindow, nsIMsgSend *aMsgSendObj) { nsresult rv = NS_OK; // Check sanity if ((!aDiskFile) || (!dstFolder)) return NS_ERROR_INVALID_ARG; //Call copyservice with dstFolder, disk file, and txnManager if(NS_SUCCEEDED(rv)) { nsRefPtr<CopyListener> copyListener = new CopyListener(); if (!copyListener) return NS_ERROR_OUT_OF_MEMORY; copyListener->SetMsgComposeAndSendObject(aMsgSendObj); nsCOMPtr<nsIThread> thread; if (aIsDraft) { nsCOMPtr<nsIMsgImapMailFolder> imapFolder = do_QueryInterface(dstFolder); nsCOMPtr<nsIMsgAccountManager> accountManager = do_GetService(NS_MSGACCOUNTMANAGER_CONTRACTID, &rv); if (NS_FAILED(rv)) return rv; bool shutdownInProgress = false; rv = accountManager->GetShutdownInProgress(&shutdownInProgress); if (NS_SUCCEEDED(rv) && shutdownInProgress && imapFolder) { // set the following only when we were in the middle of shutdown // process copyListener->mCopyInProgress = true; thread = do_GetCurrentThread(); } } nsCOMPtr<nsIMsgCopyService> copyService = do_GetService(NS_MSGCOPYSERVICE_CONTRACTID, &rv); NS_ENSURE_SUCCESS(rv, rv); rv = copyService->CopyFileMessage(aDiskFile, dstFolder, aMsgToReplace, aIsDraft, aIsDraft ? 0 : nsMsgMessageFlags::Read, EmptyCString(), copyListener, msgWindow); // copyListener->mCopyInProgress can only be set when we are in the // middle of the shutdown process while (copyListener->mCopyInProgress) { PR_CEnterMonitor(copyListener); PR_CWait(copyListener, PR_MicrosecondsToInterval(1000UL)); PR_CExitMonitor(copyListener); if (thread) NS_ProcessPendingEvents(thread); } } return rv; }