示例#1
0
static void
nandsim_loop(void *arg)
{
	struct nandsim_chip *chip = (struct nandsim_chip *)arg;
	struct nandsim_ev *ev;

	nand_debug(NDBG_SIM,"Start main loop for chip%d [%8p]", chip->chip_num,
	    chip);
	for(;;) {
		NANDSIM_CHIP_LOCK(chip);
		if (!(chip->flags & NANDSIM_CHIP_ACTIVE)) {
			chip->flags |= NANDSIM_CHIP_ACTIVE;
			wakeup(chip->nandsim_td);
		}

		if (STAILQ_EMPTY(&chip->nandsim_events)) {
			nand_debug(NDBG_SIM,"Chip%d [%8p] going sleep",
			    chip->chip_num, chip);
			msleep(chip, &chip->ns_lock, PRIBIO, "nandev", 0);
		}

		ev = STAILQ_FIRST(&chip->nandsim_events);
		STAILQ_REMOVE_HEAD(&chip->nandsim_events, links);
		NANDSIM_CHIP_UNLOCK(chip);
		if (ev->type == NANDSIM_EV_EXIT) {
			NANDSIM_CHIP_LOCK(chip);
			destroy_event(ev);
			wakeup(ev);
			while (!STAILQ_EMPTY(&chip->nandsim_events)) {
				ev = STAILQ_FIRST(&chip->nandsim_events);
				STAILQ_REMOVE_HEAD(&chip->nandsim_events,
				    links);
				destroy_event(ev);
				wakeup(ev);
			}
			NANDSIM_CHIP_UNLOCK(chip);
			nandsim_log(chip, NANDSIM_LOG_SM, "destroyed\n");
			mtx_destroy(&chip->ns_lock);
			nandsim_blk_state_destroy(chip);
			nandsim_swap_destroy(chip->swap);
			free(chip, M_NANDSIM);
			nandsim_proc = NULL;

			kthread_exit();
		}

		if (!(chip->flags & NANDSIM_CHIP_FROZEN)) {
			nand_debug(NDBG_SIM,"Chip [%x] get event [%x]",
			    chip->chip_num, ev->type);
			chip->ev_handler(chip, ev->type, ev->data);
		}

		wakeup(ev);
		destroy_event(ev);
	}

}
示例#2
0
/*
 * Destroy event function call of the call RCU.
 */
static void destroy_event_rcu(struct rcu_head *head)
{
	struct lttng_ht_node_u64 *node =
		caa_container_of(head, struct lttng_ht_node_u64, head);
	struct ust_registry_event *event =
		caa_container_of(node, struct ust_registry_event, node);

	destroy_event(event);
}
void change_shadow_to_new_grid(struct t_event *eventer)
{
	if (eventer->destroy)
	{
		if (eventer->data != NULL)
			free(eventer->data);
		return;
	}
	int * new_grid = (int*) eventer->data;
	change_shadow_light_to_brightest(new_grid);
	destroy_event(eventer);
}
/**
 * handle an event
 * 
 */
void 
TrexDpPortEvents::on_core_reporting_in(int event_id, int thread_id, bool status) {
    TrexDpPortEvent *event = lookup(event_id);
    /* event might have been deleted */
    if (!event) {
        return;
    }

    bool done = event->on_core_reporting_in(thread_id, status);

    if (done) {
        destroy_event(event_id);
    }
}
示例#5
0
/* ----------------------------------------------------------------------
 * destroy a pool of objects.  if none of the pool objects are in use,
 * the destroy will happen immediately.  otherwise, we will sleep for
 * up to 'timeout' microseconds waiting for the objects to be released.
 *
 * timeout of 0xffffffff means 'wait forever';  0 means 'do not wait'.
 *
 * returns 0 on success, -1 on error (ie. even after 'timeout' there
 * were still objects in use)
 * -------------------------------------------------------------------- */
int32_t
vc_pool_destroy( VC_POOL_T *pool, uint32_t timeout )
{
   int i;
   vcos_assert( pool->magic == POOL_MAGIC );

   // wait for all objects to become free
   for (;;) {
      lock_pool( pool );
      if ( pool->allocated == 0 )
         break;
      unlock_pool( pool );
      if ( wait_event(pool,timeout) )
         return -1; // timed out
   }

   if ( pool->mem != MEM_INVALID_HANDLE ) {

      // just a single memory object to free
      mem_release( pool->mem );

   } else {

      // release individual pool entries back to mempool
      for (i=0; i<pool->nobjects; i++)
         mem_release( pool->object[i].mem );

   }

   // remove from the global list
   rtos_latch_get(&pool_list_latch);
   VC_POOL_T **pp = &vc_pool_list;
   while (*pp != pool)
   {
      pp = &((*pp)->next);
   }
   vcos_assert(*pp);
   *pp = pool->next;
   rtos_latch_put(&pool_list_latch);


   // kill the pool struct
   pool->magic = 0;
   destroy_event( pool );
   rtos_priorityfree( pool );
   return 0;
}
void fade_out_shadows(struct t_event *eventer)
{
	if (eventer->destroy)
	{
		if (eventer->data != NULL)
			free(eventer->data);
		return;
	}
	float * shadow_intensity = (float*)(eventer->data);
	(*shadow_intensity) -= 0.05f;
	if (*shadow_intensity < -1.0f){
		destroy_event(eventer);
		return;
	}
	set_shadow_intensity(abs(*shadow_intensity));
	eventer->time_to_run += NEXT_CYCLE * 1;
}
HRESULT
bCap_Close_Server(int *pfd)
{
  int index, *sock;
  struct CONN_BCAP_SERVER *bcap_param;
  struct CONN_PARAM_COMMON *device;

  if(pfd == NULL)
    return E_HANDLE;

  index = *pfd;

  bcap_param = check_address(index);
  if(bcap_param == NULL)
    return E_HANDLE;

  device = &bcap_param->device;
  sock = &device->sock;

  /* Ends main thread */
  set_event(&bcap_param->term_main_evt);
  exit_thread(bcap_param->main_thread);

  /* Destroys event */
  destroy_event(&bcap_param->term_main_evt);

  /* Closes connection */
  device->dn_close(sock);

  /* Releases argument */
  if(device->arg != NULL) {
    free(device->arg);
    device->arg = NULL;
  }

  /* Resets connection parameters */
  memset(bcap_param, 0, sizeof(struct CONN_BCAP_SERVER));

  *pfd = 0;

  return S_OK;
}
示例#8
0
struct event *create_event(const struct kevent *ke)
{
    struct event *event = NULL;
    const struct event_ops *ops;

    switch(ke->filter) {
    case EVFILT_READ:
        if(is_devd_sock(ke->ident))
            ops = &event_mount_ops;
        else
            ops = &event_ipc_ops;
        break;
    case EVFILT_TIMER:
        if(ke->ident == 0xc0ffee) {
            ops = &event_unmount_ops;
            break;
        }
    default:
        log_fatal(LOG_COLOR_FATAL, "Unknown event type (ident=%#x filter=%hd)", ke->ident, ke->filter);
    }

    event = (struct event *)malloc(ops->size);
    if(event == NULL)
        goto error;

    memset(event, 0, ops->size);

    event->ops = ops;

    if(event->ops->create != NULL)
        if(event->ops->create(event, ke) == -1)
            goto error;

    return event;

error:
    if(event)
        destroy_event(event);

    return NULL;
}
示例#9
0
/*
 * Create a ust_registry_event from the given parameters and add it to the
 * registry hash table. If event_id is valid, it is set with the newly created
 * event id.
 *
 * On success, return 0 else a negative value. The created event MUST be unique
 * so on duplicate entry -EINVAL is returned. On error, event_id is untouched.
 *
 * Should be called with session registry mutex held.
 */
int ust_registry_create_event(struct ust_registry_session *session,
		uint64_t chan_key, int session_objd, int channel_objd, char *name,
		char *sig, size_t nr_fields, struct ustctl_field *fields,
		int loglevel_value, char *model_emf_uri, int buffer_type,
		uint32_t *event_id_p, struct ust_app *app)
{
	int ret;
	uint32_t event_id;
	struct cds_lfht_node *nptr;
	struct ust_registry_event *event = NULL;
	struct ust_registry_channel *chan;

	assert(session);
	assert(name);
	assert(sig);
	assert(event_id_p);

	rcu_read_lock();

	/*
	 * This should not happen but since it comes from the UST tracer, an
	 * external party, don't assert and simply validate values.
	 */
	if (session_objd < 0 || channel_objd < 0) {
		ret = -EINVAL;
		goto error_free;
	}

	chan = ust_registry_channel_find(session, chan_key);
	if (!chan) {
		ret = -EINVAL;
		goto error_free;
	}

	/* Check if we've reached the maximum possible id. */
	if (ust_registry_is_max_id(chan->used_event_id)) {
		ret = -ENOENT;
		goto error_free;
	}

	event = alloc_event(session_objd, channel_objd, name, sig, nr_fields,
			fields, loglevel_value, model_emf_uri, app);
	if (!event) {
		ret = -ENOMEM;
		goto error_free;
	}

	DBG3("UST registry creating event with event: %s, sig: %s, id: %u, "
			"chan_objd: %u, sess_objd: %u, chan_id: %u", event->name,
			event->signature, event->id, event->channel_objd,
			event->session_objd, chan->chan_id);

	/*
	 * This is an add unique with a custom match function for event. The node
	 * are matched using the event name and signature.
	 */
	nptr = cds_lfht_add_unique(chan->ht->ht, chan->ht->hash_fct(event,
				lttng_ht_seed), chan->ht->match_fct, event, &event->node.node);
	if (nptr != &event->node.node) {
		if (buffer_type == LTTNG_BUFFER_PER_UID) {
			/*
			 * This is normal, we just have to send the event id of the
			 * returned node and make sure we destroy the previously allocated
			 * event object.
			 */
			destroy_event(event);
			event = caa_container_of(nptr, struct ust_registry_event,
					node.node);
			assert(event);
			event_id = event->id;
		} else {
/**
 * @fn        THRET recv_thread(void *arg)
 * @brief     The receiving thread.
 * @param[in] arg The argument of receiving thread: CONN_BCAP_SERVER.
 */
static THRET THTYPE
recv_thread(void *arg)
{
#if !defined(THRET)
  THRET ret = (THRET)NULL;
#endif

  uint16_t i;
  HRESULT hr;
  struct CONN_BCAP_SERVER *bcap_param = (struct CONN_BCAP_SERVER *) arg;
  struct BCAP_PACKET *send_packet = &bcap_param->last_send, *recv_packet =
      &bcap_param->last_recv;
  struct VEC_OBJECT *pObj = NULL;
  VARIANT vnt_send, vntTmp;
  CALL_FUNC_BCAP func;

  /* Sets last send packet */
  send_packet->args = &vnt_send;
  VariantInit(&vnt_send);

  /* Creates terminal event for sub thread */
  hr = create_event(&bcap_param->term_sub_evt, 1, 0);
  if(FAILED(hr)) goto exit_proc;

  /* Creates events for synchronizing threads */
  hr = create_event(&bcap_param->exec_evt, 0, 0);
  if(FAILED(hr)) goto exit_proc;

  hr = create_event(&bcap_param->comp_evt, 1, 1);
  if(FAILED(hr)) goto exit_proc;

  /* Begins sub thread */
  begin_thread(&bcap_param->sub_thread, &exec_thread, arg);

  while(1) {
    hr = wait_event(&bcap_param->term_main_evt, 0);
    if(SUCCEEDED(hr)) {
      break;
    }

    hr = receive_execute(bcap_param);
    if(FAILED(hr) && hr != E_TIMEOUT) {
      break;
    }
  }

exit_proc:
  /* Ends sub thread */
  set_event(&bcap_param->term_sub_evt);
  exit_thread(bcap_param->sub_thread);

  /* Destroys events */
  destroy_event(&bcap_param->term_sub_evt);
  destroy_event(&bcap_param->exec_evt);
  destroy_event(&bcap_param->comp_evt);

  /* Clears last send packet */
  VariantClear(&vnt_send);

  /* Clears last received packet */
  if(recv_packet->args != NULL) {
    for(i = 0; i < recv_packet->argc; i++) {
      VariantClear(&recv_packet->args[i]);
    }
    free(recv_packet->args);
  }

  /* Release all of vector elements */
  VariantInit(&vntTmp);
  vntTmp.vt = VT_I4;

  while(1) {
    hr = pop_vector(bcap_param, &pObj, 0);
    if(FAILED(hr)) break;

    func = m_list_func[pObj->id];
    if(func != NULL) {
      vntTmp.lVal = pObj->hObj;
      func(&vntTmp, ((pObj->id == ID_SERVICE_STOP) ? 0 : 1), &vnt_send);
      VariantClear(&vnt_send);
    }

    free(pObj);
  }

  VariantClear(&vntTmp);

  /* Destroys self */
  change_relation(bcap_param, DESTROY_SELF, NULL);

#if !defined(THRET)
  return ret;
#endif
}
/**
 * @fn            HRESULT change_relation(struct CONN_BCAP_SERVER *own, int mode, int *sock)
 * @brief         Changes thread's relationships for TCP connection.
 * @param[in,out] own The connection parameter to be changed.
 * @param[in]     mode The change mode.
 * @param[in]     sock The client connection.
 */
static HRESULT
change_relation(struct CONN_BCAP_SERVER *own, int mode, int *sock)
{
  HRESULT hr = S_OK;
  struct CONN_BCAP_SERVER *node, *tmp;
  int flag_mutex;
  MUTEX *mutex;

  /* Checks mutex object */
  mutex = own->relation_mutex;
  flag_mutex = ((mutex != NULL) ? 1 : 0);

  /* Locks mutex and must not returns this function without end of one. */
  if(flag_mutex) {
    hr = lock_mutex(mutex, INFINITE);
    if(FAILED(hr)) return hr;
  }

  switch(mode) {
    case ADD_CHILD:
      /* Root node only */
      if(own->parent == NULL) {
        /* Creates new node */
        node = (struct CONN_BCAP_SERVER *) malloc(
            sizeof(struct CONN_BCAP_SERVER));
        if(node == NULL) {
          hr = E_OUTOFMEMORY;
          goto exit_proc;
        }

        /* Initializes node memory */
        memset(node, 0, sizeof(struct CONN_BCAP_SERVER));

        /* Copies device parameters from parent node */
        node->device = own->device;

        /* Sets child socket */
        node->device.sock = *sock;

        /* Copies parameters */
        node->exec_timeout = own->exec_timeout;
        node->wdt_interval = own->wdt_interval;

        /* Sets last modified */
        node->last_modified = gettimeofday_msec();

        /* Copies mutex */
        node->relation_mutex = own->relation_mutex;

        /* Sets parent node */
        node->parent = own;
        own->num_child++;

        /* Replaces the head of children */
        tmp = own->node1;
        own->node1 = node;
        node->node2 = tmp;
        if(tmp != NULL) tmp->node1 = node;

        if(flag_mutex) {
          hr = node->device.dn_set_timeout(*sock, node->device.timeout);
          if(FAILED(hr)) {
            node->device.dn_close(sock);
            free(node);
            goto exit_proc;
          }

          /* Creates terminal event for main thread */
          hr = create_event(&node->term_main_evt, 1, 0);
          if(FAILED(hr)) {
            node->device.dn_close(sock);
            free(node);
            goto exit_proc;
          }

          /* Begins child thread */
          begin_thread(&node->main_thread, &recv_thread, node);
        } else {
          switch(node->device.type) {
            case CONN_UDP:
              node->device.arg = malloc(sizeof(struct sockaddr_in));
              memcpy(node->device.arg, own->device.arg, sizeof(struct sockaddr_in));
              break;
            default:
              break;
          }

          node->last_send.args =
              (VARIANT*) malloc(sizeof(VARIANT));
          VariantInit(node->last_send.args);
        }
      }
      break;

    case DELETE_CHILD:
      /* Root node only */
      if(own->parent == NULL) {
        node = own->node2;
        while (node != NULL) {
          tmp = node->node2;

          if(flag_mutex) {
            /* Ends child thread */
            set_event(&node->term_main_evt);
            exit_thread(node->main_thread);

            /* Destroys event */
            destroy_event(&node->term_main_evt);

            /* Closes connection */
            node->device.dn_close(&node->device.sock);
          } else {
            VariantClear(node->last_send.args);
            free(node->last_send.args);

            if(node->device.arg != NULL) {
                free(node->device.arg);
            }
          }

          free(node);
          node = tmp;

          own->num_child--;
        }
        own->node2 = NULL;
      }
      break;

    case DESTROY_SELF:
      if(own->parent != NULL) {
        /* Removes own node from the children list */
        tmp = own->node1;

        if(tmp == NULL) { // If own node is the youngest children
          own->parent->node1 = own->node2;
        } else {
          tmp->node2 = own->node2;
        }

        if(own->node2 != NULL) { // If own node is not the oldest children
          own->node2->node1 = tmp;
        }

        /* Adds own node to the parent's delete */
        tmp = own->parent->node2;
        own->parent->node2 = own;
        own->node2 = tmp;
        if(tmp != NULL) tmp->node1 = own;
      } else {
        if(!flag_mutex) {
            node = own->node1;
            while(node != NULL) {
              tmp = node->node2;
              change_relation(node, DESTROY_SELF, NULL);
              node = tmp;
            }
            change_relation(own, DELETE_CHILD, NULL);
        }
      }
      break;

    default:
      hr = E_INVALIDARG;
      break;
  }

exit_proc:
  if(flag_mutex) {
    unlock_mutex(mutex);
  }

  return hr;
}
HRESULT
bCap_Open_Server(const char *connect, uint32_t timeout, int *pfd)
{
  int type, index, *sock;
  HRESULT hr;
  void *conn_param;
  struct CONN_PARAM_ETH eth_param =
    { 0, 0, htonl(INADDR_ANY), htons(5007) };
  struct CONN_PARAM_COM com_param =
    { 1, 38400, NOPARITY, 8, ONESTOPBIT, 0 };
  struct CONN_BCAP_SERVER *bcap_param;
  struct CONN_PARAM_COMMON *device;
  struct sockaddr_in *paddr;

  if(connect == NULL || pfd == NULL)
    return E_INVALIDARG;

  type = parse_conn_type(connect);

  index = find_open_address(type);
  if(index == 0)
    return E_MAX_OBJECT;

  bcap_param = &m_conn_param[index - 1];
  device = &bcap_param->device;

  /* Initializes connection parameters */
  device->type = type;
  switch(device->type) {
    case CONN_TCP:
      hr = parse_conn_param_ether(connect, &eth_param);
      conn_param = &eth_param;
      device->arg = NULL;
      device->dn_open = &tcp_open_server;
      device->dn_close = &tcp_close;
      device->dn_send = &tcp_send;
      device->dn_recv = &tcp_recv;
      device->dn_set_timeout = &tcp_set_timeout;
      break;
    case CONN_UDP:
      hr = parse_conn_param_ether(connect, &eth_param);
      conn_param = &eth_param;
      paddr = (struct sockaddr_in *) malloc(sizeof(struct sockaddr_in));
      if(paddr == NULL) {
        hr = E_OUTOFMEMORY;
        break;
      }
      paddr->sin_addr.s_addr = eth_param.dst_addr;
      paddr->sin_port = eth_param.dst_port;
      paddr->sin_family = AF_INET;
      device->arg = (void *) paddr;
      device->dn_open = &udp_open;
      device->dn_close = &udp_close;
      device->dn_send = &udp_send;
      device->dn_recv = &udp_recv;
      device->dn_set_timeout = &udp_set_timeout;
      break;
    case CONN_COM:
      hr = parse_conn_param_serial(connect, &com_param);
      conn_param = &com_param;
      device->arg = NULL;
      device->dn_open = &com_open;
      device->dn_close = &com_close;
      device->dn_send = &com_send;
      device->dn_recv = &com_recv;
      device->dn_set_timeout = &com_set_timeout;
      break;
    default:
      hr = E_INVALIDARG;
      break;
  }

  if(FAILED(hr)) {
    if(device->arg != NULL) {
      free(device->arg);
      device->arg = NULL;
    }
    memset(bcap_param, 0, sizeof(struct CONN_BCAP_SERVER));
    return hr;
  }

  /* Create terminal event for main thread */
  hr = create_event(&bcap_param->term_main_evt, 1, 0);
  if(FAILED(hr)) {
    if(device->arg != NULL) {
      free(device->arg);
      device->arg = NULL;
    }
    memset(bcap_param, 0, sizeof(struct CONN_BCAP_SERVER));
    return hr;
  }

  /* Opens connection */
  sock = &device->sock;
  hr = device->dn_open(conn_param, sock);
  if(FAILED(hr)) {
    destroy_event(&bcap_param->term_main_evt);
    if(device->arg != NULL) {
      free(device->arg);
      device->arg = NULL;
    }
    memset(bcap_param, 0, sizeof(struct CONN_BCAP_SERVER));
    return hr;
  }

  hr = device->dn_set_timeout(*sock, timeout);
  if(FAILED(hr)) {
    bCap_Close_Server(&index);
    return hr;
  }

  /* Sets parameters */
  device->timeout = timeout;
  bcap_param->exec_timeout   = INIT_EXEC_TIMEOUT;
  bcap_param->wdt_interval   = INIT_WDT_INTERVAL;

  /* Begins main thread */
  if(device->type == CONN_TCP) {
    begin_thread(&bcap_param->main_thread, &accept_thread, bcap_param);
  } else {
    begin_thread(&bcap_param->main_thread, &recv_thread, bcap_param);
  }

  *pfd = index;

  return S_OK;
}
示例#13
0
double run_laundry(laundry l){
    event current, e;
    double t;

    /*Acumulador del tiempo en funcionamiento*/
    double trunning=0;
    
    int broken = 0, waiting = 0;

    /*Se inicializa el sistema*/
    l = init_laundry(l);
    while(broken <= (l->s)){
        /*Mientras la cantidad de maquinas rotas sea menor
        o igual a la cantidad de repuestos, el sistema puede
        funcionar.       
        */
        current = next_event(l->events_list);
        trunning += get_time(current);
        
        if(get_type(current) == MACHINE_BROKEN){
            /*El evento es una maquina rota, entonces
            se genera un nuevo tiempo de falla para uno
            los repuestos que entra en funcionamiento.
            */
            t = exponential(l->rg, 1./(l->tfail));
            e = create_event(t, MACHINE_BROKEN);
            l->events_list = insert_event(l->events_list, e);
            broken++;

            if(broken <= (l->o)){
                /*Si hay un operario libre, entonces se genera
                un nuevo tiempo de reparacion para la maquina que
                entra al taller.                
                */
                t = exponential(l->rg, 1./(l->trepair));
                e = create_event(t, MACHINE_REPAIRED);
                l->events_list = insert_event(l->events_list, e);
            }
            else{
                /*Si todos los operarios estan ocupados, la maquina
                queda en espera.
                */
                waiting++;
            }
        }
        else{
            /*El evento es una maquina reparada, entonces hay una maquina
            rota menos. Ademas se acaba de liberar un operario, entonces si hay
            maquinas en espera de reparacion, se genera un nuevo tiempo de 
            reparacion.
            */
            broken--;
            if(waiting > 0){
                waiting--;
                t = exponential(l->rg, 1./(l->trepair));
                e = create_event(t, MACHINE_REPAIRED);
                l->events_list = insert_event(l->events_list, e);
            }
        }
        destroy_event(current);
    }
    
    return trunning;
}