IonNode *addNode(IonVdb *ionvdb, uvast nodeNbr) { PsmPartition ionwm = getIonwm(); PsmAddress addr; PsmAddress elt; IonNode *node; addr = psm_zalloc(ionwm, sizeof(IonNode)); if (addr == 0) { putErrmsg("Can't add node.", NULL); return NULL; } node = (IonNode *) psp(ionwm, addr); CHKNULL(node); memset((char *) node, 0, sizeof(IonNode)); node->nodeNbr = nodeNbr; elt = sm_rbt_insert(ionwm, ionvdb->nodes, addr, rfx_order_nodes, node); if (elt == 0) { psm_free(ionwm, addr); putErrmsg("Can't add node.", NULL); return NULL; } node->embargoes = sm_list_create(ionwm); return node; }
static IonVdb *_ionvdb(char **name) { static IonVdb *vdb = NULL; PsmAddress vdbAddress; PsmAddress elt; Sdr sdr; PsmPartition ionwm; if (name) { if (*name == NULL) /* Terminating. */ { vdb = NULL; return vdb; } /* Attaching to volatile database. */ ionwm = _ionwm(NULL); if (psm_locate(ionwm, *name, &vdbAddress, &elt) < 0) { putErrmsg("Failed searching for vdb.", NULL); return vdb; } if (elt) { vdb = (IonVdb *) psp(ionwm, vdbAddress); return vdb; } /* ION volatile database doesn't exist yet. */ sdr = _ionsdr(NULL); sdr_begin_xn(sdr); /* Just to lock memory. */ vdbAddress = psm_zalloc(ionwm, sizeof(IonVdb)); if (vdbAddress == 0) { sdr_exit_xn(sdr); putErrmsg("No space for volatile database.", NULL); return NULL; } vdb = (IonVdb *) psp(ionwm, vdbAddress); memset((char *) vdb, 0, sizeof(IonVdb)); if ((vdb->nodes = sm_list_create(ionwm)) == 0 || (vdb->neighbors = sm_list_create(ionwm)) == 0 || (vdb->probes = sm_list_create(ionwm)) == 0 || psm_catlg(ionwm, *name, vdbAddress) < 0) { sdr_exit_xn(sdr); putErrmsg("Can't initialize volatile database.", NULL); return NULL; } vdb->deltaFromUTC = (_ionConstants())->deltaFromUTC; sdr_exit_xn(sdr); /* Unlock memory. */ } return vdb; }
PsmPartition sptrace_start(int smkey, int smsize, char *sm, PsmPartition trace, char *name) { int nameLen; int smid; PsmMgtOutcome outcome; TraceHeader *trh; PsmAddress traceHeaderAddress; CHKNULL(trace); CHKNULL(smsize > 0); CHKNULL(name); if ((nameLen = strlen(name)) < 1 || nameLen > 31) { sptracePrint("start: name must be 1-31 characters."); return NULL; } /* Attach to shared memory used for trace operations. */ if (sm_ShmAttach(smkey, smsize, &sm, &smid) < 0) { sptracePrint("start: can't attach shared memory for trace."); return NULL; } /* Manage the shared memory region. "Trace" argument * is normally NULL. */ if (psm_manage(sm, smsize, name, &trace, &outcome) < 0) { sptracePrint("start: shared memory mgt failed."); return NULL; } switch (outcome) { case Refused: sptracePrint("start: can't psm_manage shared memory."); return NULL; case Redundant: trh = (TraceHeader *) psp(trace, psm_get_root(trace)); if (trh == NULL || strcmp(name, trh->name) != 0) { sptracePrint("start: shared memory used otherwise."); return NULL; } return trace; /* Trace already started. */ default: break; } /* Initialize the shared memory region for tracing. */ traceHeaderAddress = psm_zalloc(trace, sizeof(TraceHeader)); if (traceHeaderAddress == 0) { sptracePrint("start: not enough memory for header."); sm_ShmDetach(sm); sm_ShmDestroy(smid); return NULL; } oK(psm_set_root(trace, traceHeaderAddress)); trh = (TraceHeader *) psp(trace, traceHeaderAddress); CHKNULL(trh); trh->traceSmId = smid; memset(trh->name, 0, sizeof trh->name); istrcpy(trh->name, name, sizeof trh->name); trh->opCount = 0; trh->files = sm_list_create(trace); if (trh->files == 0) { sptracePrint("start: not enough memory for files list."); sm_ShmDetach(sm); sm_ShmDestroy(smid); return NULL; } trh->log = sm_list_create(trace); if (trh->log == 0) { sptracePrint("start: not enough memory for log."); sm_ShmDetach(sm); sm_ShmDestroy(smid); return NULL; } return trace; }
IonVdb * createIonVdb(char * ionvdbName) { IonVdb *vdb = NULL; PsmAddress vdbAddress; PsmAddress elt; Sdr sdr; PsmPartition ionwm; IonDB iondb; char * name = ionvdbName; /* Attaching to volatile database. */ ionwm = getIonwm(); if (psm_locate(ionwm, name, &vdbAddress, &elt) < 0) { putErrmsg("Failed searching for vdb.", name); return NULL; } if (elt) { vdb = (IonVdb *) psp(ionwm, vdbAddress); } if (vdb != NULL) return vdb; /* ION volatile database doesn't exist yet. */ sdr = getIonsdr(); CHKNULL(sdr_begin_xn(sdr)); /* To lock memory. */ vdbAddress = psm_zalloc(ionwm, sizeof(IonVdb)); if (vdbAddress == 0) { sdr_exit_xn(sdr); putErrmsg("No space for volatile database.", name); return NULL; } vdb = (IonVdb *) psp(ionwm, vdbAddress); memset((char *) vdb, 0, sizeof(IonVdb)); if ((vdb->nodes = sm_rbt_create(ionwm)) == 0 || (vdb->neighbors = sm_rbt_create(ionwm)) == 0 || (vdb->contactIndex = sm_rbt_create(ionwm)) == 0 || (vdb->rangeIndex = sm_rbt_create(ionwm)) == 0 || (vdb->timeline = sm_rbt_create(ionwm)) == 0 || (vdb->probes = sm_list_create(ionwm)) == 0 || (vdb->requisitions[0] = sm_list_create(ionwm)) == 0 || (vdb->requisitions[1] = sm_list_create(ionwm)) == 0 || psm_catlg(ionwm, name, vdbAddress) < 0) { sdr_exit_xn(sdr); putErrmsg("Can't initialize volatile database.", name); return NULL; } vdb->clockPid = ERROR; /* None yet. */ sdr_read(sdr, (char *) &iondb, getIonDbObject(), sizeof(IonDB)); vdb->deltaFromUTC = iondb.deltaFromUTC; sdr_exit_xn(sdr); /* Unlock memory. */ //fprintf(stderr, "ionVdb created: %d\n", getOwnNodeNbr()); return vdb; }
static IonVdb *_ionvdb(char **name) { static IonVdb *vdb = NULL; PsmAddress vdbAddress; PsmAddress elt; Sdr sdr; PsmPartition ionwm; IonDB iondb; if (name) { if (*name == NULL) /* Terminating. */ { vdb = NULL; return vdb; } /* Attaching to volatile database. */ ionwm = _ionwm(NULL); if (psm_locate(ionwm, *name, &vdbAddress, &elt) < 0) { putErrmsg("Failed searching for vdb.", *name); return NULL; } if (elt) { vdb = (IonVdb *) psp(ionwm, vdbAddress); return vdb; } /* ION volatile database doesn't exist yet. */ sdr = _ionsdr(NULL); CHKNULL(sdr_begin_xn(sdr)); /* To lock memory. */ vdbAddress = psm_zalloc(ionwm, sizeof(IonVdb)); if (vdbAddress == 0) { sdr_exit_xn(sdr); putErrmsg("No space for volatile database.", *name); return NULL; } vdb = (IonVdb *) psp(ionwm, vdbAddress); memset((char *) vdb, 0, sizeof(IonVdb)); vdb->zcoSemaphore = sm_SemCreate(SM_NO_KEY, SM_SEM_FIFO); if (vdb->zcoSemaphore == SM_SEM_NONE) { sdr_exit_xn(sdr); putErrmsg("Can't initialize volatile database.", *name); return NULL; } sm_SemTake(vdb->zcoSemaphore); /* Lock it. */ if ((vdb->nodes = sm_rbt_create(ionwm)) == 0 || (vdb->neighbors = sm_rbt_create(ionwm)) == 0 || (vdb->contactIndex = sm_rbt_create(ionwm)) == 0 || (vdb->rangeIndex = sm_rbt_create(ionwm)) == 0 || (vdb->timeline = sm_rbt_create(ionwm)) == 0 || (vdb->probes = sm_list_create(ionwm)) == 0 || psm_catlg(ionwm, *name, vdbAddress) < 0) { sdr_exit_xn(sdr); putErrmsg("Can't initialize volatile database.", *name); return NULL; } vdb->clockPid = ERROR; /* None yet. */ sdr_read(sdr, (char *) &iondb, _iondbObject(NULL), sizeof(IonDB)); vdb->deltaFromUTC = iondb.deltaFromUTC; sdr_exit_xn(sdr); /* Unlock memory. */ } return vdb; }
int main(int argc, char **argv) #endif { char *wmspace; int wmid; PsmPartition wm = NULL; PsmMgtOutcome outcome; PsmAddress testlist; sm_SemId semaphore; int cycleNbr = 1; char fileName[256]; int outputFile; PsmAddress lineListElt; PsmAddress lineAddress; char *line; if (sm_ipc_init() < 0) { return 0; } if (sm_ShmAttach(0x1108, 10000000, &wmspace, &wmid) < 0) { PERROR("can't attach to shared memory"); return 0; } if (psm_manage(wmspace, 10000000, "file2sm", &wm, &outcome) < 0 || outcome == Refused) { PUTS("can't manage shared memory"); return 0; } testlist = psm_get_root(wm); if (testlist == 0) { testlist = sm_list_create(wm); if (testlist == 0) { PUTS("can't create shared memory list"); return 0; } psm_set_root(wm, testlist); } semaphore = sm_SemCreate(0x1101, SM_SEM_FIFO); if (semaphore < 0) { PUTS("can't create semaphore"); return 0; } PUTMEMO("Working on cycle", utoa(cycleNbr)); isprintf(fileName, sizeof fileName, "file_copy_%d", cycleNbr); outputFile = iopen(fileName, O_WRONLY | O_APPEND, 0666); if (outputFile < 0) { PERROR("can't open output file"); return 0; } while (1) { while (sm_list_length(wm, testlist) == 0) { sm_SemTake(semaphore); /* Wait for line. */ } lineListElt = sm_list_first(wm, testlist); lineAddress = sm_list_data(wm, lineListElt); line = psp(wm, lineAddress); /* Process text of line. */ if (strcmp(line, "*** End of the file ***\n") == 0) { /* Close file, open next one. */ close(outputFile); cycleNbr++; PUTMEMO("Working on cycle", utoa(cycleNbr)); isprintf(fileName, sizeof fileName, "file_copy_%d", cycleNbr); outputFile = iopen(fileName, O_WRONLY | O_APPEND, 0666); if (outputFile < 0) { PERROR("Can't open output file"); return 0; } } else /* Just write line to output file. */ { if (iputs(outputFile, line) < 0) { close(outputFile); PERROR("Can't write to output file"); return 0; } } /* Delete line from shared memory list. */ psm_free(wm, lineAddress); CHKZERO(sm_list_delete(wm, lineListElt, (SmListDeleteFn) NULL, NULL) == 0); } }