コード例 #1
0
ファイル: lift_prog.c プロジェクト: joka90/RealTime-Labs
/* The shall be one task for each person. All person tasks shall be implemented by the same C function, called passenger_task. */
void passenger_task(void)//TODO
{
	//printf("blaba\n");

	int id;
	int length;
	int send_task_id;
	
	int current;
	int from;
	int to;
	
	int arrived;
    int n_travels=0;

    message_data_type msg;

    /* receive id */ 
    si_message_receive((char *) &id, &length, &send_task_id);//TODO Ska vi skicka via usertask först? Sedan ändra till att ta emot msg

	printf("Person %d spawned as task %d \n", id, id_to_task_id(id));

	/* message för att be om hisstur */
	message_data_type travel_msg;
	travel_msg.type = TRAVEL_MESSAGE;
	travel_msg.id = id;
	
	current = random_level();	

	while (n_travels < 2)
    {
	
	
		arrived = 0;
		from = current;
        to = random_level();
		
	    travel_msg.from_floor = from;
    	travel_msg.to_floor = to;
		
		si_message_send((char *) &travel_msg, sizeof(travel_msg), Lift_Task_Id);

		while(!arrived)
		{
			si_message_receive((char *) &msg, &length, &send_task_id);
			
			if(msg.type == TRAVEL_DONE_MESSAGE)
			{
				arrived = 1;
			}
		}
		
		
		current = to;

		n_travels++;
		si_wait_n_ms(TIME_TO_NEW_JOURNEY);
    }
}
コード例 #2
0
ファイル: skiplist.c プロジェクト: tiancaiamao/datastruct
void skiplist_insert(struct skiplist* s, int key) {
    struct node *p,*q,*cur[32];
    int i;
    volatile int level;

    q = s->head;
    for(i=s->level; i>=0; i--) {
        p = q->next[i];
        while(p && p->key < key) {
            q = p;
            p = p->next[i];
        }
        cur[i] = q;
    }
    level = random_level();
    if(level > s->level) {
        s->level++;
        level = s->level;
        cur[level] = s->head;
    }
    p = node_create(key,level);
    for(i=level; i>=0; i--) {
        p->next[i] = cur[i]->next[i];
        cur[i]->next[i] = p;
    }
    s->length++;
}
コード例 #3
0
ファイル: skiplist.c プロジェクト: xudifsd/lab
void skiplist_insert(struct skiplist_node *header, int value) {
    static struct skiplist_node *should_update[MAX_LEVEL];

    if (header == NULL)
        return;

    struct skiplist_node *p = header;
    int level = header->value; //header use this field to store current level
    int i;

    for (i = level; i >= 0; i--) {
        while (p->forward[i] && p->forward[i]->value < value)
            p = p->forward[i];
        should_update[i] = p;
    }
    p = p->forward[0];

    if (p == NULL || p->value != value) {
        int l = random_level();

        if (l > level) {
            for (i = level + 1; i < l + 1; i++)
                should_update[i] = header;
            level = header->value = l;
        }

        struct skiplist_node *new_node = alloc_node(l + 1);
        new_node->value = value;

        for (i = 0; i < l + 1; i++) {
            new_node->forward[i] = should_update[i]->forward[i];
            should_update[i]->forward[i] = new_node;
        }
    }
}
コード例 #4
0
ファイル: skip_list.c プロジェクト: scrime/SAS-External
static inline void
skip_list_insert (skip_list_t sl, void * data)
{
  skip_list_cell_t slc;
  int level;
  int i;

  if (skip_list_search (sl, data) != NULL)
    /* libsas specific: allow partials with same frequencies, so don't
       return. */
    ;

  level = random_level ();

  if (sl->level < level)
    {
      for (i = sl->level; i < level; i++)
	sl->update[i] = sl->header;
      sl->level = level;
    }

  slc = skip_list_cell_make (sl, level, data);

  slc->prev = sl->update[0]->fwps[0]->prev;
  sl->update[0]->fwps[0]->prev = slc;

  for (i = 0; i < level; i++)
    {
      slc->fwps[i] = sl->update[i]->fwps[i];
      sl->update[i]->fwps[i] = slc;
    }
}
コード例 #5
0
ファイル: skip_list.c プロジェクト: yshui/cgrouper
void insert(SkipSet* ss, int value) {
	int i;
	SkipNode* x = ss->header;
	SkipNode* update[MAX_LEVEL + 1];
	memset(update, 0, MAX_LEVEL + 1);

	for(i = ss->level; i >= 0; i--) {
		while(x->forward[i] != NULL && x->forward[i]->value < value) {
			x = x->forward[i];
		}
		update[i] = x;
	}
	x = x->forward[0];

	if(x == NULL || x->value != value) {
		int lvl = random_level();

		if(lvl > ss->level) {
			for(i = ss->level + 1; i <= lvl; i++) {
				update[i] = ss->header;
			}
			ss->level = lvl;
		}
		x = make_node(lvl, value);
		for(i = 0; i <= lvl; i++) {
			x->forward[i] = update[i]->forward[i];
			update[i]->forward[i] = x;
		}
	}
}
コード例 #6
0
ファイル: lift_prog.c プロジェクト: joka90/RealTime-Labs
/* The shall be one task for each person. All person tasks shall be implemented by the same C function, called passenger_task. */
void passenger_task(void)
{
	//printf("blaba\n");

	int id;
	int length;
	int send_task_id;
	
	int current;
	int from;
	int to;

    /* receive id */ 
    si_message_receive((char *) &id, &length, &send_task_id);
	
	current = random_level();	

	while (1)
    {
    	from = current;
    	to = random_level();
    	
    	
    	si_sem_wait(&mainlift->mutex);
        enter_floor(mainlift,id, current);//spawna
    	si_sem_signal(&mainlift->mutex);

    	printf("Passenger %d starting journey from %d to %d.\n", id, from, to);
    	
    	lift_travel(mainlift, id, from, to);//res
    	current = to;

    	printf("Passenger %d arrived at %d.\n", id, current);
    	
    	si_sem_wait(&mainlift->mutex);

        leave_floor(mainlift, id, current);//ta bort från våning

    	si_sem_signal(&mainlift->mutex);
    	
    	si_wait_n_ms(TIME_TO_NEW_JOURNEY);
    
    }
}
コード例 #7
0
ファイル: skip-list.c プロジェクト: armenr/algorist
/*an array of pointers to the nodes that must be updated. */
void insert_skipnode(skipset* ss, const void* key, int key_sz, 
  int (*compare_keys)(const void* key1, const void* key2)) {
  
  if(contains_skipnode(ss, key, compare_keys) != NULL) /*no duplicates allowed in set*/
    return;

  int l = random_level(); /*level of new node*/

  if(l > ss->level) 
    ss->level = l;  
  
  insert_node(ss->header, make_skipnode(ss, l, key, key_sz), l, compare_keys);
}
コード例 #8
0
ファイル: skip.c プロジェクト: enjoylife/DataMonkey
//TODO: Switch to a more robust error handling policy
extern int skip_insert(skip_t sl, unsigned int key, void *payload)
{
    int i, rand_level;
    skip_node *x, *update[SKIP_MAX];

    check_hard(sl, "Invalid Skip List");
    check_hard(key, "Invalid Key");
    check_hard(payload, " Empty payload");

    x = sl->header;
    for (i = sl->level - 1; i >= 0; i--) {
        while (x->level[i].forward && // not end NULL
                default_compare(x->level[i].forward->key, key)) {
            x = x->level[i].forward;
            //   log_warn("still in whleLoop");
        }
        update[i] = x;
        sl->finger[i] = x;
        //log_warn("still in ForLoop");
    }

    // make sure were not nulled
    x = (x->level[0].forward) ? x->level[0].forward : x;
    //TODO: Decide if we should warn, or reject an overwrite,
    //log_info(" %u,  %u", x->key, key);
    if (x->key == key) {
        sl->free_func(x->payload);
        x->payload = payload;
        //log_warn("Key overwrite");
        return 2;
    } else {
        rand_level = random_level();
        if (rand_level > sl->level) {
            for (i = sl->level; i < rand_level; i++) {
                update[i] = sl->header;
                sl->finger[i] = sl->header;
            }
            sl->level = rand_level;
            //log_info("Lvl: %d",sl->level);
        }
        x = create_skip_node(rand_level, key, payload);
        for (i = 0 ; i < rand_level; i++) {
            x->level[i].forward = update[i]->level[i].forward;
            update[i]->level[i].forward = x;
        }
        //log_success("inserted key %u and data", key);
        sl->length++;
        return 1;
    }
}
コード例 #9
0
ファイル: vec.c プロジェクト: NguyenNhatNam/salad
void vec_set(vec_t* const vec, const dim_t dim, const float value)
{
	assert(vec != NULL);

	if (value == 0.0)
	{
		delete(vec, dim);
		return;
	}

	int i;
	vec_elem_t* x = vec->header;
	vec_elem_t* update[MAX_LEVEL + 1];
	memset(update, 0, MAX_LEVEL + 1);

	for (i = vec->level; i >= 0; i--)
	{
		while (x->forward[i] != NULL && x->forward[i]->dim < dim)
		{
			x = x->forward[i];
		}
		update[i] = x;
	}
	x = x->forward[0];

	if (x == NULL || x->dim != dim)
	{
		int lvl = random_level();

		if (lvl > vec->level)
		{
			for (i = vec->level + 1; i <= lvl; i++)
			{
				update[i] = vec->header;
			}
			vec->level = lvl;
		}
		x = make_node(lvl, dim, value);
		for (i = 0; i <= lvl; i++)
		{
			x->forward[i] = update[i]->forward[i];
			update[i]->forward[i] = x;
		}
	}
	else // update existing element
	{
		x->value = value;
	}
}
コード例 #10
0
ファイル: skp.c プロジェクト: KonstantinosKr/solfec
/* key: item key
 * data: item data
 * return: skip item or NULL when out of memory;
 * insert data item */
ITEM* LIST_Insert (LIST *list, void *key, void *data)
{
  ITEM *header, **update, *x;
  int maxlevel, level, i, n;
  LIST_Compare compare;

  maxlevel = list->maxlevel;
  compare = list->compare;
  header = &list->header;
  update = list->update;
  level = list->level;
  x = header;

  for (i = level; i >= 0; i --)
  {
    if (compare) while (x->forward [i] && compare (x->forward [i]->key, key) < 0) x = x->forward [i];
    else while (x->forward [i] && x->forward [i]->key < key) x = x->forward [i];

    update [i] = x;
  }

  x = x->forward [0];

  if (x && ((compare && compare (x->key, key) == 0) || ((!compare) && x->key == key))) x->data = data;
  else
  {
    n = random_level (maxlevel);

    if (n > level)
    {
      for (i = level + 1; i <= maxlevel; i ++) update [i] = header;

      list->level = n;
    }

    if(!(x = skip_item (list, key, data))) return NULL;

    for (i = 0; i <= n; i ++)
    {
      x->forward [i] = update [i]->forward [i];
      update [i]->forward [i] = x;
    }

    list->size ++;
  }

  return x;
}
コード例 #11
0
ファイル: skip.c プロジェクト: kratorius/ads
/* Set a the key `key` to value `value` */
void
skip_set(skiplist *list, int key, int value) {
  skipitem *item = list->header;
  skipitem *update[MAX_LEVELS];
  int i, level = random_level();

  if (skip_is_empty(list)) {
    for (i = 0; i < list->level; i++) update[i] = list->header;
  } else {
    for (i = list->level - 1; i >= 0; i--) {
      while ((item->next[i] != NULL) && (item->next[i]->key < key)) {
        item = item->next[i];
      }
  
      /* Keep a list of the rightmost pointers to the various levels
       * before the key we're searching for (we need this list to
       * link the newly inserted item)
       */
      update[i] = item;
    }
  
    item = item->next[0];

    /* the key already exists, just update its value */
    if (item && (item->key == key)) {
      item->value = value;
      return;
    }
  }
  
  /* if the new item's level is higher than the current list level
   * just update the level by one
   */
  if (level > list->level) {
    update[list->level] = list->header;
    list->level++;
    level = list->level;
  }

  /* create the item and update the pointers */
  item = skipitem_new(key, value, level);
  for (i = 0; i < level; i++) {
    item->next[i] = update[i]->next[i];
    update[i]->next[i] = item;
  }
}
コード例 #12
0
ファイル: vlmap.c プロジェクト: Preetam/vlmap
static vlnode_t*
vlmap_create_node(uint64_t version, uint8_t* key, int keylength, uint8_t* value, int valuelength) {
	int level = random_level();
	vlnode_t* n = (vlnode_t*)calloc(1, sizeof(vlnode_t)+(level+1)*sizeof(vlnode_t*)+keylength+valuelength);
	n->keylength = keylength;
	n->valuelength = valuelength;
	n->created = version;
	n->level = level;

	n->next = (vlnode_t**)(n+sizeof(vlnode_t));
	n->key = (uint8_t*)(n+sizeof(vlnode_t)+(level+1)*sizeof(vlnode_t*));
	memcpy(n->key, key, keylength);

	if(value != NULL) {
		n->value = n->key+keylength;
		memcpy(n->value, value, valuelength);
	}
	return n;
}
コード例 #13
0
ファイル: ds_olist.c プロジェクト: gwtony/wsocket
static olist_node_t *olist_add_entry_unlocked(olist_t *ol, void *data)
{
	olist_node_t *update[OLIST_MAXLEVEL], *pos, *new_node;
	int i, level;

	pos = &ol->dumb;
	for (i=ol->level-1; i>=0; i--) {
		/* new data insert to front of equal elements */
		while (	(pos->level[i].next != &ol->dumb) &&
				(ol->cmp(pos->level[i].next->data, data)<0)) {
			pos = pos->level[i].next;
		}
		update[i] = pos;
	}

	level = random_level();
	if (level > ol->level) {
		for (i = ol->level; i < level; i++) {
			update[i] = &ol->dumb;
		}
		ol->level = level;
	}

	new_node = malloc(sizeof(*new_node));
	new_node->maxlevel = level;
	new_node->data = data;
	for (i = 0; i < OLIST_MAXLEVEL; i++) {
		//fprintf(stderr, "init level %d\n", i);
		if (i<level) {
			new_node->level[i].next = update[i]->level[i].next;
			new_node->level[i].prev = update[i];
			update[i]->level[i].next->level[i].prev = new_node;
			update[i]->level[i].next = new_node;
		} else {
			new_node->level[i].next = NULL;
			new_node->level[i].prev = NULL;
		}
	}

	ol->length++;

	return new_node;
}
コード例 #14
0
static void caml_insert_global_root(struct global_root_list * rootlist,
                                    value * r)
{
  struct global_root * update[NUM_LEVELS];
  struct global_root * e, * f;
  int i, new_level;

  /* Init "cursor" to list head */
  e = (struct global_root *) rootlist;
  /* Find place to insert new node */
  for (i = rootlist->level; i >= 0; i--) {
    while (1) {
      f = e->forward[i];
      if (f == NULL || f->root >= r) break;
      e = f;
    }
    update[i] = e;
  }
  e = e->forward[0];
  /* If already present, don't do anything */
  if (e != NULL && e->root == r) return;
  /* Insert additional element, updating list level if necessary */
  new_level = random_level();
  if (new_level > rootlist->level) {
    for (i = rootlist->level + 1; i <= new_level; i++)
      update[i] = (struct global_root *) rootlist;
    rootlist->level = new_level;
  }
  e = caml_stat_alloc(sizeof(struct global_root) +
                      new_level * sizeof(struct global_root *));
  e->root = r;
  for (i = 0; i <= new_level; i++) {
    e->forward[i] = update[i]->forward[i];
    update[i]->forward[i] = e;
  }
}
コード例 #15
0
void insert(SkipSet *ss, int value) {
	int i;
	SkipNode *x = ss->header;
	SkipNode *update[MAX_LEVEL+1];
	memset(update, 0, MAX_LEVEL+1);
	for(i=ss->level; i>=0; i--) {
		while(x->forward[i]!=NULL && x->forward[i]->value < value) {
			x = x->forward[i];
		}
		update[i] = x;
	}
//	int j;
//	for(j=0; j<ss->level+1; j++)
//		printf("%d ", update[j]->value);
//	printf("\n");
	x = x->forward[0];
	if(x == NULL || x->value != value) {
		int lvl = random_level();
		if(lvl > ss->level) {
			for(i=ss->level+1; i<=lvl; i++) {
				update[i] = ss->header;
			}
			ss->level = lvl;
		}
		x = make_node(lvl, value);
		for(i=0; i<=lvl; i++) {
			x->forward[i] = update[i]->forward[i];
			update[i]->forward[i] = x;
		}
	}
	val[value] = 1;
//	for(j=0; j<ss->level+1; j++)
//		printf("%d ", update[j]->value);
//	printf("\n");
//	print_skipset(ss);
}
コード例 #16
0
/*
 * Insert cache into the list
 */
static FcBool
FcCacheInsert (FcCache *cache, struct stat *cache_stat)
{
    FcCacheSkip    **update[FC_CACHE_MAX_LEVEL];
    FcCacheSkip    *s, **next;
    int		    i, level;

    lock_cache ();

    /*
     * Find links along each chain
     */
    next = fcCacheChains;
    for (i = fcCacheMaxLevel; --i >= 0; )
    {
	for (; (s = next[i]); next = s->next)
	    if (s->cache > cache)
		break;
        update[i] = &next[i];
    }

    /*
     * Create new list element
     */
    level = random_level ();
    if (level > fcCacheMaxLevel)
    {
	level = fcCacheMaxLevel + 1;
	update[fcCacheMaxLevel] = &fcCacheChains[fcCacheMaxLevel];
	fcCacheMaxLevel = level;
    }

    s = malloc (sizeof (FcCacheSkip) + (level - 1) * sizeof (FcCacheSkip *));
    if (!s)
	return FcFalse;

    s->cache = cache;
    s->size = cache->size;
    FcRefInit (&s->ref, 1);
    if (cache_stat)
    {
	s->cache_dev = cache_stat->st_dev;
	s->cache_ino = cache_stat->st_ino;
	s->cache_mtime = cache_stat->st_mtime;
#ifdef HAVE_STRUCT_STAT_ST_MTIM
	s->cache_mtime_nano = cache_stat->st_mtim.tv_nsec;
#else
	s->cache_mtime_nano = 0;
#endif
    }
    else
    {
	s->cache_dev = 0;
	s->cache_ino = 0;
	s->cache_mtime = 0;
	s->cache_mtime_nano = 0;
    }

    /*
     * Insert into all fcCacheChains
     */
    for (i = 0; i < level; i++)
    {
	s->next[i] = *update[i];
	*update[i] = s;
    }

    unlock_cache ();
    return FcTrue;
}
コード例 #17
0
ファイル: cskiplist.c プロジェクト: HPCToolkit/hpctoolkit
bool
cskl_insert(cskiplist_t *cskl, void *value,
	mem_alloc m_alloc)
{
  int max_height = cskl->max_height;
  int my_height  = random_level(cskl->max_height);

  csklnode_t *preds[max_height];
  csklnode_t *succs[max_height];
  mcs_node_t mcs_nodes[max_height];

  for (;;) {
	// Acquire pfq-rwlock before reading:
	pfq_rwlock_start_read(&cskl->pfq_lock);

	int found_layer= cskiplist_find_helper(cskl->compare,
		cskl, value, preds, succs,
		cskiplist_find_full);

	if (found_layer != NO_LAYER) {
	  csklnode_t *node = succs[found_layer];
	  if (!node->marked) {
		while (!node->fully_linked);
		// Release pfq-rwlock before returning:
		pfq_rwlock_end_read(&cskl->pfq_lock);
		return false;
	  }
	  // node is marked for deletion by some other thread, so this thread needs
	  // to try to insert again by going to the end of this for(;;) loop. As
	  // this thread tries to insert again, there may be another the thread
	  // that succeeds in inserting value before this thread gets a chance to.

	  // Release pfq-rwlock before trying to insert again:
	  pfq_rwlock_end_read(&cskl->pfq_lock);

	  continue;
	}


	//--------------------------------------------------------------------------
	// Acquire a lock on node's predecessor at each layer.
	//
	// valid condition:
	//   no predecessor is marked
	//   no successor is marked
	//   each of my predecessors still points to the successor I recorded
	//
	// If the valid condition is not satisfied, unlock all of my predecessors
	// and retry.
	//--------------------------------------------------------------------------
	int highestLocked = -1;
	csklnode_t *pred, *succ;
	csklnode_t *prevPred = NULL;
	bool valid = true;
	for (int layer = 0; valid && (layer < my_height); layer++) {
	  pred=preds[layer];
	  succ=succs[layer];
	  if (pred != prevPred) {
		mcs_lock(&pred->lock, &mcs_nodes[layer]);
		highestLocked = layer;
		prevPred = pred;
	  }
	  valid = !pred->marked && !succ->marked && pred->nexts[layer] == succ;
	}
	if (!valid) {
	  // unlock each of my predecessors, as necessary
	  unlock_preds(preds, mcs_nodes, highestLocked);

	  // Release pfq-rwlock before trying to insert again:
	  pfq_rwlock_end_read(&cskl->pfq_lock);
	  continue;
	}

	// allocate my node
//	csklnode_t *node = csklnode_new(value, my_height, max_height, m_alloc);
	csklnode_t *node = csklnode_malloc(value, my_height, max_height, m_alloc);

	// link it in at levels [0 .. my_height-1]
	for (int layer = 0; layer < my_height; layer++) {
	  node->nexts[layer] = succs[layer];
	  preds[layer]->nexts[layer] = node;
	}

	// mark the node as usable
	node->fully_linked = true;

	// unlock each of my predecessors, as necessary
	unlock_preds(preds, mcs_nodes, highestLocked);

	// Release pfq-rwlock before returning:
	pfq_rwlock_end_read(&cskl->pfq_lock);

	return true;
  }
}
コード例 #18
0
ファイル: intset.c プロジェクト: digideskio/tinystm
static int set_add(intset_t *set, val_t val, thread_data_t *td)
{
  int result, i;
  node_t *update[MAX_LEVEL + 1];
  node_t *node, *next;
  level_t level, l;
  val_t v;

# ifdef DEBUG
  printf("++> set_add(%d)\n", val);
  IO_FLUSH;
# endif

  if (!td) {
    node = set->head;
    for (i = set->level; i >= 0; i--) {
      next = node->forward[i];
      while (next->val < val) {
        node = next;
        next = node->forward[i];
      }
      update[i] = node;
    }
    node = node->forward[0];

    if (node->val == val) {
      result = 0;
    } else {
      l = random_level(set, main_seed);
      if (l > set->level) {
        for (i = set->level + 1; i <= l; i++)
          update[i] = set->head;
        set->level = l;
      }
      node = new_node(val, l, 0);
      for (i = 0; i <= l; i++) {
        node->forward[i] = update[i]->forward[i];
        update[i]->forward[i] = node;
      }
      result = 1;
    }
  } else {
    TM_START(1, RW);
    v = VAL_MIN; /* Avoid compiler warning (should not be necessary) */
    node = set->head;
    level = TM_LOAD(&set->level);
    for (i = level; i >= 0; i--) {
      next = (node_t *)TM_LOAD(&node->forward[i]);
      while (1) {
        v = TM_LOAD(&next->val);
        if (v >= val)
          break;
        node = next;
        next = (node_t *)TM_LOAD(&node->forward[i]);
      }
      update[i] = node;
    }

    if (v == val) {
      result = 0;
    } else {
      l = random_level(set, td->seed);
      if (l > level) {
        for (i = level + 1; i <= l; i++)
          update[i] = set->head;
        TM_STORE(&set->level, l);
      }
      node = new_node(val, l, 1);
      for (i = 0; i <= l; i++) {
        node->forward[i] = (node_t *)TM_LOAD(&update[i]->forward[i]);
        TM_STORE(&update[i]->forward[i], node);
      }
      result = 1;
    }
    TM_COMMIT;
  }

  return result;
}