static int
create_cond(os_handler_t  *handler,
	    os_hnd_cond_t **new_cond)
{
    os_hnd_cond_t      *cond;
    pthread_condattr_t attr;
    int                rv;

    rv = pthread_condattr_init(&attr);
    if (rv)
	return rv;

    rv = pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
    if (rv) {
	pthread_condattr_destroy(&attr);
	return rv;
    }

    cond = malloc(sizeof(*cond));
    if (!cond) {
	pthread_condattr_destroy(&attr);
	return ENOMEM;
    }

    rv = pthread_cond_init(&cond->cond, &attr);
    pthread_condattr_destroy(&attr);
    if (rv) {
	free(cond);
	return rv;
    }

    *new_cond = cond;
    return 0;
}
Exemple #2
0
int uv_cond_init(uv_cond_t* cond) {
  pthread_condattr_t attr;
  int err;

  err = pthread_condattr_init(&attr);
  if (err)
    return -err;

#if !(defined(__ANDROID__) && defined(HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC))
  err = pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
  if (err)
    goto error2;
#endif

  err = pthread_cond_init(cond, &attr);
  if (err)
    goto error2;

  err = pthread_condattr_destroy(&attr);
  if (err)
    goto error;

  return 0;

error:
  pthread_cond_destroy(cond);
error2:
  pthread_condattr_destroy(&attr);
  return -err;
}
Exemple #3
0
/*
 * Main entry point to the sync engine
 */
void start_sync_engine(sqlite3 *db) {	
    pthread_attr_t  attr;
    pthread_t       p_thread_id;
    int             return_val;
    int thread_error;
	database = db;
	
#ifndef __SYMBIAN32__	
	// Initialize thread
    return_val = pthread_attr_init(&attr);
	pthread_condattr_init(&sync_details);
	pthread_cond_init(&sync_cond, &sync_details);
	pthread_condattr_destroy(&sync_details);
    assert(!return_val);
    return_val = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    assert(!return_val);
	  
    thread_error = pthread_create(&p_thread_id, &attr, &sync_engine_main_routine, NULL);
	
    return_val = pthread_attr_destroy(&attr);
    assert(!return_val);
    if (thread_error != 0) {
		  //TODO: Report error
    }
#else
    //[AA] posix thread is not required under the Symbian because we are using native Symbian threads
	pthread_condattr_init(&sync_details);
	pthread_cond_init(&sync_cond, &sync_details);
	pthread_condattr_destroy(&sync_details);
    
#endif	  
}
Exemple #4
0
int uv_cond_init(uv_cond_t* cond) {
  pthread_condattr_t attr;

  if (pthread_condattr_init(&attr))
    return -1;

#if !defined(ANDROID)
  if (pthread_condattr_setclock(&attr, CLOCK_MONOTONIC))
    goto error2;
#endif

  if (pthread_cond_init(cond, &attr))
    goto error2;

  if (pthread_condattr_destroy(&attr))
    goto error;

  return 0;

error:
  pthread_cond_destroy(cond);
error2:
  pthread_condattr_destroy(&attr);
  return -1;
}
COND_HANDLE Condition_Init(void)
{
    // If we don't know our time basis, find it.
    if (time_basis == -1)
    {
        pthread_condattr_t cattr;
        pthread_condattr_init(&cattr);
        pthread_condattr_getclock(&cattr, &time_basis);
        pthread_condattr_destroy(&cattr);
    }

    // Codes_SRS_CONDITION_18_002: [ Condition_Init shall create and return a CONDITION_HANDLE ]
    pthread_cond_t * cond = (pthread_cond_t*)malloc(sizeof(pthread_cond_t));
    if (cond != NULL)
    {
        // set our time basis when configuring the condition
        pthread_condattr_t cattr;
        pthread_condattr_init(&cattr);
        pthread_condattr_setclock(&cattr, time_basis);
        pthread_cond_init(cond, &cattr);
        pthread_condattr_destroy(&cattr);
    }
    // Codes_SRS_CONDITION_18_008: [ Condition_Init shall return NULL if it fails to allocate the CONDITION_HANDLE ]
    return cond;
}
Exemple #6
0
int uv_cond_init(uv_cond_t* cond) {
  pthread_condattr_t attr;
  int err;

  err = pthread_condattr_init(&attr);
  if (err)
    return UV__ERR(err);

#if !(defined(__ANDROID_API__) && __ANDROID_API__ < 21)
  err = pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
  if (err)
    goto error2;
#endif

  err = pthread_cond_init(cond, &attr);
  if (err)
    goto error2;

  err = pthread_condattr_destroy(&attr);
  if (err)
    goto error;

  return 0;

error:
  pthread_cond_destroy(cond);
error2:
  pthread_condattr_destroy(&attr);
  return UV__ERR(err);
}
Exemple #7
0
int uv_cond_init(uv_cond_t* cond) {
  pthread_condattr_t attr;

  if (pthread_condattr_init(&attr))
    return -1;
#ifndef __ANDROID__
  if (pthread_condattr_setclock(&attr, CLOCK_MONOTONIC))
#else
  if (pthread_condattr_setpshared(&attr, CLOCK_MONOTONIC))
#endif
    goto error2;

  if (pthread_cond_init(cond, &attr))
    goto error2;

  if (pthread_condattr_destroy(&attr))
    goto error;

  return 0;

error:
  pthread_cond_destroy(cond);
error2:
  pthread_condattr_destroy(&attr);
  return -1;
}
Exemple #8
0
qp_cond_t
qp_cond_create(qp_cond_t cond, bool shared)
{
    pthread_condattr_t    attr;
    
    if (NULL == cond) {
        cond = (qp_cond_t)qp_alloc(sizeof(struct qp_ipc_cond_s));
        
        if (NULL == cond) {
            return NULL;
        }
        
        memset(cond, 0, sizeof(struct qp_ipc_cond_s));
        qp_cond_set_alloced(cond);
        
    } else {
        memset(cond, 0, sizeof(struct qp_ipc_cond_s));
    }
    
    if (QP_SUCCESS != pthread_condattr_init(&attr)) {
        qp_cond_is_alloced(cond) ? qp_free(cond) : 1;
        return NULL;
    }
    
    if (NULL == qp_lock_init(&cond->cond_lock, shared, false)) {
        qp_cond_is_alloced(cond) ? qp_free(cond) : 1;
        return NULL;
    }
    
#ifdef _POSIX_THREAD_PROCESS_SHARED
    if (shared) {
        
        if (QP_SUCCESS != pthread_condattr_setpshared(&attr,
            PTHREAD_PROCESS_SHARED))
        {
            pthread_condattr_destroy(&attr);
            qp_lock_destroy(&cond->cond_lock);
            qp_cond_is_alloced(cond) ? qp_free(cond) : 1;
            return NULL;
        }
        
        qp_cond_set_shared(cond);
    }
#endif
    
    if (QP_SUCCESS != pthread_cond_init(&(cond->cond), &attr)) {
        pthread_condattr_destroy(&attr);
        qp_lock_destroy(&cond->cond_lock);
        qp_cond_is_alloced(cond) ? qp_free(cond) : 1;
        return NULL;
    }
    
    qp_cond_set_inited(cond);
    return cond;
}
int my_rw_init(my_rw_lock_t *rwp)
{
  pthread_condattr_t	cond_attr;

#ifdef _WIN32
  /*
    Once initialization is used here rather than in my_init(), in order to
    - avoid  my_init() pitfalls- (undefined order in which initialization should
    run)
    - be potentially useful C++ (static constructors) 
    - just to simplify  the API. 
    Also, the overhead is of my_pthread_once is very small.
  */
  static my_pthread_once_t once_control= MY_PTHREAD_ONCE_INIT;
  my_pthread_once(&once_control, check_srwlock_availability);

  if (have_srwlock)
    return srw_init(rwp);
#endif

  pthread_mutex_init( &rwp->lock, MY_MUTEX_INIT_FAST);
  pthread_condattr_init( &cond_attr );
  pthread_cond_init( &rwp->readers, &cond_attr );
  pthread_cond_init( &rwp->writers, &cond_attr );
  pthread_condattr_destroy(&cond_attr);

  rwp->state	= 0;
  rwp->waiters	= 0;
#ifdef SAFE_MUTEX
  rwp->write_thread   = 0;
#endif

  return(0);
}
Exemple #10
0
void CPosixThreadImpl::start(IRhoRunnable *pRunnable, IRhoRunnable::EPriority ePriority)
{
#if defined(OS_ANDROID)
    // Android has no pthread_condattr_xxx API
    pthread_cond_init(&m_condSync, NULL);
#else
    pthread_condattr_t sync_details;
    pthread_condattr_init(&sync_details);
    pthread_cond_init(&m_condSync, &sync_details);
    pthread_condattr_destroy(&sync_details);
#endif

    pthread_attr_t  attr;
    int return_val = pthread_attr_init(&attr);
    return_val = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    RHO_ASSERT(!return_val);

    if ( ePriority != IRhoRunnable::epNormal)
    {
        sched_param param;
        return_val = pthread_attr_getschedparam (&attr, &param);
        param.sched_priority = ePriority == IRhoRunnable::epLow ? 20 : 100; //TODO: sched_priority
        return_val = pthread_attr_setschedparam (&attr, &param);
    }

    int thread_error = pthread_create(&m_thread, &attr, &runProc, pRunnable);
    return_val = pthread_attr_destroy(&attr);
    RHO_ASSERT(!return_val);
    RHO_ASSERT(thread_error==0);
}
Exemple #11
0
int OSA_queCreate(OSA_QueHndl *hndl, Uint32 maxLen)
{
  pthread_mutexattr_t mutex_attr;
  pthread_condattr_t cond_attr;
  int status=OSA_SOK;

  hndl->curRd = hndl->curWr = 0;
  hndl->count = 0;
  hndl->len   = maxLen;
  hndl->queue = OSA_memAlloc(sizeof(Int32)*hndl->len);
  
  if(hndl->queue==NULL) {
    OSA_ERROR("OSA_queCreate() = %d \r\n", status);
    return OSA_EFAIL;
  }
 
  status |= pthread_mutexattr_init(&mutex_attr);
  status |= pthread_condattr_init(&cond_attr);  
  
  status |= pthread_mutex_init(&hndl->lock, &mutex_attr);
  status |= pthread_cond_init(&hndl->condRd, &cond_attr);    
  status |= pthread_cond_init(&hndl->condWr, &cond_attr);  

  if(status!=OSA_SOK)
    OSA_ERROR("OSA_queCreate() = %d \r\n", status);
    
  pthread_condattr_destroy(&cond_attr);
  pthread_mutexattr_destroy(&mutex_attr);
    
  return status;
}
    int ConditionVariable::Construct()
    {
#if defined(CIO_IOS)
        pthread_cond_init(&cond_, nullptr);
#elif defined(CIO_ANDROID)
        int result = 0;
        pthread_condattr_t cond_attr;
        result = pthread_condattr_init(&cond_attr);
        if (result != 0) {
            return -1;
        }
        result = pthread_condattr_setclock(&cond_attr, CLOCK_MONOTONIC);
        if (result != 0) {
            return -1;
        }
        result = pthread_cond_init(&cond_, &cond_attr);
        if (result != 0) {
            return -1;
        }
        result = pthread_condattr_destroy(&cond_attr);
        if (result != 0) {
            return -1;
        }
#endif
        
        return 0;
    }
Exemple #13
0
IceUtil::Cond::Cond()
{
    pthread_condattr_t attr;

    int rc = pthread_condattr_init(&attr);
    if(rc != 0)
    {
        throw ThreadSyscallException(__FILE__, __LINE__, rc);
    }

#if !defined(__hpux) && !defined(__APPLE__)
    rc = pthread_condattr_setclock(&attr, CLOCK_MONOTONIC); 
    if(rc != 0)
    {
        throw ThreadSyscallException(__FILE__, __LINE__, rc);
    }
#endif

    rc = pthread_cond_init(&_cond, &attr);
    if(rc != 0)
    {
        throw ThreadSyscallException(__FILE__, __LINE__, rc);
    }

    rc = pthread_condattr_destroy(&attr);
    if(rc != 0)
    {
        throw ThreadSyscallException(__FILE__, __LINE__, rc);
    }
}
AREXPORT ArCondition::ArCondition() :
  myFailedInit(false),
  myCond(),
  myMutex()
{
  myMutex.setLogName("ArCondition::myMutex");
  pthread_condattr_t attr;

  pthread_condattr_init(&attr);
  if (pthread_cond_init(&myCond, &attr) != 0)
  {
    ArLog::log(ArLog::Terse, "ArCondition::ArCondition: Unknown error trying to create the condition.");
    myFailedInit=true;
  }

  pthread_condattr_destroy(&attr);

  ourStrMap[STATUS_FAILED]="General failure";
  ourStrMap[STATUS_FAILED_DESTROY]=
  "Another thread is waiting on this condition so it can not be destroyed";
  ourStrMap[STATUS_FAILED_INIT] =
  "Failed to initialize thread. Requested action is imposesible";
  ourStrMap[STATUS_MUTEX_FAILED_INIT]="The underlying mutex failed to init";
  ourStrMap[STATUS_MUTEX_FAILED]="The underlying mutex failed in some fashion";
}
Exemple #15
0
int
NdbCondition_Init(struct NdbCondition* ndb_cond)
{
  int result;

  assert(init); /* Make sure library has been initialized */

#if defined HAVE_CLOCK_GETTIME && defined HAVE_PTHREAD_CONDATTR_SETCLOCK && \
    defined CLOCK_MONOTONIC
  if (clock_id == CLOCK_MONOTONIC)
  {
    pthread_condattr_t attr;
    pthread_condattr_init(&attr);
    pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
    result = pthread_cond_init(&ndb_cond->cond, &attr);
    pthread_condattr_destroy(&attr);
  }
  else
  {
    result = pthread_cond_init(&ndb_cond->cond, NULL);
  }
#else
  result = native_cond_init(&ndb_cond->cond);
#endif
  assert(result==0);
  return result;
}
Exemple #16
0
/**
* @fn                   :tc_pthread_pthread_cond_init_destroy
* @brief                :pthread_cond_init initialises the condition variable referenced by cond with attributes referenced by attr
*                        pthread_cond_destroy destroy the given condition variable specified by cond
* @Scenario             :pthread_cond_init initialises the condition variable referenced by cond with attributes referenced by attr
*                        pthread_cond_destroy destroy the given condition variable specified by cond
* API's covered         :pthread_cond_init, pthread_cond_destroy
* Preconditions         :none
* Postconditions        :none
* @return               :void
*/
static void tc_pthread_pthread_cond_init_destroy(void)
{
	int ret_chk;
	pthread_condattr_t attr;
	pthread_cond_t cond_nullparam;
	pthread_cond_t cond_attrparam;

	ret_chk = pthread_condattr_init(&attr);
	TC_ASSERT_EQ("pthread_condattr_init", ret_chk, OK);

	/* parameters of pthread_cond_init are of opaque data type hence parameter check not possible */
	ret_chk = pthread_cond_init(&cond_nullparam, NULL);
	TC_ASSERT_EQ("pthread_cond_init", ret_chk, OK);

	ret_chk = pthread_cond_init(&cond_attrparam, &attr);
	TC_ASSERT_EQ("pthread_cond_init", ret_chk, OK);

	ret_chk = pthread_cond_destroy(&cond_nullparam);
	TC_ASSERT_EQ("pthread_cond_destroy", ret_chk, OK);

	ret_chk = pthread_cond_destroy(&cond_attrparam);
	TC_ASSERT_EQ("pthread_cond_destroy", ret_chk, OK);

	ret_chk = pthread_condattr_destroy(&attr);
	TC_ASSERT_EQ("pthread_condattr_destroy", ret_chk, OK);

	TC_SUCCESS_RESULT();
}
Exemple #17
0
int OSA_msgqCreate(OSA_MsgqHndl *hndl)
{
  pthread_mutexattr_t mutex_attr;
  pthread_condattr_t cond_attr;
  int status=OSA_SOK;
 
  status |= pthread_mutexattr_init(&mutex_attr);
  status |= pthread_condattr_init(&cond_attr);  
  
  status |= pthread_mutex_init(&hndl->lock, &mutex_attr);
  status |= pthread_cond_init(&hndl->condRd, &cond_attr);    
  status |= pthread_cond_init(&hndl->condWr, &cond_attr);  

  hndl->curRd = hndl->curWr = 0;
  hndl->count = 0;
  hndl->len   = OSA_MSGQ_LEN_MAX;

  if(status!=OSA_SOK)
    OSA_ERROR("OSA_msgqCreate() = %d \r\n", status);
    
  pthread_condattr_destroy(&cond_attr);
  pthread_mutexattr_destroy(&mutex_attr);
    
  return status;
}
static DBusCondVar *
_dbus_pthread_condvar_new (void)
{
  DBusCondVarPThread *pcond;
  pthread_condattr_t attr;
  int result;
  
  pcond = dbus_new (DBusCondVarPThread, 1);
  if (pcond == NULL)
    return NULL;

  pthread_condattr_init (&attr);
#ifdef HAVE_MONOTONIC_CLOCK
  if (have_monotonic_clock)
    pthread_condattr_setclock (&attr, CLOCK_MONOTONIC);
#endif

  result = pthread_cond_init (&pcond->cond, &attr);
  pthread_condattr_destroy (&attr);

  if (result == EAGAIN || result == ENOMEM)
    {
      dbus_free (pcond);
      return NULL;
    }
  else
    {
      PTHREAD_CHECK ("pthread_cond_init", result);
    }
  
  return DBUS_COND_VAR (pcond);
}
Exemple #19
0
Cond::Cond()
{
    pthread_condattr_t attr;
    int rt = pthread_condattr_init(&attr);
#ifdef _NO_EXCEPTION
    assert( 0 == rt );
#else
    if( 0 != rt )
    {
        throw ThreadSyscallException(__FILE__, __LINE__, rt);
    }
#endif
    
    rt =  pthread_cond_init(&_cond, &attr);
#ifdef _NO_EXCEPTION
    assert( 0 == rt );
#else
    if( 0 != rt )
    {
        throw ThreadSyscallException(__FILE__, __LINE__, rt);
    }
#endif

    rt = pthread_condattr_destroy(&attr);
#ifdef _NO_EXCEPTION
    assert( 0 == rt );
#else
    if( 0 != rt )
    {
        throw ThreadSyscallException(__FILE__, __LINE__, rt);
    }
#endif
}
Exemple #20
0
Threading::Cond::Cond(void) 
{
    pthread_condattr_t condattr;

    int returnVal = pthread_condattr_init(&condattr);
    if (0 != returnVal)
    {
        throw ThreadSyscallException(__FILE__, __LINE__, returnVal);
    }

#if ! defined(__hpux) && ! defined(__APPLE__)
    returnVal = pthread_condattr_setclock(&condattr, CLOCK_MONOTONIC); 
    if (0 != returnVal)
    {
        throw ThreadSyscallException(__FILE__, __LINE__, returnVal);
    }
#endif

    returnVal = pthread_cond_init(&m_cond, &condattr);
    if (0 != returnVal)
    {
        throw ThreadSyscallException(__FILE__, __LINE__, returnVal);
    }

    returnVal = pthread_condattr_destroy(&condattr);
    if (0 != returnVal)
    {
        throw ThreadSyscallException(__FILE__, __LINE__, returnVal);
    }
}
short TU_Condition_pthread::Set_Attribs(pthread_condattr_t * attr)
{
        // Check and see if the thread_lib used is ours.
        if ((this->lib.bIsPlugin != TU_LibID_pthreads.bIsPlugin) ||
	    (this->lib.Name == NULL) || (strcmp(this->lib.Name, TU_LibID_pthreads.Name) != 0))    // PThreads ID.
        {
                // This condition variable was not created by our library. Abort.
                return -1;
        }

        // Set the attribs object.
        if (attr != NULL)
        {
                // Copy the attrib object.
                this->cattribs = *attr;

                // Set the flags.
                this->cuser_attrib = true;
                this->cattrib_init = true;
        }
        else
        {
                // Destroy the object.
                this->rc_from_prevOP = pthread_condattr_destroy(&cattribs);

                // Set the flags.
                this->cuser_attrib = false;
                this->cattrib_init = true;
        }

        // Exit function.
        return 0;
}
Exemple #22
0
int get_thread_attr()
{
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    print_thread_attr(&attr);
    pthread_attr_destroy(&attr);
    printf("\n");

    pthread_mutexattr_t mutex_attr;
    pthread_mutexattr_init(&mutex_attr);
    print_mutexattr(&mutex_attr);
    pthread_mutexattr_destroy(&mutex_attr);
    printf("\n");

    pthread_rwlockattr_t rwlock_attr;
    pthread_rwlockattr_init(&rwlock_attr);
    print_rwlockattr(&rwlock_attr);
    pthread_rwlockattr_destroy(&rwlock_attr);
    printf("\n");

    pthread_condattr_t cond_attr;
    pthread_condattr_init(&cond_attr);
    print_condattr(&cond_attr);
    pthread_condattr_destroy(&cond_attr);
    printf("\n");

    return THREAD_ATTR_OK;
}
Exemple #23
0
int main()
{
	pthread_condattr_t condattr;

	/* Initialize a condition variable attributes object */
	if (pthread_condattr_init(&condattr) != 0)
	{
		fprintf(stderr,"Cannot initialize condition variable attributes object\n");
		return PTS_UNRESOLVED;
	}

	/* Destroy the condition variable attributes object */
	if (pthread_condattr_destroy(&condattr) != 0)
	{
		fprintf(stderr,"Cannot destroy the condition variable attributes object\n");
		return PTS_UNRESOLVED;
	}

	/* Initialize the condition variable attributes object again.  This shouldn't result in an error. */
	if (pthread_condattr_init(&condattr) != 0)
	{
		printf("Test FAILED\n");
		return PTS_FAIL;
	}
	else
	{
		printf("Test PASSED\n");
		return PTS_PASS;
	}
}
int
pth_condattr_destroy (pth_cond_t *c) {
	if (c != (pth_cond_t *)NULL) {
		return pthread_condattr_destroy (&(c->attr));
	}
	return CAF_ERROR;
}
Exemple #25
0
int main(int argc, char **argv)
{
	pthread_mutexattr_t mattr;
	pthread_condattr_t cattr;
	int err;

	for ( ; ; ) {
		if ((err = pthread_mutexattr_init(&mattr)) != 0) {
			fprintf(stderr, "pthread_mutexattr_init error: %s\n", strerror(err));
			exit(1);
		}
		if ((err = pthread_mutexattr_destroy(&mattr)) != 0) {
			fprintf(stderr, "pthread_mutexattr_destroy error: %s\n", strerror(err));
			exit(1);
		}
		if ((err = pthread_condattr_init(&cattr)) != 0) {
			fprintf(stderr, "pthread_condattr_init error: %s\n", strerror(err));
			exit(1);
		}
		if ((err = pthread_condattr_destroy(&cattr)) != 0) {
			fprintf(stderr, "pthread_condattr_destroy error: %s\n", strerror(err));
			exit(1);
		}
	}

	exit(0);
}
Exemple #26
0
int OSA_semCreate(OSA_SemHndl *hndl, Uint32 maxCount, Uint32 initVal)
{
  pthread_mutexattr_t mutex_attr;
  pthread_condattr_t cond_attr;
  int status=OSA_SOK;
 
  status |= pthread_mutexattr_init(&mutex_attr);
  status |= pthread_condattr_init(&cond_attr);  
  
  status |= pthread_mutex_init(&hndl->lock, &mutex_attr);
  status |= pthread_cond_init(&hndl->cond, &cond_attr);  

  hndl->count = initVal;
  hndl->maxCount = maxCount;

  if(hndl->maxCount==0)  
    hndl->maxCount=1;
    
  if(hndl->count>hndl->maxCount)
    hndl->count = hndl->maxCount;

  if(status!=OSA_SOK)
    OSA_ERROR("OSA_semCreate() = %d \r\n", status);
    
  pthread_condattr_destroy(&cond_attr);
  pthread_mutexattr_destroy(&mutex_attr);
  
  return status;
}
MonotonicCond::~MonotonicCond()
{
    if (m_initialized) {
        pthread_cond_destroy(&m_cond);
        pthread_condattr_destroy(&m_condattr);
    }
}
Exemple #28
0
static void
native_cond_initialize(rb_nativethread_cond_t *cond, int flags)
{
#ifdef HAVE_PTHREAD_COND_INIT
    int r;
# if USE_MONOTONIC_COND
    pthread_condattr_t attr;

    pthread_condattr_init(&attr);

    cond->clockid = CLOCK_REALTIME;
    if (flags & RB_CONDATTR_CLOCK_MONOTONIC) {
	r = pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
	if (r == 0) {
	    cond->clockid = CLOCK_MONOTONIC;
	}
    }

    r = pthread_cond_init(&cond->cond, &attr);
    pthread_condattr_destroy(&attr);
# else
    r = pthread_cond_init(&cond->cond, NULL);
# endif
    if (r != 0) {
	rb_bug_errno("pthread_cond_init", r);
    }

    return;
#endif
}
Exemple #29
0
/**
 * Add a file to the download queue, or subscribe to a file that has already
 * been added to the download queue. The thread waits until the file has been
 * downloaded.
 * @param fileId [in] The ID of the file in the database.
 * @param owner [in] User who made the cache request.
 * @return Nothing.
 * Test: unit test (test-downloadqueue.c).
 */
void
ReceiveDownload(
	sqlite3_int64 fileId,
	uid_t         owner
	            )
{
	GList                       *subscriptionEntry;
	struct DownloadSubscription *subscription;
	pthread_mutexattr_t         mutexAttr;
	pthread_condattr_t          condAttr;

	/* Find out if a subscription to the same file is already queued. */
	pthread_mutex_lock( &mainLoop_mutex );
	subscriptionEntry = g_queue_find_custom( &downloadQueue, &fileId,
											 FindInDownloadQueue );
	/* No other subscriptions, so create one. */
	if( subscriptionEntry == NULL )
	{
		/* Create a new subscription entry. */
		subscription = malloc( sizeof( struct DownloadSubscription ) );
		subscription->fileId         = fileId;
		subscription->downloadActive = false;
		subscription->subscribers    = 1;
		/* Prepare wait condition and acknowledgment mutexes. */
		pthread_mutexattr_init( &mutexAttr );
		pthread_mutex_init( &subscription->waitMutex, &mutexAttr );
		pthread_mutex_init( &subscription->acknowledgeMutex, &mutexAttr );
		pthread_mutexattr_destroy( &mutexAttr );
		pthread_condattr_init( &condAttr );
		pthread_cond_init( &subscription->waitCond, &condAttr );
		pthread_cond_init( &subscription->acknowledgeCond, &condAttr );
		pthread_condattr_destroy( &condAttr );
		/* Add the entry to the transfers list. */
		Query_AddDownload( fileId, owner );
		/* Enqueue the entry. */
		g_queue_push_tail( &downloadQueue, subscription );
	}
	/* Another thread is subscribing to the same download, so use its wait
	   condition. */
	else
	{
		/* Subscribe to the download. */
		subscription = subscriptionEntry->data;
		subscription->subscribers++;
	}

	/* Lock the download completion mutex before the download is clear to
	   begin. */
	pthread_mutex_lock( &subscription->waitMutex );
	/* Tell the download manager that a new subscription has been entered. */
	pthread_cond_signal( &mainLoop_cond );
	pthread_mutex_unlock( &mainLoop_mutex );

	/* Wait until the download is complete. */
	pthread_cond_wait( &subscription->waitCond, &subscription->waitMutex );
	pthread_mutex_unlock( &subscription->waitMutex );

	/* Unsubscribe from the download. */
	UnsubscribeFromDownload( subscription );
}
CBinarySemaphore::CBinarySemaphore(bool isFull, bool isProcessShared) : mCounter(1)
{
	Int32 retVal;
	pthread_mutexattr_t mutexAttr;
	retVal = pthread_mutexattr_init(&mutexAttr);
	sAssertion(0 == retVal, "(CBinarySemaphore::CBinarySemaphore()) : Failed to init Mutex-Attribute!");

	retVal = pthread_mutexattr_settype(&mutexAttr, PTHREAD_MUTEX_ERRORCHECK);
	sAssertion(0 == retVal, "(CBinarySemaphore::CBinarySemaphore()) : Failed to set Mutex-Type!");

	retVal = pthread_mutex_init(&mMutex, &mutexAttr);
	sAssertion(0 == retVal, "(CBinarySemaphore::CBinarySemaphore()) : Failed to init Mutex!");

	retVal = pthread_mutexattr_destroy(&mutexAttr);
	sAssertion(0 == retVal, "(CBinarySemaphore::CBinarySemaphore()) : Failed to destroy Mutex-Attribute!");

	pthread_condattr_t conditionAttr;
	retVal = pthread_condattr_init(&conditionAttr);
	sAssertion(0 == retVal, "(CBinarySemaphore::CBinarySemaphore()) : Failed to init Condition-Attribute!");

	retVal = pthread_condattr_setpshared(&conditionAttr,
										 isProcessShared ? PTHREAD_PROCESS_SHARED : PTHREAD_PROCESS_PRIVATE);
	sAssertion(0 == retVal, "(CBinarySemaphore::CBinarySemaphore()) : Failed to set Condition-Attribute!");

	pthread_cond_init(&mCondition, &conditionAttr);
	pthread_condattr_destroy(&conditionAttr);


	if(false == isFull)
	{
		mCounter = 0;
	}
}