Comparing two task handles!

I have an (cpp)object which can be shared across multiple tasks but is owned by a single task(owner task handle is saved when the object is created in TaskHandle_t ownerTask). Now, when I run a certain function of that object, I need to check if the current task which has requested that function execution is the owner task.

How can I test if the current task is the same whose handle is already saved?
The catches are that configuse_TRACE_FACILITY is set to 0 in the project(and is meant to be like that) and hence the TCB(which is what the handles point to) doesn’t have the member uxTaskNumber.

Is it a good idea to add a member in the TCB by yourself that keeps track of the task number like for example taskID and then set it while the task is being added to the ready list?

The uxTaskNumber value is only required to determine if a task was deleted, and then another task was created that had the same task handle (which is just a pointer to the tasks TCB) - in which case the handles would be the same but the uxTaskNumber value would be different. So if the owner of the object is not deleted while the object exists you probably don’t need the uxTaskNumber value - you can just compare the task handles directly. You can use to obtain the handle of the currently running task.

1 Like

As Richard says, the Task Handle will be unique as long as the task continues to exist. If you consider a task as ‘owning’ some resource, then it should generally ‘known’ it before it terminates (if it ever terminates) so remembering the task handle should be good enough.

One big question for this issue is what are you doing if a ‘non-owner’ tries to use the resource, and does it cause a problem if the owner died, and some other task was created with the same Task Handle and thus became the new ‘owner’. Most situations that I can think of, are in a problem state if the ‘owner’ of an object that needs to deal with ownership goes away while retaining ownership, so we ran into the problem long before the task handle reuse occurred.

Thanks @rtel! And by comparing the handles you mean plain and simple memcmp type compare of the handles returned from xTaskGetCurrentHandle, right?

Thanks @richard-damon. the handling of the case where non-owner tries to do something will be done as per the application. What I mean is, either we assert or log but thats not decided yet. the owner task never dies as far as I know the system yet.

You don’t need to do a memcmp(), simple test with handle1 == handle2.

Yeah I just realized that after replying the last time… The comparing of the address of the tcb that the handles are pointing to… Thanks a ton @rtel… And I also want to.thank you for the super cool book you have written on freeRTOS…