static void aeApiFree(aeEventLoop *eventLoop) { aeApiState *state = eventLoop->apidata; close(state->kqfd); nn_free(state->events); nn_free(state); }
static void nn_bipc_shutdown (struct nn_fsm *self, int src, int type, void *srcptr) { struct nn_bipc *bipc; struct nn_list_item *it; struct nn_aipc *aipc; bipc = nn_cont (self, struct nn_bipc, fsm); if (nn_slow (src == NN_FSM_ACTION && type == NN_FSM_STOP)) { nn_aipc_stop (bipc->aipc); bipc->state = NN_BIPC_STATE_STOPPING_AIPC; } if (nn_slow (bipc->state == NN_BIPC_STATE_STOPPING_AIPC)) { if (!nn_aipc_isidle (bipc->aipc)) return; nn_aipc_term (bipc->aipc); nn_free (bipc->aipc); bipc->aipc = NULL; nn_usock_stop (&bipc->usock); bipc->state = NN_BIPC_STATE_STOPPING_USOCK; } if (nn_slow (bipc->state == NN_BIPC_STATE_STOPPING_USOCK)) { if (!nn_usock_isidle (&bipc->usock)) return; for (it = nn_list_begin (&bipc->aipcs); it != nn_list_end (&bipc->aipcs); it = nn_list_next (&bipc->aipcs, it)) { aipc = nn_cont (it, struct nn_aipc, item); nn_aipc_stop (aipc); } bipc->state = NN_BIPC_STATE_STOPPING_AIPCS; goto aipcs_stopping; } if (nn_slow (bipc->state == NN_BIPC_STATE_STOPPING_AIPCS)) { nn_assert (src == NN_BIPC_SRC_AIPC && type == NN_AIPC_STOPPED); aipc = (struct nn_aipc *) srcptr; nn_list_erase (&bipc->aipcs, &aipc->item); nn_aipc_term (aipc); nn_free (aipc); /* If there are no more aipc state machines, we can stop the whole bipc object. */ aipcs_stopping: if (nn_list_empty (&bipc->aipcs)) { bipc->state = NN_BIPC_STATE_IDLE; nn_fsm_stopped_noevent (&bipc->fsm); nn_epbase_stopped (&bipc->epbase); return; } return; } nn_fsm_bad_state(bipc->state, src, type); }
static void nn_btcp_shutdown (struct nn_fsm *self, int src, int type, void *srcptr) { struct nn_btcp *btcp; struct nn_list_item *it; struct nn_atcp *atcp; btcp = nn_cont (self, struct nn_btcp, fsm); if (nn_slow (src == NN_FSM_ACTION && type == NN_FSM_STOP)) { nn_atcp_stop (btcp->atcp); btcp->state = NN_BTCP_STATE_STOPPING_ATCP; } if (nn_slow (btcp->state == NN_BTCP_STATE_STOPPING_ATCP)) { if (!nn_atcp_isidle (btcp->atcp)) return; nn_atcp_term (btcp->atcp); nn_free (btcp->atcp); btcp->atcp = NULL; nn_usock_stop (&btcp->usock); btcp->state = NN_BTCP_STATE_STOPPING_USOCK; } if (nn_slow (btcp->state == NN_BTCP_STATE_STOPPING_USOCK)) { if (!nn_usock_isidle (&btcp->usock)) return; for (it = nn_list_begin (&btcp->atcps); it != nn_list_end (&btcp->atcps); it = nn_list_next (&btcp->atcps, it)) { atcp = nn_cont (it, struct nn_atcp, item); nn_atcp_stop (atcp); } btcp->state = NN_BTCP_STATE_STOPPING_ATCPS; goto atcps_stopping; } if (nn_slow (btcp->state == NN_BTCP_STATE_STOPPING_ATCPS)) { nn_assert (src == NN_BTCP_SRC_ATCP && type == NN_ATCP_STOPPED); atcp = (struct nn_atcp *) srcptr; nn_list_erase (&btcp->atcps, &atcp->item); nn_atcp_term (atcp); nn_free (atcp); /* If there are no more atcp state machines, we can stop the whole btcp object. */ atcps_stopping: if (nn_list_empty (&btcp->atcps)) { btcp->state = NN_BTCP_STATE_IDLE; nn_fsm_stopped_noevent (&btcp->fsm); nn_epbase_stopped (&btcp->epbase); return; } return; } nn_fsm_bad_action(btcp->state, src, type); }
int nn_msgqueue_recv (struct nn_msgqueue *self, struct nn_msg *msg) { struct nn_msgqueue_chunk *o; /* If there is no message in the queue. */ if (nn_slow (!self->count)) return -EAGAIN; /* Move the message from the pipe to the user. */ nn_msg_mv (msg, &self->in.chunk->msgs [self->in.pos]); /* Move to the next position. */ ++self->in.pos; if (nn_slow (self->in.pos == NN_MSGQUEUE_GRANULARITY)) { o = self->in.chunk; self->in.chunk = self->in.chunk->next; self->in.pos = 0; if (nn_fast (!self->cache)) self->cache = o; else nn_free (o); } /* Adjust the statistics. */ --self->count; self->mem -= (nn_chunkref_size (&msg->hdr) + nn_chunkref_size (&msg->body)); return 0; }
int main(void) { int i; int cyc; nn_sys_t nnins; nn_init(&nnins, 2, 7, 1, 100, 0.5, 0.00001); init_teacher_vect(&nnins); for (cyc = 0; cyc < 30000; cyc++) { float e = 0.0; for (i = 0; i < nnins.teacher_n; i++) { nn_bp(&nnins, i); e += nn_calcerr(&nnins, i); } fprintf(stderr, "e:%f\r", e); } for (i = 0; i < 10000; i++) { float rx = (float)rand() / RAND_MAX; float ry = (float)rand() / RAND_MAX; float inputarr[2] = {rx, ry}; nn_compute(&nnins, inputarr); printf("#XY %f %f %f %f\n", rx, ry, nnins.output[0], nnins.output[0] - rx * rx / 2 - ry * ry / 2); } printf("\n"); nn_free(&nnins); return 0; }
static void nn_tcp_optset_destroy (struct nn_optset *self) { struct nn_tcp_optset *optset; optset = nn_cont (self, struct nn_tcp_optset, base); nn_free (optset); }
struct nn_trie_node *nn_node_compact (struct nn_trie_node *self) { /* Tries to merge the node with the child node. Returns pointer to the compacted node. */ struct nn_trie_node *ch; /* Node that is a subscription cannot be compacted. */ if (nn_node_has_subscribers (self)) return self; /* Only a node with a single child can be compacted. */ if (self->type != 1) return self; /* Check whether combined prefixes would fix into a single node. */ ch = *nn_node_child (self, 0); if (self->prefix_len + ch->prefix_len + 1 > NN_TRIE_PREFIX_MAX) return self; /* Concatenate the prefixes. */ memmove (ch->prefix + self->prefix_len + 1, ch->prefix, ch->prefix_len); memcpy (ch->prefix, self->prefix, self->prefix_len); ch->prefix [self->prefix_len] = self->u.sparse.children [0]; ch->prefix_len += self->prefix_len + 1; /* Get rid of the obsolete parent node. */ nn_free (self); /* Return the new compacted node. */ return ch; }
int nn_close (int s) { int rc; NN_BASIC_CHECKS; /* TODO: nn_sock_term can take a long time to accomplish. It should not be performed under global critical section. */ nn_glock_lock (); /* Deallocate the socket object. */ rc = nn_sock_term (self.socks [s]); if (nn_slow (rc == -EINTR)) { errno = EINTR; return -1; } /* Remove the socket from the socket table, add it to unused socket table. */ nn_free (self.socks [s]); self.socks [s] = NULL; self.unused [NN_MAX_SOCKETS - self.nsocks] = s; --self.nsocks; /* Destroy the global context if there's no socket remaining. */ nn_global_term (); nn_glock_unlock (); return 0; }
int nn_sock_add_ep (struct nn_sock *self, struct nn_transport *transport, int bind, const char *addr) { int rc; struct nn_ep *ep; int eid; nn_ctx_enter (&self->ctx); /* Instantiate the endpoint. */ ep = nn_alloc (sizeof (struct nn_ep), "endpoint"); rc = nn_ep_init (ep, NN_SOCK_SRC_EP, self, self->eid, transport, bind, addr); if (nn_slow (rc < 0)) { nn_free (ep); nn_ctx_leave (&self->ctx); return rc; } nn_ep_start (ep); /* Increase the endpoint ID for the next endpoint. */ eid = self->eid; ++self->eid; /* Add it to the list of active endpoints. */ nn_list_insert (&self->eps, &ep->item, nn_list_end (&self->eps)); nn_ctx_leave (&self->ctx); return eid; }
int nn_binproc_create (void *hint, struct nn_epbase **epbase) { int rc; struct nn_binproc *self; self = nn_alloc (sizeof (struct nn_binproc), "binproc"); alloc_assert (self); nn_ins_item_init (&self->item, &nn_binproc_vfptr, hint); nn_fsm_init_root (&self->fsm, nn_binproc_handler, nn_binproc_shutdown, nn_epbase_getctx (&self->item.epbase)); self->state = NN_BINPROC_STATE_IDLE; nn_list_init (&self->sinprocs); /* Start the state machine. */ nn_fsm_start (&self->fsm); /* Register the inproc endpoint into a global repository. */ rc = nn_ins_bind (&self->item, nn_binproc_connect); if (nn_slow (rc < 0)) { nn_list_term (&self->sinprocs); /* TODO: Now, this is ugly! We are getting the state machine into the idle state manually. How should it be done correctly? */ self->fsm.state = 1; nn_fsm_term (&self->fsm); nn_ins_item_term (&self->item); nn_free (self); return rc; } *epbase = &self->item.epbase; return 0; }
void nn_hash_term (hash *self) { uint32_t i; for (i = 0; i != self->slots; ++i) nn_assert (self->array [i] == NULL); nn_free (self->array); }
/* Deallocate a message chunk and remove it from array. */ static void nn_msg_chunk_term (struct msg_chunk *it, struct nn_list *msg_array) { nn_chunkref_term (&it->chunk); nn_list_erase (msg_array, &it->item); nn_list_item_term (&it->item); nn_free (it); }
static void nn_xbus_destroy (struct nn_sockbase *self) { struct nn_xbus *xbus; xbus = nn_cont (self, struct nn_xbus, sockbase); nn_xbus_term (xbus); nn_free (xbus); }
static int aeApiCreate(aeEventLoop *eventLoop) { aeApiState *state = nn_malloc(sizeof(aeApiState)); if (!state) return -1; state->events = nn_malloc(sizeof(struct kevent)*eventLoop->setsize); if (!state->events) { nn_free(state); return -1; } state->kqfd = kqueue(); if (state->kqfd == -1) { nn_free(state->events); nn_free(state); return -1; } eventLoop->apidata = state; return 0; }
void nn_surveyor_destroy (struct nn_sockbase *self) { struct nn_surveyor *surveyor; surveyor = nn_cont (self, struct nn_surveyor, xsurveyor.sockbase); nn_surveyor_term (surveyor); nn_free (surveyor); }
static void nn_xreq_destroy (struct nn_sockbase *self) { struct nn_xreq *xreq; xreq = nn_cont (self, struct nn_xreq, sockbase); nn_xreq_term (xreq); nn_free (xreq); }
void nn_xpull_destroy (struct nn_sockbase *self) { struct nn_xpull *xpull; xpull = nn_cont (self, struct nn_xpull, sockbase); nn_xpull_term (xpull); nn_free (xpull); }
void nn_xpush_destroy (struct nn_sockbase *self) { struct nn_xpush *xpush; xpush = nn_cont (self, struct nn_xpush, sockbase); nn_xpush_term (xpush); nn_free (xpush); }
void nn_xsub_destroy (struct nn_sockbase *self) { struct nn_xsub *xsub; xsub = nn_cont (self, struct nn_xsub, sockbase); nn_xsub_term (xsub); nn_free (xsub); }
void nn_rep_destroy (struct nn_sockbase *self) { struct nn_rep *rep; rep = nn_cont (self, struct nn_rep, xrep.sockbase); nn_rep_term (rep); nn_free (rep); }
static void nn_req_destroy (struct nn_sockbase *self) { struct nn_req *req; req = nn_cont (self, struct nn_req, xreq.sockbase); nn_req_term (req); nn_free (req); }
static void nn_xsurveyor_destroy (struct nn_sockbase *self) { struct nn_xsurveyor *xsurveyor; xsurveyor = nn_cont (self, struct nn_xsurveyor, sockbase); nn_xsurveyor_term (xsurveyor); nn_free (xsurveyor); }
void nn_xpair_destroy (struct nn_sockbase *self) { struct nn_xpair *xpair; xpair = nn_cont (self, struct nn_xpair, sockbase); nn_xpair_term (xpair); nn_free (xpair); }
void nn_respondent_destroy (struct nn_sockbase *self) { struct nn_respondent *respondent; respondent = nn_cont (self, struct nn_respondent, xrespondent.sockbase); nn_respondent_term (respondent); nn_free (respondent); }
static void nn_xrespondent_destroy (struct nn_sockbase *self) { struct nn_xrespondent *xrespondent; xrespondent = nn_cont (self, struct nn_xrespondent, sockbase); nn_xrespondent_term (xrespondent); nn_free (xrespondent); }
static void nn_cinproc_destroy (void *self) { struct nn_cinproc *cinproc = self; nn_list_term (&cinproc->sinprocs); nn_fsm_term (&cinproc->fsm); nn_ins_item_term (&cinproc->item); nn_free (cinproc); }
static void nn_xpull_rm (struct nn_sockbase *self, struct nn_pipe *pipe) { struct nn_xpull *xpull; struct nn_xpull_data *data; xpull = nn_cont (self, struct nn_xpull, sockbase); data = nn_pipe_getdata (pipe); nn_fq_rm (&xpull->fq, pipe, &data->fq); nn_free (data); }
static void nn_xsub_rm (struct nn_sockbase *self, struct nn_pipe *pipe) { struct nn_xsub *xsub; struct nn_xsub_data *data; xsub = nn_cont (self, struct nn_xsub, sockbase); data = nn_pipe_getdata (pipe); nn_fq_rm (&xsub->fq, &data->fq); nn_free (data); }
static void nn_cstream_terminating_closed (const struct nn_cp_sink **self, struct nn_usock *usock) { struct nn_cstream *cstream; cstream = nn_cont (self, struct nn_cstream, sink); nn_epbase_term (&cstream->epbase); nn_free (cstream); }
static void nn_xpush_rm (struct nn_sockbase *self, struct nn_pipe *pipe) { struct nn_xpush *xpush; struct nn_xpush_data *data; xpush = nn_cont (self, struct nn_xpush, sockbase); data = nn_pipe_getdata (pipe); nn_lb_rm (&xpush->lb, pipe, &data->lb); nn_free (data); }