Page 2 of 2
Posted: Fri Jun 22, 2007 4:17 pm
by astrocrep
Combuster:
Thank you soo much. You have given me enough a ton of excellent information! Ill be busy for weeks!
Thanks again,
Rich
Posted: Sat Jun 23, 2007 2:17 am
by Combuster
astrocrep wrote:Thank you soo much. You have given me enough a ton of excellent information! Ill be busy for weeks!
You're welcome.
![Very Happy :D](./images/smilies/icon_biggrin.gif)
Posted: Sat Jun 23, 2007 9:45 am
by astrocrep
My fetch and increment:
Code: Select all
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: Select all
int a =0;
fetch_and_increment(&a); // Returns 0
fetch_and_increment(&a); // Returns 1
fetch_and_increment(&a); // Returns 2
Thanks,
Rich
Posted: Sat Jun 23, 2007 1:45 pm
by astrocrep
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: Select all
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
Posted: Sat Jun 23, 2007 2:26 pm
by Combuster
Have you made sure that 'entries' and 'exits' are shared among all threads?
Posted: Sat Jun 23, 2007 2:47 pm
by astrocrep
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
Posted: Sat Jun 23, 2007 3:07 pm
by Combuster
no, just shared memory (kernel?)
Posted: Sat Jun 23, 2007 3:10 pm
by astrocrep
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