When can WaitForSingleObject return WAIT_FAILED?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sean Kelly

    When can WaitForSingleObject return WAIT_FAILED?

    When both waiting on an event and a simple unnamed mutex, I'm wondering
    when WaitForSingleOb ject might return WAIT_FAILED. These are both
    execution paths I'd very much like to avoid exceptional behavior but I
    haven't found much documentation on the specifics. Can anyone offer advice?

    Sean

  • William DePalo

    #2
    Re: When can WaitForSingleOb ject return WAIT_FAILED?

    Sean Kelly <kensai@pacbell .net> wrote in message
    news:uLKunk#zDH A.2180@TK2MSFTN GP12.phx.gbl...[color=blue]
    > When both waiting on an event and a simple unnamed mutex, I'm wondering
    > when WaitForSingleOb ject might return WAIT_FAILED. These are both
    > execution paths I'd very much like to avoid exceptional behavior but I
    > haven't found much documentation on the specifics. Can anyone offer[/color]
    advice?

    Well, suppose you pass an invalid handle to the function? I'd expect that
    you will get a WAIT_FAILED status and then INVALID_HANDLE in the call to
    GetLastError(). I don't think there is any way you can avoid checking the
    status of the code _somewhere_.

    Regards,
    Will


    Comment

    • Sean Kelly

      #3
      Re: When can WaitForSingleOb ject return WAIT_FAILED?

      William DePalo wrote:[color=blue]
      > Sean Kelly <kensai@pacbell .net> wrote in message
      > news:uLKunk#zDH A.2180@TK2MSFTN GP12.phx.gbl...
      >[color=green]
      >>When both waiting on an event and a simple unnamed mutex, I'm wondering
      >>when WaitForSingleOb ject might return WAIT_FAILED. These are both
      >>execution paths I'd very much like to avoid exceptional behavior but I
      >>haven't found much documentation on the specifics. Can anyone offer[/color]
      >
      > advice?
      >
      > Well, suppose you pass an invalid handle to the function? I'd expect that
      > you will get a WAIT_FAILED status and then INVALID_HANDLE in the call to
      > GetLastError(). I don't think there is any way you can avoid checking the
      > status of the code _somewhere_.[/color]

      This is in an object and ensapsulation ensures that the parameters
      passed will always be valid. Basically, I'm trying to avoid the
      possibility that an exception may be thrown when acquiring a lock on a
      mutex. Critical section operations are guranteed to succeed for this
      reason. More info: it's an unnamed mutex without any security
      restrictions. Basically, just an object that creates the mutex on
      construction, locks/unlocks the mutex when requested, and destroys the
      mutex on destruction. What I'm looking for is someone with enough
      knowledge of the inner workings of the call that they could tell be what
      conditions might cause a WAIT_FAILED in this instance. I'm hoping that
      if I can satisfy enough preconditions before calling the function I can
      be guranteed that the function will never fail in this way.

      Sean

      Comment

      • William DePalo [MVP VC++]

        #4
        Re: When can WaitForSingleOb ject return WAIT_FAILED?

        Sean Kelly <kensai@pacbell .net> wrote in message
        news:uKsEvCY0DH A.3196@TK2MSFTN GP11.phx.gbl...[color=blue]
        > This is in an object and ensapsulation ensures that the parameters
        > passed will always be valid.[/color]

        In an environment where "naked" pointers are used, memory corruption is not
        impossible, encapsulation be damned. It is hard to imagine a Win32 program
        without a single naked pointer.
        [color=blue]
        > What I'm looking for is someone with enough
        > knowledge of the inner workings of the call that they could tell be what
        > conditions might cause a WAIT_FAILED in this instance.[/color]

        Then I suggest that you post again in the kernel group. Perhaps there you'll
        run into someone who has seen the source or reverse engineered it.
        [color=blue]
        > I'm hoping that if I can satisfy enough preconditions
        > before calling the function I can be guranteed that
        > the function will never fail in this way.[/color]

        Good luck.

        Regards,
        Will


        Comment

        • Carl Daniel [VC++ MVP]

          #5
          Re: When can WaitForSingleOb ject return WAIT_FAILED?

          William DePalo [MVP VC++] wrote:[color=blue]
          > Sean Kelly <kensai@pacbell .net> wrote in message
          > news:uKsEvCY0DH A.3196@TK2MSFTN GP11.phx.gbl...[color=green]
          >> This is in an object and ensapsulation ensures that the parameters
          >> passed will always be valid.[/color]
          >
          > In an environment where "naked" pointers are used, memory corruption
          > is not impossible, encapsulation be damned. It is hard to imagine a
          > Win32 program without a single naked pointer.
          >[color=green]
          >> What I'm looking for is someone with enough
          >> knowledge of the inner workings of the call that they could tell be
          >> what conditions might cause a WAIT_FAILED in this instance.[/color]
          >
          > Then I suggest that you post again in the kernel group. Perhaps there
          > you'll run into someone who has seen the source or reverse engineered
          > it.[/color]

          If you're waiting on a mutex, you'll normally get WAIT_FAILED for only two
          reasons: 1. The HANDLE you passed in is invalid. 2. Another thread that
          owned the mutex was terminated without releasing ownership.

          There's no guarantee that those are the only two circumstances though.
          There are probably other, extremely rare and esoteric failures possible.

          -cd


          Comment

          • Sean Kelly

            #6
            Re: When can WaitForSingleOb ject return WAIT_FAILED?

            Carl Daniel [VC++ MVP] wrote:[color=blue]
            >
            > If you're waiting on a mutex, you'll normally get WAIT_FAILED for only two
            > reasons: 1. The HANDLE you passed in is invalid. 2. Another thread that
            > owned the mutex was terminated without releasing ownership.[/color]

            From what I read in MSDN I thought that case 2 would return
            WAIT_ABANDONED and mutex ownership would be transferred.
            [color=blue]
            > There's no guarantee that those are the only two circumstances though.
            > There are probably other, extremely rare and esoteric failures possible.[/color]

            That's what I'm afraid of. Worst case I suppose I can just always use
            critical sections to avoid the possibility of failure.

            Sean

            Comment

            • Carl Daniel [VC++ MVP]

              #7
              Re: When can WaitForSingleOb ject return WAIT_FAILED?

              Sean Kelly wrote:[color=blue]
              > Carl Daniel [VC++ MVP] wrote:[color=green]
              >>
              >> If you're waiting on a mutex, you'll normally get WAIT_FAILED for
              >> only two reasons: 1. The HANDLE you passed in is invalid. 2.
              >> Another thread that owned the mutex was terminated without releasing
              >> ownership.[/color]
              >
              > From what I read in MSDN I thought that case 2 would return
              > WAIT_ABANDONED and mutex ownership would be transferred.[/color]

              You're right about that.

              -cd


              Comment

              Working...