static void *worker(void *_dummy) { worker_cleanup_t wc; pth_msgport_t mp; pth_event_t ev; struct query *q; int i; fprintf(stderr, "worker: start\n"); wc.mp = mp = pth_msgport_create("worker"); wc.ev = ev = pth_event(PTH_EVENT_MSG, mp); pth_cleanup_push(worker_cleanup, &wc); for (;;) { if ((i = pth_wait(ev)) != 1) continue; while ((q = (struct query *)pth_msgport_get(mp)) != NULL) { fprintf(stderr, "worker: recv query <%s>\n", q->string); for (i = 0; q->string[i] != NUL; i++) q->string[i] = toupper(q->string[i]); fprintf(stderr, "worker: send reply <%s>\n", q->string); pth_msgport_reply((pth_message_t *)q); } } return NULL; }
int pth_mutex_acquire(pth_mutex_t *mutex, int tryonly, pth_event_t ev_extra) { static pth_key_t ev_key = PTH_KEY_INIT; pth_event_t ev; pth_debug2("pth_mutex_acquire: called from thread \"%s\"", pth_current->name); /* consistency checks */ if (mutex == NULL) return pth_error(FALSE, EINVAL); if (!(mutex->mx_state & PTH_MUTEX_INITIALIZED)) return pth_error(FALSE, EDEADLK); /* still not locked, so simply acquire mutex? */ if (!(mutex->mx_state & PTH_MUTEX_LOCKED)) { mutex->mx_state |= PTH_MUTEX_LOCKED; mutex->mx_owner = pth_current; mutex->mx_count = 1; pth_ring_append(&(pth_current->mutexring), &(mutex->mx_node)); pth_debug1("pth_mutex_acquire: immediately locking mutex"); return TRUE; } /* already locked by caller? */ if (mutex->mx_count >= 1 && mutex->mx_owner == pth_current) { /* recursive lock */ mutex->mx_count++; pth_debug1("pth_mutex_acquire: recursive locking"); return TRUE; } /* should we just tryonly? */ if (tryonly) return pth_error(FALSE, EBUSY); /* else wait for mutex to become unlocked.. */ pth_debug1("pth_mutex_acquire: wait until mutex is unlocked"); for (;;) { ev = pth_event(PTH_EVENT_MUTEX|PTH_MODE_STATIC, &ev_key, mutex); if (ev_extra != NULL) pth_event_concat(ev, ev_extra, NULL); pth_wait(ev); if (ev_extra != NULL) { pth_event_isolate(ev); if (pth_event_status(ev) == PTH_STATUS_PENDING) return pth_error(FALSE, EINTR); } if (!(mutex->mx_state & PTH_MUTEX_LOCKED)) break; } /* now it's again unlocked, so acquire mutex */ pth_debug1("pth_mutex_acquire: locking mutex"); mutex->mx_state |= PTH_MUTEX_LOCKED; mutex->mx_owner = pth_current; mutex->mx_count = 1; pth_ring_append(&(pth_current->mutexring), &(mutex->mx_node)); return TRUE; }
bool EMI1Layer2::Close () { if (!Layer2::Close ()) return false; TRACEPRINTF (t, 2, this, "CloseL2"); uchar t[] = { 0x46, 0x01, 0x00, 0x60, 0xc0 }; iface->Send_Packet (CArray (t, sizeof (t))); if (!iface->Send_Queue_Empty ()) { pth_event_t e = pth_event (PTH_EVENT_SEM, iface->Send_Queue_Empty_Cond ()); pth_wait (e); pth_event_free (e, PTH_FREE_THIS); } return true; }
bool EMI2Layer2::leaveBusmonitor () { if (!Layer2::leaveBusmonitor ()) return false; TRACEPRINTF (t, 2, this, "CloseBusmon"); uchar t[] = { 0xa9, 0x1E, 0x12, 0x34, 0x56, 0x78, 0x9a }; iface->Send_Packet (CArray (t, sizeof (t))); while (!iface->Send_Queue_Empty ()) { pth_event_t e = pth_event (PTH_EVENT_SEM, iface->Send_Queue_Empty_Cond ()); pth_wait (e); pth_event_free (e, PTH_FREE_THIS); } return true; }
bool EMI1Layer2::Open () { if (!Layer2::Open ()) return false; const uchar t1[] = { 0x46, 0x01, 0x00, 0x60, 0x12 }; TRACEPRINTF (t, 2, this, "OpenL2"); iface->SendReset (); iface->Send_Packet (CArray (t1, sizeof (t1))); while (!iface->Send_Queue_Empty ()) { pth_event_t e = pth_event (PTH_EVENT_SEM, iface->Send_Queue_Empty_Cond ()); pth_wait (e); pth_event_free (e, PTH_FREE_THIS); } return true; }
CArray * BCU1SerialLowLevelDriver::Get_Packet (pth_event_t stop) { if (stop != NULL) pth_event_concat (getwait, stop, NULL); pth_wait (getwait); if (stop) pth_event_isolate (getwait); if (pth_event_status (getwait) == PTH_STATUS_OCCURRED) { pth_sem_dec (&out_signal); CArray *c = outqueue.get (); t->TracePacket (1, this, "Recv", *c); return c; } else return 0; }
LPDU * TPUARTLayer2Driver::Get_L_Data (pth_event_t stop) { if (stop != NULL) pth_event_concat (getwait, stop, NULL); pth_wait (getwait); if (stop) pth_event_isolate (getwait); if (pth_event_status (getwait) == PTH_STATUS_OCCURRED) { pth_sem_dec (&out_signal); LPDU *l = outqueue.get (); TRACEPRINTF (t, 2, this, "Recv %s", l->Decode ()()); return l; } else return 0; }
bool EMI2Layer2::enterBusmonitor () { if (!Layer2::enterBusmonitor ()) return false; const uchar t1[] = { 0xa9, 0x1E, 0x12, 0x34, 0x56, 0x78, 0x9a }; const uchar t2[] = { 0xa9, 0x90, 0x18, 0x34, 0x45, 0x67, 0x8a }; TRACEPRINTF (t, 2, this, "OpenBusmon"); iface->SendReset (); iface->Send_Packet (CArray (t1, sizeof (t1))); iface->Send_Packet (CArray (t2, sizeof (t2))); if (!iface->Send_Queue_Empty ()) { pth_event_t e = pth_event (PTH_EVENT_SEM, iface->Send_Queue_Empty_Cond ()); pth_wait (e); pth_event_free (e, PTH_FREE_THIS); } return true; }
LPDU * EIBNetIPTunnel::Get_L_Data (pth_event_t stop) { if (stop != NULL) pth_event_concat (getwait, stop, NULL); pth_wait (getwait); if (stop) pth_event_isolate (getwait); if (pth_event_status (getwait) == PTH_STATUS_OCCURRED) { pth_sem_dec (&outsignal); LPDU *c = outqueue.get (); if (c) TRACEPRINTF (t, 2, this, "Recv %s", c->Decode ()()); return c; } else return 0; }
void FrSimThreadEvent::wait() { pth_wait(ev); // pth_status_t status = pth_event_status(ev); // std::cout << "wait returned "; // if (status == PTH_STATUS_PENDING) { // std::cout << "PENDING" << std::endl; // } else if (status == PTH_STATUS_OCCURRED) { // std::cout << "OCCURED" << std::endl; // } else if (status == PTH_STATUS_FAILED) { // std::cout << "FAILED" << std::endl; // } // std::cout << "waiting for " << pth_msgport_pending(msgPrt) << std::endl; int i = 0; while (pth_msgport_pending(msgPrt) != 0) { pth_msgport_get(msgPrt); // std::cout << "now waiting for " << pth_msgport_pending(msgPrt) << std::endl; i++; if (i == 10) std::exit(1); } }
void BCU1SerialLowLevelDriver::Run (pth_sem_t * stop1) { pth_event_t stop = pth_event (PTH_EVENT_SEM, stop1); pth_event_t input = pth_event (PTH_EVENT_SEM, &in_signal); pth_event_t timeout = pth_event (PTH_EVENT_RTIME, pth_time (0, 10)); while (pth_event_status (stop) != PTH_STATUS_OCCURRED) { int error; timeout = pth_event (PTH_EVENT_RTIME | PTH_MODE_REUSE, timeout, pth_time (0, 150)); pth_event_concat (stop, input, timeout, NULL); pth_wait (stop); pth_event_isolate (stop); pth_event_isolate (input); timeout = pth_event (PTH_EVENT_RTIME | PTH_MODE_REUSE, timeout, pth_time (0, 200)); pth_event_concat (stop, timeout, NULL); struct timeval v1, v2; gettimeofday (&v1, 0); CArray e; CArray r; uchar s; if (!inqueue.isempty ()) { const CArray & c = inqueue.top (); e.resize (c () + 1); s = c () & 0x1f; s |= 0x20; s |= 0x80 * bitcount (s); e[0] = s; e.setpart (c, 1); } else { e.resize (1); e[0] = 0xff; } if (!startsync ()) { error = 1; goto err; } if (!exchange (e[0], s, stop)) { error = 3; goto err; } if (!endsync ()) { error = 2; goto err; } if (s == 0xff && e[0] != 0xff) { for (unsigned i = 1; i < e (); i++) { if (!startsync ()) { error = 1; goto err; } if (!exchange (e[i], s, stop)) { error = 3; goto err; } if (endsync ()) { error = 2; goto err; } } if (s != 0x00) { error = 10; goto err; } inqueue.get (); TRACEPRINTF (t, 0, this, "Sent"); pth_sem_dec (&in_signal); if (inqueue.isempty ()) pth_sem_set_value (&send_empty, 1); } else if (s != 0xff) { r.resize ((s & 0x1f)); for (unsigned i = 0; i < (s & 0x1f); i++) { if (!startsync ()) { error = 1; goto err; } if (!exchange (0, r[i], stop)) { error = 3; goto err; } if (!endsync ()) { error = 2; goto err; } } TRACEPRINTF (t, 0, this, "Recv"); outqueue.put (new CArray (r)); pth_sem_inc (&out_signal, 1); } gettimeofday (&v2, 0); TRACEPRINTF (t, 1, this, "Recvtime: %d", v2.tv_sec * 1000000L + v2.tv_usec - (v1.tv_sec * 1000000L + v1.tv_usec)); if (0) { err: gettimeofday (&v2, 0); TRACEPRINTF (t, 1, this, "ERecvtime: %d", v2.tv_sec * 1000000L + v2.tv_usec - (v1.tv_sec * 1000000L + v1.tv_usec)); setstat (getstat () & ~(TIOCM_RTS | TIOCM_CTS)); pth_usleep (2000); while ((getstat () & TIOCM_CTS)); TRACEPRINTF (t, 0, this, "Restart %d", error); } pth_event_isolate (timeout); } pth_event_free (timeout, PTH_FREE_THIS); pth_event_free (stop, PTH_FREE_THIS); pth_event_free (input, PTH_FREE_THIS); }
void USBLoop::Run (pth_sem_t * stop1) { fd_set r, w, e; int rc, fds, i; struct timeval tv, tv1; const struct libusb_pollfd **usbfd, **usbfd_orig; pth_event_t stop = pth_event (PTH_EVENT_SEM, stop1); // The next two are dummy allocations which will be replaced later pth_event_t event = pth_event (PTH_EVENT_SEM, stop1); pth_event_t timeout = pth_event (PTH_EVENT_SEM, stop1); tv1.tv_sec = tv1.tv_usec = 0; TRACEPRINTF (t, 10, this, "LoopStart"); while (pth_event_status (stop) != PTH_STATUS_OCCURRED) { TRACEPRINTF (t, 10, this, "LoopBegin"); FD_ZERO (&r); FD_ZERO (&w); FD_ZERO (&e); fds = 0; rc = 0; usbfd = libusb_get_pollfds (context); usbfd_orig = usbfd; if (usbfd) while (*usbfd) { if ((*usbfd)->fd > fds) fds = (*usbfd)->fd; if ((*usbfd)->events & POLLIN) FD_SET ((*usbfd)->fd, &r); if ((*usbfd)->events & POLLOUT) FD_SET ((*usbfd)->fd, &w); usbfd++; } free (usbfd_orig); i = libusb_get_next_timeout (context, &tv); if (i < 0) break; if (i > 0) { pth_event (PTH_EVENT_RTIME | PTH_MODE_REUSE, timeout, pth_time (tv.tv_sec, tv.tv_usec)); pth_event_concat (stop, timeout, NULL); } pth_event (PTH_EVENT_SELECT | PTH_MODE_REUSE, event, &rc, fds + 1, &r, &w, &e); pth_event_concat (stop, event, NULL); TRACEPRINTF (t, 10, this, "LoopWait"); pth_wait (stop); TRACEPRINTF (t, 10, this, "LoopProcess"); pth_event_isolate (event); pth_event_isolate (timeout); if (libusb_handle_events_timeout (context, &tv1)) break; TRACEPRINTF (t, 10, this, "LoopEnd"); } TRACEPRINTF (t, 10, this, "LoopStop"); pth_event_free (timeout, PTH_FREE_THIS); pth_event_free (event, PTH_FREE_THIS); pth_event_free (stop, PTH_FREE_THIS); }
void Layer3::Run (pth_sem_t * stop1) { pth_event_t stop = pth_event (PTH_EVENT_SEM, stop1); unsigned i; running = true; for (i = 0; i < layer2 (); i++) layer2[i].Start (); TRACEPRINTF (t, 3, this, "L3 started"); while (pth_event_status (stop) != PTH_STATUS_OCCURRED) { pth_event_t bufev = pth_event (PTH_EVENT_SEM, &bufsem); pth_event_concat (bufev, stop, NULL); pth_wait (bufev); pth_event_isolate (bufev); if (pth_event_status (bufev) != PTH_STATUS_OCCURRED) { pth_event_free (bufev, PTH_FREE_THIS); continue; } pth_event_free (bufev, PTH_FREE_THIS); pth_sem_dec (&bufsem); LPDU *l = buf.get (); if (!l) continue; if (l->getType () == L_Busmonitor) { L_Busmonitor_PDU *l1, *l2; l1 = (L_Busmonitor_PDU *) l; TRACEPRINTF (t, 3, this, "Recv %s", l1->Decode ()()); for (i = 0; i < busmonitor (); i++) { l2 = new L_Busmonitor_PDU (*l1); busmonitor[i].cb->Send_L_Busmonitor (l2); } for (i = 0; i < vbusmonitor (); i++) { l2 = new L_Busmonitor_PDU (*l1); vbusmonitor[i].cb->Send_L_Busmonitor (l2); } } if (l->getType () == L_Data) { L_Data_PDU *l1; l1 = (L_Data_PDU *) l; if (l1->repeated) { CArray d1 = l1->ToPacket (); for (i = 0; i < ignore (); i++) if (d1 == ignore[i].data) { TRACEPRINTF (t, 3, this, "Repeated discareded"); goto wt; } } l1->repeated = 1; ignore.resize (ignore () + 1); ignore[ignore () - 1].data = l1->ToPacket (); ignore[ignore () - 1].end = getTime () + 1000000; l1->repeated = 0; if (l1->AddrType == IndividualAddress && l1->dest == defaultAddr) l1->dest = 0; TRACEPRINTF (t, 3, this, "Recv %s", l1->Decode ()()); if (l1->AddrType == GroupAddress && l1->dest == 0) { for (i = 0; i < broadcast (); i++) broadcast[i].cb->Send_L_Data (new L_Data_PDU (*l1)); } if (l1->AddrType == GroupAddress && l1->dest != 0) { for (i = 0; i < group (); i++) { Group_Info &grp = group[i]; if (grp.dest == l1->dest || grp.dest == 0) grp.cb->Send_L_Data (new L_Data_PDU (*l1)); } } if (l1->AddrType == IndividualAddress) { for (i = 0; i < individual (); i++) { Individual_Info &indiv = individual[i]; if (indiv.dest == l1->dest || indiv.dest == 0) if (indiv.src == l1->source || indiv.src == 0) indiv.cb->Send_L_Data (new L_Data_PDU (*l1)); } } // finally, send to all (other(?)) L2 interfaces // TODO: filter by addresses send_L_Data(l1); } // ignore[] is ordered, any timed-out items are at the front for (i = 0; i < ignore (); i++) if (ignore[i].end >= getTime ()) break; if (i) ignore.deletepart (0, i); wt: delete l; } TRACEPRINTF (t, 3, this, "L3 stopping"); running = false; for (i = 0; i < layer2 (); i++) layer2[i].Stop (); pth_event_free (stop, PTH_FREE_THIS); }
int main(int argc, char *argv[]) { char caLine[MAXLINELEN]; pth_event_t ev = NULL; pth_event_t evt = NULL; pth_t t_worker = NULL; pth_t t_ticker = NULL; pth_attr_t t_attr; pth_msgport_t mp = NULL; pth_msgport_t mp_worker = NULL; struct query *q = NULL; int n; if (!pth_init()) { perror("pth_init"); exit(1); } /* murray added for tmp debug */ /* pth_time_t intval, former; printf("-------------------------\n"); pth_time_set(&former, PTH_TIME_NOW); pth_usleep(300); pth_time_set(&intval, PTH_TIME_NOW); pth_time_sub(&intval, &former); double val = pth_time_t2d(&intval); printf("the intval is [%f]\n", val); pth_debug2("the intval is [%f]\n", val); return 0; */ fprintf(stderr, "This is TEST_MP, a Pth test using message ports.\n"); fprintf(stderr, "\n"); fprintf(stderr, "Lines on stdin are send to a worker thread via message\n"); fprintf(stderr, "ports, translated to upper case by the worker thread and\n"); fprintf(stderr, "send back to the main thread via message ports.\n"); fprintf(stderr, "Additionally a useless ticker thread awakens every 5s.\n"); fprintf(stderr, "Enter \"quit\" on stdin for stopping this test.\n"); fprintf(stderr, "\n"); t_attr = pth_attr_new(); pth_attr_set(t_attr, PTH_ATTR_NAME, "worker"); pth_attr_set(t_attr, PTH_ATTR_JOINABLE, TRUE); pth_attr_set(t_attr, PTH_ATTR_STACK_SIZE, 16*1024); t_worker = pth_spawn(t_attr, worker, NULL); pth_attr_set(t_attr, PTH_ATTR_NAME, "ticker"); t_ticker = pth_spawn(t_attr, ticker, NULL); pth_attr_destroy(t_attr); pth_yield(NULL); mp_worker = pth_msgport_find("worker"); mp = pth_msgport_create("main"); q = (struct query *)malloc(sizeof(struct query)); ev = pth_event(PTH_EVENT_MSG, mp); evt = NULL; for (;;) { if (evt == NULL) evt = pth_event(PTH_EVENT_TIME, pth_timeout(20,0)); else evt = pth_event(PTH_EVENT_TIME|PTH_MODE_REUSE, evt, pth_timeout(20,0)); n = pth_readline_ev(STDIN_FILENO, caLine, MAXLINELEN, evt); if (n == -1 && pth_event_status(evt) == PTH_STATUS_OCCURRED) { fprintf(stderr, "main: Hey, what are you waiting for? Type in something!\n"); continue; } if (n < 0) { fprintf(stderr, "main: I/O read error on stdin\n"); break; } if (n == 0) { fprintf(stderr, "main: EOF on stdin\n"); break; } caLine[n-1] = NUL; if (strcmp(caLine, "quit") == 0) { fprintf(stderr, "main: quit\n"); break; } fprintf(stderr, "main: out --> <%s>\n", caLine); q->string = caLine; q->head.m_replyport = mp; pth_msgport_put(mp_worker, (pth_message_t *)q); pth_wait(ev); q = (struct query *)pth_msgport_get(mp); fprintf(stderr, "main: in <-- <%s>\n", q->string); } free(q); pth_event_free(ev, PTH_FREE_THIS); pth_event_free(evt, PTH_FREE_THIS); pth_msgport_destroy(mp); pth_cancel(t_worker); pth_join(t_worker, NULL); pth_cancel(t_ticker); pth_join(t_ticker, NULL); pth_kill(); return 0; }
int main(int argc, const char * args[]) { if (argc < 2) { printf("Modo de uso: ./centrality archivo.sg [#threads]\n"); printf("Si #threads es menor a 1 no se calculara la intermediacion.\n"); return EXIT_SUCCESS; } char *filename = (char*) args[1]; printf("File: %s\n", filename); int NT = (argc > 2) ? atoi(args[2]) : 4, i, j, gap; float *BC; struct data **D; struct p_th *P; if ( (G = file_to_graph(filename)) == NULL) return EXIT_FAILURE; if (NT > 1) { printf("Threads: %d\n", NT); gap = (G->size%NT == 0) ? G->size/NT : (G->size/NT)+1; BC = (float*) malloc(sizeof(float) * G->size); D = (struct data**) malloc(sizeof(struct data*)*NT); P = pth_create(NT); for (i = 0; i < NT; i++) { D[i] = (struct data*) malloc(sizeof(struct data)); D[i]->init = i*gap; D[i]->end = (i+1)*gap; D[i]->bc = (float*) malloc(sizeof(float) * G->size); } for (i = 0; i < NT; i++) { pth_send_job(P, __cent, D[i]); } pth_wait(P); for (i = 0; i < G->size; i++) { //se puede acumular todo en el primero. BC[i] = 0.0; for (j = 0; j < NT; j++) { BC[i] += D[j]->bc[i]; } } pth_del(P); for (i = 0; i < NT; i++) { free(D[i]->bc); free(D[i]); } free(D); } else if (NT == 1) { printf("Threads: Single core\n"); BC = betweenness_centrality(G); } else { printf("Calculando solo centralidad de grado.\n"); BC = (float *) calloc(G->size, sizeof(float)); } /* Guardando los resultados en archivos. */ short fn_size = strlen(filename); char *bc_out = (char *) malloc(sizeof(char) * (fn_size + 11)), *idc_out = (char *) malloc(sizeof(char) * (fn_size + 12)), *odc_out = (char *) malloc(sizeof(char) * (fn_size + 12)); strcpy(bc_out, filename); strcpy(idc_out, filename); strcpy(odc_out, filename); strcat(bc_out, ".bc.result"); strcat(idc_out, ".idc.result"); strcat(odc_out, ".odc.result"); FILE *fbc = fopen(bc_out, "w"), *fidc = fopen(idc_out, "w"), *fodc = fopen(odc_out, "w"); for (i=0; i< G->size; i++){ fprintf(fbc,"%d: %f\n", i, BC[i]); fprintf(fidc,"%d: %d\n", i, IDC[i]); fprintf(fodc,"%d: %d\n", i, ODC[i]); } fclose(fbc); fclose(fidc); fclose(fodc); free(bc_out); free(idc_out); free(odc_out); graph_del(G); free(BC); free(IDC); free(ODC); return EXIT_SUCCESS; }