本文整理汇总了C++中sem_trywait函数的典型用法代码示例。如果您正苦于以下问题:C++ sem_trywait函数的具体用法?C++ sem_trywait怎么用?C++ sem_trywait使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了sem_trywait函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: sem_wait
int FileQueue<T>::doPop(T *&pointer, bool block) {
if (block) {
sem_wait(&(this->_semPop));
} else {
if (sem_trywait(&(this->_semPop)) != 0) {
return ERROR_QUEUE_IS_EMPTY;
}
}
pthread_mutex_lock(&_mutexReadWrite);
openFile();
lseek(_file, _head, SEEK_SET);
this->create(pointer);
read(_file, pointer, this->_dataSize);
_head = lseek(_file, 0L, SEEK_CUR);
lseek(_file, 0L, SEEK_SET);
write(_file, &_head, sizeof(off_t));
pthread_mutex_unlock(&_mutexReadWrite);
return 0;
}
示例2: while
int CSemaphore::wait(u_int32_t msec)
{
int ret = 0;
u_int32_t wait_time = 0;
if(INFINITE == msec)
{
while(0 != (ret = sem_wait(&m_semaphore)) && EINTR == errno);
}
else
{
struct timespec delay_time;
delay_time.tv_sec = 0;
delay_time.tv_nsec = 1000000; // 1ms = 1000us // 1us = 1000ns
while (true)
{
ret = sem_trywait(&m_semaphore);
if (0 == ret)
break;
if (EAGAIN != errno)
{
ret = -1;
break;
}
if (msec <= wait_time)
{
ret = -1;
break;
}
nanosleep(&delay_time, NULL);
wait_time++;
}
}
return ret;
}
示例3: sem_trywait
HX_RESULT HXPthreadMacSemaphore::_TryWait()
{
HX_RESULT retVal = HXR_OK;
int nResult = 0;
nResult = sem_trywait( m_semaphore );
if( nResult<0 && errno == EAGAIN )
{
retVal = HXR_WOULD_BLOCK;
}
else if( nResult < 0 )
{
#ifdef _DEBUG
fprintf( stderr, "Can't wait on semaphore: %d %s\n", errno, strerror(errno) );
#endif
retVal = HXR_FAIL;
}
return retVal;
}
示例4: semlock_iszero
static PyObject *
semlock_iszero(SemLockObject *self)
{
#ifdef HAVE_BROKEN_SEM_GETVALUE
if (sem_trywait(self->handle) < 0) {
if (errno == EAGAIN)
Py_RETURN_TRUE;
return _PyMp_SetError(NULL, MP_STANDARD_ERROR);
} else {
if (sem_post(self->handle) < 0)
return _PyMp_SetError(NULL, MP_STANDARD_ERROR);
Py_RETURN_FALSE;
}
#else
int sval;
if (SEM_GETVALUE(self->handle, &sval) < 0)
return _PyMp_SetError(NULL, MP_STANDARD_ERROR);
return PyBool_FromLong((long)sval == 0);
#endif
}
示例5: test1_second_thread
static void test1_second_thread(void)
{
puts("second: sem_trywait");
if (sem_trywait(&s) == 0) {
puts("second: sem_trywait failed");
}
puts("second: sem_trywait done with == 0");
puts("second: wait for post");
if (sem_wait(&s) != 1) {
puts("second: sem_wait failed");
}
puts("second: sem was posted");
puts("second: end");
}
示例6: sem_trywait
void *S2ESynchronizedObjectInternal::tryAquire()
{
SyncHeader *hdr = (SyncHeader*)m_sharedBuffer;
#ifdef CONFIG_DARWIN
if (__sync_lock_test_and_set(&hdr->lock, 0) != 0) {
return NULL;
}
#else
int ret;
do {
ret = sem_trywait(&hdr->lock);
if (ret < 0 && errno == EAGAIN) {
return NULL;
}
}while(ret);
#endif
return ((uint8_t*)m_sharedBuffer + m_headerSize);
}
示例7: smf_wait
void smf_wait()
{
sem_t *sp;
pthread_mutex_lock(&mutex);
if(sem_trywait(&sem) == 0)
pthread_mutex_unlock(&mutex);
else {
sp = (sem_t *)malloc(sizeof(sem_t));
sem_init(sp, 0, 0);
_queue_push(sp);
pthread_mutex_unlock(&mutex);
sem_wait(sp);
sem_destroy(sp);
free(sp);
}
}
示例8: amp_semaphore_trywait_PROPOSED
int amp_semaphore_trywait_PROPOSED(amp_semaphore_t semaphore)
{
assert(NULL != semaphore);
errno = 0;
int retval = sem_trywait(&semaphore->semaphore);
int return_code = AMP_SUCCESS;
if(0 != retval) {
switch (errno) {
case EAGAIN: /* Semaphore was already locked */
return_code = AMP_BUSY;
break;
default:
assert(0); /* Programming error */
return_code = AMP_ERROR;
}
}
return return_code;
}
示例9: while
bool MMFMutex::Lock( uint32 dwTimeout )
{
if( m_owner == pthread_self() )
{
m_recursive_count++;
return true;
}
bool bUseTimeout = (dwTimeout != 0);
while(true) {
bool bGotSemaphore = false;
if( m_key == 0 )
{
bGotSemaphore = (sem_trywait(&m_semaphore) == 0);
}
else
{
struct sembuf operations[1];
operations[0].sem_num = 0;
operations[0].sem_op = -1;
operations[0].sem_flg = SEM_UNDO|IPC_NOWAIT;
bGotSemaphore = (semop(m_id, operations, 1) >= 0);
}
if( bGotSemaphore )
{
m_owner = pthread_self();
m_recursive_count = 1;
return true;
}
sleep(1);
if( bUseTimeout )
{
if( dwTimeout > 1000 )
dwTimeout -= 1000;
else
dwTimeout = 0;
if( dwTimeout == 0 )
return false;
}
}
}
示例10: write_jack
static int write_jack(out123_handle *ao, unsigned char *buf, int len)
{
jack_handle_t *handle = (jack_handle_t*)ao->userptr;
size_t bytes_left;
unsigned int strike = 0;
bytes_left = len;
while(bytes_left && handle->alive)
{
size_t piece;
debug("writing to ringbuffer");
/* No help: piece1 = jack_ringbuffer_write_space(handle->rb); */
piece = jack_ringbuffer_write(handle->rb, (char*)buf, bytes_left);
debug1("wrote %"SIZE_P" B", (size_p)piece);
buf += piece;
bytes_left -= piece;
/* Allow nothing being written some times, but not too often.
Don't know how often in a row that would be supposed to happen. */
if(!piece)
{
if(++strike > 100)
{
if(!AOQUIET)
error("Cannot write to ringbuffer.");
break;
}
/* Avoid busy waiting and semaphore accumulation:
Wait once on the semaphore, then clear it. We count on it being
posted by the process callback and we are going to push new data
so that that one gets the chance. */
sem_wait(&handle->sem);
do errno = 0;
while(sem_trywait(&handle->sem) == 0 || errno == EINTR);
}
else
strike = 0;
}
return len-bytes_left;
}
示例11: main
int main(void)
{
char semname[NAME_MAX - 4];
sem_t *mysemp;
int val;
snprintf(semname, sizeof(semname), "/" FUNCTION "_" TEST "_%ld",
(long)getpid());
mysemp = sem_open(semname, O_CREAT, 0777, 1);
if (mysemp == SEM_FAILED || mysemp == NULL) {
perror(ERROR_PREFIX "sem_open");
return PTS_UNRESOLVED;
}
/* Lock Semaphore */
if (sem_trywait(mysemp) == -1) {
perror(ERROR_PREFIX "trywait");
return PTS_UNRESOLVED;
}
if (sem_getvalue(mysemp, &val) < 0) {
perror(ERROR_PREFIX "sem_getvalue");
return PTS_UNRESOLVED;
}
/*
printf("Current value is: %d\n", val);
*/
if (val <= 0) {
puts("TEST PASSED");
sem_close(mysemp);
sem_unlink(semname);
return PTS_PASS;
} else {
puts("TEST FAILED");
return PTS_FAIL;
}
}
示例12: printf_bad_list_cleint_persistent
void printf_bad_list_cleint_persistent(void) {
printf(" USER NUMBER\n");
sem_t *sem_client = sem_open(SHARED_MEMORY_SEM, 0, 0600, 1);
int trys = 1, sem_reopen = 0;
if (sem_client != SEM_FAILED) {
while (trys) {
if (sem_trywait(sem_client) == 0) {
if (bad_list_clents_global && (bad_list_clents_global
!= MAP_FAILED)) {
long index = 0;
for (index = 0; index < bad_list_clents_global->numbers; index++) {
printf(" %-16s %ld\n",
bad_list_clents_global->items[index].username,
index);
}
}
trys = 0;
} else {
if (errno == EAGAIN) {
trys++;
if (trys == 100) {
trys = 1;
sem_close(sem_client);
sem_client = sem_open(SHARED_MEMORY_SEM, 0, 0600, 1);
sem_reopen++;
if (sem_reopen == 4)
break;
}
} else {
trys = 0;
}
}
}
sem_post(sem_client);
sem_close(sem_client);
}
return;
}
示例13: Thread_wait_sem
/**
* Wait for a semaphore to be posted, or timeout.
* @param sem the semaphore
* @param timeout the maximum time to wait, in milliseconds
* @return completion code
*/
int Thread_wait_sem(sem_type sem, int timeout)
{
/* sem_timedwait is the obvious call to use, but seemed not to work on the Viper,
* so I've used trywait in a loop instead. Ian Craggs 23/7/2010
*/
int rc = -1;
#if !defined(WIN32) && !defined(WIN64)
#define USE_TRYWAIT
#if defined(USE_TRYWAIT)
int i = 0;
int interval = 10000; /* 10000 microseconds: 10 milliseconds */
int count = (1000 * timeout) / interval; /* how many intervals in timeout period */
#else
struct timespec ts;
#endif
#endif
FUNC_ENTRY;
#if defined(WIN32) || defined(WIN64)
rc = WaitForSingleObject(sem, timeout);
#elif defined(USE_TRYWAIT)
while (++i < count && (rc = sem_trywait(sem)) != 0)
{
if (rc == -1 && ((rc = errno) != EAGAIN))
{
rc = 0;
break;
}
usleep(interval); /* microseconds - .1 of a second */
}
#else
if (clock_gettime(CLOCK_REALTIME, &ts) != -1)
{
ts.tv_sec += timeout;
rc = sem_timedwait(sem, &ts);
}
#endif
FUNC_EXIT_RC(rc);
return rc;
}
示例14: sem_trywait
void EventCtrl::runOnce_()
{
#ifndef MONO_THREAD
sem_trywait(&queueSem);
#endif
if (eventQueue.size() == 0)
return;
#ifndef MONO_THREAD
pthread_mutex_lock(&queueMutex);
#endif
while (eventQueue.size() > 0)
{
Event event = eventQueue.front();
eventQueue.pop();
#ifndef MONO_THREAD
pthread_mutex_unlock(&queueMutex);
#endif
send(event);
}
}
示例15: sem_lock
BaseGDL* sem_lock(EnvT *e)
{
SizeT nParam = e->NParam(1);
DString name;
e->AssureStringScalarPar(0, name);
sem_data_t &data = sem_get_data(name, e);
if (sem_is_locked(data))
{
// lock already held
return new DIntGDL(1);
}
if (sem_trywait(data.sem) == 0)
{
sem_set_locked(data, true);
return new DIntGDL(1);
}
return new DIntGDL(0);
}