/* If the dataset name isn't in the list already, add it. */ int socket_recordgraph(struct list_t *graphlist, const char *dataset) { char *graphstr; int graphinlist; struct list_elem_t *graphelem; char *tmpstr; graphinlist = 0; /* Add the graph name to the list of available graphs */ for(graphelem = graphlist->start; graphelem != NULL; graphelem = graphelem->next) { graphstr = graphelem->data; /* Graph is already in list */ if(strcmp(graphstr, dataset) == 0) { graphinlist = 1; return 1; } } /* If the graph wasn't in the list, add it. */ if(!graphinlist) { tmpstr = malloc(strlen(dataset)); strcpy(tmpstr, dataset); list_add_after(graphlist, NULL, tmpstr); } return 0; }
// insert_vma_struct -insert vma in mm's list link void insert_vma_struct(struct mm_struct *mm, struct vma_struct *vma) { assert(vma->vm_start < vma->vm_end); list_entry_t *list = &(mm->mmap_list); list_entry_t *le_prev = list, *le_next; list_entry_t *le = list; while ((le = list_next(le)) != list) { struct vma_struct *mmap_prev = le2vma(le, list_link); if (mmap_prev->vm_start > vma->vm_start) { break; } le_prev = le; } le_next = list_next(le_prev); /* check overlap */ if (le_prev != list) { check_vma_overlap(le2vma(le_prev, list_link), vma); } if (le_next != list) { check_vma_overlap(vma, le2vma(le_next, list_link)); } vma->vm_mm = mm; list_add_after(le_prev, &(vma->list_link)); mm->map_count ++; }
SegMan * Get_free_seg() { assert(!list_empty(&free_seg)); ListHead *new_seg = free_seg.next; list_del(new_seg); list_add_after(&used_seg, new_seg); return list_entry(new_seg, SegMan, list); }
static void nice_enqueue(struct runQueue *rq, struct ProcStruct *proc) { printf("nice_enqueue!\n\n"); assert(list_empty(&(proc->runLink))); list_add_after(&(rq->run_list), &(proc->runLink)); proc->rq = rq; rq->proc_num++; }
void list_ins_sort(Elem *a) { Elem *b, *c; for (b = a->next->next; b != a; b = b->next) { for (c = b->prev; c != a && b->v < c->v; c = c->prev); list_delete(b); list_add_after(c, b); } }
void wakeup(PCB *p){ /* printk("%d\n", list_size(&ready));*/ if(!list_exist(&ready, &p->list)) { if(list_exist(&block, &p->list)) { list_del(&p->list); list_add_after(&ready, &p->list); } } }
/* * (3)_fifo_map_swappable: According FIFO PRA, we should link the most recent arrival page at the back of pra_list_head qeueue */ static int _fifo_map_swappable(struct mm_struct *mm, uintptr_t addr, struct Page *page, int swap_in) { list_entry_t *head=(list_entry_t*) mm->sm_priv; list_entry_t *entry=&(page->pra_page_link); assert(entry != NULL && head != NULL); //record the page access situlation /*LAB3 EXERCISE 2: 2012012017*/ //(1)link the most recent arrival page at the back of the pra_list_head qeueue. list_add_after(head, entry); return 0; }
void enterProcQ(bool stall, PCB* pcb, ListHead* q) { ListHead* i; if(stall) { if (stalllen==100) panic("Stall Queue Overflowed!\n"); i=q->prev; for (; stallpool[stalltail].va; stalltail++, stalltail%=100); stallpool[stalltail].va=1; stallpool[stalltail].pcb=pcb; list_add_after(i, &stallpool[stalltail].li); stalllen++; } else { if (readylen==100) panic("Ready Queue Overflowed!\n"); i=q->prev; for (; readypool[readytail].va; readytail++, readytail%=100); readypool[readytail].va=1; readypool[readytail].pcb=pcb; list_add_after(i, &readypool[readytail].li); readylen++; } }
// insert_vma_struct -insert vma in mm's rb tree link & list link void insert_vma_struct(struct mm_struct *mm, struct vma_struct *vma) { assert(vma->vm_start < vma->vm_end); list_entry_t *list = &(mm->mmap_list); list_entry_t *le_prev = list, *le_next; if (mm->mmap_tree != NULL) { struct vma_struct *mmap_prev; insert_vma_rb(mm->mmap_tree, vma, &mmap_prev); if (mmap_prev != NULL) { le_prev = &(mmap_prev->list_link); } } else { list_entry_t *le = list; while ((le = list_next(le)) != list) { struct vma_struct *mmap_prev = le2vma(le, list_link); if (mmap_prev->vm_start > vma->vm_start) { break; } le_prev = le; } } le_next = list_next(le_prev); /* check overlap */ if (le_prev != list) { check_vma_overlap(le2vma(le_prev, list_link), vma); } if (le_next != list) { check_vma_overlap(vma, le2vma(le_next, list_link)); } vma->vm_mm = mm; list_add_after(le_prev, &(vma->list_link)); mm->map_count ++; if (mm->mmap_tree == NULL && mm->map_count >= RB_MIN_MAP_COUNT) { /* try to build red-black tree now, but may fail. */ mm->mmap_tree = rb_tree_create(vma_compare); if (mm->mmap_tree != NULL) { list_entry_t *list = &(mm->mmap_list), *le = list; while ((le = list_next(le)) != list) { insert_vma_rb(mm->mmap_tree, le2vma(le, list_link), NULL); } } } }
int ipc_sem_init(int value) { assert(current->sem_queue != NULL); sem_undo_t *semu; if ((semu = semu_create(NULL, value)) == NULL) { return -E_NO_MEM; } sem_queue_t *sem_queue = current->sem_queue; down(&(sem_queue->sem)); list_add_after(&(sem_queue->semu_list), &(semu->semu_link)); up(&(sem_queue->sem)); return sem2semid(semu->sem); }
void sleep(void){ ListHead *tmp; /* list_foreach(tmp, ¤t->list) { printk("bcurrent: %x\n", tmp); }*/ tmp=¤t->list; list_del(¤t->list); /* list_foreach(tmp, ¤t->list) { printk("acurrent: %x\n", tmp); }*/ list_add_after(&block, tmp); wait_intr(); /*asm volatile("int $0x80"); */ }
static sem_undo_t * semu_list_search(list_entry_t *list, sem_t sem_id) { if (VALID_SEMID(sem_id)) { semaphore_t *sem = semid2sem(sem_id); list_entry_t *le = list; while ((le = list_next(le)) != list) { sem_undo_t *semu = le2semu(le, semu_link); if (semu->sem == sem) { list_del(le); list_add_after(list, le); return semu; } } } return NULL; }
void free_funct_entry_struct(hal_funct_entry_t * funct_entry) { hal_funct_t *funct; if (funct_entry->funct_ptr > 0) { /* entry points to a function, update the function struct */ funct = SHMPTR(funct_entry->funct_ptr); funct->users--; } /* clear contents of struct */ funct_entry->funct_ptr = 0; funct_entry->arg = 0; funct_entry->funct.l = 0; /* add it to free list */ list_add_after((hal_list_t *) funct_entry, &(hal_data->funct_entry_free)); }
static void insert_waiting_thread(struct mutex *m, struct thread *t) { struct thread *thread; if (m->waiting) { list_for_every_entry(&m->waiting_threads, thread, struct thread, event_node) { #ifdef CONFIG_SCHEDULE_ROUND_ROBIN if (!list_next(&m->waiting_threads, &thread->event_node)) { list_add_after(&thread->event_node, &t->event_node); break; } #elif defined(CONFIG_SCHEDULE_PRIORITY) if (t->priority > thread->priority) list_add_before(&thread->event_node, &t->event_node); #endif } } else {
int main() { long int i, n; Elem *list = list_create_elem(), *y, *p; scanf("%ld", &n); for (i = 0; i < n; i++) { y = list_create_elem(); scanf("%d", &(y->v)); list_add_after(list, y); } list_ins_sort(list); for (y = list->next; y != list;) { printf("%d ", y->v); p = y->next; free(y); y = p; } free(list); return 0; }
/* Recieves 16 byte buffers which will be freed upon return */ int sockets_readdoneh(struct graphhost_t *inst, uint8_t *inbuf, size_t bufsize, struct list_t *list, struct list_elem_t *elem) { struct socketconn_t *conn = elem->data; char *buf; char *graphstr; char *tmpstr; struct list_elem_t *clelem; inbuf[15] = 0; graphstr = ((char *)inbuf)+1; switch(inbuf[0]) { case 'c': /* Start sending data for the graph specified by graphstr. */ buf = malloc(strlen(graphstr)); strcpy(buf, graphstr); list_add_after(conn->datasets, NULL, buf); break; case 'd': /* Stop sending data for the graph specified by graphstr. */ for(clelem = inst->connlist->start; clelem != NULL; clelem = clelem->next) { tmpstr = clelem->data; if(strcmp(tmpstr, graphstr) == 0) { list_delete(conn->datasets, clelem); free(tmpstr); break; } } break; case 'l': /* If this fails, we just ignore it. There's really nothing we can do about it right now. */ sockets_sendlist(inst, list, elem); } return 0; }
/** * Enqueue a route on a kernel add/chg/del queue. */ static void olsr_enqueue_rt(struct list_node *head_node, struct rt_entry *rt) { const struct rt_nexthop *nh; /* if this node is already on some changelist we are done */ if (list_node_on_list(&rt->rt_change_node)) { return; } /* * For easier route dependency tracking we enqueue nexthop routes * at the head of the queue and non-nexthop routes at the tail of the queue. */ nh = olsr_get_nh(rt); if (ipequal(&rt->rt_dst.prefix, &nh->gateway)) { list_add_after(head_node, &rt->rt_change_node); } else { list_add_before(head_node, &rt->rt_change_node); } }
static int ipc_sem_find_or_init_with_address(uintptr_t addr, int value, int create) { assert(pls_read(current)->sem_queue != NULL); sem_queue_t *sem_queue = pls_read(current)->sem_queue; down(&(sem_queue->sem)); sem_t sem_id = semu_search_with_addr(&(sem_queue->semu_list), addr); up(&(sem_queue->sem)); if(sem_id != -1) return sem_id; if(!create) return -E_NO_MEM; sem_undo_t *semu; if((semu = semu_create_with_address(NULL, addr, value)) == NULL) { return -E_NO_MEM; } down(&(sem_queue->sem)); list_add_after(&(sem_queue->semu_list), &(semu->semu_link)); up(&(sem_queue->sem)); return sem2semid(semu->sem); }
static void insert_shmn(struct shmem_struct *shmem, shmn_t *shmn) { list_entry_t *list = &(shmem->shmn_list), *le = list; list_entry_t *le_prev = list, *le_next; while ((le = list_next(le)) != list) { shmn_t *shmn_prev = le2shmn(le, list_link); if (shmn_prev->start > shmn->start) { break; } le_prev = le; } le_next = list_next(le_prev); /* check overlap */ if (le_prev != list) { check_shmn_overlap(le2shmn(le_prev, list_link), shmn); } if (le_next != list) { check_shmn_overlap(shmn, le2shmn(le_next, list_link)); } list_add_after(le_prev, &(shmn->list_link)); }
/** * Walk through the timer list and check if any timer is ready to fire. * Callback the provided function with the context pointer. */ static void walk_timers(uint32_t * last_run) { unsigned int total_timers_walked = 0, total_timers_fired = 0; unsigned int wheel_slot_walks = 0; /* * Check the required wheel slots since the last time a timer walk was invoked, * or check *all* the wheel slots, whatever is less work. * The latter is meant as a safety belt if the scheduler falls behind. */ while ((*last_run <= now_times) && (wheel_slot_walks < TIMER_WHEEL_SLOTS)) { struct list_node tmp_head_node; /* keep some statistics */ unsigned int timers_walked = 0, timers_fired = 0; /* Get the hash slot for this clocktick */ struct list_node *const timer_head_node = &timer_wheel[*last_run & TIMER_WHEEL_MASK]; /* Walk all entries hanging off this hash bucket. We treat this basically as a stack * so that we always know if and where the next element is. */ list_head_init(&tmp_head_node); while (!list_is_empty(timer_head_node)) { /* the top element */ struct list_node *const timer_node = timer_head_node->next; struct timer_entry *const timer = list2timer(timer_node); /* * Dequeue and insert to a temporary list. * We do this to avoid loosing our walking context when * multiple timers fire. */ list_remove(timer_node); list_add_after(&tmp_head_node, timer_node); timers_walked++; /* Ready to fire ? */ if (TIMED_OUT(timer->timer_clock)) { OLSR_PRINTF(7, "TIMER: fire %s timer %p, ctx %p, " "at clocktick %u (%s)\n", timer->timer_cookie->ci_name, timer, timer->timer_cb_context, (unsigned int)*last_run, olsr_wallclock_string()); /* This timer is expired, call into the provided callback function */ timer->timer_cb(timer->timer_cb_context); /* Only act on actually running timers */ if (timer->timer_flags & OLSR_TIMER_RUNNING) { /* * Don't restart the periodic timer if the callback function has * stopped the timer. */ if (timer->timer_period) { /* For periodical timers, rehash the random number and restart */ timer->timer_random = random(); olsr_change_timer(timer, timer->timer_period, timer->timer_jitter_pct, OLSR_TIMER_PERIODIC); } else { /* Singleshot timers are stopped */ olsr_stop_timer(timer); } } timers_fired++; } } /* * Now merge the temporary list back to the old bucket. */ list_merge(timer_head_node, &tmp_head_node); /* keep some statistics */ total_timers_walked += timers_walked; total_timers_fired += timers_fired; /* Increment the time slot and wheel slot walk iteration */ (*last_run)++; wheel_slot_walks++; } OLSR_PRINTF(7, "TIMER: processed %4u/%d clockwheel slots, " "timers walked %4u/%u, timers fired %u\n", wheel_slot_walks, TIMER_WHEEL_SLOTS, total_timers_walked, timer_mem_cookie->ci_usage, total_timers_fired); /* * If the scheduler has slipped and we have walked all wheel slots, * reset the last timer run. */ *last_run = now_times; }
int hal_add_funct_to_thread(const char *funct_name, const char *thread_name, int position) { hal_funct_t *funct; hal_list_t *list_root, *list_entry; int n; hal_funct_entry_t *funct_entry; CHECK_HALDATA(); CHECK_LOCK(HAL_LOCK_CONFIG); CHECK_STR(funct_name); CHECK_STR(thread_name); HALDBG("adding function '%s' to thread '%s'", funct_name, thread_name); { hal_thread_t *thread __attribute__((cleanup(halpr_autorelease_mutex))); /* get mutex before accessing data structures */ rtapi_mutex_get(&(hal_data->mutex)); /* make sure position is valid */ if (position == 0) { /* zero is not allowed */ HALERR("bad position: 0"); return -EINVAL; } /* search function list for the function */ funct = halpr_find_funct_by_name(funct_name); if (funct == 0) { HALERR("function '%s' not found", funct_name); return -EINVAL; } // type-check the functions which go onto threads switch (funct->type) { case FS_LEGACY_THREADFUNC: case FS_XTHREADFUNC: break; default: HALERR("cant add type %d function '%s' " "to a thread", funct->type, funct_name); return -EINVAL; } /* found the function, is it available? */ if ((funct->users > 0) && (funct->reentrant == 0)) { HALERR("function '%s' may only be added " "to one thread", funct_name); return -EINVAL; } /* search thread list for thread_name */ thread = halpr_find_thread_by_name(thread_name); if (thread == 0) { /* thread not found */ HALERR("thread '%s' not found", thread_name); return -EINVAL; } #if 0 /* ok, we have thread and function, are they compatible? */ if ((funct->uses_fp) && (!thread->uses_fp)) { HALERR("function '%s' needs FP", funct_name); return -EINVAL; } #endif /* find insertion point */ list_root = &(thread->funct_list); list_entry = list_root; n = 0; if (position > 0) { /* insertion is relative to start of list */ while (++n < position) { /* move further into list */ list_entry = list_next(list_entry); if (list_entry == list_root) { /* reached end of list */ HALERR("position '%d' is too high", position); return -EINVAL; } } } else { /* insertion is relative to end of list */ while (--n > position) { /* move further into list */ list_entry = list_prev(list_entry); if (list_entry == list_root) { /* reached end of list */ HALERR("position '%d' is too low", position); return -EINVAL; } } /* want to insert before list_entry, so back up one more step */ list_entry = list_prev(list_entry); } /* allocate a funct entry structure */ funct_entry = alloc_funct_entry_struct(); if (funct_entry == 0) NOMEM("thread->function link"); /* init struct contents */ funct_entry->funct_ptr = SHMOFF(funct); funct_entry->arg = funct->arg; funct_entry->funct.l = funct->funct.l; funct_entry->type = funct->type; /* add the entry to the list */ list_add_after((hal_list_t *) funct_entry, list_entry); /* update the function usage count */ funct->users++; } return 0; }
void Free_seg(SegMan *val) { list_del(&val->list); list_add_after(&free_seg, &val->list); }
void list_add_first(struct list *l, struct link *e) { list_add_after(&l->head, e); }
void schedule(void) { /* implement process/thread schedule here */ //PCB *last = current; //PCB *next = NULL; //printk("schedule\n"); /* if(current->state == TASK_BLOCKED) { list_add_after(block.prev,¤t->head); current = NULL; }else if(current->state != TASK_DEAD && current != &idle) { list_add_after(ready.prev,¤t->head); } else if(current->state == TASK_DEAD) { list_add_after(free.prev,¤t->head); current->state = TASK_EMPTY; } if(ready.next == &ready) current = &idle; else { current = list_entry(ready.next,PCB,head); list_del(¤t->head); } */ //new scheduling here //printk("new scheduler, pid = %d\n",current->pid); //printk("new scheduler, state=%d\n",current->state); if(current == &idle) { current = NULL; }else if(current->state == TASK_DEAD) { list_add_after(free.prev,¤t->head); printk("task_dead, pid = %d\n",current->pid); current->state = TASK_EMPTY; current = NULL; }else if(current->state == TASK_BLOCKED) { list_add_after(block.prev,¤t->head); current = NULL; }else if(current == &idle) { current->state = TASK_READY; current = NULL; }else { //current->state == TASK_RUNNING // printk("state = %d\n",current->state); if (current->state != TASK_RUNNING) { printk("state = %x\n",current->state); } assert(current->state == TASK_RUNNING); current->counter -= 1; if(current->counter == 0) { current->state = TASK_READY; current->counter = MAX_TIME_SLOT; list_add_after(ready.prev,¤t->head); current = NULL; }else //no scheduling, just return return ; } if(ready.next == &ready) current = &idle; else { current = list_entry(ready.next,PCB,head); list_del(¤t->head); } current->state = TASK_RUNNING; if(current->cr3.val == 0) { write_cr3(get_kcr3()); //kernel page dir }else { write_cr3(¤t->cr3); } uint32_t esp0 = (uint32_t)current->tf; esp0 += sizeof(struct TrapFrame); set_tss_esp0(esp0); //printk("after new scheduler, pid = %d\n",current->pid); // printk("after new scheduler, state=%d\n",current->state); }
/* add element at the head of the list */ void list_add_head( struct list *list, struct list *elem ) { list_add_after( list, elem ); }
void sockets_accept(struct list_t* connlist, int listenfd) { int new_fd; #ifdef __VXWORKS__ int clilen; #else unsigned int clilen; #endif struct socketconn_t* conn; struct sockaddr_in cli_addr; int error; #ifdef __VXWORKS__ int on; #endif #ifndef __VXWORKS__ int flags; #endif clilen = sizeof(struct sockaddr_in); /* Accept a new connection */ new_fd = accept(listenfd, (struct sockaddr*)&cli_addr, &clilen); /* Make sure that the file descriptor is valid */ if (new_fd < 1) { perror(""); return; } #ifdef __VXWORKS__ /* Set the socket non-blocking. */ on = 1; error = ioctl(new_fd, (int)FIONBIO, on); if (error == -1) { perror(""); close(new_fd); return; } #else /* Set the socket non-blocking. */ flags = fcntl(new_fd, F_GETFL, 0); if (flags == -1) { perror(""); close(new_fd); return; } error = fcntl(new_fd, F_SETFL, flags | O_NONBLOCK); if (error == -1) { perror(""); close(new_fd); return; } #endif conn = malloc(sizeof(struct socketconn_t)); conn->fd = new_fd; conn->selectflags = SOCKET_READ | SOCKET_ERROR; conn->datasets = list_create(); conn->writequeue = queue_init(20); conn->writebuf = NULL; conn->writebuflength = 0; conn->writebufoffset = 0; conn->readbuf = NULL; conn->readbuflength = 0; conn->readbufoffset = 0; conn->orphan = 0; /* Add it to the list, this makes it a bit non-thread-safe */ conn->elem = list_add_after(connlist, NULL, conn); return; }