taskLock and unLock - VxWorks

This is a discussion on taskLock and unLock - VxWorks ; In my code, to protect mutilple task access to a critical data, I am using taksLock n unlock, but this is needed at too many places and I want to know what are the disadvantages of calling taskLock and taskUnlock ...

+ Reply to Thread
Results 1 to 2 of 2

Thread: taskLock and unLock

  1. taskLock and unLock

    In my code, to protect mutilple task access to a critical data, I am
    using taksLock n unlock, but this is needed at too many places and I
    want to know
    what are the disadvantages of calling taskLock and taskUnlock pair in
    the code. One is that: no other task can be scheduled in that time,
    What else?

    I also hear that taskUnlock is heavy and adds delay because the task
    scheduling has to restart. Is it so?


  2. Re: taskLock and unLock

    Subhash wrote:

    >In my code, to protect mutilple task access to a critical data, I am
    >using taksLock n unlock, but this is needed at too many places and I
    >want to know
    >what are the disadvantages of calling taskLock and taskUnlock pair in
    >the code. One is that: no other task can be scheduled in that time,
    >What else?
    >
    >I also hear that taskUnlock is heavy and adds delay because the task
    >scheduling has to restart. Is it so?


    I don't know about task locking being "heavy" but preventing other tasks
    from being scheduled is bad enough, especially if low priority tasks
    will hold off high priority tasks. You would probably be better off to
    use a semaphore, either binary of mutex, to protect your critical data.
    Here's a trivial example of how it's done.


    #include
    #include
    #include
    #include

    // module data

    LOCAL int moduleData = 0;

    // controls access to module data

    LOCAL SEM_ID moduleDataMutex;


    void moduleInit ( void ) {

    // create module data mutual exclusion semaphore

    if (NULL == (moduleDataMutex = semMCreate (SEM_Q_PRIORITY | SEM_INVERSION_SAFE))) {
    perror("moduleInit: semMCreate() failed");
    }
    }


    void moduleDataSet ( void ) {

    // protect module data

    semTake (moduleDataMutex, WAIT_FOREVER);

    // set module data

    if (someCondition) {
    moduleData = 1;
    } else {
    moduleData = 0;
    }

    // done protecting module data

    semGive (moduleDataMutex);
    }


    void moduleDataOut ( void ) {

    // protect module data

    semTake (moduleDataMutex, WAIT_FOREVER);

    // output module data

    *someLocation = moduleData;

    // done protecting module data

    semGive (moduleDataMutex);
    }


    moduleData is protected from multiple access and will not be altered
    while being output, all with protection against priority inversion. See
    the Intertask Communication section of the VxWorks Programmer Guide for
    more information.

    --
    ================================================== ======================
    Michael Kesti | "And like, one and one don't make
    | two, one and one make one."
    mrkesti at hotmail dot com | - The Who, Bargain

+ Reply to Thread