Exemplo n.º 1
0
int sfgeReadChannelBlocking( sfgeChannel channel, void *data, int size ) {
    char i;
    sfgeChan chan;
    if( channel < channelCount ) {
        chan = channels[channel];
        mtx_lock(&chan.mutex);
        while( 1 ) {
            if( chan.readIndex != chan.writeIndex ) {
                i = ((char*)chan.data)[chan.readIndex];
                /* First byte = size of message. If it's zero, then the writer has
                 * wrapped around. */
                if( i == 0 ) {
                    chan.readIndex = 0;
                    i = ((char*)chan.data)[chan.readIndex];
                    if( chan.readIndex != chan.writeIndex ) {
                        if( size == i ) {
                            memcpy(data, chan.data, size);
                            /* Plus one for the leading size byte */
                            chan.readIndex += size + 1;
                            mtx_unlock(&chan.mutex);
                            cnd_broadcast(&chan.writeCond);
                            return 1;
                        }
                        else {
                            /* If there's data in the pipe but the wrong struct
                             * passed in, then immediately return 0. */
                            mtx_unlock(&chan.mutex);
                            return 0;
                        }
                    }
                }
                else {
                    if( size == i ) {
                        memcpy(data, chan.data + chan.readIndex, size);
                        /* Plus one for the leading size byte */
                        chan.readIndex += size + 1;
                        mtx_unlock(&chan.mutex);
                        cnd_broadcast(&chan.writeCond);
                        return 1;
                    }
                    else {
                        /* If there's data in the pipe but the wrong struct
                         * passed in, then immediately return 0. */
                        mtx_unlock(&chan.mutex);
                        return 0;
                    }
                }
            }
            /* Read and write cursors met, no data to read. */
            cnd_wait(&chan.readCond, &chan.mutex);
        }
    }
    else {
        printf("WARN: Attempted to read from non-existant channel: %d!\n",
               channel);
        return 0;
    }
}
Exemplo n.º 2
0
int sfgeWriteChannelBlocking( sfgeChannel channel, void *data, int size ) {
    sfgeChan chan;
    if( channel < channelCount ) {
        chan = channels[channel];
        mtx_lock(&chan.mutex);
        while( 1 ) {
            if( chan.writeIndex + size > CHAN_BUF_SIZE ) {
                /* Mark the size byte to indicate that we've wrapped around */
                ((char*)chan.data)[chan.writeIndex] = 0;
                chan.writeIndex = 0;
            }
            if( chan.writeIndex > chan.readIndex
             || chan.writeIndex + size < chan.readIndex ) {
                ((char*)chan.data)[chan.writeIndex] = (char)size;
                /* Plus 1 for the leading size byte */
                memcpy(chan.data+1, data, size);
                chan.writeIndex += size + 1;
                mtx_unlock(&chan.mutex);
                cnd_broadcast(&chan.readCond);
                return 1;
            }
            else {
                cnd_wait(&chan.writeCond, &chan.mutex);
            }
        }
    }
    else {
        printf("WARN: Attempted to read from non-existant channel: %d!\n",
               channel);
        return 0;
    }
}
Exemplo n.º 3
0
/* Thread function: Condition notifier */
int ThreadCondition1(void * aArg)
{
  mtx_lock(&gMutex);
  -- gCount;
  cnd_broadcast(&gCond);
  mtx_unlock(&gMutex);
  return 0;
}
void Barrier2() {  
  __VERIFIER_atomic_acquire();
  count++;
  if (count == 3) {
    cnd_broadcast(COND); //pthread_cond_broadcast(&cond);
    count = 0; }
  else
    cnd_wait(COND,MTX); //pthread_cond_wait(&cond, &m);
  __VERIFIER_atomic_release(); }
Exemplo n.º 5
0
/* Thread function: Condition notifier */
static int thread_condition_notifier(void * aArg)
{
  (void)aArg;

  mtx_lock(&gMutex);
  -- gCount;
  cnd_broadcast(&gCond);
  mtx_unlock(&gMutex);
  return 0;
}
Exemplo n.º 6
0
/**
 * @brief The background event callback is called automatically
 *        by librdkafka from a background thread.
 */
static void background_event_cb (rd_kafka_t *rk, rd_kafka_event_t *rkev,
                                 void *opaque) {
        mtx_lock(&last_event_lock);
        TEST_ASSERT(!last_event, "Multiple events seen in background_event_cb "
                    "(existing %s, new %s)",
                    rd_kafka_event_name(last_event), rd_kafka_event_name(rkev));
        last_event = rkev;
        mtx_unlock(&last_event_lock);
        cnd_broadcast(&last_event_cnd);
        rd_sleep(1);
}
Exemplo n.º 7
0
/**
 * @brief Set socket delay to kick in after \p after ms
 */
static void set_delay (int after, int delay) {
        TEST_SAY("Set delay to %dms (after %dms)\n", delay, after);

        mtx_lock(&ctrl.lock);
        ctrl.cmd.ts_at = test_clock() + (after*1000);
        ctrl.cmd.delay = delay;
        ctrl.cmd.ack = 0;
        cnd_broadcast(&ctrl.cnd);

        /* Wait for ack from sockem thread */
        while (!ctrl.cmd.ack) {
                TEST_SAY("Waiting for sockem control ack\n");
                cnd_timedwait_ms(&ctrl.cnd, &ctrl.lock, 1000);
        }
        mtx_unlock(&ctrl.lock);
}
Exemplo n.º 8
0
/**
 * @brief Sockem connect, called from **internal librdkafka thread** through
 *        librdkafka's connect_cb
 */
static int connect_cb (struct test *test, sockem_t *skm, const char *id) {

        mtx_lock(&ctrl.lock);
        if (ctrl.skm) {
                /* Reject all but the first connect */
                mtx_unlock(&ctrl.lock);
                return ECONNREFUSED;
        }

        ctrl.skm = skm;

        /* signal wakeup to main thread */
        cnd_broadcast(&ctrl.cnd);
        mtx_unlock(&ctrl.lock);

        return 0;
}
Exemplo n.º 9
0
sfgeChannel sfgeRegisterChannel( const char *id ) {
    int c;
    mtx_lock(&messengerLock);
    c = channelCount;
    channels[c].id = id;
    channels[c].data = malloc(CHAN_BUF_SIZE);
    channels[c].readIndex = 0;
    channels[c].writeIndex = 0;
    cnd_init(&channels[c].readCond);
    cnd_init(&channels[c].writeCond);
    mtx_init(&channels[c].mutex, mtx_plain);
    channelCount++;
    mtx_unlock(&messengerLock);
    /* If any threads are waiting on a specific channel, signal them to check
     * again. */
    cnd_broadcast(&requireCond);
    return c;
}
Exemplo n.º 10
0
COND_RESULT Condition_Post(COND_HANDLE handle)
{
    COND_RESULT result;
    if (handle == NULL)
    {
        // Codes_SRS_CONDITION_18_001: [ Condition_Post shall return COND_INVALID_ARG if handle is NULL ]
        result = COND_INVALID_ARG;
    }
    else
    {
        if (cnd_broadcast((cnd_t*)handle) == thrd_success)
        {
            // Codes_SRS_CONDITION_18_003: [ Condition_Post shall return COND_OK if it succcessfully posts the condition ]
            result = COND_OK;
        }
        else
        {
            result = COND_ERROR;
        }
    }
    return result;
}