Refactor process_execute to remove use of 'goto'
This commit is contained in:
@@ -67,9 +67,6 @@ process_execute (const char *cmd)
|
|||||||
|
|
||||||
struct process_start_data data;
|
struct process_start_data data;
|
||||||
|
|
||||||
sema_init (&data.loaded, 0);
|
|
||||||
data.success = false;
|
|
||||||
|
|
||||||
/* Make a copy of command.
|
/* Make a copy of command.
|
||||||
Otherwise there's a race between the caller and load(). */
|
Otherwise there's a race between the caller and load(). */
|
||||||
cmd_copy = palloc_get_page (0);
|
cmd_copy = palloc_get_page (0);
|
||||||
@@ -99,6 +96,8 @@ process_execute (const char *cmd)
|
|||||||
arguments. For details of arguments, see 'start_process'. */
|
arguments. For details of arguments, see 'start_process'. */
|
||||||
data.cmd = cmd_copy;
|
data.cmd = cmd_copy;
|
||||||
strlcpy (data.file_name, file_name, FNAME_MAX_LEN + 1);
|
strlcpy (data.file_name, file_name, FNAME_MAX_LEN + 1);
|
||||||
|
sema_init (&data.loaded, 0);
|
||||||
|
data.success = false;
|
||||||
|
|
||||||
tid = thread_create (file_name, PRI_DEFAULT, start_process, &data);
|
tid = thread_create (file_name, PRI_DEFAULT, start_process, &data);
|
||||||
if (tid == TID_ERROR)
|
if (tid == TID_ERROR)
|
||||||
@@ -126,7 +125,6 @@ static void
|
|||||||
start_process (void *proc_start_data)
|
start_process (void *proc_start_data)
|
||||||
{
|
{
|
||||||
struct intr_frame if_;
|
struct intr_frame if_;
|
||||||
bool success;
|
|
||||||
|
|
||||||
struct process_start_data *data = proc_start_data;
|
struct process_start_data *data = proc_start_data;
|
||||||
|
|
||||||
@@ -143,34 +141,36 @@ start_process (void *proc_start_data)
|
|||||||
if (exec_file == NULL)
|
if (exec_file == NULL)
|
||||||
{
|
{
|
||||||
lock_release (&filesys_lock);
|
lock_release (&filesys_lock);
|
||||||
goto fail;
|
sema_up (&data->loaded);
|
||||||
|
thread_exit ();
|
||||||
}
|
}
|
||||||
thread_current ()->exec_file = exec_file;
|
thread_current ()->exec_file = exec_file;
|
||||||
file_deny_write (exec_file);
|
file_deny_write (exec_file);
|
||||||
lock_release (&filesys_lock);
|
lock_release (&filesys_lock);
|
||||||
|
|
||||||
success = load (data->file_name, &if_.eip, &if_.esp);
|
data->success = load (data->file_name, &if_.eip, &if_.esp);
|
||||||
|
|
||||||
/* If load failed, quit. */
|
/* If load failed, quit. */
|
||||||
if (!success)
|
if (!data->success)
|
||||||
{
|
{
|
||||||
palloc_free_page (data->cmd);
|
palloc_free_page (data->cmd);
|
||||||
goto fail;
|
sema_up (&data->loaded);
|
||||||
|
thread_exit ();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Initialize user process stack and free page used to store the
|
/* Initialize user process stack and free page used to store the
|
||||||
command that executed the process. */
|
command that executed the process. */
|
||||||
success = process_init_stack (data->cmd_saveptr, &if_.esp, data->file_name);
|
data->success = process_init_stack (data->cmd_saveptr, &if_.esp, data->file_name);
|
||||||
|
bool success = data->success;
|
||||||
palloc_free_page (data->cmd);
|
palloc_free_page (data->cmd);
|
||||||
|
sema_up (&data->loaded);
|
||||||
|
|
||||||
/* If stack initialization failed, free resources and quit. */
|
/* If stack initialization failed, free resources and quit. */
|
||||||
if (!success)
|
if (!success)
|
||||||
{
|
{
|
||||||
goto fail;
|
thread_exit ();
|
||||||
}
|
}
|
||||||
|
|
||||||
data->success = true;
|
|
||||||
sema_up (&data->loaded);
|
|
||||||
/* Start the user process by simulating a return from an
|
/* Start the user process by simulating a return from an
|
||||||
interrupt, implemented by intr_exit (in
|
interrupt, implemented by intr_exit (in
|
||||||
threads/intr-stubs.S). Because intr_exit takes all of its
|
threads/intr-stubs.S). Because intr_exit takes all of its
|
||||||
@@ -179,12 +179,6 @@ start_process (void *proc_start_data)
|
|||||||
and jump to it. */
|
and jump to it. */
|
||||||
asm volatile ("movl %0, %%esp; jmp intr_exit" : : "g" (&if_) : "memory");
|
asm volatile ("movl %0, %%esp; jmp intr_exit" : : "g" (&if_) : "memory");
|
||||||
NOT_REACHED ();
|
NOT_REACHED ();
|
||||||
|
|
||||||
/* If starting the process failed, exit. */
|
|
||||||
fail:
|
|
||||||
data->success = false;
|
|
||||||
sema_up (&data->loaded);
|
|
||||||
thread_exit ();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Helper function that initializes the stack of a newly created
|
/* Helper function that initializes the stack of a newly created
|
||||||
|
|||||||
Reference in New Issue
Block a user