Hi,
I'm working on a NXHX500 Evaluation board, and I am running the examples that I find in the "netX insiders guide". In particularly I am blocked in the example that show how the mutex API calls work.
When I try to run the code with the lock and un-lock functions I see that in the task2 the programm doesn't stop on the lock function to wait for the resource but just skip it and go further. The result of the function
rX_MtxLockMutex(hMutex1, RX_INFINITE);
is 0x00000019 25 that means "The handle is invalid". How can it be possible if I am running an simple example from a guide. Am I doing something wrong?
Thank you for reply
Now I have started to analyze the Semaphore example and I think I found the same error. I tried to fix It, but I am not satisfied how I did it. I put both create functions in task1 and it worked. But I dont really understand why I find problem if I create the semafore entity in two different tasks (the problem is that task1 cant see the read semafore).
Luca
Hilscher Gesellschaft für Systemautomation mbH
I put both create functions in task1 and it worked. But I dont really understand why I find problem if I create the semafore entity in two different tasks (the problem is that task1 cant see the read semafore).
So you are creating two different Mutexes/Semaphores. They are not related to each other, so why should should Task1 see the semaphore from task2, if it is two different object.
You can either put the Mutex in a global variable and initialize it in task1 and use it in both tasks, or you can create the mutex in Task1 and Identify it in Task2 (this is the way I told you in my first post). In both cases the tasks will use the same object.
MT
Maybe I explained it not clearly
What I wanted to said was that now I understand the bug in the example that use the mutex functions. Now I fix it, and it works.
The problem now is with the example that uses the semaphore functions that you can also find it in the "Insider Guide" pag. 200. As you see from the guide In task1 the WRITE semaphore is defined and in task2 the READ semaphore is defined. So I thought that the problem was same like in the mutex example and I put the rX_SemIdentifySemaphore in both tasks, but I leaved the rX_SemCreateSemaphore separated and it didnt work.
So the point is, as you said, that in the guide also the semaphore example is not right and the correct way is to defined both semaphore WRITE and READ in task1 and than if I want to use it in another task I just need to Identify them. Am I rigth? Is this the better way?
Another question, I tried to use the variable buffer shared between the two task like in the example but it didnt work. Should I have to use the functions
rX_MemAllocateMemory()
rX_MemCreateSharedMemory()
rX_MemIdentifySharedMemory()
?
Ok, I defined a variable like volatile int buffer[4] in the config file then I linked it, in both tasks, with extern int buffer[4] and It worked. But wath do you think about this?
Hilscher Gesellschaft für Systemautomation mbH
Hi Ichos,
There are several ways to achieve sharing of objects between Threads/Tasks. Most of them are OS independent and just rely on what you are doing within your source code.
1. Place the objects in global variables and reference them in each task. (May sure they exist when the task first accesses them)
2. Allocate/Create them before starting your tasks and pass them as pointer via the threads/tasks user parameter.
Note: This is a little tricky in rcX when using the static task list, as you would need to modify the task list structure. When starting the tasks manually via rX_SysCreateTask, this becomes easier.
3. Place all shared data in a common memory block which is known by your threads/tasks. The can either be done by passing pointers to the task, allocated shared memory objects or by creating them in global namespace.
4. Allocate them in one task via Create functions and identify them in the other task. Note: To make sure this happens synchronized in rcX you can use the static task list here and call the Create function before calling rX_SysSetTaskInitialized() and calling the Identify function in the other task right after rX_SysSetTaskInizialized.
And note that you cannot create objects with the same name in rcX and expect them to refer to the same object. Thats why there is a create and an identify (which is like an "open existing object") function.
Regards
MT
M T
Hilscher Gesellschaft für Systemautomation mbH
Hi Ichnos,
The Example contains an error. As you can see the variable hMutex1 is allocated on the stack an never assigned.
You will need to open the mutex through the following function call:
eRslt = rX_MtxIdentifyMutex("MUTEX1", 0, &hMutex1);if(eRslt != 0) while(1) ;
Regards,
MT