int main() { int rtn,i; //存放子线程id pthread_t pthread_id = 0; rtn = pthread_create(&pthread_id,NULL,thread_worker,NULL); if(rtn != 0) { printf("pthread_create ERROR!\n"); return -1; } for(i=0;i<LOOP_TIMES;i++) { pthread_mutex_lock(&mutex1); // printf("main lock1\n"); pthread_mutex_lock(&mutex2); // printf("main lock2\n"); critical_section(1,i); pthread_mutex_unlock(&mutex2); // printf("main unlock2\n"); pthread_mutex_unlock(&mutex1); // printf("main unlock1\n"); } pthread_mutex_destroy(&mutex1); pthread_mutex_destroy(&mutex2); return 0; }
STDMETHODIMP CEnumConnectionPoints::Next(ULONG cConnections, IConnectionPoint** rgpCP, ULONG* pcFetched) { LOGCALL(("CEnumConnectionPoints::Next(%ld)\n", cConnections)); if (rgpCP == NULL) return E_POINTER; if (cConnections > 1 && pcFetched == NULL) return E_POINTER; CriticalSection critical_section(this); ULONG iConnections = 0; while (iConnections < cConnections) { IConnectionPoint* pCP = m_rgpCP[m_iCurrent]; if (pCP == NULL) break; m_iCurrent++; rgpCP[iConnections++] = pCP; pCP->AddRef(); } if (pcFetched) *pcFetched = iConnections; return iConnections < cConnections ? S_FALSE : S_OK; }
STDMETHODIMP CEnumConnections::Skip(ULONG cConnections) { LOGCALL(("CEnumConnections::Skip()\n")); CriticalSection critical_section(this); if (cConnections > 0) { bool fHasNext = false; DWORD dwCookie; if (m_fReset) { fHasNext = m_pCP->GetFirstConnection(&dwCookie, NULL); if (fHasNext) m_fReset = false; } else { fHasNext = m_pCP->GetNextConnection(m_dwCookieCurrent, &dwCookie, NULL); } while (fHasNext) { cConnections--; m_dwCookieCurrent = dwCookie; fHasNext = m_pCP->GetNextConnection(m_dwCookieCurrent, &dwCookie, NULL); } } return cConnections ? S_FALSE : S_OK; }
STDMETHODIMP CConnectionPoint::QueryInterface(REFIID riid, void** ppv) { LOGCALL(("CConnectionPoint::QueryInterface(riid = %s)\n", StringFromGuid(riid))); if (ppv == NULL) return E_INVALIDARG; *ppv = NULL; if (riid == IID_IUnknown || riid == IID_IConnectionPoint) { AddRef(); *ppv = static_cast<IConnectionPoint*>(this); return S_OK; } else if (riid == IID_IMarshal) { CriticalSection critical_section(&Module::m_GlobalSync); if (m_pUnkFTM == NULL) CoCreateFreeThreadedMarshaler(static_cast<IConnectionPoint*>(this), &m_pUnkFTM); if (m_pUnkFTM != NULL) return m_pUnkFTM->QueryInterface(riid, ppv); } return E_NOINTERFACE; }
bool CConnectionPoint::HasConnections() { LOGCALL (("CConnectionPoint::HasConnections()\n")); CriticalSection critical_section(this); return !m_cookies.empty(); }
STDMETHODIMP CGetDataSequentialStream::Read(void* pv, ULONG cb, ULONG* pcbRead) { LOGCALL(("CGetDataSequentialStream::Read(cb = %d)\n", cb)); if (pcbRead != NULL) *pcbRead = 0; if (cb == 0) return S_OK; if (pv == NULL) return STG_E_INVALIDPOINTER; assert(m_pStreamSync != NULL); CriticalSection critical_section(m_pStreamSync); if (m_status == STATUS_ZOMBIE) return E_UNEXPECTED; if (m_status == STATUS_FINISHED) return S_FALSE; SQLLEN cbRead; if (m_status == STATUS_INITIALIZED) { #if 0 HRESULT hr = m_pdsp->ResetLongData(m_iRecordID, m_iFieldOrdinal); if (FAILED(hr)) return hr; #endif m_status = STATUS_INPROGRESS; } HRESULT hr = m_pgd->GetLongData(m_iRecordID, m_iFieldOrdinal, m_wSqlCType, (char*) pv, cb, cbRead); if (FAILED(hr)) return hr; if (hr == S_FALSE) { m_status = STATUS_FINISHED; return hr; } ULONG cbTerm = 0; if (m_wSqlCType == SQL_C_CHAR) cbTerm = sizeof(CHAR); else if (m_wSqlCType == SQL_C_WCHAR) cbTerm = sizeof(WCHAR); if (cbRead == SQL_NO_TOTAL || cbRead + cbTerm > cb) { cbRead = cb - cbTerm; if (m_wSqlCType == SQL_C_WCHAR) cbRead -= cbRead % sizeof(WCHAR); // FIXME: Is it necessary to fill the last 0-3 bytes of the buffer? } if (pcbRead != NULL) *pcbRead = (ULONG)cbRead; return S_OK; }
void CGetDataSequentialStream::Kill() { LOGCALL(("CGetDataSequentialStream::Kill()\n")); assert(m_pStreamSync != NULL); CriticalSection critical_section(m_pStreamSync); m_status = STATUS_ZOMBIE; }
STDMETHODIMP CEnumConnectionPoints::Reset() { LOGCALL(("CEnumConnectionPoints::Reset()\n")); CriticalSection critical_section(this); m_iCurrent = 0; return S_OK; }
void* thread_worker(void*p){ int i; for(i = 0; i < LOOP_TIMES;i++){ sem_wait(&s1); sem_wait(&s2); critical_section(2,i); sem_post(&s1); sem_post(&s2); } }
STDMETHODIMP CEnumConnections::Reset() { LOGCALL(("CEnumConnections::Reset()\n")); CriticalSection critical_section(this); m_fReset = true; return S_OK; }
void bar(threaddata<int> * td) { func_struct<int> fs; fs.func = &inc; fs.params = NULL; for(int i = 0; i < NON_DOM_ACCESSES; i++) critical_section(td->threadid, fs, td->sd); td->done = true; }
void foo(threaddata<int> * td) { func_struct<int> fs; fs.func = &inc; fs.params = NULL; shared_data<int> sd; for(int i = 0; i < DOM_ACCESSES; i++) critical_section(td->threadid, fs, td->sd); }
void* thread_worker(void* p) { int i; for (i=0; i<LOOP_TIMES; i++) { pthread_mutex_lock(&mutex2); pthread_mutex_lock(&mutex1); critical_section(2, i); pthread_mutex_unlock(&mutex2); pthread_mutex_unlock(&mutex1); } }
void *process_two(void *a) { int i; for (i = 0; i < TIMES; i++) { own_tasks(); wants2 = 1; __sync_synchronize(); whoWaits = 2; while (wants1 && (whoWaits == 2)) { } critical_section(); wants2 = 0; } pthread_exit(NULL); return NULL; }
STDMETHODIMP CEnumConnectionPoints::Skip(ULONG cConnections) { LOGCALL(("CEnumConnectionPoints::Skip()\n")); CriticalSection critical_section(this); while (cConnections--) { IConnectionPoint* pCP = m_rgpCP[m_iCurrent]; if (pCP == NULL) return S_FALSE; m_iCurrent++; } return S_OK; }
STDMETHODIMP CEnumConnectionPoints::Clone(IEnumConnectionPoints** ppEnum) { LOGCALL(("CEnumConnectionPoints::Clone()\n")); if (ppEnum == NULL) return E_POINTER; *ppEnum = NULL; CriticalSection critical_section(this); CEnumConnectionPointsInitializer initializer = { m_pUnkCPC, m_rgpCP, m_iCurrent }; return ComImmediateObj<CEnumConnectionPoints>::CreateInstance ( NULL, IID_IEnumConnectionPoints, (void**) ppEnum, &initializer ); }
static void remove_info(const char *resource) { int num_open; pthread_mutex_lock(&lv_hash_lock); dm_hash_remove(lv_hash, resource); /* When last lock is remove, validate there are not left opened devices */ if (!dm_hash_get_first(lv_hash)) { if (critical_section()) log_error(INTERNAL_ERROR "No volumes are locked however clvmd is in activation mode critical section."); if ((num_open = dev_cache_check_for_open_devices())) log_error(INTERNAL_ERROR "No volumes are locked however %d devices are still open.", num_open); } pthread_mutex_unlock(&lv_hash_lock); }
STDMETHODIMP CConnectionPoint::Unadvise(DWORD dwCookie) { LOGCALL(("CConnectionPoint::Unadvise()\n")); CriticalSection critical_section(this); CookieIter iter = m_cookies.find(dwCookie); if (iter == m_cookies.end()) return CONNECT_E_NOCONNECTION; HRESULT hr = Module::GetGIT()->RevokeInterfaceFromGlobal(dwCookie); if (FAILED(hr)) return hr; return S_OK; }
void CGetDataSequentialStream::Delete() { LOGCALL(("CGetDataSequentialStream::Delete()\n")); if (m_pStreamSync != NULL) { CriticalSection critical_section(m_pStreamSync); m_pStreamSync->SetStreamStatus(false); m_pStreamSync = NULL; } if (m_pUnkFTM != NULL) { m_pUnkFTM->Release(); m_pUnkFTM = NULL; } }
STDMETHODIMP CEnumConnections::Next(ULONG cConnections, CONNECTDATA *rgpCD, ULONG *pcFetched) { LOGCALL(("CEnumConnections::Next(%ld)\n", cConnections)); if (rgpCD == NULL) return E_POINTER; if (cConnections > 1 && pcFetched == NULL) return E_POINTER; CriticalSection critical_section(this); ULONG iConnections = 0; if (cConnections > 0) { bool fHasNext = false; DWORD dwCookie; IUnknown* pUnkSink; if (m_fReset) { fHasNext = m_pCP->GetFirstConnection(&dwCookie, &pUnkSink); if (fHasNext) m_fReset = false; } else { fHasNext = m_pCP->GetNextConnection(m_dwCookieCurrent, &dwCookie, &pUnkSink); } while (fHasNext) { m_dwCookieCurrent = dwCookie; rgpCD[iConnections].dwCookie = dwCookie; rgpCD[iConnections].pUnk = pUnkSink; iConnections++; fHasNext = m_pCP->GetNextConnection(m_dwCookieCurrent, &dwCookie, &pUnkSink); } } if (pcFetched) *pcFetched = iConnections; return iConnections < cConnections ? S_FALSE : S_OK; }
void thread_switch(struct thread *otd, struct thread *td) { ASSERT(critical_section(), "cannot switch outside a critical section."); if (otd == NULL) otd = current_thread(); ASSERT(otd != td, "cannot switch from a thread to itself."); if (otd != NULL) { if (cpu_context_save(otd)) { otd = current_thread(); thread_set_upcall(otd, thread_error, otd); /* * We've been restored by something, return. */ return; } } cpu_context_restore(td); }
int main (int argc, char* argv[]){ volatile int thing = UNLOCKED; //Create a team of threads on each processor #pragma omp parallel { //Each thread executes this code block independently lock(&thing); critical_section(); unlock(&thing); struct timespec time; clock_gettime(CLOCK_MONOTONIC, &time); printf("CPU %d finished: %lu\n", sched_getcpu(), time.tv_sec); } return 0; }
void* thread_worker(void* p) { int i; for(i=0;i<LOOP_TIMES;i++) { // pthread_mutex_lock(&mutex2); // printf("thread lock2\n"); pthread_mutex_lock(&mutex1); // printf("thread lock1\n"); pthread_mutex_lock(&mutex2); critical_section(2,i); pthread_mutex_unlock(&mutex2); // printf("thread unlock2\n"); pthread_mutex_unlock(&mutex1); // printf("thread unlock1\n"); } }
STDMETHODIMP CGetDataSequentialStream::Write(void const* pv, ULONG cb, ULONG* pcbWritten) { LOGCALL(("CGetDataSequentialStream::Write(cb = %d)\n", cb)); if (pcbWritten != NULL) *pcbWritten = 0; if (cb == 0) return S_OK; if (pv == NULL) return STG_E_INVALIDPOINTER; assert(m_pStreamSync != NULL); CriticalSection critical_section(m_pStreamSync); if (m_status == STATUS_ZOMBIE) return E_UNEXPECTED; return STG_E_CANTSAVE; }
HRESULT CEnumConnectionPoints::GetInterface(REFIID riid, IUnknown** ppUnknown) { LOGCALL (("CEnumConnectionPoints::GetInterface(%s)\n", STRINGFROMGUID (riid))); IUnknown* pUnknown = NULL; if (riid == IID_IEnumConnectionPoints) pUnknown = static_cast<IEnumConnectionPoints*>(this); else if (riid == IID_IMarshal) { CriticalSection critical_section(this); if (m_pUnkFTM == NULL) CoCreateFreeThreadedMarshaler(GetControllingUnknown(), &m_pUnkFTM); if (m_pUnkFTM != NULL) return m_pUnkFTM->QueryInterface(riid, (void**) ppUnknown); } if (pUnknown == NULL) return E_NOINTERFACE; *ppUnknown = pUnknown; return S_OK; }
int main(void) { int rtn,i; sem_init(&s1,0,1); sem_init(&s2,0,1); pthread_t pthread_id = 0; rtn = pthread_create(&pthread_id,NULL,thread_worker,NULL); if(rtn!=0){ printf("pthread_create ERROR!\n"); return -1; } for(i = 0;i < LOOP_TIMES;i++){ sem_wait(&s1); sem_wait(&s2); critical_section(1,i); sem_post(&s1); sem_post(&s2); sem_destroy(&s1); sem_destroy(&s2); } return 0; }
STDMETHODIMP CConnectionPoint::Advise(IUnknown *pUnkSink, DWORD *pdwCookie) { LOGCALL(("CConnectionPoint::Advise()\n")); if (pdwCookie == NULL) return E_POINTER; *pdwCookie = 0; if (pUnkSink == NULL) return E_POINTER; AutoInterface<IUnknown> pSink; HRESULT hr = pSink.QueryInterface(pUnkSink, m_riid); if (FAILED(hr)) { if (hr == E_NOINTERFACE) hr = CONNECT_E_CANNOTCONNECT; return hr; } CriticalSection critical_section(this); DWORD dwCookie; hr = Module::GetGIT()->RegisterInterfaceInGlobal(pSink.Get(), IID_IUnknown, &dwCookie); if (FAILED(hr)) return hr; try { m_cookies.insert(dwCookie); } catch (...) { Module::GetGIT()->RevokeInterfaceFromGlobal(dwCookie); return E_OUTOFMEMORY; } *pdwCookie = dwCookie; return S_OK; }
HRESULT CGetDataSequentialStream::GetInterface(REFIID riid, IUnknown** ppUnknown) { LOGCALL (("CGetDataSequentialStream::GetInterface(%s)\n", STRINGFROMGUID (riid))); IUnknown* pUnknown = NULL; if (riid == IID_ISequentialStream) pUnknown = static_cast<ISequentialStream*>(this); else if (riid == IID_ISupportErrorInfo) pUnknown = static_cast<ISupportErrorInfo*>(this); else if (riid == IID_IMarshal) { CriticalSection critical_section(m_pStreamSync); if (m_pUnkFTM == NULL) CoCreateFreeThreadedMarshaler(GetControllingUnknown(), &m_pUnkFTM); if (m_pUnkFTM != NULL) return m_pUnkFTM->QueryInterface(riid, (void**) ppUnknown); } if (pUnknown == NULL) return E_NOINTERFACE; *ppUnknown = pUnknown; return S_OK; }
bool CConnectionPoint::GetNextConnection(DWORD dwCookiePrev, DWORD* pdwCookie, IUnknown** ppUnkSink) { LOGCALL (("CConnectionPoint::GetNextConnection()\n")); CriticalSection critical_section(this); CookieIter iter = m_cookies.upper_bound(dwCookiePrev); if (iter == m_cookies.end()) return false; DWORD dwCookie = *iter; if (pdwCookie != NULL) *pdwCookie = dwCookie; if (ppUnkSink != NULL) { HRESULT hr = Module::GetGIT()->GetInterfaceFromGlobal(dwCookie, IID_IUnknown, (void**) ppUnkSink); if (FAILED(hr)) return false; } return true; }
int main(void) { int rtn, i; pthread_t pthread_id = 0; /* 存放子线程的id */ rtn = pthread_create(&pthread_id,NULL, thread_worker, NULL ); if(rtn != 0) { printf("pthread_create ERROR!\n"); return -1; } for (i=0; i<LOOP_TIMES; i++) { pthread_mutex_lock(&mutex1); pthread_mutex_lock(&mutex2); critical_section(1, i); pthread_mutex_unlock(&mutex2); pthread_mutex_unlock(&mutex1); } pthread_mutex_destroy(&mutex1); pthread_mutex_destroy(&mutex2); //sleep(1); return 0; }