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; }
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; }
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; }
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; }
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; }
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; } }
/* 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; }
/* 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; }
/* 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; }
/* 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; }
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; }
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; }
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; }
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); }
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; }
/*virtual*/ void WriteLockHandle::unlockRead() { unlockWrite(); }
/* 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; }
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; }
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, ¤tSetId, 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; }