Applies To:
  • CitectSCADA 1.00 1.01 1.10 1.11 1.20 2.00 2.01 3.00

Question: I am using SemWait to wait for another cicode task to complete, however when I call SemWait() for a second time it will not wait. What is going wrong with this function.

SemWait is working correctly. What is happening is that you are calling SemWait() when you already own the semaphore. Whenever SemWait gives ownership of an semaphore it saves the cicode task handle with that semaphore. If the same cicode tasks asks for the semaphore again then SemWait sees that the cicode task already owns the semaphore and will always return. SemWait has been designed this way to stop deadlock conditions from occurring. If semaphores did not work this way a deadlock could occur if you had a cicode task which called SemWait() and then called SemWait() again. The second SemWait would wait for the first matching SemSignal() to be called, which would never happen, so the cicode would be deadlocked.

If you want semaphores to operate in a counting manner you can use the QueXXX() functions. You can create a queue and then place 1 element into it. You can then use QueRead() for the SemWait() and QueWrite() for the SemSignal(). This allows you to write say 3 elements into the queue so you can have a counting semaphore. In this case you can call QueRead() up to 3 times before it will block your cicode task waiting for another QueWrite() from some other task.