Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/queue.h>
- struct myprocess {
- pid_t id; // process id
- int waiting; // process status: 0 - not waiting, != 0 - waiting
- LIST_ENTRY(myprocess) link; // link to next process in process list
- };
- struct mymutex {
- int d; // mutex descriptor
- char *name; // mutex name
- int is_locked; // mutex status: 0 - not waiting, != 0 - waiting
- LIST_HEAD(, myprocess) plist; // process list
- LIST_ENTRY(mymutex) link; // link to next mutex in mutex list
- }
- LIST_HEAD(, mymutex) mtxlist; // mutex list;
- int mtxlist_is_init; // mutex list status: 0 - not initialized, != 0 - initialized
- int t_pause; // wait channel (the resource for which the processes need to wait)
- pid_t wake_pid; // id of currently granted process
- int
- sys_mtxlock(struct proc *p, void *v, register_t *retval) {
- int error = 0;
- if (mtxlist_is_init == 0) {
- LIST_INIT(&mtxlist);
- mtxlist_is_init = 1;
- }
- struct sys_mtxlock_args *uap = v;
- int d = SCARG(uap, d);
- struct mymutex *mtx_found = NULL;
- struct mymutex *mtx_it;
- LIST_FOREACH(mtx_it, &mtxlist, link) {
- if (mtx_it->d == d) {
- mtx_found = it;
- break;
- }
- }
- if (mtx_found == NULL) {
- printf("LOCK: there is no mutex having descriptor %d\n", d);
- *retval = -1;
- return 0;
- }
- printf("LOCK: mutex to be locked name %s, descriptor %d\n", mtx_found->name, mtx_found->d);
- struct myprocess *proc_found = NULL;
- struct myprocess *proc_it;
- LIST_FOREACH(proc_it, &(mtx_found->plist), link) {
- if (proc_it->id == p->p_tid) {
- proc_found = proc_it;
- break;
- }
- }
- if (proc_found == NULL) {
- printf("LOCK: proc %d has not opened mutex name %s\n", p->p_tid, mtx_found->name);
- *retval = -1;
- return 0;
- }
- printf("LOCK: proc %d start waiting mutex %s\n", proc_found->id, mtx_found->name);
- proc_found->waiting = 1;
- // should check mutex not in use (is_locked == 0) to skip sleep
- sleep:
- error = tsleep(&t_pause, PSOCK | PCATCH, "awaiting lock", 0);
- if (error) {
- proc_found->waiting = 0;
- *retval = -1;
- return error;
- }
- if (wake_pid != p->p_tid) {
- goto sleep;
- }
- wake_pid = 0;
- proc_found->waiting = 0;
- *retval = 0;
- return 0;
- }
- int
- sys_mtxunlock(struct proc *p, void *v, register_t *retval)
- {
- if (mtxlist_is_init == 0) {
- LIST_INIT(&mtxlist);
- mtxlist_is_init = 1;
- }
- struct sys_mtxclose_args *uap = v;
- int d = SCARG(uap, d);
- struct mymutex *mtx_it;
- // struct myprocess *proc_it, *proc_found = NULL;
- LIST_FOREACH(mtx_it, &mtxlist, link) {
- if (mtx_it->d == d) {
- // should check mutex d actually opened by proc p?
- /*LIST_FOREACH(proc_it, &(mtx_it->plist), link) {
- if (proc_it->id == p->p_tid) {
- proc_found = proc_it;
- break;
- }
- }
- if (proc_found == NULL) {
- printf("UNLOCK: proc %d has not opened mutex name %s\n", p->p_tid, mtx_found->name");
- *retval = -1;
- return 0;
- }*/
- mtx_it->is_locked = 0;
- break;
- }
- }
- *retval = 0;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment