static
void
initiateMating()
{
	if (mateLock==NULL) {
		mateLock = lock_create("mateLock");
		if (mateLock == NULL) {
			panic("mateLock: lock_create failed\n");
		}
	}
	if (male_go==NULL) {
		male_go = cv_create("male_go");
		if (male_go == NULL) {
			panic("male_go: cv_create failed\n");
		}
	}
	if (female_go==NULL) {
		female_go = cv_create("female_go");
		if (female_go == NULL) {
			panic("female_go: cv_create failed\n");
		}
	}
	if (matchmaker_go==NULL) {
		matchmaker_go = cv_create("matchmaker_go");
		if (matchmaker_go == NULL) {
			panic("matchmaker_go: cv_create failed\n");
		}
	}
	if (doneSem==NULL) {
		doneSem = sem_create("doneSem", 0);
		if (doneSem == NULL) {
			panic("doneSem: sem_create failed\n");
		}
	}
}
void whalemating_init() {
  maleCv = cv_create("male");
  femaleCv = cv_create("female");
  matchMakerCv = cv_create("matchMaker");
  whaleLock = lock_create("whaleLock");
  return;
}
Esempio n. 3
0
static
void
inititems(void)
{
	if (BrSem==NULL) {
		BrSem = sem_create("BrSem", 3);
		if (BrSem == NULL) {
			panic("BrSem: sem_create failed\n");
		}
	}
	if (doneSem==NULL) {
		doneSem = sem_create("doneSem", 0);
		if (doneSem == NULL) {
			panic("synchtest: sem_create failed\n");
		}
	}
	if (BrLock==NULL) {
		BrLock = lock_create("BrLock");
		if (BrLock == NULL) {
			panic("BrLock: lock_create failed\n");
		}
	}
	if (boyCV==NULL) {
		boyCV = cv_create("boyCV");
		if (boyCV == NULL) {
			panic("boyCV: cv_create failed\n");
		}
	}
	if (girlCV==NULL) {
		girlCV = cv_create("girlCV");
		if (girlCV == NULL) {
			panic("girlCV: cv_create failed\n");
		}
	}
}
Esempio n. 4
0
void whalemating_init() {
  wm_lock = lock_create("whales");

  wm_mcv = cv_create("males");
  wm_fcv = cv_create("females");
  wm_mmcv = cv_create("matchmakers");
}
Esempio n. 5
0
/* 
 * The simulation driver will call this function once before starting
 * the simulation
 *
 * You can use it to initialize synchronization and other variables.
 * 
 */
void
intersection_sync_init(void)
{
  /* replace this default implementation with your own implementation */

  // intersectionSem = sem_create("intersectionSem",1);
  intersection_lock = lock_create("intersectionlock"); 
  
  next_lights = q_create(4);

  // // Direction x = north;
  // n = south;
  // Direction *x_p = &n;
  // // Direction x1 = south;
  // Direction *x1_p = &n;
  // // Direction x2 = east;
  // Direction *x2_p = &n;

  north_dir = north;
  south_dir = south;
  east_dir = east;
  west_dir = west;
  num_vehicles_waiting_north = 0;
  num_vehicles_waiting_south = 0;
  num_vehicles_waiting_east = 0;
  num_vehicles_waiting_west = 0;
  num_vehicles_waiting_b = 0;
  // Direction *x3_p = &n;
  
  // q_addtail(next_lights, x_p);
  // q_addtail(next_lights, x1_p);
  // q_addtail(next_lights, x2_p);
  // q_addtail(next_lights, x3_p);
  // current_light = q_peek(next_lights);
  // n = north;
  // current_light = &n;
  //*num_vehicles_waiting = 0;
  is_waiting = false;
  cv_south_go = cv_create("cv_south");
  cv_north_go = cv_create("cv_north");
  cv_east_go = cv_create("cv_east");
  cv_west_go = cv_create("cv_west");
  // if (intersectionSem == NULL) {
  //   panic("could not create intersection semaphore");
  // }

  if (intersection_lock == NULL) {
    panic("could not create intersection lock");
  }

  if (cv_south_go == NULL || cv_north_go == NULL || cv_east_go == NULL || cv_west_go == NULL) {
    panic("could not create intersection cv lock");
  }

  return;
}
Esempio n. 6
0
void pid_bootstrap(){
	int s = splhigh();

	//kprintf("bootstraping pid\n");
	int i;
	errsuperflag = 0;

	curthread->ppid = kernelpid;
	pid_array[kernelpid] = curthread;

	exit_array[kernelpid] = 0;
	interest_array[kernelpid]=0;
	parent_array[kernelpid]=kernelpid;

	for(i=1;i<MAX_PIDS;i++){
		pid_array[i] = NULL;
		exit_array[i] = 1234;
		interest_array[i]=-1;
		parent_array[i]=-1;
		wait_array[i]=cv_create("cv");
		wait_lock[i]=lock_create("lock");
	}
	splx(s);
	return;
}
Esempio n. 7
0
void
test_cv_broadcast()
{

	long i;
        int result;

	unintr_printf("starting cv broadcast test\n");
	unintr_printf("threads should print out in reverse order\n");

	testcv_broadcast = cv_create();
	testlock = lock_create();
	done = 0;
	testval1 = NTHREADS - 1;
	for (i = 0; i < NTHREADS; i++) {
		result = thread_create((void (*)(void *))
				       test_cv_broadcast_thread, (void *)i);
		assert(thread_ret_ok(result));
	}

	while (__sync_fetch_and_add(&done, 0) < NTHREADS) {
		/* this requires thread_yield to be working correctly */
		thread_yield(THREAD_ANY);
	}

	assert(interrupts_enabled());
	cv_destroy(testcv_broadcast);
	assert(interrupts_enabled());	
	unintr_printf("cv broadcast test done\n");
}
Esempio n. 8
0
/* 
 * The CatMouse simulation will call this function once before any cat or
 * mouse tries to each.
 *
 * You can use it to initialize synchronization and other variables.
 * 
 * parameters: the number of bowls
 */
void
catmouse_sync_init(int bowls)
{
  /* replace this default implementation with your own implementation of catmouse_sync_init */
  /* This is the default implementation
  (void)bowls; // keep the compiler from complaining about unused parameters
  globalCatMouseSem = sem_create("globalCatMouseSem",1);
  if (globalCatMouseSem == NULL) {
    panic("could not create global CatMouse synchronization semaphore");
  }
  return;
  */
  myLock = lock_create("myLock");
  KASSERT(myLock != NULL);
  lock_acquire(myLock);
  ava_bowl = kmalloc(bowls * sizeof(bool));
  KASSERT(ava_bowl != NULL);
  for(int i = 0; i < bowls; i++) {
     ava_bowl[i] = true;
  }
  num_cat_eating = 0;
  num_mouse_eating = 0;
//  wchan_cat = cv_create("cat");
//  wchan_mouse = cv_create("mouse");
  wchan_all = cv_create("all");
  lock_release(myLock);
  return;
}
Esempio n. 9
0
File: pid.c Progetto: gapry/os161
/*
 * Create a pidinfo structure for the specified pid.
 */
static
struct pidinfo *
pidinfo_create(pid_t pid, pid_t ppid)
{
	struct pidinfo *pi;

	KASSERT(pid != INVALID_PID);

	pi = kmalloc(sizeof(struct pidinfo));
	if (pi==NULL) {
		return NULL;
	}

	pi->pi_cv = cv_create("pidinfo cv");
	if (pi->pi_cv == NULL) {
		kfree(pi);
		return NULL;
	}

	pi->pi_pid = pid;
	pi->pi_ppid = ppid;
	pi->pi_exited = false;
	pi->pi_exitstatus = 0xbaad;  /* Recognizably invalid value */

	return pi;
}
Esempio n. 10
0
static
void
inititems(void)
{
	if (testsem==NULL) {
		testsem = sem_create("testsem", 2);
		if (testsem == NULL) {
			panic("synchtest: sem_create failed\n");
		}
	}
	if (testlock==NULL) {
		testlock = lock_create("testlock");
		if (testlock == NULL) {
			panic("synchtest: lock_create failed\n");
		}
	}
	if (testcv==NULL) {
		testcv = cv_create("testlock");
		if (testcv == NULL) {
			panic("synchtest: cv_create failed\n");
		}
	}
	if (donesem==NULL) {
		donesem = sem_create("donesem", 0);
		if (donesem == NULL) {
			panic("synchtest: sem_create failed\n");
		}
	}
}
Esempio n. 11
0
pid_t
newprocess(pid_t parent)
{
	struct process *newproc;
	pid_t newpid;
	struct cv *newcv;

	newproc = (struct process *) kmalloc(sizeof(struct process));
	if (newproc==NULL)
		return (pid_t) -ENOMEM;

	newcv = cv_create("childexit");
	if (newcv==NULL)
	{
		kfree(newproc);
		return (pid_t) -ENOMEM;
	}

	newproc->filetable = NULL;
	newproc->parentpid = parent;
	newproc->childexit = newcv;
	newproc->exited = 0;

	lock_acquire(proctable_lock);

	newpid = array_getnum(proctable);	
	array_add(proctable, newproc);

	lock_release(proctable_lock);

	return newpid+1;
}
Esempio n. 12
0
void
initialize_pid(struct thread *thr,pid_t processid)
{
	if(curthread!=NULL)
		lock_acquire(pid_lock);
	struct process_control *p_array;

	p_array=kmalloc(sizeof(struct process_control));

	thr->t_pid=processid;

	p_array->parent_id=-1;
	p_array->childlist=NULL;
	p_array->exit_code=-1;
	p_array->exit_status=false;
	p_array->mythread=thr;
	p_array->waitstatus=false;
	p_array->process_sem = sem_create(thr->t_name,0);

	//Create the lock and CV
	p_array->process_lock=lock_create(thr->t_name);
	p_array->process_cv = cv_create(thr->t_name);

	//Copy back into the thread
	process_array[processid]=p_array;

	if(curthread!=NULL)
		lock_release(pid_lock);

}
Esempio n. 13
0
/* 
 * The simulation driver will call this function once before starting
 * the simulation
 *
 * You can use it to initialize synchronization and other variables.
 * 
 */
void
intersection_sync_init(void)
{
  /* replace this default implementation with your own implementation */
	
	inservice=0;
	wait_total=0;
  intersectionSem = sem_create("intersectionSem",1);
  if (intersectionSem == NULL) {
    panic("could not create intersection semaphore");
  }
  mutex=lock_create("intersection lock");
  if(mutex==NULL)
  {
  	panic("could not create intersection lock");
  }
  int i;
  for(i=0;i<4;i++)
  {
  	CV[i]=cv_create("condition varaible");
  	if(CV[i]==NULL)
  	{
  		panic("could not create condition variable");
  	}
  }
  return;
}
Esempio n. 14
0
void
daemon_init(void)
{
	int err;
	char *daemon_name;

	if (!USE_DAEMON) {
		return;
	}

	daemon_name = kstrdup("writeback daemon:");
	if (daemon_name == NULL) {
		panic("daemon_init: could not launch thread");
	}

	daemon.d_cv = cv_create("daemon cv");
	if (daemon.d_cv == NULL) {
		panic("daemon_init: could not launch thread");
	}

	daemon.d_lock = lock_create("daemon lock");
	if (daemon.d_lock == NULL) {
		panic("daemon_init: could not launch thread");
	}

	daemon.d_awake = true;

	err = thread_fork(daemon_name, NULL, daemon_thread, NULL, 0);
	if (err) {
		panic("daemon_init: could not launch thread");
	}
}
Esempio n. 15
0
static struct ipc_port *
ipc_port_alloc(void)
{
	struct ipc_port *ipcp;
	struct task *task;
	int error;

	task = current_task();

	ipcp = pool_allocate(&ipc_port_pool);
	mutex_init(&ipcp->ipcp_mutex, "IPC Port", MUTEX_FLAG_DEFAULT);
	ipcp->ipcp_cv = cv_create(&ipcp->ipcp_mutex);
	ipcp->ipcp_port = IPC_PORT_UNKNOWN;
	ipcp->ipcp_flags = IPC_PORT_FLAG_NEW;
	TAILQ_INIT(&ipcp->ipcp_msgs);
	BTREE_NODE_INIT(&ipcp->ipcp_link);
	BTREE_ROOT_INIT(&ipcp->ipcp_rights);

	/*
	 * Insert a receive right.
	 */
	error = ipc_port_right_insert(ipcp, task, IPC_PORT_RIGHT_RECEIVE);
	if (error != 0)
		panic("%s: ipc_port_right_insert failed: %m", __func__,
		      error);


	return (ipcp);
}
Esempio n. 16
0
void whalemating_init() {
	male_lock = lock_create("male");
	female_lock = lock_create("female");
	match_maker_lock = lock_create("match_maker");
	thread_count_lock = lock_create("counter");
	thread_count_cv = cv_create("count_cv");
	return;
}
static void init(){
  if (cats == NULL){
    cats = cv_create("Cats");
  }
  if (mouse == NULL){
    mouse = cv_create("mouse");
  }
  if (catEatLock == NULL){
    catEatLock = lock_create("catEatLock");
  }
  if (mouseEatLock == NULL){
    mouseEatLock = lock_create("mouseEatLock");
  }
  if (bowlLock == NULL){
    bowlLock = lock_create("bowlLock");
  }
}
Esempio n. 18
0
void whalemating_init() {
 hold=lock_create("My lock");
 male_sem=sem_create("Male Semaphore",0);
 female_sem=sem_create("Female Semaphore",0);
mate_cv=cv_create("mating cv");
male_count=0;
female_count=0;
  return;
}
Esempio n. 19
0
int 
ldap_pvt_thread_cond_wait( ldap_pvt_thread_cond_t *cond, 
	ldap_pvt_thread_mutex_t *mutex )
{
	if ( ! cond->lcv_created ) {
		cv_create( &cond->lcv_cv, *mutex );
		cond->lcv_created = 1;
	}

	return( cv_wait( cond->lcv_cv ) );	
}
Esempio n. 20
0
static void
init_members(member *mems, int length, const char *name)
{
  struct lock *common_lock = lock_create(name);
  struct cv *common_cv = cv_create(name);
  for (int i = 0; i < length; i++)
  {
    mems[i].m_name = NULL;
    mems[i].m_cv = common_cv;
    mems[i].m_lock = common_lock;
  }
}
Esempio n. 21
0
File: synch.c Progetto: ghd213/OS161
struct rwlock * rwlock_create(const char *name)
{
  struct rwlock *rwlock;
  rwlock = kmalloc(sizeof(struct rwlock));
  if (rwlock == NULL) {
    return NULL;
  }

  rwlock->rwlock_name = kstrdup(name);
  if (rwlock->rwlock_name == NULL) {
    kfree(rwlock);
    return NULL;
  }

  rwlock->write_cv = cv_create(rwlock->rwlock_name);
  if (rwlock->write_cv == NULL)
	{
		cv_destroy(rwlock->write_cv);
		return NULL;
	}

  rwlock->read_cv = cv_create(rwlock->rwlock_name);
  if (rwlock->read_cv == NULL)
  {
    cv_destroy(rwlock->read_cv);
    return NULL;
  }

  rwlock->lk = lock_create(rwlock->rwlock_name);
  if (rwlock->lk == NULL)
  {
    lock_destroy(rwlock->lk);
    return NULL;
  }
  rwlock->hold_readers = 0;
  rwlock->is_writing = 0;
  rwlock->num_readers = 0;

  return rwlock;
}
Esempio n. 22
0
File: pid.c Progetto: gapry/os161
/*
 * Create a pidinfo structure for the specified pid.
 */
static
struct pidinfo *
pidinfo_create(pid_t pid, pid_t ppid)
{
	struct pidinfo *pi;

	KASSERT(pid != INVALID_PID);

	pi = kmalloc(sizeof(struct pidinfo));
	if (pi==NULL) {
		return NULL;
	}

	pi->pi_cv = cv_create("pidinfo cv");
	if (pi->pi_cv == NULL) {
		kfree(pi);
		return NULL;
	}
    
    /* Initialize pi_signal_cv. */
    pi->pi_signal_cv = cv_create("pidinfo signal cv");
	if (pi->pi_signal_cv == NULL) {
		kfree(pi);
		return NULL;
	}

	pi->pi_pid = pid;
	pi->pi_ppid = ppid;
	pi->pi_exited = false;
	pi->pi_exitstatus = 0xbaad;  /* Recognizably invalid value */
    pi->detached = false;
    pi->sigkill = false;
    pi->sigstop = false;
    pi->sigcont = false;
    pi->waitingthreads = 0;

	return pi;
}
Esempio n. 23
0
void stoplight_init() {
	intersect_cv=cv_create("Intersection Condition Variable");
	if(intersect_cv==NULL)
	{
		///return NULL;
		panic("condition variable not created");
	}
	intersect_lock=lock_create("A lock on the intersection");
	if(intersect_lock==NULL)
	{
		//return NULL;
		panic("Lock could not be created");
	}
	
	
  return;
}
Esempio n. 24
0
/*
 * Thread initialization.
 */
struct thread *
thread_bootstrap(void)
{
	struct thread 
	lock_exit = lock_create("lock_exit");
	cv_parent_queue = cv_create("parent_queue");

	/* Create the DAta structures we need. */
	sleepers = array_create();
	if (sleepers==NULL) {
		panic("Cannot create sleepers array\n");
	}

	zombies = array_create();
	if (zombies==NULL) {
		panic("Cannot create zombies array\n");
	}
	
	/*
	 * Create the thread structure for the first thread
	 * (the one that's already running)
	 */
	me = thread_create("<boot/menu>");
	if (me==NULL) {
		panic("thread_bootstrap: Out of memory\n");
	}

	/*
	 * Leave me->t_stack NULL. This means we're using the boot stack,
	 * which can't be freed.
	 */

	/* Initialize the first thread's pcb */
	md_initpcb0(&me->t_pcb);

	/* Set curthread */
	curthread = me;

	/* Number of threads starts at 1 */
	numthreads = 1;

	/* Done */
	return me;
}
Esempio n. 25
0
int
cvtest2(int nargs, char **args)
{
	unsigned i;
	int result;

	(void)nargs;
	(void)args;

	for (i=0; i<NCVS; i++) {
		testlocks[i] = lock_create("cvtest2 lock");
		testcvs[i] = cv_create("cvtest2 cv");
	}
	gatesem = sem_create("gatesem", 0);
	exitsem = sem_create("exitsem", 0);

	kprintf("cvtest2...\n");

	result = thread_fork("cvtest2", NULL, sleepthread, NULL, 0);
	if (result) {
		panic("cvtest2: thread_fork failed\n");
	}
	result = thread_fork("cvtest2", NULL, wakethread, NULL, 0);
	if (result) {
		panic("cvtest2: thread_fork failed\n");
	}

	P(exitsem);
	P(exitsem);

	sem_destroy(exitsem);
	sem_destroy(gatesem);
	exitsem = gatesem = NULL;
	for (i=0; i<NCVS; i++) {
		lock_destroy(testlocks[i]);
		cv_destroy(testcvs[i]);
		testlocks[i] = NULL;
		testcvs[i] = NULL;
	}

	kprintf("cvtest2 done\n");
	return 0;
}
Esempio n. 26
0
void whalemating_init() {

    //extern int male_head, male_tail, female_head, female_tail;
    //extern struct lock *pop_lock;
    //extern struct cv *maker_cv;

    male_head = male_tail = 0;
    female_head = female_tail = 0;

    pop_lock = lock_create("population");
    if(pop_lock == NULL) {
        panic("whalemating: lock failed\n");
    }

    maker_cv = cv_create("maker cv");
    if(maker_cv == NULL) {
        panic("whalemating: condition variable failed\n");
    }

    //return;
}
Esempio n. 27
0
struct kitchen *kitchen_create() {
    int i;

    // Create the top-level struct
    struct kitchen *k = kmalloc(sizeof(struct kitchen));

    if (k == NULL) {
        return NULL;
    }

    // Construct the bowl lock array
    k->bowl_locks = kmalloc(NumBowls * sizeof(struct lock *));
    if (k->bowl_locks == NULL) {
        kfree(k);
        return NULL;
    }

    // Construct each bowl lock
    for (i = 0; i < NumBowls; i++) {
        k->bowl_locks[i] = lock_create("bowl");
    }

    // Construct the entrance lock
    k->kitchen_lock = lock_create("enter");

    // Construct the kitchen cv
    k->kitchen_cv = cv_create("kitchen");

    // Construct the group queue
    k->group_list = q_create(1);

    // Initialize the current_creature flag
    k->current_creature = 2;

    // Initialize the counter
    k->creature_count = 0;

    return k;
}
Esempio n. 28
0
int
hungrybirds()
{
		
	int	n; // number of baby birds
	
	kprintf("Making mama bird...\n");
	// Create the mama condition variable
	mama = cv_create("mamacv");
	// Create the bowl lock
	bowl_lock = lock_create("bowl");
	
	// Create the mama bird
	kprintf("Forking mama bird...\n");
	thread_fork("Mama_thread", 0, 0, &mama_bird, NULL);	
	
	// Put some food in the bowl
	int f = random() % MAXFOOD;
	// Have at least some reasonable amount of starting food
	if (f < 3)
		f = 3;

	// Create a random number of baby birds
	n = random() % MAXBABIES;
	if (n < 2)	
		n = 2; // At least have a couple of babies!


	kprintf("Making %d babies...\n", n);
	int i;
	for (i = 0; i < n; i++)
	{
		kprintf("Made a baby bird.\n");
		thread_fork("Baby_thread", 0, 0, &baby_bird, NULL);
	}
	
	return 0;
}
Esempio n. 29
0
/*
 * Create a pidinfo structure for the specified pid.
 */
static
struct pidinfo *
pidinfo_create(pid_t pid, pid_t ppid)
{
	struct pidinfo *pi;
	struct pidinfo *ppi;

	if(ppid != INVALID_PID){
		ppi = pi_get(ppid);
		ppi->kids[ppi->kids_tail] = pid;
		ppi->kids_tail++;
	}

	KASSERT(pid != INVALID_PID);

	pi = kmalloc(sizeof(struct pidinfo));
	if (pi==NULL) {
		return NULL;
	}

	pi->pi_cv = cv_create("pidinfo cv");
	if (pi->pi_cv == NULL) {
		kfree(pi);
		return NULL;
	}

	pi->pi_pid = pid;
	pi->pi_ppid = ppid;
	pi->pi_exited = false;
	pi->pi_exitstatus = 0xbaad;  /* Recognizably invalid value */
	pi->joined_pid = 0;
	pi->detached = false;
	pi->kids_tail = 0;

	return pi;
}
Esempio n. 30
0
void stoplight_init() {

	// @forkloop
	//
//	int quad_status[4];
//	struct lock *quad_lock;
//	struct cv *quad_cv;

	quad_status[0]=quad_status[1]=quad_status[2]=quad_status[3]=0;

	quad_lock = lock_create("quadrant lock");
	if(quad_lock == NULL) {

		panic("stoplight: lock created failure.\n");
	}
	quad_cv = cv_create("quadrant cv");
	if(quad_cv == NULL) {

		panic("stoplight: cv created failure.\n");
	}
/*
    struct lock lock_list[4];
    char lock_name[6];



    for(int i=0;i<4;i++) {
        snprintf(lock_name, sizeof(lock_name), "lock%d\0", i);
        lock_list[i] = lock_create(lock_name);
        if(lock_list[i] == NULL) {
            panic("stoplight: lock created failure.\n");
        }
    }
	//return;
*/
}