Compare commits

...

2 Commits

Author SHA1 Message Date
b79977ab99 lab3.2: cv 2020-05-28 22:06:08 +02:00
6733fafc29 fix: sys write 2020-05-28 22:05:43 +02:00
3 changed files with 48 additions and 10 deletions

View File

@ -127,6 +127,8 @@ struct cv {
char *cv_name;
// add what you need here
// (don't forget to mark things volatile as needed)
struct wchan *wchan;
struct spinlock slk;
};
struct cv *cv_create(const char *name);

View File

@ -4,7 +4,7 @@
size_t sys_read(int fh, userptr_t buf, size_t size) {
int i=0;
int * ary = (int*)buf;
char * ary = (char*)buf;
if (fh!=STDIN_FILENO) {
kprintf("sys_read supported only to stdin\n");
return -1;
@ -18,7 +18,7 @@ size_t sys_read(int fh, userptr_t buf, size_t size) {
size_t sys_write(int fh, const userptr_t buf, size_t size) {
int i=0;
int * ary = (int*)buf;
char * ary = (char*)buf;
if (fh!=STDOUT_FILENO && fh!=STDERR_FILENO) {
kprintf("sys_write supported only to stdout\n");

View File

@ -227,7 +227,7 @@ lock_release(struct lock *lock)
#elif OPT_WCHANLOCK
spinlock_acquire(&lock->slk);
lock->owner = NULL;
wchan_wakeall(lock->wchan, &lock->slk);
wchan_wakeone(lock->wchan, &lock->slk);
spinlock_release(&lock->slk);
#else
lock->owner = NULL;
@ -272,8 +272,15 @@ cv_create(const char *name)
return NULL;
}
// add stuff here as needed
#if OPT_WCHANLOCK
cv->wchan = wchan_create(name);
if (cv->wchan == NULL) {
kfree(cv->cv_name);
kfree(cv);
return NULL;
}
spinlock_init(&cv->slk);
#endif
return cv;
}
@ -282,8 +289,10 @@ cv_destroy(struct cv *cv)
{
KASSERT(cv != NULL);
// add stuff here as needed
#if OPT_WCHANLOCK
spinlock_cleanup(&cv->slk);
wchan_destroy(cv->wchan);
#endif
kfree(cv->cv_name);
kfree(cv);
}
@ -291,23 +300,50 @@ cv_destroy(struct cv *cv)
void
cv_wait(struct cv *cv, struct lock *lock)
{
// Write this
#if OPT_WCHANLOCK
KASSERT(cv != NULL);
KASSERT(lock_do_i_hold(lock));
spinlock_acquire(&cv->slk);
lock_release(lock);
wchan_sleep(cv->wchan,&cv->slk);
spinlock_release(&cv->slk);
lock_acquire(lock);
#else
(void)cv; // suppress warning until code gets written
(void)lock; // suppress warning until code gets written
#endif
}
void
cv_signal(struct cv *cv, struct lock *lock)
{
// Write this
#if OPT_WCHANLOCK
KASSERT(cv != NULL);
KASSERT(lock_do_i_hold(lock));
spinlock_acquire(&cv->slk);
wchan_wakeone(cv->wchan, &cv->slk);
spinlock_release(&cv->slk);
#else
(void)cv; // suppress warning until code gets written
(void)lock; // suppress warning until code gets written
#endif
}
void
cv_broadcast(struct cv *cv, struct lock *lock)
{
// Write this
#if OPT_WCHANLOCK
KASSERT(cv != NULL);
KASSERT(lock_do_i_hold(lock));
spinlock_acquire(&cv->slk);
wchan_wakeall(cv->wchan, &cv->slk);
spinlock_release(&cv->slk);
#else
(void)cv; // suppress warning until code gets written
(void)lock; // suppress warning until code gets written
#endif
}