Beispiel #1
0
int dbFlushAll(void)
{
    dictIterator *iter = NULL;
    dictEntry *entry = NULL;

    lockWrite(sets);

    if (NULL == (iter = dictGetIterator(sets)))
    {
        unlockWrite(sets);
        return -1;
    }

    while (NULL != (entry = dictNext(iter)))
    {
        set *s = (set *) dictGetEntryVal(entry);
        lockWrite(s);
        if (!s->registered)
            setDestroy(s);
        unregisterSyncObject(s);
    }

    dictReleaseIterator(iter);
    dictEmpty(sets);
    unlockWrite(sets);
    return 0;
}
Beispiel #2
0
int dbSet(const sds setName, const set *s)
{
    set *prevSet = NULL;

    if (NULL == setName || 0 == strlen(setName) || NULL == s)
        return -1;

    lockWrite(sets);
    if (NULL != (prevSet = (set *) dictFetchValue(sets, setName)))
    {
        lockWrite(prevSet);
        if (!prevSet->registered)
            setDestroy(prevSet);
        unregisterSyncObject(prevSet);
    }

    if (0 != registerSyncObject(s) && 1 != syncObjectIsRegistered(s))
    {
        unlockWrite(sets);
        return -1;
    }

    dictReplace(sets, setName, (void *) s);
    unlockWrite(sets);
    return 0;
}
Beispiel #3
0
/* ChapelIO.chpl:294 */
void _write_530783(Writer _this_530787, SingleLocaleArithmeticArray__complex128_int64_t_0_1_0 _e0_args, int32_t _ln, _string _fn) {
    chpl_bool T1;
    chpl_bool T3;
    file T2 = NULL;
    chpl_bool T4;
    file T5 = NULL;
    T1 = (((object)_this_530787)->_cid == _e_file);
    if (T1) {
        T2 = ((file)(_this_530787));
        T3 = lockWrite(T2, _ln, _fn);
    } else {
        T3 = _lockWrite_18710(_this_530787);
    }
    _writeThis_531087(_e0_args, _this_530787, _ln, _fn);
    if (T3) {
        T4 = (((object)_this_530787)->_cid == _e_file);
        if (T4) {
            T5 = ((file)(_this_530787));
            unlockWrite(T5);
        } else {
            _unlockWrite_18718(_this_530787);
        }
    }
    return;
}
Beispiel #4
0
/* ChapelIO.chpl:294 */
void _write_421726(Writer _this_421730, _string _e0_args, int64_t _e1_args, int32_t _ln, _string _fn) {
    chpl_bool T1;
    chpl_bool T3;
    file T2 = NULL;
    _string T4;
    chpl_bool T5;
    file T6 = NULL;
    T1 = (((object)_this_421730)->_cid == _e_file);
    if (T1) {
        T2 = ((file)(_this_421730));
        T3 = lockWrite(T2, _ln, _fn);
    } else {
        T3 = _lockWrite_18710(_this_421730);
    }
    writeThis(_e0_args, _this_421730, _ln, _fn);
    T4 = int64_t_to_string(_e1_args);
    writeThis(T4, _this_421730, _ln, _fn);
    if (T3) {
        T5 = (((object)_this_421730)->_cid == _e_file);
        if (T5) {
            T6 = ((file)(_this_421730));
            unlockWrite(T6);
        } else {
            _unlockWrite_18718(_this_421730);
        }
    }
    return;
}
Beispiel #5
0
/* ChapelIO.chpl:294 */
void _write_530015(Writer _this_530019, _string _e0_args, _array_int64_t__complex128_1_SingleLocaleArithmeticArray__complex128_int64_t_0_1_0* const _e1_args, _string _e2_args, _string _e3_args, int32_t _ln, _string _fn) {
    _array_int64_t__complex128_1_SingleLocaleArithmeticArray__complex128_int64_t_0_1_0 T1;
    chpl_bool T2;
    chpl_bool T4;
    file T3 = NULL;
    _array_int64_t__complex128_1_SingleLocaleArithmeticArray__complex128_int64_t_0_1_0 T5;
    chpl_bool T6;
    file T7 = NULL;
    T1 = (*_e1_args);
    T2 = (((object)_this_530019)->_cid == _e_file);
    if (T2) {
        T3 = ((file)(_this_530019));
        T4 = lockWrite(T3, _ln, _fn);
    } else {
        T4 = _lockWrite_18710(_this_530019);
    }
    writeThis(_e0_args, _this_530019, _ln, _fn);
    T5 = T1;
    _writeThis_530684(&(T5), _this_530019, _ln, _fn);
    writeThis(_e2_args, _this_530019, _ln, _fn);
    writeThis(_e3_args, _this_530019, _ln, _fn);
    if (T4) {
        T6 = (((object)_this_530019)->_cid == _e_file);
        if (T6) {
            T7 = ((file)(_this_530019));
            unlockWrite(T7);
        } else {
            _unlockWrite_18718(_this_530019);
        }
    }
    return;
}
Beispiel #6
0
bool ConnectionCache::prepareToWrite(const char *str, size_t len) {
    lockWrite();
    int size = (int)len + 1;
    if(size > (int)wsize_ - 1 - wpos_){
        size_t nwsize = max((int)wsize_ * 2,(int)wpos_ + size + 1024);
        printf("resize wbuf\n");
        char * nwbuf = new(nothrow)char[nwsize];
        if(nwbuf == NULL){
            printf("resize wbuf failed\n");
            unlockWrite();
            return false;
        }else{
            if(wpos_>0){
                memcpy(nwbuf,wbuf_,wpos_);
            }
            delete wbuf_;
            wsize_ = nwsize;
            wbuf_ = nwbuf;
        }
    }
    memcpy(wbuf_ + wpos_, str, len);
    wpos_ += len;
    wbuf_[wpos_] = '\0';
    wpos_ ++;
    printf("ready to send:%s\n",wbuf_);
    unlockWrite();
    return true;
}
Beispiel #7
0
const set *dbCreate(const sds setName)
{
    set *newSet = NULL;

    if (NULL == setName || 0 == strlen(setName))
    {
        return NULL;
    }

    if (NULL == (newSet = setCreate()))
        return NULL;

    lockWrite(sets);

    if (DICT_OK != dictAdd(sets, setName, newSet))
    {
        unlockWrite(sets);
        setDestroy(newSet);
        return NULL;
    }

    if (0 != registerSyncObject(newSet) && 1 != syncObjectIsRegistered(newSet))
    {
        unlockWrite(sets);
        dictDelete(sets, setName);
        setDestroy(newSet);
        return NULL;
    }

    unlockWrite(sets);
    return newSet;
}
Beispiel #8
0
bool ConnectionCache::write(bool block)
{
    lockWrite();
    if(wpos_ == 0){
        unlockWrite();
        return true;
    }
    size_t size = send(fd_,wbuf_,wpos_,block?0:MSG_DONTWAIT);
    printf("send:%s\n",wbuf_);
    if((int)size < 0){
        unlockWrite();
        printf("write failed\n");
        return false;
    }else{
        memmove(wbuf_,wbuf_ + size,wpos_ - size);
        wpos_ -= (int)size;
        bool finished = (wpos_ == 0);
        unlockWrite();
        if(!finished){
            printf("send not finished\n");
        }
        return finished;
        
    }
}
Beispiel #9
0
/* ChapelIO.chpl:294 */
void _write_650174(Writer _this_650178, _string _e0_args, _string _e1_args, _string _e2_args, int32_t _ln, _string _fn) {
    chpl_bool T1;
    chpl_bool T3;
    file T2 = NULL;
    chpl_bool T4;
    file T5 = NULL;
    T1 = (((object)_this_650178)->_cid == _e_file);
    if (T1) {
        T2 = ((file)(_this_650178));
        T3 = lockWrite(T2, _ln, _fn);
    } else {
        T3 = _lockWrite_18710(_this_650178);
    }
    writeThis(_e0_args, _this_650178, _ln, _fn);
    writeThis(_e1_args, _this_650178, _ln, _fn);
    writeThis(_e2_args, _this_650178, _ln, _fn);
    if (T3) {
        T4 = (((object)_this_650178)->_cid == _e_file);
        if (T4) {
            T5 = ((file)(_this_650178));
            unlockWrite(T5);
        } else {
            _unlockWrite_18718(_this_650178);
        }
    }
    return;
}
Beispiel #10
0
bool ConnectionCache::prepareToWrite(string &s)
{
    lockWrite();
    int size = (int)s.size() + 1;
    if(size > (int)wsize_ - 1 - wpos_){
        size_t nwsize = max((int)wsize_ * 2,(int)wpos_ + size + 1024);
        printf("resize wbuf\n");
        char * nwbuf = new(nothrow)char[nwsize];
        if(nwbuf == NULL){
            printf("resize wbuf failed\n");
            unlockWrite();
            return false;
        }else{
            if(wpos_>0){
                memcpy(nwbuf,wbuf_,wpos_);
            }
            delete wbuf_;
            wsize_ = nwsize;
            wbuf_ = nwbuf;
        }
    }
    for(size_t i=0;i<s.size();i++){
        wbuf_[wpos_] = s[i];
        wpos_++;
    }
    wbuf_[wpos_] = '\0';
    wpos_ ++;
    printf("ready to send:%s\n",wbuf_);
    unlockWrite();
    return true;
}
Beispiel #11
0
void BufferFrame::lockFrame(bool isExclusive) {
    if(isExclusive) {
        lockWrite();
    }
    else {
        lockRead();
    }
}
 void TransactionalDocument::grantWrite ( XProcessor& xproc )
 {
   if ( isWritable() )
     {
       if ( !isLockedWrite() )
         lockWrite();
       return;
     }
   reopen ( xproc );
 }
Beispiel #13
0
    void remove(int pos)
    {
        writeLock  lockWrite(m_rwMutex);
        if(size()==0 || pos>=size())
            return;

        std::list<int>::iterator itr = m_list.begin();
        std::advance(itr , pos);
        m_list.erase(itr);

//        std::cout<<"remove! size is "<<size()<<" Thread id is "<<boost::this_thread::get_id()<<std::endl;
    }
Beispiel #14
0
valType dbSetTrunc(void)
{
    valType freed = 0, i;
    dictIterator *iter = NULL;
    dictEntry *entry = NULL;

    lockWrite(objectIndex);
    lockRead(sets);

    if (NULL == (iter = dictGetIterator(sets)))
    {
        unlockRead(sets);
        unlockWrite(objectIndex);
        return 0;
    }

    while (NULL != (entry = dictNext(iter)))
    {
        lockWrite(entry);
        freed += setTrunc((set *) dictGetEntryVal(entry));
        unlockWrite(entry);
    }

    for (i = 0; i < objectIndexLength; i++)
    {
        if (NULL != objectIndex[i] &&
            objectSet == objectIndex[i]->objectType)
        {
            freed += setTrunc(objectIndex[i]->objectPtr.setPtr);
        }
    }

    dictReleaseIterator(iter);

    unlockRead(sets);
    unlockWrite(objectIndex);

    return freed;
}
Beispiel #15
0
int dbRemove(const sds setName)
{
    int result = DICT_OK;
    set *s = NULL;
    lockWrite(sets);
    s = (set *) dictFetchValue(sets, setName);
    if (NULL != s)
    {
        unregisterSyncObject(s);
        if (!s->registered)
            setDestroy(s);
        result = dictDelete(sets, setName);
    }
    unlockWrite(sets);
    return result;
}
Beispiel #16
0
int dbUnregisterObject(valType id)
{
    lockWrite(objectIndex);
    if (id >= objectIndexLength || NULL == objectIndex[id])
    {
        unlockWrite(objectIndex);
        return -1;
    }

    dbObjectRelease((dbObject *) objectIndex[id]);
    free((void *) objectIndex[id]);
    objectIndex[id] = NULL;
    objectIndexCount++;
    unlockWrite(objectIndex);

    return 1;
}
Beispiel #17
0
void dbPrintIndex(FILE *f)
{
    valType i;

    if (NULL == f)
        return;

    fprintf(f, "Object index dump. Total: %u\r\n", objectIndexCount);

    lockWrite(objectIndex);

    for (i = 0; i < objectIndexLength; i++)
    {
        if (NULL != objectIndex[i])
        {
            const dbObject *obj = objectIndex[i];
            fprintf(f, "%10u: ", i);

            switch (obj->objectType)
            {
                case objectSet:
                    fprintf(f, "set = ");
                    break;

                case objectTuple:
                    fprintf(f, "tuple = ");
                    break;

                case objectVal:
                    fprintf(f, "val = ");
                    break;
            }

            dbObjectPrint(obj, f, 0);
            fprintf(f, "\r\n");
        }
    }

    unlockWrite(objectIndex);
}
Beispiel #18
0
int dbRename(const sds oldSetName, const sds newSetName)
{
    const set *oldSet = NULL;

    if (NULL == oldSetName || NULL == newSetName ||
        0 == strlen(oldSetName) || 0 == strlen(newSetName))
    {
        return -1;
    }

    if (NULL == (oldSet = dbGet(oldSetName)) ||
        NULL != dbGet(newSetName))
    {
        return -1;
    }

    lockWrite(sets);
    dictDelete(sets, oldSetName);
    dictAdd(sets, newSetName, (void *) oldSet);
    unlockWrite(sets);
    return 0;
}
Beispiel #19
0
/////////////////////////////////////////////////////////////////////////
/// Run
/// @description
///     This is the main communication engine.
///
/// @I/O
///     At every timestep, a message is sent to FPGA via TCP socket connection,
///     then a message is retrieved from FPGA via the same connection.
///     On the FPGA side, it's the reverse order -- receive and then send.
///     Both DGI and FPGA sides' receive function will block until a message
///     arrives, creating a synchronous, lock-step communication between DGI
///     and FPGA. In this sense, how frequently send and receive get executed
///     by CRtdsAdapter is dependent on how fast FPGA runs.
///
/// @Error_Handling
///     Throws an exception if reading from or writing to the socket fails
///
/// @pre
///     Connection with FPGA is established.
///
/// @post
///     All values in the cmdTable is written to a buffer and sent to FPGA.
///     All values in the stateTable is rewritten with value received
///     from FPGA.
///
/// @limitations
///     Synchronous commnunication
//////////////////////////////////////////////////////////////////////////
void CRtdsAdapter::Run()
{
    Logger.Trace << __PRETTY_FUNCTION__ << std::endl;
    //TIMESTEP is used by deadline_timer.async_wait at the end of Run().  
    //We keep TIMESTEP very small as we actually do not care if we wait at all.
    //We simply need to use deadline_timer.async_wait to pass control back
    //to io_service, so it can schedule Run() with other callback functions 
    //under its watch.
    const int TIMESTEP = 1; //in microseconds. NEEDS MORE TESTING TO SET CORRECTLY

    //**********************************
    //* Always send data to FPGA first *
    //**********************************
    {
        boost::shared_lock<boost::shared_mutex> lockRead(m_cmdTable.m_mutex);
        Logger.Debug << "Obtained mutex as reader" << std::endl;
        //read from cmdTable
        memcpy(m_txBuffer, m_cmdTable.m_data, m_txBufSize);
        Logger.Debug << "Released reader mutex" << std::endl;
    }// the scope is needed for mutex to auto release

    // FPGA will send values in big-endian byte order
    // If host machine is in little-endian byte order, convert to big-endian
#if __BYTE_ORDER == __LITTLE_ENDIAN

    for (int i = 0; i < m_txCount; i++)
    {
        //should be 4 bytes in float.
        endian_swap((char *) &m_txBuffer[4 * i], sizeof (float));
    }

#endif

    // send to FPGA
    try
    {
        boost::asio::write(m_socket,
                boost::asio::buffer(m_txBuffer, m_txBufSize));
    }
    catch (std::exception & e)
    {
        std::stringstream ss;
        ss << "Send to FPGA failed for the following reason: " << e.what();
        throw std::runtime_error(ss.str());
    }

    //*******************************
    //* Receive data from FPGA next *
    //*******************************
    try
    {
        boost::asio::read(m_socket,
                boost::asio::buffer(m_rxBuffer, m_rxBufSize));
    }
    catch (std::exception & e)
    {
        std::stringstream ss;
        ss << "Receive from FPGA failed for the following reason" << e.what();
        throw std::runtime_error(ss.str());
    }

    // FPGA will send values in big-endian byte order
    // If host machine is in little-endian byte order, convert to little-endian
#if __BYTE_ORDER == __LITTLE_ENDIAN

    for (int j = 0; j < m_rxCount; j++)
    {
        endian_swap((char *) &m_rxBuffer[4 * j], sizeof (float));
    }

#endif
    {
        boost::unique_lock<boost::shared_mutex> lockWrite(m_stateTable.m_mutex);
        Logger.Debug << "Client_RTDS - obtained mutex as writer" << std::endl;

        //write to stateTable
        memcpy(m_stateTable.m_data, m_rxBuffer, m_rxBufSize);

        Logger.Debug << "Client_RTDS - released writer mutex" << std::endl;
    } //scope is needed for mutex to auto release

    //Start the timer; on timeout, this function is called again
    m_GlobalTimer.expires_from_now(boost::posix_time::microseconds(TIMESTEP));
    m_GlobalTimer.async_wait(boost::bind(&CRtdsAdapter::Run, this));
}
Beispiel #20
0
/* ChapelIO.chpl:294 */
void _write_532216(Writer _this_532220, _complex128* const _e0_args, int32_t _ln, _string _fn) {
    chpl_bool T1;
    chpl_bool T3;
    file T2 = NULL;
    _string T9;
    _real64 T4;
    chpl_bool T5;
    chpl_bool T6;
    _real64 T7;
    chpl_bool T8;
    _real64 T10;
    _string T11;
    _string T12;
    _string T15;
    _real64 T13;
    _string T14;
    _real64 T16;
    _real64 T17;
    chpl_bool T18;
    _real64 T19;
    _real64 T20;
    _string T21;
    _string T22;
    _string T23;
    int32_t T24;
    chpl_bool T25;
    _string T26;
    _string T27;
    _string T28;
    _string T29;
    _string T30;
    chpl_bool T31;
    file T32 = NULL;
    T1 = (((object)_this_532220)->_cid == _e_file);
    if (T1) {
        T2 = ((file)(_this_532220));
        T3 = lockWrite(T2, _ln, _fn);
    } else {
        T3 = _lockWrite_18710(_this_532220);
    }
    T4 = ((*_e0_args).re);
    T5 = isnan(T4);
    if (T5) {
        T6 = true;
    } else {
        T7 = ((*_e0_args).im);
        T8 = isnan(T7);
        T6 = T8;
    }
    if (T6) {
        T9 = "nan";
        goto _end__cast;
    }
    T10 = ((*_e0_args).re);
    T11 = _real64_to_string(T10);
    T12 = " + ";
    T13 = ((*_e0_args).im);
    T14 = _real64_to_string(T13);
    T15 = T14;
    T16 = ((*_e0_args).im);
    T17 = ((_real64)(0));
    T18 = (T16<T17);
    if (T18) {
        T19 = ((*_e0_args).im);
        T20 = (-T19);
        T21 = _real64_to_string(T20);
        T22 = string_copy(T21, _ln, _fn);
        T15 = T22;
        T23 = string_copy(" - ", _ln, _fn);
        T12 = T23;
    } else {
        T24 = _string_compare(T14, "-0.0");
        T25 = (T24==0);
        if (T25) {
            T26 = string_copy("0.0", _ln, _fn);
            T15 = T26;
            T27 = string_copy(" - ", _ln, _fn);
            T12 = T27;
        }
    }
    T28 = string_concat(T11, T12, _ln, _fn);
    T29 = string_concat(T28, T15, _ln, _fn);
    T30 = string_concat(T29, "i", _ln, _fn);
    T9 = T30;
_end__cast:
    ;
    writeThis(T9, _this_532220, _ln, _fn);
    if (T3) {
        T31 = (((object)_this_532220)->_cid == _e_file);
        if (T31) {
            T32 = ((file)(_this_532220));
            unlockWrite(T32);
        } else {
            _unlockWrite_18718(_this_532220);
        }
    }
    return;
}
Beispiel #21
0
int dbRegisterObject(dbObject **object, valType *id)
{
    valType prevObjectIndexFreeId = 0;

    if (NULL == id || NULL == object || NULL == *object)
    {
        return -1;
    }

    lockWrite(objectIndex);

    prevObjectIndexFreeId = objectIndexFreeId;

    if (1 == dbFindObject(*object, id, 0))
    {
        dbObjectRelease(*object);
        free(*object);
        *object = (dbObject *) objectIndex[*id];
        unlockWrite(objectIndex);
        return 0;
    }

    while (objectIndexFreeId < objectIndexLength && NULL != objectIndex[objectIndexFreeId])
        objectIndexFreeId++;

    if (objectIndexFreeId < objectIndexLength)
    {
        objectIndex[objectIndexFreeId] = *object;
        *id = objectIndexFreeId;
        objectIndexFreeId++;
        objectIndexCount++;
        unlockWrite(objectIndex);

        if (objectSet == (*object)->objectType)
        {
            (*object)->objectPtr.setPtr->registered = 1;
        }

        // Debug
        printf("Registered object %u = ", *id);
        dbObjectPrint(*object, stdout, 0);
        printf("\r\n");

        (*object)->id = *id;

        return 0;
    }

    objectIndexLength += 256;
    if (NULL == (objectIndex = (const dbObject **) realloc((void *) objectIndex, objectIndexLength * sizeof(dbObject *))))
    {
        objectIndexLength = prevObjectIndexFreeId;
        unlockWrite(objectIndex);
        return -1;
    }

    memset((void *) (objectIndex + prevObjectIndexFreeId), 0, 256);

    objectIndex[objectIndexFreeId] = *object;
    *id = objectIndexFreeId;
    objectIndexFreeId++;
    objectIndexCount++;
    unlockWrite(objectIndex);

    if (objectSet == (*object)->objectType)
    {
        (*object)->objectPtr.setPtr->registered = 1;
    }

    (*object)->id = *id;

    return 0;
}
Beispiel #22
0
    void write(int value)
    {
        writeLock   lockWrite(m_rwMutex);
        m_list.push_back(value);
//        std::cout<<"write! now size is "<<size()<<" Thread id is "<<boost::this_thread::get_id()<<std::endl;
    }
Beispiel #23
0
valType dbGC(void)
{
    valType collected = 0, i;
    dictIterator *iter = NULL;
    dictEntry *entry = NULL;
    set *acc = NULL;

    lockWrite(objectIndex);
    lockRead(sets);

    // Step 1. Union all sets in db.
    if (NULL == (iter = dictGetIterator(sets)))
    {
        unlockRead(sets);
        unlockWrite(objectIndex);
        return 0;
    }

    if (NULL == (acc = setCreate()))
    {
        unlockRead(sets);
        unlockWrite(objectIndex);
        return 0;
    }

    while (NULL != (entry = dictNext(iter)))
    {
        valType currentSetId = 0;
        set *tmp = NULL, *currentSet = (set *) dictGetEntryVal(entry);
        set *flattened = setFlatten(currentSet, 0);

        if (NULL == flattened)
        {
            continue;
        }

        if (1 == dbFindSet(currentSet, &currentSetId, 0))
        {
            if (-1 == setAdd(acc, currentSetId))
            {
                setDestroy(acc);
                dictReleaseIterator(iter);
                unlockRead(sets);
                unlockWrite(objectIndex);
                return 0;
            }
        }

        if (NULL == (tmp = setUnion(acc, flattened)))
        {
            setDestroy(flattened);
            setDestroy(acc);
            dictReleaseIterator(iter);
            unlockRead(sets);
            unlockWrite(objectIndex);
            return 0;
        }

        setDestroy(flattened);
        setDestroy(acc);
        acc = tmp;
    }

    dictReleaseIterator(iter);

    // Step 2. Find objects not present in grand total union.
    for (i = 0; i < objectIndexLength; i++)
    {
        if (NULL != objectIndex[i] && !setIsMember(acc, i))
        {
            dbObjectRelease((dbObject *) objectIndex[i]);
            free((dbObject *) objectIndex[i]);
            objectIndex[i] = NULL;

            if (i < objectIndexFreeId)
            {
                objectIndexFreeId = i;
            }

            collected++;
        }
    }

    setDestroy(acc);

    unlockRead(sets);
    unlockWrite(objectIndex);

    return collected;
}
Beispiel #24
0
/*virtual*/ void WriteLockHandle::lockRead(LOCKVAL_SRC_POS_DECL)
{
    lockWrite(LOCKVAL_SRC_POS_ARGS);
}