mirror of
https://github.com/drasko/codezero.git
synced 2026-02-28 17:53:13 +01:00
Simplified l4_mutex test, in that the test now updates a variable by thread
switching instead of updating a whole page.
This commit is contained in:
@@ -20,9 +20,7 @@
|
|||||||
*/
|
*/
|
||||||
struct shared_page {
|
struct shared_page {
|
||||||
struct l4_mutex mutex;
|
struct l4_mutex mutex;
|
||||||
int child_has_run;
|
int shared_var;
|
||||||
int parent_has_run;
|
|
||||||
char page_rest[];
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static struct shared_page *shared_page;
|
static struct shared_page *shared_page;
|
||||||
@@ -38,7 +36,6 @@ int user_mutex_test(void)
|
|||||||
{
|
{
|
||||||
pid_t child, parent;
|
pid_t child, parent;
|
||||||
int map_size = PAGE_SIZE;
|
int map_size = PAGE_SIZE;
|
||||||
int buf_size;
|
|
||||||
void *base;
|
void *base;
|
||||||
|
|
||||||
/* Get parent pid */
|
/* Get parent pid */
|
||||||
@@ -59,19 +56,11 @@ int user_mutex_test(void)
|
|||||||
|
|
||||||
shared_page = base;
|
shared_page = base;
|
||||||
|
|
||||||
/* Find the buffer size */
|
|
||||||
buf_size = map_size - sizeof(*shared_page);
|
|
||||||
printf("Total buffer size: %d\n", buf_size);
|
|
||||||
|
|
||||||
/* Initialize the mutex */
|
/* Initialize the mutex */
|
||||||
l4_mutex_init(&shared_page->mutex);
|
l4_mutex_init(&shared_page->mutex);
|
||||||
|
|
||||||
/* Initialize buffer with unique child value */
|
/* Initialize the shared variable */
|
||||||
printf("Initializing shared page with child values.\n");
|
shared_page->shared_var = 0;
|
||||||
shared_page->child_has_run = 0;
|
|
||||||
shared_page->parent_has_run = 0;
|
|
||||||
for (int i = 0; i < buf_size; i++)
|
|
||||||
shared_page->page_rest[i] = 0;
|
|
||||||
|
|
||||||
/* Fork the current task */
|
/* Fork the current task */
|
||||||
if ((child = fork()) < 0) {
|
if ((child = fork()) < 0) {
|
||||||
@@ -87,80 +76,82 @@ int user_mutex_test(void)
|
|||||||
/* Child locks and produces */
|
/* Child locks and produces */
|
||||||
if (child == 0) {
|
if (child == 0) {
|
||||||
|
|
||||||
for (int x = 0; x < 255; x++) {
|
for (int x = 0; x < 10000; x++) {
|
||||||
/* Lock */
|
int temp;
|
||||||
|
|
||||||
|
/* Lock page */
|
||||||
printf("Child locking page.\n");
|
printf("Child locking page.\n");
|
||||||
l4_mutex_lock(&shared_page->mutex);
|
l4_mutex_lock(&shared_page->mutex);
|
||||||
|
|
||||||
|
/* Read variable */
|
||||||
printf("Child locked page.\n");
|
printf("Child locked page.\n");
|
||||||
|
temp = shared_page->shared_var;
|
||||||
|
|
||||||
|
/* Thread switch */
|
||||||
l4_thread_switch(0);
|
l4_thread_switch(0);
|
||||||
/* Get sample value */
|
|
||||||
//char val = shared_page->page_rest[0];
|
|
||||||
|
|
||||||
/* Write a unique child value to whole buffer */
|
/* Update local copy */
|
||||||
for (int i = 0; i < buf_size; i++) {
|
temp++;
|
||||||
#if 0
|
|
||||||
/* Check sample is same in all */
|
/* Thread switch */
|
||||||
if (shared_page->page_rest[i] != val) {
|
l4_thread_switch(0);
|
||||||
printf("Sample values dont match. "
|
|
||||||
"page_rest[%d] = %c, "
|
/* Write back the result */
|
||||||
"val = %c\n", i,
|
shared_page->shared_var = temp;
|
||||||
shared_page->page_rest[i], val);
|
|
||||||
goto out_err;
|
printf("Child modified. Unlocking...\n");
|
||||||
}
|
|
||||||
#endif
|
|
||||||
shared_page->page_rest[i]--;
|
|
||||||
}
|
|
||||||
printf("Child produced. Unlocking...\n");
|
|
||||||
/* Unlock */
|
/* Unlock */
|
||||||
l4_mutex_unlock(&shared_page->mutex);
|
l4_mutex_unlock(&shared_page->mutex);
|
||||||
printf("Child unlocked page.\n");
|
printf("Child unlocked page.\n");
|
||||||
|
|
||||||
|
/* Thread switch */
|
||||||
|
l4_thread_switch(0);
|
||||||
|
|
||||||
}
|
}
|
||||||
/* Sync with the parent */
|
/* Sync with the parent */
|
||||||
l4_send(parent, L4_IPC_TAG_SYNC);
|
l4_send(parent, L4_IPC_TAG_SYNC);
|
||||||
|
|
||||||
/* Parent locks and consumes */
|
/* Parent locks and consumes */
|
||||||
} else {
|
} else {
|
||||||
for (int x = 0; x < 255; x++) {
|
for (int x = 0; x < 10000; x++) {
|
||||||
printf("Parent locking page.\n");
|
int temp;
|
||||||
|
|
||||||
/* Lock the page */
|
/* Lock page */
|
||||||
|
printf("Parent locking page.\n");
|
||||||
l4_mutex_lock(&shared_page->mutex);
|
l4_mutex_lock(&shared_page->mutex);
|
||||||
|
|
||||||
|
/* Read variable */
|
||||||
printf("Parent locked page.\n");
|
printf("Parent locked page.\n");
|
||||||
|
temp = shared_page->shared_var;
|
||||||
|
|
||||||
|
/* Thread switch */
|
||||||
l4_thread_switch(0);
|
l4_thread_switch(0);
|
||||||
|
|
||||||
// printf("Parent reading:\n");
|
/* Update local copy */
|
||||||
|
temp--;
|
||||||
|
|
||||||
/* Test and consume the page */
|
/* Thread switch */
|
||||||
for (int i = 0; i < buf_size; i++) {
|
l4_thread_switch(0);
|
||||||
// printf("%c", shared_page->page_rest[i]);
|
|
||||||
/* Test that child has produced */
|
/* Write back the result */
|
||||||
#if 0
|
shared_page->shared_var = temp;
|
||||||
if (shared_page->page_rest[i] != 'c') {
|
|
||||||
printf("Child not produced. "
|
printf("Parent modified. Unlocking...\n");
|
||||||
"page_rest[%d] = %c, "
|
|
||||||
"expected = 'c'\n",
|
/* Unlock */
|
||||||
i, shared_page->page_rest[i]);
|
|
||||||
BUG();
|
|
||||||
goto out_err;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
/* Consume the page */
|
|
||||||
shared_page->page_rest[i]++;
|
|
||||||
}
|
|
||||||
// printf("\n\n");
|
|
||||||
printf("Parent consumed. Unlocking...\n");
|
|
||||||
l4_mutex_unlock(&shared_page->mutex);
|
l4_mutex_unlock(&shared_page->mutex);
|
||||||
printf("Parent unlocked page.\n");
|
printf("Parent unlocked page.\n");
|
||||||
|
|
||||||
|
/* Thread switch */
|
||||||
|
l4_thread_switch(0);
|
||||||
}
|
}
|
||||||
/* Sync with the child */
|
/* Sync with the child */
|
||||||
l4_receive(child);
|
l4_receive(child);
|
||||||
|
|
||||||
printf("Parent checking validity of values.\n");
|
printf("Parent checking validity of value.\n");
|
||||||
for (int i = 0; i < 255; i++)
|
if (shared_page->shared_var != 0)
|
||||||
if (shared_page->page_rest[i] != 0)
|
goto out_err;
|
||||||
goto out_err;
|
|
||||||
|
|
||||||
printf("USER MUTEX TEST: -- PASSED --\n");
|
printf("USER MUTEX TEST: -- PASSED --\n");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user