Example #1
1
void * sender(void * arg)
{
    int l = INT_MIN;
    int r = INT_MIN;
    int a = INT_MIN;
    int u = 0;
    int v = 0;
    int todo;

    while (1) {

        todo = 0;

        if (pthread_mutex_lock(&mtx) < 0) {
            perror("pthread_mutex_lock");
            exit(1);
        }

        if (l == left && r == right) {
            u++;
        } else {
            u = 0;
            l = left;
            r = right;
        }
        if (a == angle) {
            v++;
        } else {
            v = 0;
            a = angle;
        }

        if (pthread_mutex_unlock(&mtx) < 0) {
            perror("pthread_mutex_unlock");
            exit(1);
        }

        if (u < NB_CONSIGNE) {
            fprintf(can, "asserv speed %d %d\n", r, l);
            fflush(can);
            todo = 1;
        }
        if (v < NB_CONSIGNE) {
            fprintf(can, "ax 1 angle set %d\n", a);
            fflush(can);
            usleep(100*1000);
            fprintf(can, "ax 2 angle set %d\n", a);
            fflush(can);
            todo = 1;
        }
        if (todo) {
            usleep(DELAY * 1000);
        } else {
            pthread_mutex_lock(&cnd_mtx);
            pthread_cond_wait(&cnd, &cnd_mtx);
            pthread_mutex_unlock(&cnd_mtx);
        }

    }
}
Example #2
0
bool w_should_log_to_clients(int level)
{
    w_ht_iter_t iter;
    bool result = false;

    pthread_mutex_lock(&w_client_lock);

    if (!clients) {
        pthread_mutex_unlock(&w_client_lock);
        return false;
    }

    if (w_ht_first(clients, &iter)) do {
            struct watchman_client *client = w_ht_val_ptr(iter.value);

            if (client->log_level != W_LOG_OFF && client->log_level >= level) {
                result = true;
                break;
            }

        } while (w_ht_next(clients, &iter));
    pthread_mutex_unlock(&w_client_lock);

    return result;
}
Example #3
0
static void Emulator_run(JNIEnv *env, jobject self)
{
    jEnv = env;

    pthread_mutex_init(&emuStateMutex, NULL);
    pthread_cond_init(&emuStateCond, NULL);

    while (true) {
        pthread_mutex_lock(&emuStateMutex);
        while (emuState == EMUSTATE_PAUSED)
            pthread_cond_wait(&emuStateCond, &emuStateMutex);
        if (emuState == EMUSTATE_QUIT) {
            pthread_mutex_unlock(&emuStateMutex);
            break;
        }
        if (emuState == EMUSTATE_REQUEST_RUN) {
            emuState = EMUSTATE_RUNNING;
            pthread_cond_signal(&emuStateCond);
        }
        pthread_mutex_unlock(&emuStateMutex);

        runEmulator();

        pthread_mutex_lock(&emuStateMutex);
        if (emuState == EMUSTATE_REQUEST_PAUSE) {
            emuState = EMUSTATE_PAUSED;
            pthread_cond_signal(&emuStateCond);
        }
        pthread_mutex_unlock(&emuStateMutex);
    }
}
Example #4
0
static void *emuThreadProc(void *arg)
{
    while (true) {
        pthread_mutex_lock(&emuStateMutex);
        while (emuState == EMUSTATE_PAUSED)
            pthread_cond_wait(&emuStateCond, &emuStateMutex);
        if (emuState == EMUSTATE_QUIT) {
            pthread_mutex_unlock(&emuStateMutex);
            break;
        }
        if (emuState == EMUSTATE_REQUEST_RUN) {
            emuState = EMUSTATE_RUNNING;
            pthread_cond_signal(&emuStateCond);
        }
        pthread_mutex_unlock(&emuStateMutex);

        runEmulator();

        pthread_mutex_lock(&emuStateMutex);
        if (emuState == EMUSTATE_REQUEST_PAUSE) {
            emuState = EMUSTATE_PAUSED;
            pthread_cond_signal(&emuStateCond);
        }
        pthread_mutex_unlock(&emuStateMutex);
    }
    return NULL;
}
Example #5
0
void* resolveDNS(void* output) {
    FILE* outputFile = fopen((char*) output, "a");
    if(!outputFile) {
        fprintf(stderr, "Error Opening Output File.\n");
        return NULL;
    }
    char* hostname;
    char ipString[INET6_ADDRSTRLEN];

    while(1) {
        pthread_mutex_lock(&buffer_mutex);
        hostname = queue_pop(&buffer); //mutual exclusion on queue
        pthread_mutex_unlock(&buffer_mutex);

        if(hostname != NULL) {
            if(dnslookup(hostname, ipString, sizeof(ipString)) == UTIL_FAILURE) {
                printf("DNSLookup Error: %s\n", hostname);
                strncpy(ipString, "", sizeof(ipString));
            }
            pthread_mutex_lock(&output_mutex);
            fprintf(outputFile, "%s,%s\n", hostname, ipString); //print hostname and ip to output file
            pthread_mutex_unlock(&output_mutex);

            free(hostname); //deallocate memory
        }
        else if(numOfThreads == 0) {
            break;
        }
    }

    free(hostname); //deallocate memory
    fclose(outputFile); //close output file
    return NULL;
}
void mujer_quiere_entrar () {
    int imprimir;
    int esperando = 1;
    pthread_mutex_lock(&mutexMujeresEnEspera);
    mujeresEnEspera++;
    printf("Llega mujer (%d en espera)\n", mujeresEnEspera);
    pthread_mutex_unlock(&mutexMujeresEnEspera);

    while (esperando) {
        pthread_mutex_lock(&mutexBanio);
        imprimir = tipodDeBanio == 2 && personasEnBanio == 0;
        if (tipodDeBanio == 1 || tipodDeBanio == 0 || (tipodDeBanio == 2 && personasEnBanio == 0)) {
            tipodDeBanio = 1;
            personasEnBanio++;
            mujeresEnEspera--;
            printf("Entra una mujer (%d en espera)\n",mujeresEnEspera);
            esperando = 0;
        } else {
            printf("Mujer esperando el baño\n");
            pthread_cond_wait(&esperandoBanio, &mutexBanio);
        }
        pthread_mutex_unlock(&mutexBanio);
        if (imprimir)
        {
            printf("Baño ocupado por mujeres\n");
        }
    }

}
void hombre_quiere_entrar () {
    int imprimir;
    int esperando = 1;

    pthread_mutex_lock(&mutexHombresEnEspera);
    hombresEnEspera++;
    printf("Llega hombre (%d en espera)\n", hombresEnEspera);
    pthread_mutex_unlock(&mutexHombresEnEspera);

    while(esperando) {
        pthread_mutex_lock(&mutexBanio);
        imprimir = tipodDeBanio == 1 && personasEnBanio == 0;
        if (tipodDeBanio == 2 || tipodDeBanio == 0 || (tipodDeBanio == 1 && personasEnBanio == 0)) {
            tipodDeBanio = 2;
            personasEnBanio++;
            hombresEnEspera--;
            printf("Entra un hombre (%d en espera)\n",hombresEnEspera);
            esperando = 0;
        } else {
            printf("Hombre esperando el baño\n");
            pthread_cond_wait(&esperandoBanio, &mutexBanio);
        }
        pthread_mutex_unlock(&mutexBanio);
        if (imprimir)
        {
            printf("Baño ocupado por hombres\n");
        }
    }
}
Example #8
0
//async thread start here
void * async_thread_start(void *d)
{
    stone_async_thread *a = d;
    opool_iter *iter, *head;
    stone_async_t *a_t;
    void *temp;
    pthread_cleanup_push(async_thread_destroy, a);
    while(1) {
        pthread_mutex_lock(&a->lock);
        iter = opool_requestAll(a->que);
        if(iter == NULL) {
            pthread_mutex_unlock(&a->lock);
            sleep(60);
            continue;
        }
        //exchange use and back
        temp = a->use;
        a->use = a->back;
        a->back  = temp;
        pthread_mutex_unlock(&a->lock);
        head = iter;//store head to release
        //now deal with que
        while(iter) {
            a_t = iter->data;
            a_t->fn(a_t->fd, a_t->data);
            iter = iter->next;
        }
        //after all, reset pool and return all to que
        ngx_reset_pool(a->back);
        opool_releaseAll(a->que, head);
    }
    pthread_cleanup_pop(0);
}
Example #9
0
/*
 * Write the message into log file according to different level.
 */
void
debug_print(FILE *out, int level, const char *fmt, ...)
{
    va_list ap;

    if (level <= s_debuglevel) {
        if (out == NULL) {
            if (s_logfile != NULL) {
                (void) pthread_mutex_lock(&s_debug_ctl.mutex);
                (void) fprintf(s_logfile,
                               "%"PRIu64": ", current_ms() / 1000);
                va_start(ap, fmt);
                (void) vfprintf(s_logfile, fmt, ap);
                va_end(ap);
                (void) fflush(s_logfile);
                (void) pthread_mutex_unlock(
                    &s_debug_ctl.mutex);
            }
        } else {
            (void) pthread_mutex_lock(&s_debug_ctl.mutex);
            (void) fprintf(out,
                           "%"PRIu64": ", current_ms() / 1000);
            va_start(ap, fmt);
            (void) vfprintf(out, fmt, ap);
            va_end(ap);
            (void) fflush(out);
            (void) pthread_mutex_unlock(&s_debug_ctl.mutex);
        }
    }
}
Example #10
0
static Page *bufManLoadUninitPage(stasis_buffer_manager_t *ignored, int xid, pageid_t pageid) {

    Page * ret = pthread_getspecific(lastPage);

    if(ret && ret->id == pageid) {
        pthread_mutex_lock(&loadPagePtr_mutex);
        readlock(ret->loadlatch, 1);
        if(ret->id != pageid) {
            unlock(ret->loadlatch);
            ret = 0;
        } else {
            cacheHitOnPage(ret);
            pthread_mutex_unlock(&loadPagePtr_mutex);
        }
    } else {
        ret = 0;
    }
    if(!ret) {
        ret = bufManGetPage(xid, pageid, RO, 1, UNKNOWN_TYPE_PAGE);
        pthread_setspecific(lastPage, ret);
    }

#ifdef PIN_COUNT
    pthread_mutex_lock(&pinCount_mutex);
    pinCount ++;
    pthread_mutex_unlock(&pinCount_mutex);
#endif

    return ret;
}
Example #11
0
static void * work (void * voidptr) {
    Worker * worker = voidptr;
    int wid = worker - workers;
    LGL * lgl = worker->lgl;
    assert (0 <= wid && wid < nworkers);
    msg (wid, 1, "running");
    assert (workers <= worker && worker < workers + nworkers);
    worker->res = lglsat (lgl);
    msg (wid, 1, "result %d", worker->res);
    if (pthread_mutex_lock (&donemutex))
        warn ("failed to lock 'done' mutex in worker");
    done = 1;
    if (pthread_mutex_unlock (&donemutex))
        warn ("failed to unlock 'done' mutex in worker");
    msg (wid, 2, "%d decisions, %d conflicts, %.0f props, %.1f MB",
         lglgetdecs (lgl), lglgetconfs (lgl), lglgetprops (lgl), lglmb (lgl));
    if (verbose >= 2) {
        if (pthread_mutex_lock (&fixedmutex))
            warn ("failed to lock 'fixed' in work");
        msg (wid, 2, "consumed %d units %.0f%%, produced %d units %.0f%%",
             worker->consumed, percent (worker->consumed, nfixed),
             worker->produced, percent (worker->produced, nfixed));
        if (pthread_mutex_unlock (&fixedmutex))
            warn ("failed to unlock 'fixed' in work");
    }
    return worker->res ? worker : 0;
}
void *llegada_reno(void *param) {

    while (1) {
#ifndef NOSLEEP
        sleep(1 * random() % 5);
#endif
        pthread_mutex_lock(&santa_duerme);
        pthread_mutex_lock(&sala_espera_m);
        if (sala_espera < MAX_RENOS) {
            sala_espera++;
            printf("Llega reno... Total: %d\n", sala_espera);
            if (sala_espera == MAX_RENOS) {
                //Esta la sala llena con todos los renos?
                pthread_mutex_unlock(&sala_espera_m);
                printf("todos renos, repartimos!\n");
                pthread_cond_signal(&santa_libre);
                pthread_cond_wait(&sala_espera_cond, &santa_duerme);
            } else {
                //no? Desbloqueamos variables
                pthread_mutex_unlock(&sala_espera_m);
                pthread_cond_wait(&sala_espera_cond, &santa_duerme);
            }
        } else {
            pthread_mutex_unlock(&sala_espera_m);
        }
        pthread_mutex_unlock(&santa_duerme);
    }
}
Example #13
0
/*!
  Signal the event.
  The behaviour of this message is influenced by the broadcast flag, if this
  is a broadcast event then the event will be signaled to all the waiting
  threads otherwise only one thread will be signaled.
  \param id The calling thread id.
 */
int Event::signal (u_long id)
{
    if (!initialized)
        return -1;

#ifdef HAVE_PTHREAD
    if (broadcast)
    {
        pthread_mutex_lock (&mutex);
        pthread_cond_broadcast (&event);
        pthread_mutex_unlock (&mutex);
    }
    else
    {
        pthread_mutex_lock (&mutex);
        pthread_cond_signal (&event);
        pthread_mutex_unlock (&mutex);
    }
#else
    SetEvent (event);

    /* Reset the event for later reusability.  */
    ResetEvent (event);

#endif
    return 0;
}
Example #14
0
void *timeshift_writer ( void *aux )
{
    int run = 1;
    timeshift_t *ts = aux;
    streaming_queue_t *sq = &ts->wr_queue;
    streaming_message_t *sm;

    pthread_mutex_lock(&sq->sq_mutex);

    while (run) {

        /* Get message */
        sm = TAILQ_FIRST(&sq->sq_queue);
        if (sm == NULL) {
            pthread_cond_wait(&sq->sq_cond, &sq->sq_mutex);
            continue;
        }
        TAILQ_REMOVE(&sq->sq_queue, sm, sm_link);
        pthread_mutex_unlock(&sq->sq_mutex);

        _process_msg(ts, sm, &run);

        pthread_mutex_lock(&sq->sq_mutex);
    }

    pthread_mutex_unlock(&sq->sq_mutex);
    return NULL;
}
Example #15
0
void obs_sceneitem_remove(obs_sceneitem_t item)
{
    obs_scene_t scene;

    if (!item)
        return;

    scene = item->parent;

    if (scene)
        pthread_mutex_lock(&scene->mutex);

    if (item->removed) {
        if (scene)
            pthread_mutex_unlock(&scene->mutex);
        return;
    }

    item->removed = true;

    signal_item_remove(item);
    detach_sceneitem(item);

    if (scene)
        pthread_mutex_unlock(&scene->mutex);

    obs_sceneitem_release(item);
}
void santa() {

    while (1) {

        pthread_mutex_lock(&santa_duerme);
        printf("santa duerme\n");

        while ((sala_espera < MAX_RENOS) && (elfos_dudosos < MAX_ELFOS)) {
            pthread_cond_wait(&santa_libre, &santa_duerme);
        }
        printf("santa se despierta!\n");
        pthread_mutex_lock(&elfos_dudosos_m);
        pthread_mutex_lock(&sala_espera_m);
        if (sala_espera >= MAX_RENOS) {
            pthread_mutex_unlock(&elfos_dudosos_m);
            pthread_mutex_unlock(&sala_espera_m);
            repartir_regalos();
        } else if (elfos_dudosos >= MAX_ELFOS) {
            pthread_mutex_unlock(&elfos_dudosos_m);
            pthread_mutex_unlock(&sala_espera_m);
            resolver_dudas();
        }
        pthread_mutex_unlock(&santa_duerme);
    }
}
Example #17
0
//************************************************************************//
//* free memory allocated from the av heap.
//************************************************************************//
void av_heap_free(void* vaddr)
{
    int            block_idx;
    unsigned char* paddr;
    int            offset;

    if(heap_ctx == NULL)
    {
        loge("av heap not initialized yet.");
        return;
    }

    pthread_mutex_lock(&heap_ctx->mutex);

    block_idx = av_heap_find_block_by_vaddr(heap_ctx, vaddr);
    if(block_idx < 0)
    {
        loge("can not find a block of specific virtual address %x.", vaddr);
        pthread_mutex_unlock(&heap_ctx->mutex);
        return;
    }

    paddr  = (unsigned char*)heap_ctx->block_info[block_idx].paddr;
    offset = (int)(paddr - (heap_ctx->env_info.phymem_start & 0x3fffffff));

    munmap(heap_ctx->block_info[block_idx].vaddr, heap_ctx->block_info[block_idx].size);

    allocator_free(heap_ctx->allocator, offset);

    memset(&heap_ctx->block_info[block_idx], 0, sizeof(block_info_t));

    pthread_mutex_unlock(&heap_ctx->mutex);

    return;
}
Example #18
0
static void *pthread_wrapper(void *arg)
{
    struct cbs_struct *cs;
    timer_t timer;
    struct sigevent se;
    struct itimerspec its, itsoff;
    double mi_frac;
    cs = arg;

    memset(&se, 0, sizeof(se));
    se.sigev_notify = SIGEV_THREAD_ID;
    se.sigev_signo = SIGXCPU;
    se.sigev_notify_thread_id = syscall(__NR_gettid);
    if (timer_create(CLOCK_MONOTONIC, &se, &timer) != 0)
        abort();

    mi_frac = cs->cpu / BOGO_MIPS;
    memset(&its, 0, sizeof(its));
    if (cs->type == CBS_RT)
    {
        its.it_value.tv_sec = mi_frac;
        its.it_value.tv_nsec = (long)(mi_frac * NANO) % NANO;

        if (its.it_value.tv_sec > cs->period.tv_sec)
            its.it_value.tv_sec = cs->period.tv_sec;
        if (its.it_value.tv_nsec > cs->period.tv_usec * 1000)
            its.it_value.tv_nsec = cs->period.tv_usec * 1000;
    }

    memset(&itsoff, 0, sizeof(itsoff));

    cs->ret = 0;
    while (cs->ret == 0)
    {
        pthread_mutex_lock(&cs->run_lock);
        while (cs->run != CBS_STATE_READY)
            pthread_cond_wait(&cs->run_signal, &cs->run_lock);

        cs->run = CBS_STATE_RUNNING;
        pthread_mutex_unlock(&cs->run_lock);

        if (timer_settime(timer, 0, &its, NULL) != 0)
            abort();

        cs->ret = cs->entry(cs->arg);

        if (timer_settime(timer, 0, &itsoff, NULL) != 0)
            abort();

        pthread_mutex_lock(&cs->run_lock);
        cs->run = CBS_STATE_SLEEP;
        pthread_mutex_unlock(&cs->run_lock);
    }

    pthread_mutex_lock(&cs->run_lock);
    cs->run = CBS_STATE_DONE;
    pthread_mutex_unlock(&cs->run_lock);

    return NULL;
}
Example #19
0
static void *pthread_sleeper(void *arg)
{
    struct cbs_struct *cs;
    cs = arg;

    pthread_mutex_lock(&cs->run_lock);
    while (cs->run != CBS_STATE_DONE)
    {
        pthread_mutex_unlock(&cs->run_lock);

        if (cs->period.tv_sec != 0)
            sleep(cs->period.tv_sec);
        usleep(cs->period.tv_usec);

        pthread_mutex_lock(&cs->run_lock);
        if (cs->run == CBS_STATE_SLEEP)
            cs->run = CBS_STATE_READY;

        pthread_cond_signal(&cs->run_signal);
        pthread_mutex_unlock(&cs->run_lock);
    }

    cs->run = CBS_STATE_DONE;
    pthread_mutex_unlock(&cs->run_lock);

    return NULL;
}
Example #20
0
/* close communication with mainframe under logical number 'id'
   and stop thread */
int
sy1527Stop(unsigned id)
{
    char name[MAX_CAEN_NAME];
    int i, j, ret;

    /* lock global mutex to prevent other mainframes to be stoped
       until we are done with this one */
    pthread_mutex_lock(&global_mutex);

    CHECK_ID(id);
    CHECK_OPEN(id);
    strcpy(name, Measure[id].name);

    /* stop thread */
    force_exit[id] = 1;

    ret = CAENHVDeinitSystem(name);

    if(ret == CAENHV_OK)
    {
        printf("\nConnection >%s< closed (num. %d)\n\n", name,ret);
        Measure[id].id = -1;
    }
    else
    {
        printf("\nERROR(sy1527): %s (num. %d)\n\n", CAENHVGetError(name), ret);
        pthread_mutex_unlock(&global_mutex);
        return(CAENHV_SYSERR);
    }

    /* unregister mainframe */
    j = -1;
    for(i=0; i<nmainframes; i++)
    {
        if(mainframes[i] == id)
        {
            j = i;
            break;
        }
    }
    if(j==-1)
    {
        printf("ERROR: mainframe %d was not registered\n",id);
        pthread_mutex_unlock(&global_mutex);
        return(CAENHV_SYSERR);
    }
    for(i=j; i<nmainframes; i++) mainframes[i] = mainframes[i+1];
    nmainframes --;

    pthread_mutex_unlock(&global_mutex);

    return(CAENHV_OK);
}
Example #21
0
int circular_buffer_write(circular_buffer_t *buf, uint8_t *data, uint32_t len, bool_t blocking)
{
    if (buf->closed)
        return 0;

    pthread_mutex_lock(&buf->lock);
    int spaceleft = circular_buffer_available(buf);
    int seq_end_update_offset = 0; //ABE: used for when seq_end must be updated while blocking to avoid screwing things up when we update it before returning

    if (spaceleft >= len || blocking == BUFFER_BLOCKING) {
        len = len <= buf->maxsize ? len : buf->maxsize;

        uint32_t space_needed = len;
        uint32_t j = 0;
        uint32_t k = buf->end;
        if (k == buf->start && spaceleft)
            buf->empty = 1;
        while (space_needed) {
            if (k == buf->start && !buf->empty) {
                buf->full = 1;
                buf->end = buf->start;
                seq_end_update_offset += circular_buffer_available(buf);
                buf->seq_end += circular_buffer_available(buf);
                //uint32_t available;
                //while (!(available = circular_buffer_available(buf)))
                while (buf->full) {
                    //if (buf->closed) {
                    //pthread_mutex_unlock(&buf->lock);
                    //return j;
                    pthread_cond_wait (&buf->notFull, &buf->lock);
                }
                if (k == buf->start)
                    buf->empty = 1;
            }
            while ((k != buf->start || buf->empty) && k < buf->maxsize && space_needed) {
                if (buf->closed) {
                    pthread_mutex_unlock(&buf->lock);
                    return j;
                }
                *(buf->data + k++) = *(data + j++);
                pthread_cond_signal(&buf->notEmpty);
                buf->empty = 0;
                space_needed--;
            }
            if (k == buf->maxsize)
                k = 0;
        }
        buf->end = k;
        buf->seq_end += len - seq_end_update_offset;
        pthread_mutex_unlock(&buf->lock);
        return len;
    }
    return CHITCP_EWOULDBLOCK;
}
Example #22
0
static void * locker(void * arg)
{
    assert(pthread_mutex_lock(&mutex) == 0);
    lockCount++;
    assert(pthread_mutex_trylock(&mutex) == EBUSY);
    lockCount++;
    assert(pthread_mutex_unlock(&mutex) == 0);
    assert(pthread_mutex_unlock(&mutex) == EPERM);

    return 0;
}
Example #23
0
void fuse_req_interrupt_func(fuse_req_t req, fuse_interrupt_func_t func,
                             void *data)
{
    pthread_mutex_lock(&req->lock);
    pthread_mutex_lock(&req->f->lock);
    req->u.ni.func = func;
    req->u.ni.data = data;
    pthread_mutex_unlock(&req->f->lock);
    if (req->interrupted && func)
        func(req, data);
    pthread_mutex_unlock(&req->lock);
}
Example #24
0
OMX_ERRORTYPE mm_jpeg_ftbdone(OMX_HANDLETYPE hComponent,
                              OMX_PTR pAppData,
                              OMX_BUFFERHEADERTYPE* pBuffer)
{
    int rc = 0;
    void* node = NULL;
    mm_jpeg_job_entry* job_entry = NULL;
    mm_jpeg_obj * my_obj = (mm_jpeg_obj*)pAppData;

    if (NULL != my_obj) {
        CDBG_ERROR("%s: pAppData is NULL, return here", __func__);
        return rc;
    }

    /* signal JPEG_DONE event */
    pthread_mutex_lock(&my_obj->omx_evt_lock);
    my_obj->omx_evt_rcvd.evt = MM_JPEG_EVENT_MASK_JPEG_DONE;
    pthread_cond_signal(&my_obj->omx_evt_cond);
    pthread_mutex_unlock(&my_obj->omx_evt_lock);

    /* If OMX can support multi encoding, it should provide a way to pass jobID.
     * then we can find job by jobID from ongoing queue.
     * For now, since OMX only support one job per time, we simply dequeue it. */
    pthread_mutex_lock(&my_obj->job_lock);
    node = mm_jpeg_queue_deq(&my_obj->ongoing_job_q);
    if (NULL != node) {
        job_entry = &(((mm_jpeg_job_q_node_t *)node)->entry);;
        /* find job that is OMX ongoing */
        job_entry->jpeg_size = pBuffer->nFilledLen;
        job_entry->job_status = JPEG_JOB_STATUS_DONE;
        CDBG("%s:filled len = %u, status = %d",
             __func__, job_entry->jpeg_size, job_entry->job_status);

        if (NULL != job_entry->job.encode_job.jpeg_cb) {
            /* has callback, create a thread to send CB */
            pthread_create(&job_entry->cb_pid,
                           NULL,
                           mm_jpeg_notify_thread,
                           node);

        } else {
            CDBG_ERROR("%s: no cb provided, return here", __func__);
            free(node);
        }
    }
    pthread_mutex_unlock(&my_obj->job_lock);

    /* Wake up jobMgr thread to work on next job if there is any */
    sem_post(&my_obj->job_mgr.job_sem);

    return rc;
}
Example #25
0
int circular_buffer_read(circular_buffer_t *buf, uint8_t *dst, uint32_t len, bool_t blocking)
{
    if (buf->closed)
        return 0;

    //int len_buf = circular_buffer_count(buf);
    pthread_mutex_lock(&buf->lock);
    if (blocking == BUFFER_BLOCKING) {
        //while(!len_buf) {
        while(buf->empty) {
            //if (buf->closed)
            //return 0;
            //len_buf = circular_buffer_count(buf);
            pthread_cond_wait (&buf->notEmpty, &buf->lock);
        }
        if (buf->closed)
            return 0;
    } else if (buf->empty) {
        pthread_mutex_unlock(&buf->lock);
        return CHITCP_EWOULDBLOCK;
    }

    //pthread_mutex_lock(&buf->lock);
    int len_buf = circular_buffer_count(buf);
    int toread = len < len_buf ? len : len_buf;
    int read_counter = toread;

    if(dst) {
        uint32_t j = 0;
        uint32_t k = buf->start;
        while(read_counter--) {
            if (buf->closed) {
                pthread_mutex_unlock(&buf->lock);
                return 0;
            }
            *(dst + j++) = *(buf->data + k++);
            if (k == buf->maxsize)
                k = 0;
            pthread_cond_signal(&buf->notFull);
            buf->full = 0;
        }
        buf->start = k;
    }
    buf->seq_start += toread;

    if (buf->start == buf->end  && toread)
        buf->empty = 1;

    pthread_mutex_unlock(&buf->lock);
    return toread;
}
Example #26
0
//*************************************************************************//
//* allocate memory from the av heap.
//*************************************************************************//
void* av_heap_alloc(int size)
{
    int 		 offset;
    int 		 block_idx;
    unsigned int paddr;

    if(size <= 0)
    {
        loge("request memory of invalid size %d.", size);
        return NULL;
    }

    if(heap_ctx == NULL)
    {
        loge("av heap not initialized yet, return fail.");
        return NULL;
    }

    pthread_mutex_lock(&heap_ctx->mutex);

    offset = allocator_alloc(heap_ctx->allocator, size);
    if(offset < 0)
    {
        loge("av heap out of memory.");
        pthread_mutex_unlock(&heap_ctx->mutex);

        return NULL;
    }

    paddr = offset + heap_ctx->env_info.phymem_start;

    block_idx = av_heap_get_a_free_block(heap_ctx);
    if(block_idx < 0)
    {
        loge("av heap can not get a free block, return fail.");

        allocator_free(heap_ctx->allocator, offset);
        pthread_mutex_unlock(&heap_ctx->mutex);

        return NULL;
    }

    heap_ctx->block_info[block_idx].vaddr   = (void*)mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, heap_ctx->fd, paddr);
    heap_ctx->block_info[block_idx].paddr   = (void*)(paddr & 0x3fffffff);
    heap_ctx->block_info[block_idx].size    = size;
    heap_ctx->block_info[block_idx].is_used = 1;

    pthread_mutex_unlock(&heap_ctx->mutex);

    return heap_ctx->block_info[block_idx].vaddr;
}
Example #27
0
static struct watchman_client *make_new_client(w_stm_t stm) {
    struct watchman_client *client;
    pthread_attr_t attr;
    pthread_t thr;

    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);

    client = calloc(1, sizeof(*client));
    if (!client) {
        pthread_attr_destroy(&attr);
        return NULL;
    }
    client->stm = stm;
    w_log(W_LOG_DBG, "accepted client:stm=%p\n", client->stm);

    if (!w_json_buffer_init(&client->reader)) {
        // FIXME: error handling
    }
    if (!w_json_buffer_init(&client->writer)) {
        // FIXME: error handling
    }
    client->ping = w_event_make();
    if (!client->ping) {
        // FIXME: error handling
    }
    client->subscriptions = w_ht_new(2, &subscription_hash_funcs);

    pthread_mutex_lock(&w_client_lock);
    w_ht_set(clients, w_ht_ptr_val(client), w_ht_ptr_val(client));
    pthread_mutex_unlock(&w_client_lock);

    // Start a thread for the client.
    // We used to use libevent for this, but we have
    // a low volume of concurrent clients and the json
    // parse/encode APIs are not easily used in a non-blocking
    // server architecture.
    if (pthread_create(&thr, &attr, client_thread, client)) {
        // It didn't work out, sorry!
        pthread_mutex_lock(&w_client_lock);
        w_ht_del(clients, w_ht_ptr_val(client));
        pthread_mutex_unlock(&w_client_lock);
        client_delete(client);
    }

    pthread_attr_destroy(&attr);

    return client;
}
Example #28
0
int
libzfs_core_init(void)
{
    (void) pthread_mutex_lock(&g_lock);
    if (g_refcount == 0) {
        g_fd = open("/dev/zfs", O_RDWR);
        if (g_fd < 0) {
            (void) pthread_mutex_unlock(&g_lock);
            return (errno);
        }
    }
    g_refcount++;
    (void) pthread_mutex_unlock(&g_lock);
    return (0);
}
Example #29
0
int PBS_resc(

    int          c,
    int          reqtype,
    char       **rescl,
    int          ct,
    resource_t   rh)

{
    int rc;
    int sock;
    struct tcp_chan *chan = NULL;

    if ((c < 0) ||
            (c >= PBS_NET_MAX_CONNECTIONS))
    {
        return(PBSE_IVALREQ);
    }

    sock = connection[c].ch_socket;

    /* setup DIS support routines for following DIS calls */

    if ((chan = DIS_tcp_setup(sock)) == NULL)
    {
        pthread_mutex_unlock(connection[c].ch_mutex);
        rc = PBSE_PROTOCOL;
        return rc;
    }
    else if ((rc = encode_DIS_ReqHdr(chan, reqtype, pbs_current_user)) ||
             (rc = encode_DIS_Resc(chan, rescl, ct, rh)) ||
             (rc = encode_DIS_ReqExtend(chan, (char *)0)))
    {
        connection[c].ch_errtxt = strdup(dis_emsg[rc]);

        pthread_mutex_unlock(connection[c].ch_mutex);
        DIS_tcp_cleanup(chan);
        return (PBSE_PROTOCOL);
    }

    if (DIS_tcp_wflush(chan))
    {
        rc = PBSE_PROTOCOL;
    }

    DIS_tcp_cleanup(chan);
    return rc;
} /* END PBS_resc() */
Example #30
0
static void zalarm_signal_action(int sig, siginfo_t * si, void *uc)
{
    ZALARM *ala;
    ZRBTREE_NODE *rn;
    struct itimerspec its;
    long delay;
    ZALARM_CB_FN callback;
    void *context;

    while (1) {
        delay = 0;
        rn = 0;
        if (zalarm_used_pthread) {
            pthread_mutex_lock(&zalarm_timer_locker);
        }
        rn = zrbtree_first(&zalarm_timer_tree);
        if (!rn) {
            break;
        }
        ala = ZCONTAINER_OF(rn, ZALARM, rbnode_time);
        delay = zmtime_left(ala->timeout);
        if (delay > 0) {
            break;
        }
        callback = ala->callback;
        context = ala->context;
        zrbtree_detach(&zalarm_timer_tree, &(ala->rbnode_time));
        ala->in_time = 0;
        if (zalarm_used_pthread) {
            pthread_mutex_unlock(&zalarm_timer_locker);
        }
        if (callback) {
            callback(ala, context);
        }
    }
    if (zalarm_used_pthread) {
        pthread_mutex_unlock(&zalarm_timer_locker);
    }

    if (rn) {
        ala = ZCONTAINER_OF(rn, ZALARM, rbnode_time);
        its.it_value.tv_sec = (long)(ala->timeout / 1000);
        its.it_value.tv_nsec = (long)((ala->timeout % 1000) * 1000);
        its.it_interval.tv_sec = 0;
        its.it_interval.tv_nsec = 0;
        timer_settime(&zalarm_timer_inner, TIMER_ABSTIME, &its, 0);
    }
}