예제 #1
0
int pthread_rwlock_timedwrlock(pthread_rwlock_t *rwlock, const struct timespec *abs_timeout)
{
    int thread_id, ret = 0;

    if (rwlock == NULL)
        return EINVAL;

    pthread_mutex_lock(&rwlock->lock);
    thread_id = __get_thread_id();
    if (__unlikely(!write_precondition(rwlock, thread_id))) {
        /* If we can't read yet, wait until the rwlock is unlocked
         * and try again. Increment pendingReaders to get the
         * cond broadcast when that happens.
         */
        rwlock->pendingWriters += 1;
        do {
            ret = pthread_cond_timedwait(&rwlock->cond, &rwlock->lock, abs_timeout);
        } while (ret == 0 && !write_precondition(rwlock, thread_id));
        rwlock->pendingWriters -= 1;
        if (ret != 0)
            goto EXIT;
    }
    rwlock->numLocks ++;
    rwlock->writerThreadId = thread_id;
EXIT:
    pthread_mutex_unlock(&rwlock->lock);
    return ret;
}
예제 #2
0
_WCRTLINK int pthread_setschedparam(pthread_t __thr, int __policy, const struct sched_param *__param)
{
pid_t tid;

    tid = __get_thread_id(__thr);

    return( sched_setscheduler(tid, __policy, __param) );
}
예제 #3
0
int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock)
{
    int ret = 0;

    if (rwlock == NULL)
        return EINVAL;

    pthread_mutex_lock(&rwlock->lock);
    if (__unlikely(!read_precondition(rwlock, __get_thread_id())))
        ret = EBUSY;
    else
        rwlock->numLocks ++;
    pthread_mutex_unlock(&rwlock->lock);

    return ret;
}
예제 #4
0
_WCRTLINK int pthread_getschedparam(pthread_t __thr, int *__policy, struct sched_param *__param)
{
int ret;
pid_t tid;

    ret = 0;

    tid = __get_thread_id(__thr);

    if(__policy != NULL)
        *__policy = sched_getscheduler(tid);
    
    if(__param != NULL)
        ret = sched_getparam(tid, __param);

    return( ret );
}
예제 #5
0
int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock)
{
    int thread_id, ret = 0;

    if (rwlock == NULL)
        return EINVAL;

    pthread_mutex_lock(&rwlock->lock);
    thread_id = __get_thread_id();
    if (__unlikely(!write_precondition(rwlock, thread_id))) {
        ret = EBUSY;
    } else {
        rwlock->numLocks ++;
        rwlock->writerThreadId = thread_id;
    }
    pthread_mutex_unlock(&rwlock->lock);
    return ret;
}
예제 #6
0
int pthread_rwlock_timedrdlock(pthread_rwlock_t *rwlock, const struct timespec *abs_timeout)
{
    int thread_id, ret = 0;

    if (rwlock == NULL)
        return EINVAL;

    pthread_mutex_lock(&rwlock->lock);
    thread_id = __get_thread_id();
    if (__unlikely(!read_precondition(rwlock, thread_id))) {
        rwlock->pendingReaders += 1;
        do {
            ret = pthread_cond_timedwait(&rwlock->cond, &rwlock->lock, abs_timeout);
        } while (ret == 0 && !read_precondition(rwlock, thread_id));
        rwlock->pendingReaders -= 1;
        if (ret != 0)
            goto EXIT;
    }
    rwlock->numLocks ++;
EXIT:
    pthread_mutex_unlock(&rwlock->lock);
    return ret;
}
예제 #7
0
int pthread_rwlock_unlock(pthread_rwlock_t *rwlock)
{
    int  ret = 0;

    if (rwlock == NULL)
        return EINVAL;

    pthread_mutex_lock(&rwlock->lock);

    /* The lock must be held */
    if (rwlock->numLocks == 0) {
        ret = EPERM;
        goto EXIT;
    }

    /* If it has only readers, writerThreadId is 0 */
    if (rwlock->writerThreadId == 0) {
        if (--rwlock->numLocks == 0)
            _pthread_rwlock_pulse(rwlock);
    }
    /* Otherwise, it has only a single writer, which
     * must be ourselves.
     */
    else {
        if (rwlock->writerThreadId != __get_thread_id()) {
            ret = EPERM;
            goto EXIT;
        }
        if (--rwlock->numLocks == 0) {
            rwlock->writerThreadId = 0;
            _pthread_rwlock_pulse(rwlock);
        }
    }
EXIT:
    pthread_mutex_unlock(&rwlock->lock);
    return ret;
}