本文整理汇总了C++中pthread_mutex_trylock函数的典型用法代码示例。如果您正苦于以下问题:C++ pthread_mutex_trylock函数的具体用法?C++ pthread_mutex_trylock怎么用?C++ pthread_mutex_trylock使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了pthread_mutex_trylock函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。
示例1: call_trylock
int
call_trylock()
{
return pthread_mutex_trylock(&call_mutex);
}
开发者ID:possiblybhavin,项目名称:mx27_sdk,代码行数:5,代码来源:call.c
示例2: __cilkrts_os_mutex_trylock
int __cilkrts_os_mutex_trylock(struct os_mutex *p)
{
int status;
status = pthread_mutex_trylock (&p->mutex);
return (status == 0);
}
开发者ID:PrasadG193,项目名称:gcc_gimple_fe,代码行数:6,代码来源:os_mutex-unix.c
示例3: pthread_mutex_trylock
bool PThreadMutex::TryEnter()
{
return pthread_mutex_trylock(&mutex) == 0;
}
开发者ID:ronsaldo,项目名称:chela,代码行数:4,代码来源:PThreads.cpp
示例4: pthread_rwlock_trywrlock
int
pthread_rwlock_trywrlock (pthread_rwlock_t * rwlock)
{
int result, result1;
pthread_rwlock_t rwl;
if (rwlock == NULL || *rwlock == NULL)
{
return EINVAL;
}
/*
* We do a quick check to see if we need to do more work
* to initialise a static rwlock. We check
* again inside the guarded section of ptw32_rwlock_check_need_init()
* to avoid race conditions.
*/
if (*rwlock == PTHREAD_RWLOCK_INITIALIZER)
{
result = ptw32_rwlock_check_need_init (rwlock);
if (result != 0 && result != EBUSY)
{
return result;
}
}
rwl = *rwlock;
if (rwl->nMagic != PTW32_RWLOCK_MAGIC)
{
return EINVAL;
}
if ((result = pthread_mutex_trylock (&(rwl->mtxExclusiveAccess))) != 0)
{
return result;
}
if ((result =
pthread_mutex_trylock (&(rwl->mtxSharedAccessCompleted))) != 0)
{
result1 = pthread_mutex_unlock (&(rwl->mtxExclusiveAccess));
return ((result1 != 0) ? result1 : result);
}
if (rwl->nExclusiveAccessCount == 0)
{
if (rwl->nCompletedSharedAccessCount > 0)
{
rwl->nSharedAccessCount -= rwl->nCompletedSharedAccessCount;
rwl->nCompletedSharedAccessCount = 0;
}
if (rwl->nSharedAccessCount > 0)
{
if ((result =
pthread_mutex_unlock (&(rwl->mtxSharedAccessCompleted))) != 0)
{
(void) pthread_mutex_unlock (&(rwl->mtxExclusiveAccess));
return result;
}
if ((result =
pthread_mutex_unlock (&(rwl->mtxExclusiveAccess))) == 0)
{
result = EBUSY;
}
}
else
{
rwl->nExclusiveAccessCount = 1;
}
}
else
{
result = EBUSY;
}
return result;
}
开发者ID:ACanadianKernel,项目名称:pcsx2,代码行数:81,代码来源:pthread_rwlock_trywrlock.c
示例5: main
int main ( void )
{
int r;
/* pthread_t thr; */
/* pthread_attr_t thra; */
pthread_mutexattr_t mxa, mxa2;
pthread_mutex_t mx, mx2, mx3, mx4;
pthread_cond_t cv;
struct timespec abstime;
pthread_rwlock_t rwl;
pthread_rwlock_t rwl2;
pthread_rwlock_t rwl3;
sem_t s1;
# if __GLIBC_PREREQ(2,4)
fprintf(stderr,
"\n\n------ This is output for >= glibc 2.4 ------\n");
# else
fprintf(stderr,
"\n\n------ This is output for < glibc 2.4 ------\n");
# endif
/* --------- pthread_create/join --------- */
fprintf(stderr,
"\n---------------- pthread_create/join ----------------\n\n");
/* make pthread_create fail */
/* It's amazingly difficult to make pthread_create fail
without first soaking up all the machine's resources.
Instead, in order to demonstrate that it's really wrapped,
create a child thread, generate a race error, and join with it
again. */
/* This just segfaults:
memset( &thra, 0xFF, sizeof(thra) );
r= pthread_create( &thr, NULL, lazy_child, NULL ); assert(r);
*/
{ pthread_t child;
r= pthread_create( &child, NULL, racy_child, NULL ); assert(!r);
sleep(1); /* just to ensure parent thread reports race, not child */
unprotected = 5678;
r= pthread_join( child, NULL ); assert(!r);
}
/* make pthread_join fail */
r= pthread_join( pthread_self(), NULL ); assert(r);
/* --------- pthread_mutex_lock et al --------- */
fprintf(stderr,
"\n---------------- pthread_mutex_lock et al ----------------\n\n");
/* make pthread_mutex_init fail */
#if defined(__sun__)
pthread_mutexattr_init( &mxa );
memset( mxa.__pthread_mutexattrp, 0xFF, 5 * sizeof(int) );
#else
memset( &mxa, 0xFF, sizeof(mxa) );
#endif
r= pthread_mutex_init( &mx, &mxa );
# if __GLIBC_PREREQ(2,4)
assert(r); /* glibc >= 2.4: the call should fail */
# else
assert(!r); /* glibc < 2.4: oh well, glibc didn't bounce this */
# endif
/* make pthread_mutex_destroy fail */
r= pthread_mutex_init( &mx2, NULL ); assert(!r);
r= pthread_mutex_lock( &mx2 ); assert(!r);
r= pthread_mutex_destroy( &mx2 );
/* make pthread_mutex_lock fail (skipped on < glibc 2.4 because it
doesn't fail, hence hangs the test) */
# if __GLIBC_PREREQ(2,4)
memset( &mx3, 0xFF, sizeof(mx3) );
r= pthread_mutex_lock( &mx3 ); assert(r);
# else
fprintf(stderr, "\nmake pthread_mutex_lock fail: "
"skipped on glibc < 2.4\n\n");
# endif
/* make pthread_mutex_trylock fail */
memset( &mx3, 0xFF, sizeof(mx3) );
r= pthread_mutex_trylock( &mx3 ); assert(r);
/* make pthread_mutex_timedlock fail */
memset( &abstime, 0, sizeof(abstime) );
memset( &mx3, 0xFF, sizeof(mx3) );
r= pthread_mutex_timedlock( &mx3, &abstime ); assert(r);
/* make pthread_mutex_unlock fail */
memset( &mx3, 0xFF, sizeof(mx3) );
r= pthread_mutex_unlock( &mx3 );
# if __GLIBC_PREREQ(2,4)
assert(r);
# else
assert(!r);
# endif
/* --------- pthread_cond_wait et al --------- */
//.........这里部分代码省略.........
开发者ID:Darriall,项目名称:opt-cpp-backend,代码行数:101,代码来源:tc20_verifywrap.c
示例6: return
bool Mutex::trylock() {
return (pthread_mutex_trylock(&(this->_m)) == 0);
}
开发者ID:Hexatyla,项目名称:Common,代码行数:3,代码来源:Mutex.cpp
示例7: my_pthread_mutex_trylock
int
my_pthread_mutex_trylock (pthread_mutex_t *__mutex)
{
pthread_mutex_t *realmutex = late_init_pthread_mutex(__mutex);
return pthread_mutex_trylock(realmutex);
}
开发者ID:Axxelloss,项目名称:apkenv,代码行数:6,代码来源:pthread_wrappers.c
示例8: pthread_self
bool CAMutex::Try(bool& outWasLocked)
{
bool theAnswer = false;
outWasLocked = false;
#if TARGET_OS_MAC
pthread_t theCurrentThread = pthread_self();
if(!pthread_equal(theCurrentThread, mOwner))
{
// this means the current thread doesn't already own the lock
#if Log_Ownership
DebugPrintfRtn(DebugPrintfFileComma "%p %.4f: CAMutex::Try: thread %p is try-locking %s, owner: %p\n", theCurrentThread, ((Float64)(CAHostTimeBase::GetCurrentTimeInNanos()) / 1000000.0), theCurrentThread, mName, mOwner);
#endif
// go ahead and call trylock to see if we can lock it.
int theError = pthread_mutex_trylock(&mMutex);
if(theError == 0)
{
// return value of 0 means we successfully locked the lock
mOwner = theCurrentThread;
theAnswer = true;
outWasLocked = true;
#if Log_Ownership
DebugPrintfRtn(DebugPrintfFileComma "%p %.4f: CAMutex::Try: thread %p has locked %s, owner: %p\n", theCurrentThread, ((Float64)(CAHostTimeBase::GetCurrentTimeInNanos()) / 1000000.0), theCurrentThread, mName, mOwner);
#endif
}
else if(theError == EBUSY)
{
// return value of EBUSY means that the lock was already locked by another thread
theAnswer = false;
outWasLocked = false;
#if Log_Ownership
DebugPrintfRtn(DebugPrintfFileComma "%p %.4f: CAMutex::Try: thread %p failed to lock %s, owner: %p\n", theCurrentThread, ((Float64)(CAHostTimeBase::GetCurrentTimeInNanos()) / 1000000.0), theCurrentThread, mName, mOwner);
#endif
}
else
{
// any other return value means something really bad happenned
ThrowIfError(theError, CAException(theError), "CAMutex::Try: call to pthread_mutex_trylock failed");
}
}
else
{
// this means the current thread already owns the lock
theAnswer = true;
outWasLocked = false;
}
#elif TARGET_OS_WIN32
if(mOwner != GetCurrentThreadId())
{
// this means the current thread doesn't own the lock
#if Log_Ownership
DebugPrintfRtn(DebugPrintfFileComma "%lu %.4f: CAMutex::Try: thread %lu is try-locking %s, owner: %lu\n", GetCurrentThreadId(), ((Float64)(CAHostTimeBase::GetCurrentTimeInNanos()) / 1000000.0), GetCurrentThreadId(), mName, mOwner);
#endif
// try to acquire the mutex
OSStatus theError = WaitForSingleObject(mMutex, 0);
if(theError == WAIT_OBJECT_0)
{
// this means we successfully locked the lock
mOwner = GetCurrentThreadId();
theAnswer = true;
outWasLocked = true;
#if Log_Ownership
DebugPrintfRtn(DebugPrintfFileComma "%lu %.4f: CAMutex::Try: thread %lu has locked %s, owner: %lu\n", GetCurrentThreadId(), ((Float64)(CAHostTimeBase::GetCurrentTimeInNanos()) / 1000000.0), GetCurrentThreadId(), mName, mOwner);
#endif
}
else if(theError == WAIT_TIMEOUT)
{
// this means that the lock was already locked by another thread
theAnswer = false;
outWasLocked = false;
#if Log_Ownership
DebugPrintfRtn(DebugPrintfFileComma "%lu %.4f: CAMutex::Try: thread %lu failed to lock %s, owner: %lu\n", GetCurrentThreadId(), ((Float64)(CAHostTimeBase::GetCurrentTimeInNanos()) / 1000000.0), GetCurrentThreadId(), mName, mOwner);
#endif
}
else
{
// any other return value means something really bad happenned
ThrowIfError(theError, CAException(GetLastError()), "CAMutex::Try: call to lock the mutex failed");
}
}
else
{
// this means the current thread already owns the lock
theAnswer = true;
outWasLocked = false;
}
#endif
return theAnswer;
}
开发者ID:63n,项目名称:ardour,代码行数:96,代码来源:CAMutex.cpp
示例9: pthread_mutex_trylock
bool CLinuxMutex::TryLock()
{
//pthread_mutex_trylock returns 0 if lock is acquired.
//Will return EBUSY if lock is not acquired
return pthread_mutex_trylock(&_mtx) == 0;
}
开发者ID:lordio,项目名称:insanity,代码行数:6,代码来源:CLinuxMutex.cpp
示例10: core_queue_packet
int core_queue_packet(struct packet *p, unsigned int flags, unsigned int thread_affinity) {
// Update the counters
registry_perf_inc(p->input->perf_pkts_in, 1);
registry_perf_inc(p->input->perf_bytes_in, p->len);
if (!core_run)
return POM_ERR;
debug_core("Queuing packet %p (%u.%06u)", p, pom_ptime_sec(p->ts), pom_ptime_usec(p->ts));
// Find the right thread to queue to
struct core_processing_thread *t = NULL;
if (flags & CORE_QUEUE_HAS_THREAD_AFFINITY) {
t = core_processing_threads[thread_affinity % core_num_threads];
pom_mutex_lock(&t->pkt_queue_lock);
} else {
static volatile unsigned int start = 0;
unsigned int i;
while (1) {
unsigned int thread_id = start;
for (i = 0; i < core_num_threads; i++) {
thread_id++;
if (thread_id >= core_num_threads)
thread_id -= core_num_threads;
t = core_processing_threads[thread_id];
int res = pthread_mutex_trylock(&t->pkt_queue_lock);
if (res == EBUSY) {
// Thread is busy, go to the next one
continue;
} else if (res) {
pomlog(POMLOG_ERR "Error while locking a processing thread pkt_queue mutex : %s", pom_strerror(res));
abort();
return POM_ERR;
}
// We've got the lock, check if it's ok to queue here
if (t->pkt_count < CORE_THREAD_PKT_QUEUE_MAX) {
// Use this thread
break;
}
// Too many packets pending in this thread, go to the next one
pom_mutex_unlock(&t->pkt_queue_lock);
}
if (i < core_num_threads) {
// We locked on a thread
start = thread_id;
break;
}
// No thread found
if (core_pkt_queue_count >= ((CORE_THREAD_PKT_QUEUE_MAX - 1) * core_num_threads)) {
// Queue full
if (flags & CORE_QUEUE_DROP_IF_FULL) {
packet_release(p);
registry_perf_inc(perf_pkt_dropped, 1);
debug_core("Dropped packet %p (%u.%06u) to thread %u", p, pom_ptime_sec(p->ts), pom_ptime_usec(p->ts));
return POM_OK;
}
// We're not going to drop this. Wait then
debug_core("All queues full. Waiting ...");
pom_mutex_lock(&core_pkt_queue_wait_lock);
// Recheck the count after locking
if (core_pkt_queue_count >= ((CORE_THREAD_PKT_QUEUE_MAX - 1) * core_num_threads)) {
int res = pthread_cond_wait(&core_pkt_queue_wait_cond, &core_pkt_queue_wait_lock);
if (res) {
pomlog(POMLOG_ERR "Error while waiting for the core pkt_queue condition : %s", pom_strerror(res));
abort();
}
}
pom_mutex_unlock(&core_pkt_queue_wait_lock);
}
}
}
// We've got the thread's lock, add it to the queue
struct core_packet_queue *tmp = NULL;
if (t->pkt_queue_unused) {
tmp = t->pkt_queue_unused;
t->pkt_queue_unused = tmp->next;
} else {
tmp = malloc(sizeof(struct core_packet_queue));
if (!tmp) {
pom_mutex_unlock(&t->pkt_queue_lock);
pom_oom(sizeof(struct core_packet_queue));
return POM_ERR;
}
}
tmp->pkt = p;
tmp->next = NULL;
//.........这里部分代码省略.........
开发者ID:gmsoft-tuxicoman,项目名称:pom-ng,代码行数:101,代码来源:core.c
示例11: do_test
//.........这里部分代码省略.........
return 1;
}
if (pthread_mutexattr_destroy (&a) != 0)
{
puts ("mutexattr_destroy failed");
return 1;
}
if (pthread_barrierattr_init (&ba) != 0)
{
puts ("barrierattr_init failed");
return 1;
}
if (pthread_barrierattr_setpshared (&ba, PTHREAD_PROCESS_SHARED) != 0)
{
puts ("barrierattr_setpshared failed");
return 1;
}
if (pthread_barrier_init (b, &ba, 2) != 0)
{
puts ("barrier_init failed");
return 1;
}
if (pthread_barrierattr_destroy (&ba) != 0)
{
puts ("barrierattr_destroy failed");
return 1;
}
err = pthread_mutex_trylock (m);
if (err == 0)
{
puts ("mutex_trylock succeeded");
return 1;
}
else if (err != EBUSY)
{
puts ("mutex_trylock didn't return EBUSY");
return 1;
}
*p = 0;
if (pthread_mutex_unlock (m) != 0)
{
puts ("parent: 1st mutex_unlock failed");
return 1;
}
puts ("going to fork now");
pid = fork ();
if (pid == -1)
{
puts ("fork failed");
return 1;
}
else if (pid == 0)
{
if (pthread_mutex_lock (m) != 0)
{
puts ("child: mutex_lock failed");
return 1;
开发者ID:AdvancedC,项目名称:glibc,代码行数:67,代码来源:tst-mutex4.c
示例12: mutex_try_lock
inline int mutex_try_lock(mutex_t m)
{
return pthread_mutex_trylock(&m->m_mutex);
}
开发者ID:benjiam,项目名称:epoll_mutilthread,代码行数:4,代码来源:sync.c
示例13: main
//.........这里部分代码省略.........
}
}
}
// altrimenti
else {
// prova ad aprire il file che mappa la memoria condivisa
if ((sm=shm_open("/shared_mtx",O_RDWR,S_IRUSR|S_IWUSR))<0) {
printf("Error !!! File %s, line %d\n",__FILE__,__LINE__);
exit(EXIT_FAILURE);
}
}
// se sei il primo processo
if (d==-1)
// imposta le dimensioni del file/memoria condivisa per
// contenere il mutex
if (ftruncate(sm,sizeof(pthread_mutex_t))<0) {
printf("Error !!! File %s, line %d\n",__FILE__,__LINE__);
exit(EXIT_FAILURE);
}
// mappa il file associato alla memoria ottenura dal sistema
// per la condivisone nel proprio spazio di memoria
if ((mutex=(void *)mmap(NULL,sizeof(pthread_mutex_t),PROT_WRITE|PROT_READ,MAP_SHARED,sm,0))==MAP_FAILED) {
printf("Error !!! File %s, line %d\n",__FILE__,__LINE__);
exit(EXIT_FAILURE);
}
printf("Shared mutex obtained\n");
// se sei il primo processo
if (d==-1) {
// inizializza il mutex per essere condiviso tra processi
// e non solo tra thread
pthread_mutexattr_init(&mattr);
pthread_mutexattr_setpshared(&mattr,PTHREAD_PROCESS_SHARED);
pthread_mutex_init(mutex,&mattr);
pthread_mutexattr_destroy(&mattr);
printf("Mutex initialized\n");
}
// finche' non si deve uscire
while (!wanna_exit) {
// genera un intervallo casuale
i=rand()%9+1;
// esci se richiesto
if (wanna_exit) {
printf("Exit...\n");
break;
}
// o vai a dormire
printf("Going to sleep for %d sec...\n",i);
sleep(i);
// esci se richiesto
if (wanna_exit) {
printf("Exit...\n");
break;
}
// prova ad ottenere il mutex
if (pthread_mutex_trylock(mutex)) {
printf("Wait for the mutex...\n");
pthread_mutex_lock(mutex);
}
// quando l'hai ottenuto
// genera un intervallo casuale
i=rand()%9+1;
// esci se richiesto dopo aver rilasciato il mutex
if (wanna_exit) {
pthread_mutex_unlock(mutex);
printf("Exit...\n");
break;
}
// altrimenti dormi
printf("Mutex obtained and now wait for %d sec...\n",i);
sleep(i);
// se si deve uscire rilascia prima il mutex
if (wanna_exit) {
pthread_mutex_unlock(mutex);
printf("Exit...\n");
break;
}
// sblocca il mutex
printf("Unlock mutex...\n");
pthread_mutex_unlock(mutex);
}
// abbiamo finito e rimuoviamo l'associazione della file condiviso
// alla memoria del processo
munmap(mutex,sizeof(pthread_mutex_t));
// se siamo il primo processo
if (d==-1) {
// impostiamo la cancellazione del file/memoria condivisa
// (verra' effettivamente rimosso quando tutti i processi che lo
// condividono chiuderanno il descrittore relativo (in questo
// caso alla terminazione del processo)
shm_unlink("/shared_mtx");
printf("Removing shared mutex\n");
}
exit(EXIT_SUCCESS);
}
开发者ID:DesiD,项目名称:CsInfoPa,代码行数:101,代码来源:process_mutex.c
示例14: csoundLockMutexNoWait
PUBLIC int csoundLockMutexNoWait(void *mutex_)
{
return pthread_mutex_trylock((pthread_mutex_t*) mutex_);
}
开发者ID:BlakeJarvis,项目名称:csound,代码行数:4,代码来源:threads.c
示例15: try_lock
bool try_lock() { return pthread_mutex_trylock(&_pmutex) == 0; }
开发者ID:Lezval,项目名称:fix8,代码行数:1,代码来源:ff_wrapper.hpp
示例16: conntrack_get_unique_from_parent
int conntrack_get_unique_from_parent(struct proto_process_stack *stack, unsigned int stack_index) {
struct conntrack_node_list *child = NULL;
struct conntrack_list *lst = NULL;
struct proto_process_stack *s = &stack[stack_index];
struct proto_process_stack *s_prev = &stack[stack_index - 1];
struct conntrack_entry *parent = s_prev->ce;
if (!s->proto) {
pomlog(POMLOG_ERR "Cannot allocate conntrack for NULL proto");
return POM_ERR;
}
if (!parent) {
pomlog(POMLOG_ERR "Cannot allocate unique conntrack without a parent");
return POM_ERR;
}
if (s->ce) { // This should only occur in the case that an expectation matched
// Make sure the conntrack is locked
int res = pthread_mutex_trylock(&s->ce->lock);
if (res && res != EBUSY && res == EDEADLK) {
pomlog(POMLOG_ERR "Error while locking the conntrack : %s", pom_strerror(res));
return POM_ERR;
}
return POM_OK;
}
conntrack_lock(parent);
struct conntrack_tables *ct = s->proto->ct;
struct conntrack_entry *res = NULL;
// Look for the conntrack
if (parent->children) {
struct conntrack_node_list *child = parent->children;
for (child = parent->children; child && child->ce->proto != s->proto; child = child->next);
if (child)
res = child->ce;
}
if (!res) {
// Alloc the conntrack
res = malloc(sizeof(struct conntrack_entry));
if (!res) {
pom_oom(sizeof(struct conntrack_entry));
goto err;
}
memset(res, 0, sizeof(struct conntrack_entry));
res->proto = s->proto;
if (pom_mutex_init_type(&res->lock, PTHREAD_MUTEX_ERRORCHECK) != POM_OK)
goto err;
// Alloc the child list
child = malloc(sizeof(struct conntrack_node_list));
if (!child)
goto err;
memset(child, 0, sizeof(struct conntrack_node_list));
child->ce = res;
child->ct = ct;
// Alloc the parent node
res->parent = malloc(sizeof(struct conntrack_node_list));
if (!res->parent) {
free(child);
goto err;
}
memset(res->parent, 0, sizeof(struct conntrack_node_list));
res->parent->ce = parent;
res->parent->ct = parent->proto->ct;
res->parent->hash = parent->hash;
// Alloc the list node
lst = malloc(sizeof(struct conntrack_list));
if (!lst) {
pom_oom(sizeof(struct conntrack_list));
goto err;
}
memset(lst, 0, sizeof(struct conntrack_list));
lst->ce = res;
// Add the child to the parent
child->next = parent->children;
if (child->next)
child->next->prev = child;
parent->children = child;
// Add the conntrack to the table
pom_mutex_lock(&ct->locks[0]);
lst->next = ct->table[0];
if (lst->next)
lst->next->prev = lst;
//.........这里部分代码省略.........
开发者ID:elfixit,项目名称:pom-ng,代码行数:101,代码来源:conntrack.c
示例17: try_lock
/// Non-blocking attempt to acquire a lock on the mutex
inline bool try_lock() const {
return pthread_mutex_trylock( &m_mut ) == 0;
}
开发者ID:HanumathRao,项目名称:graphlab,代码行数:4,代码来源:mutex.hpp
示例18: sql_get_socket
/*************************************************************************
*
* Function: sql_get_socket
*
* Purpose: Return a SQL sqlsocket from the connection pool
*
*************************************************************************/
SQLSOCK * sql_get_socket(SQL_INST * inst)
{
SQLSOCK *cur, *start;
int tried_to_connect = 0;
int unconnected = 0;
time_t now = time(NULL);
/*
* Start at the last place we left off.
*/
start = inst->last_used;
if (!start) start = inst->sqlpool;
cur = start;
while (cur) {
#ifdef HAVE_PTHREAD_H
/*
* If this socket is in use by another thread,
* skip it, and try another socket.
*
* If it isn't used, then grab it ourselves.
*/
if (pthread_mutex_trylock(&cur->mutex) != 0) {
goto next;
} /* else we now have the lock */
#endif
/*
* If the socket has outlived its lifetime, and
* is connected, close it, and mark it as open for
* reconnections.
*/
if (inst->config->lifetime && (cur->state == sockconnected) &&
((cur->connected + inst->config->lifetime) < now)) {
DEBUG2("Closing socket %d as its lifetime has been exceeded", cur->id);
(inst->module->sql_close)(cur, inst->config);
cur->state = sockunconnected;
goto reconnect;
}
/*
* If we have performed too many queries over this
* socket, then close it.
*/
if (inst->config->max_queries && (cur->state == sockconnected) &&
(cur->queries >= inst->config->max_queries)) {
DEBUG2("Closing socket %d as its max_queries has been exceeded", cur->id);
(inst->module->sql_close)(cur, inst->config);
cur->state = sockunconnected;
goto reconnect;
}
/*
* If we happen upon an unconnected socket, and
* this instance's grace period on
* (re)connecting has expired, then try to
* connect it. This should be really rare.
*/
if ((cur->state == sockunconnected) && (now > inst->connect_after)) {
reconnect:
radlog(L_INFO, "rlm_sql (%s): Trying to (re)connect unconnected handle %d..", inst->config->xlat_name, cur->id);
tried_to_connect++;
connect_single_socket(cur, inst);
}
/* if we still aren't connected, ignore this handle */
if (cur->state == sockunconnected) {
DEBUG("rlm_sql (%s): Ignoring unconnected handle %d..", inst->config->xlat_name, cur->id);
unconnected++;
#ifdef HAVE_PTHREAD_H
pthread_mutex_unlock(&cur->mutex);
#endif
goto next;
}
/* should be connected, grab it */
DEBUG("rlm_sql (%s): Reserving sql socket id: %d", inst->config->xlat_name, cur->id);
if (unconnected != 0 || tried_to_connect != 0) {
DEBUG("rlm_sql (%s): got socket %d after skipping %d unconnected handles, tried to reconnect %d though", inst->config->xlat_name, cur->id, unconnected, tried_to_connect);
}
/*
* The socket is returned in the locked
* state.
*
* We also remember where we left off,
* so that the next search can start from
* here.
*
* Note that multiple threads MAY over-write
* the 'inst->last_used' variable. This is OK,
//.........这里部分代码省略.........
开发者ID:101,项目名称:freeradius-server,代码行数:101,代码来源:sql.c
示例19: job_queue_run_jobs
void job_queue_run_jobs(job_queue_type * queue , int num_total_run, bool verbose) {
int trylock = pthread_mutex_trylock( &queue->run_mutex );
if (trylock != 0)
util_abort("%s: another thread is already running the queue_manager\n",__func__);
else if (!queue->user_exit) {
/* OK - we have got an exclusive lock to the run_jobs code. */
//Check if queue is open. Fails hard if not open
job_queue_check_open(queue);
/*
The number of threads in the thread pool running callbacks. Memory consumption can
potentially be quite high while running the DONE callback - should therefor not use
too many threads.
*/
const int NUM_WORKER_THREADS = 4;
queue->work_pool = thread_pool_alloc( NUM_WORKER_THREADS , true );
{
bool new_jobs = false;
bool cont = true;
int phase = 0;
queue->running = true;
do {
bool local_user_exit = false;
job_list_get_rdlock( queue->job_list );
/*****************************************************************/
if (queue->user_exit) {/* An external thread has called the job_queue_user_exit() function, and we should kill
all jobs, do some clearing up and go home. Observe that we will go through the
queue handling codeblock below ONE LAST TIME before exiting. */
job_queue_user_exit__( queue );
local_user_exit = true;
}
job_queue_check_expired(queue);
/*****************************************************************/
{
bool update_status = job_queue_update_status( queue );
if (verbose) {
if (update_status || new_jobs)
job_queue_print_summary(queue , update_status );
job_queue_update_spinner( &phase );
}
{
int num_complete = job_queue_status_get_count(queue->status, JOB_QUEUE_SUCCESS) +
job_queue_status_get_count(queue->status, JOB_QUEUE_FAILED) +
job_queue_status_get_count(queue->status, JOB_QUEUE_IS_KILLED);
if ((num_total_run > 0) && (num_total_run == num_complete))
/* The number of jobs completed is equal to the number
of jobs we have said we want to run; so we are finished.
*/
cont = false;
else {
if (num_total_run == 0) {
/* We have not informed about how many jobs we will
run. To check if we are complete we perform the two
tests:
1. All the jobs which have been added with
job_queue_add_job() have completed.
2. The user has used job_queue_complete_submit()
to signal that no more jobs will be forthcoming.
*/
if ((num_complete == job_list_get_size( queue->job_list )) && queue->submit_complete)
cont = false;
}
}
}
if (cont) {
/* Submitting new jobs */
int max_submit = 5; /* This is the maximum number of jobs submitted in one while() { ... } below.
Only to ensure that the waiting time before a status update is not too long. */
int total_active = job_queue_status_get_count(queue->status, JOB_QUEUE_PENDING) + job_queue_status_get_count(queue->status, JOB_QUEUE_RUNNING);
int num_submit_new;
{
int max_running = job_queue_get_max_running( queue );
if (max_running > 0)
num_submit_new = util_int_min( max_submit , max_running - total_active );
else
/*
If max_running == 0 that should be interpreted as no limit; i.e. the queue layer will
attempt to send an unlimited number of jobs to the driver - the driver can reject the jobs.
*/
num_submit_new = util_int_min( max_submit , job_queue_status_get_count(queue->status, JOB_QUEUE_WAITING));
}
new_jobs = false;
if (job_queue_status_get_count(queue->status, JOB_QUEUE_WAITING) > 0) /* We have waiting jobs at all */
if (num_submit_new > 0) /* The queue can allow more running jobs */
new_jobs = true;
if (new_jobs) {
int submit_count = 0;
//.........这里部分代码省略.........
开发者ID:agchitu,项目名称:ert,代码行数:101,代码来源:job_queue.c
示例20: NaClFastMutexTryLock
int NaClFastMutexTryLock(struct NaClFastMutex *flp) {
return NaClXlateErrno(pthread_mutex_trylock(&flp->mu));
}
开发者ID:ClarkWang-2013,项目名称:native_client,代码行数:3,代码来源:nacl_fast_mutex.c
注:本文中的pthread_mutex_trylock函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论