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
1
int node_send(URLNODE_T * urlNode, char *url)
{
    int len;
    char *p, buf[2048];

    assert(urlNode && url);

    urlNode->urlLen = strlen(url);

    len = sizeof(URLNODE_T) + urlNode->urlLen;
    assert(len < 2048);

    memcpy(buf, urlNode, sizeof(URLNODE_T));
    memcpy(buf + sizeof(URLNODE_T), url, urlNode->urlLen);

    pthread_mutex_lock(&send_mutex);
    //printf("sending to center.............\n");
    while (htcp_send(sendSock, buf, len) == -1)
    {
        sleep(1);
        close(sendSock);

        while ((sendSock =
                    htcp_connect("127.0.0.1", g_basePort + 1)) == -1)
        {
            printf("htcp_connect error.\n");
            sleep(1);
        }
        printf("send htcp_connect success.\n");
    }
    pthread_mutex_unlock(&send_mutex);

    return 0;
}
Example #3
1
int node_recv(void *buf, int len, void *html, int maxLen)
{
    assert(buf && len && len > sizeof(URLNODE_T));

    pthread_mutex_lock(&recv_mutex);

    while (htcp_recv(recvSock, buf, len) == -1
            || htcp_recv(recvSock, html, maxLen) == -1)
    {
        sleep(1);
        close(recvSock);

        while ((recvSock =
                    htcp_connect("127.0.0.1", g_basePort + 2)) == -1)
        {
            sleep(1);
            printf("htcp_connect error.\n");
        }
        printf("htcp_connect success.\n");
    }

    pthread_mutex_unlock(&recv_mutex);

    return 0;
}
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 #5
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;
}
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);
    }
}
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 #8
0
File: 1-2.c Project: Nan619/ltp
/***** Threads functions *****/
void *deadlk_issue(void *arg)
{
    int ret, tmp;

    if ((ret = pthread_mutex_lock(p_mtx))) {
        UNRESOLVED(ret, "First mutex lock in deadlk_issue");
    }
    pthread_cleanup_push(cleanup_deadlk, NULL);
    if ((ret = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &tmp))) {
        UNRESOLVED(ret, "Set cancel type in deadlk_issue");
    }
    if ((ret = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &tmp))) {
        UNRESOLVED(ret, "Set cancel state in deadlk_issue");
    }
#if VERBOSE >1
    output("Thread releases the semaphore...\n");
#endif
    if ((ret = sem_post(&semA))) {
        UNRESOLVED(errno, "Sem_post in deadlk_issue");
    }

    returned = 0;
    retval = pthread_mutex_lock(p_mtx);
    returned = 1;

    if ((ret = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &tmp))) {
        UNRESOLVED(ret, "Set cancel state in deadlk_issue");
    }
    pthread_cleanup_pop(0);
    return NULL;
}
Example #9
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 #10
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 #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;
}
Example #12
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 #13
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 #14
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");
        }
    }

}
Example #16
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 #17
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 #18
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 #19
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);
}
static void
_dbus_pthread_mutex_lock (DBusMutex *mutex)
{
    DBusMutexPThread *pmutex = DBUS_MUTEX_PTHREAD (mutex);
    pthread_t self = pthread_self ();

    /* If the count is > 0 then someone had the lock, maybe us. If it is
     * 0, then it might immediately change right after we read it,
     * but it will be changed by another thread; i.e. if we read 0,
     * we assume that this thread doesn't have the lock.
     *
     * Not 100% sure this is safe, but ... seems like it should be.
     */
    if (pmutex->count == 0)
    {
        /* We know we don't have the lock; someone may have the lock. */

        PTHREAD_CHECK ("pthread_mutex_lock", pthread_mutex_lock (&pmutex->lock));

        /* We now have the lock. Count must be 0 since it must be 0 when
         * the lock is released by another thread, and we just now got
         * the lock.
         */
        _dbus_assert (pmutex->count == 0);

        pmutex->holder = self;
        pmutex->count = 1;
    }
    else
    {
        /* We know someone had the lock, possibly us. Thus
         * pmutex->holder is not pointing to junk, though it may not be
         * the lock holder anymore if the lock holder is not us.  If the
         * lock holder is us, then we definitely have the lock.
         */

        if (pthread_equal (pmutex->holder, self))
        {
            /* We already have the lock. */
            _dbus_assert (pmutex->count > 0);
        }
        else
        {
            /* Wait for the lock */
            PTHREAD_CHECK ("pthread_mutex_lock", pthread_mutex_lock (&pmutex->lock));
            pmutex->holder = self;
            _dbus_assert (pmutex->count == 0);
        }

        pmutex->count += 1;
    }
}
Example #21
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 #22
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;
}
void notify_main_thread(sp_session *session)
{
    pthread_mutex_lock(&notify_mutex);
    notify_events = 1;
    pthread_cond_signal(&notify_cond);
    pthread_mutex_unlock(&notify_mutex);
}
void cmd_done(void)
{
    pthread_mutex_lock(&notify_mutex);
    show_prompt = 1;
    pthread_cond_signal(&prompt_cond);
    pthread_mutex_unlock(&notify_mutex);
}
Example #25
0
void w_log_to_clients(int level, const char *buf)
{
    json_t *json = NULL;
    w_ht_iter_t iter;

    if (!clients) {
        return;
    }

    pthread_mutex_lock(&w_client_lock);
    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) {
                json = make_response();
                if (json) {
                    set_prop(json, "log", json_string_nocheck(buf));
                    if (!enqueue_response(client, json, true)) {
                        json_decref(json);
                    }
                }
            }

        } while (w_ht_next(clients, &iter));
    pthread_mutex_unlock(&w_client_lock);
}
Example #26
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;
}
static int demux_xmms_fill_buffer(demuxer_t* demuxer, demux_stream_t *ds) {
    sh_audio_t *sh_audio = demuxer->audio->sh;
    xmms_priv_t *priv=demuxer->priv;
    demux_packet_t*  dp;

    if (xmms_length<=0) demuxer->seekable=0;
    else demuxer->seekable=1;

    while (xmms_audiopos<XMMS_PACKETSIZE/2) {
        if((priv->ip->get_time()<0) || !xmms_playing)
            return 0;
        usleep(1000);
    }

    pthread_mutex_lock(&xmms_mutex);
    dp = new_demux_packet(XMMS_PACKETSIZE/2);
    dp->pts = priv->spos / sh_audio->wf->nAvgBytesPerSec;
    ds->pos = priv->spos;

    memcpy(dp->buffer,xmms_audiobuffer,XMMS_PACKETSIZE/2);
    memcpy(xmms_audiobuffer,&xmms_audiobuffer[XMMS_PACKETSIZE/2],
           xmms_audiopos-XMMS_PACKETSIZE/2);
    xmms_audiopos-=XMMS_PACKETSIZE/2;
    pthread_mutex_unlock(&xmms_mutex);

    ds_add_packet(ds,dp);

    return 1;
}
Example #28
0
int ependingpool::waitforstop(int timeout)
{
    struct timeval tv_old;
    struct timeval tv;
    EPPOOLLOG(UL_LOG_DEBUG, "queue_len is %d", get_queue_len());
    stop();
    int handle = -1;
    pthread_mutex_lock(&m_mutex);
    while (!queue_empty()) {
        queue_pop(&handle);
        reset_item(handle, false);
    }
    pthread_mutex_unlock(&m_mutex);
    EPPOOLLOG(UL_LOG_DEBUG, "queue all close, queue is empty");
    gettimeofday(&tv_old, NULL);
    while (1) {
        gettimeofday(&tv, NULL);
        if ((tv.tv_sec - tv_old.tv_sec)*1000L + (tv.tv_usec - tv_old.tv_usec)/1000L > timeout) {
            break;
        }
        check_item();
        if (0 == m_read_socket_num && 0 == m_write_socket_num) {
            break;
        }
    }
    int close_sock = close_ready_socket();
    EPPOOLLOG(UL_LOG_DEBUG, "close socket num is %d, queue_len is %d", close_sock, get_queue_len());
    return 0;
}
Example #29
0
static inline void libirc_mutex_lock(port_mutex_t * mutex) {
#if defined (_WIN32)
    EnterCriticalSection(mutex);
#else
    pthread_mutex_lock(mutex);
#endif
}
Example #30
0
void Mutex::lock()
{
    PEGASUS_DEBUG_ASSERT(_magic);

    int r = pthread_mutex_lock(&_rep.mutex);

    if (r == 0)
    {
#if defined(PEGASUS_DEBUG)
        _rep.count++;
#endif
    }
    else
    {
        if (r != -1)
        {
            // Special behavior for Single UNIX Specification, Version 3
            errno = r;
        }

        throw Exception(MessageLoaderParms(
                            MUTEX_LOCK_FAILED_KEY,
                            MUTEX_LOCK_FAILED_MSG,
                            PEGASUS_SYSTEM_ERRORMSG_NLS));
    }
}