Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
20 years ago

locking the mutex

Hi,

Whats wrong with my code??

My system is running with two cpu's.

To get started with the different commands I'm just testing with a Hello World app:

int main()

{

char *test;

test = mutex_uart1_name;

int i = 0;

while(1)

{

if (altera_avalon_mutex_trylock(&test, 16) == 0)

{

printf("cpu 1: int = %i. hello from nios ii!\n", i);

altera_avalon_mutex_unlock(&test);

i++;

}

usleep(2000000);

}

return 0;

}

The application is running on both cpu's, but even though the trylock returns 0 (=mutex lock ok), the other cpu's also get to lock the mutex, or, i guess, none of them do.....

I also wonder why this code is not working (skipping the test-pointer):

int main()

{

int i = 0;

while(1)

{

if (altera_avalon_mutex_trylock(mutex_uart1_name, 0) == 0)

{

printf("cpu 2: int = %i. hello from nios ii!\n", i);

altera_avalon_mutex_unlock(mutex_uart1_name);

i++;

}

usleep(2000000);

}

return 0;

}

Stian

2 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Hi,

    I also have the same scenario, but when I program and start my first core it works. As I program and start the second core, it freezes the first code.

    These happen only when i implement the mutex for sharing some common address space between two cores. Otherwise if I run two cores independently, it RUNs.

    Please suggest.