OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Apr 18, 2024 9:16 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 23 posts ]  Go to page Previous  1, 2
Author Message
 Post subject:
PostPosted: Fri Jun 22, 2007 4:17 pm 
Offline
Member
Member
User avatar

Joined: Sat Apr 21, 2007 7:21 pm
Posts: 127
Combuster:

Thank you soo much. You have given me enough a ton of excellent information! Ill be busy for weeks!

Thanks again,
Rich


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 23, 2007 2:17 am 
Offline
Member
Member
User avatar

Joined: Wed Oct 18, 2006 3:45 am
Posts: 9301
Location: On the balcony, where I can actually keep 1½m distance
astrocrep wrote:
Thank you soo much. You have given me enough a ton of excellent information! Ill be busy for weeks!

You're welcome. :D

_________________
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 23, 2007 9:45 am 
Offline
Member
Member
User avatar

Joined: Sat Apr 21, 2007 7:21 pm
Posts: 127
My fetch and increment:

Code:
unsigned long fetch_and_increment(volatile unsigned long *mem)
{
   unsigned long val = 1;
    asm volatile ("lock; xadd %1, (%2)"                             
      : "=r"(val)         
        : "r"(val), "r"(mem)
        : "%1","memory");   
    return val;
}


Looks ok right?

It seems to be working.

If I do:
Code:
int a =0;
fetch_and_increment(&a); // Returns 0
fetch_and_increment(&a); // Returns 1
fetch_and_increment(&a); // Returns 2


Thanks,
Rich


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 23, 2007 1:45 pm 
Offline
Member
Member
User avatar

Joined: Sat Apr 21, 2007 7:21 pm
Posts: 127
So... no... there not working...

At first I tried to use a struct per mutex... and failed miserably...

Now I am try something much simpler:

Code:
unsigned long fetch_and_increment(volatile unsigned long *mem)
{
   unsigned long val = 1;
    asm volatile ("lock; xadd %1, (%2)"                             
      : "=r"(val)         
        : "r"(val), "r"(mem)
        : "%1","memory");   
    return val;
}


int entries = 0;
int exits = 0;

void mutex_lock(volatile mutex_t mt)
{
     int ticket = fetch_and_increment(&entries);
     kprintf("GOT TICKET: %d\n",ticket);
     while (ticket != exits)
     {
          //schedule(); // or anything else you can do while waiting for your turn
     }
}

void mutex_unlock(volatile mutex_t mt)
{
     exits = exits + 1;
     kprintf("MADE EXIT: %d\n",exits);
}


It runs for a brief while giving and closing tickets... but it usually locks up pretty quick...

After about 10 - 100 tickets...
However, I actually think that the tickets are all on the same thread and after the switch the switch it blows up. Ignore the function headers the volatile mutex_t mt was for something else I was testing and failed... Also if I turn entries and exits volatile, I get two grabs for ticket 1 and no exits...

If you think it matters, I post my scheduler code... but I am attaching screen shots of the output.

Thanks,
Rich


Attachments:
File comment: Tickets WITH volatile.
volatile tickets.jpg
volatile tickets.jpg [ 69.93 KiB | Viewed 4018 times ]
File comment: Tickets with out volatile.
crazy tickets.jpg
crazy tickets.jpg [ 47.36 KiB | Viewed 4019 times ]
Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 23, 2007 2:26 pm 
Offline
Member
Member
User avatar

Joined: Wed Oct 18, 2006 3:45 am
Posts: 9301
Location: On the balcony, where I can actually keep 1½m distance
Have you made sure that 'entries' and 'exits' are shared among all threads?

_________________
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 23, 2007 2:47 pm 
Offline
Member
Member
User avatar

Joined: Sat Apr 21, 2007 7:21 pm
Posts: 127
No... each thread has its own esp0 stack allocated at run time.

However, I was assuming since these values would accessing the same physical address it shouldn't matter... I was wrong.

So do I need to do a context switch when I want to access a mutex?

Thanks,
Rich


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 23, 2007 3:07 pm 
Offline
Member
Member
User avatar

Joined: Wed Oct 18, 2006 3:45 am
Posts: 9301
Location: On the balcony, where I can actually keep 1½m distance
no, just shared memory (kernel?)

_________________
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 23, 2007 3:10 pm 
Offline
Member
Member
User avatar

Joined: Sat Apr 21, 2007 7:21 pm
Posts: 127
Yeah... the first 4mb is identity mapped and the kernel starts at 1mb. Those declarations for entries and exits where with in the kernel code.

EDIT: I think I got it working!!

Thanks!!!

Thanks,
Rich


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 23 posts ]  Go to page Previous  1, 2

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], bookman and 102 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group