Jump to content

WinWaitMuxWaitSem: Difference between revisions

From EDM2
Ak120 (talk | contribs)
mNo edit summary
No edit summary
 
(6 intermediate revisions by 2 users not shown)
Line 1: Line 1:
=== Syntax ===
This function waits for a muxwait semaphore to clear or for a Presentation Manager message.
rc = WinWaitMuxWaitSem( ''hmuxSemaphore'',
                        ''ulTimeOut'',
                        ''pulUserField'' );


=== Parameters ===
== Syntax ==
;HEV ''hevSemaphore'' (input):The handle of the semaphore to block on.
;ULONG ''ulTimeout'' (input):The number of milliseconds the function will wait before
returning.
:*Set to '''SEM_IMMEDIATE_RETURN''' (0), the function will return immediately regardless of whether or not the semaphore is posted.
:*Set to '''SEM_INDEFINITE_WAIT''' (-1), the function will block indefinitely (forever) until the semaphore is posted.
;PULONG ''pulUserField'' (output):A pointer to a ULONG that will contain the value of the user field from the SEMRECORD structure. If the MuxWait semaphore was created with the DCMW_WAIT_ALL flag, this will contain the user field of the last semaphore that was posted or released. If the MuxWait semaphore was created with the DCMW_WAIT_ANY flag, this will contain the user field of the semaphore that was posted or released. If the thread did not block, this will contain the user field of the last semaphore in the MuxWait list.


=== Returns ===
  WinWaitMuxWaitSem(hmux, ulTimeout, pulUser);
  APIRET rc
The following values can be returned
{|class="wikitable"
|-
|0
|NO_ERROR
|Operation successful
|-
|6
|ERROR_INVALID_HANDLE
|Error, The value in ''phmuxSemaphore'' does not point to a valid semaphore
|-
|8
|ERROR_NOT_ENOUGH_MEMORY
|Error, The system memory limit has been exceeded
|-
|87
|ERROR_INVALID_PARAMETER
|Error, One or more parameters is not recognized, See parameters above,
Both ''pszSemaphoreName'' and ''phmuxSemaphore'' may be NULL
|-
|95
|ERROR_INTERRUPT
|Error, The thread has become unblocked by an external event such as an exception
|-
|103
|ERROR_TOO_MANY_SEM_REQUESTS
|Error
|-
|105
|ERROR_SEM_OWNER_DIED
|Error, The owner of a mutex semaphore in the muxwait list has died without freeing the semaphore
|-
|286
|ERROR_EMPTY_MUXWAIT
|Error, The MuxWait semaphore ''hmuxSemaphore'' is empty
|-
|287
|ERROR_MUTEX_OWNED
|Error, A mutex semaphore in the muxwait list is currently owned
|-
|292
|ERROR_WRONG_TYPE
|Error
|-
|640
|ERROR_TIMEOUT
|Error, The caller was blocked for ''ulTimeout'' milliseconds but ownership of the
semaphore could not be obtained within this time limit, Time has expired
|}


=== Include Info ===
== Parameters ==
#define  INCL_WINMESSAGEMGR
;''hmux'' ([[HMUX]]) - input: The handle of the muxwait semaphore to wait for.
#include <os2.h>


=== Usage Explanation ===
;''ulTimeout'' ([[ULONG]]) - input: Time-out in milliseconds.
WinWaitMuxWaitSem blocks until the specified muxwait semaphore clears.
:This is the maximum amount of time the user wants to allow the thread to be blocked.
MuxWait semaphores are edge triggered. This means that if a muxwait semaphore consists of a number of event semaphores and all have posted except for one, if one of the semaphores resets before the last hold-out posts then it will need to wait for the reset semaphore to post again.
:SEM_IMMEDIATE_RETURN (0): WinWaitMuxWaitSem returns without blocking the calling thread.
:SEM_INDEFINITE_WAIT (-1): WinWaitMuxWaitSem blocks the calling thread indefinitely.


If the MuxWait semaphore consists of mutex semaphores and was created with the DCMW_WAIT_ALL attribute, the process obtains ownership of all mutex semaphores only when all semaphores have been released. If the MuxWait semaphore was created with the DCMW_WAIT_ANY attribute, the process obtains ownership of the first mutex semaphore to release.
;''pulUser'' ([[PULONG]]) - output: Pointer to receive the user field.
:A pointer to receive the user field (from the muxwait semaphore data structure) of the semaphore that was posted or released.
:If DCMW_WAIT_ANY was specified in the ''flAttr'' parameter when the muxwait semaphore was created, this will be the user field of the semaphore that was posted or released. If the muxwait semaphore consists of mutex semaphores, any mutex semaphore that is released is owned by the caller.
:If DCMW_WAIT_ALL was specified in the ''flAttr'' parameter when the muxwait semaphore was created, this will be the user field of the last semaphore that was posted or released. (If the thread did not block, the last semaphore that was posted or released will also be the last semaphore in the muxwait-semaphore list.) If the muxwait semaphore consists of mutex semaphores, all of the mutex semaphores that are released are owned by the caller.


WinWaitMuxWaitSem is intended to be used in Presentation Manager applications in place of the DosWaitMuxWaitSem counterpart to avoid hanging the PM Message Queue.
== Returns ==
;''ulrc'' ([[APIRET]]) - returns: Return Code.
=== Gotchas ===
:0: NO_ERROR
The process calling WinWaitMuxWaitSem must first obtain access to the semaphore in question or ERROR_INVALID_HANDLE will be returned.
:6: ERROR_INVALID_HANDLE
If the thread to be blocked, waiting on an event semaphore, is not a PM thread,
:8: ERROR_NOT_ENOUGH_MEMORY
[[DosWaitMuxWaitSem]] should be used instead of WinWaitMuxWaitSem.
:87: ERROR_INVALID_PARAMETER
:95: ERROR_INTERRUPT
:103: ERROR_TOO_MANY_SEM_REQUESTS
:105: ERROR_SEM_OWNER_DIED
:286: ERROR_EMPTY_MUXWAIT
:287: ERROR_MUTEX_OWNED
:292: ERROR_WRONG_TYPE
:640: ERROR_TIMEOUT


=== Sample Code ===
== Errors ==
<pre>  
Possible returns from [[WinGetLastError]]
;0 (NO_ERROR)
:Successful completion.
;6 (ERROR_INVALID_HANDLE)
:An invalid handle was specified.
;8 (ERROR_NOT_ENOUGH_MEMORY)
:Not enough memory was available.
;87 (ERROR_INVALID_PARAMETER)
:An invalid parameter was specified.
;95 (ERROR_INTERRUPT)
:The call was interrupted.
;103 (ERROR_TOO_MANY_SEM_REQUESTS)
:Too many semaphore requests were made.
;105 (ERROR_SEM_OWNER_DIED)
:The semaphore owner died.
;286 (ERROR_EMPTY_MUXWAIT)
:The muxwait semaphore list was empty.
;287 (ERROR_MUTEX_OWNED)
:The mutex semaphore is already owned.
;292 (ERROR_WRONG_TYPE)
:The semaphore type was incorrect.
;640 (ERROR_TIMEOUT)
:The call timed out.
 
== Remarks ==
WinWaitMuxWaitSem is similar to DosWaitMuxWaitSem and enables a thread to wait for a muxwait semaphore to clear or for a window message sent by the WinSendMsg function from another thread to be received.
 
This function can be issued by any thread in the process that created the semaphore. Threads in other processes can also issue this function, but they must first gain access to the semaphore by issuing DosOpenMuxWaitSem.
 
Since the processing of a window message may take longer than the value specified by the ''ulTimeout'' parameter, this function may not return within the time specified by that value.
 
If the main thread (which normally processes messages) is waiting on a semaphore for a prolonged period of time, messages will not be processed, and the "Bad App" dialog can occur. This is expected behavior. To avoid this, a message-box can be displayed telling the user that the window is being initialized. While the window loads, that message box can process the incoming system messages.
 
== Example Code ==
Declaration:
<PRE>
#define INCL_WINMESSAGEMGR
#include <os2.h>
 
HMUX    hmux;      /* The handle of the muxwait semaphore to wait for. */
ULONG    ulTimeout;  /* Time-out in milliseconds. */
PULONG  pulUser;    /* Pointer to receive the user field. */
APIRET  ulrc;      /* Return Code. */
 
ulrc = WinWaitMuxWaitSem(hmux, ulTimeout, pulUser);
</PRE>
This example waits for a muxwait semaphore to clear. The handle of the semaphore is assumed to be in ''hmux''. The ''ulTimeout'' is the number of milliseconds that the calling thread will wait for the muxwait semaphore to clear. If the specified muxwait semaphore is not cleared during this time interval, the request times out.
<pre>
#define INCL_DOSSEMAPHORES  /* Semaphore values */
#define INCL_WINMESSAGEMGR
#define INCL_WINMESSAGEMGR
#include  
#include <os2.h>
#include <stdio.h>
 
#ifndef ERROR_TIMEOUT
  #define ERROR_TIMEOUT    640
  #define ERROR_INTERRUPT  95
#endif
 
HMUX  hmux;                  /* Muxwait semaphore handle */
ULONG ulTimeout;            /* Number of milliseconds to wait */
ULONG ulUser;                /* User field for the semaphore that was posted or released (returned) */
ULONG rc;                    /* Return code */
 
ulTimeout = 60000;  /* Wait for a maximum of 1 minute */
 
rc = WinWaitMuxWaitSem(hmux, ulTimeout, &ulUser);


HMUX  hmuxMySemaphore;    /* MySemaphore handle */
/* On successful return, the ulUser variable contains the user identifier of the semaphore that caused the wait to terminate. */
ULONG TimeOut= -1;        /* the number of milliseconds the */
/* If the caller had to wait for all the semaphores within the muxwait semaphore to clear, then the value corresponds to the last semaphore within the muxwait semaphore to clear. */
                          /* the caller will block for the sem */
/* If the caller had to wait for any semaphore with the muxwait semaphore to clear, then the value corresponds to that semaphore. */
                          /* this example will block forever */
ULONG ulUserField;        /* to be filled with useful info */
APIRET rc;                /* return code */


/* access is gained to the semaphore in question */
if (rc == ERROR_TIMEOUT)
/* either by DosCreateEventSem ... */
{
/* ... or by DosOpenEventSem */
    printf("WinWaitMuxWaitSem call timed out\n");
/* its handle is placed in hevMySemaphore */
    return;
}


rc = WinWaitMuxWaitSem(hevMySemaphore, TimeOut,&ulUserField);
if (rc == ERROR_INTERRUPT)
{
    printf("WinWaitMuxWaitSem call was interrupted\n");
    return;
}


if (rc != 0)
if (rc != 0)
{
{
  /* We got an error to take care of. */
    printf("WinWaitMuxWaitSem error: return code = %ld\n", rc);
}
    return;
}
</pre>
</pre>


=== See Also ===
== Related Functions ==
*[[DosAddMuxWaitSem]]
* [[WinPostMsg]]
*[[DosCloseMuxWaitSem]]
* [[WinSendMsg]]
*[[DosCreateMuxWaitSem]]
*[[DosDeleteMuxWaitSem]]
*[[DosOpenMuxWaitSem]]
*[[DosQueryMuxWaitSem]]
*[[DosWaitMuxWaitSem]]  


[[Category:Win]]
[[Category:Win]]

Latest revision as of 16:41, 15 May 2025

This function waits for a muxwait semaphore to clear or for a Presentation Manager message.

Syntax

WinWaitMuxWaitSem(hmux, ulTimeout, pulUser);

Parameters

hmux (HMUX) - input
The handle of the muxwait semaphore to wait for.
ulTimeout (ULONG) - input
Time-out in milliseconds.
This is the maximum amount of time the user wants to allow the thread to be blocked.
SEM_IMMEDIATE_RETURN (0): WinWaitMuxWaitSem returns without blocking the calling thread.
SEM_INDEFINITE_WAIT (-1): WinWaitMuxWaitSem blocks the calling thread indefinitely.
pulUser (PULONG) - output
Pointer to receive the user field.
A pointer to receive the user field (from the muxwait semaphore data structure) of the semaphore that was posted or released.
If DCMW_WAIT_ANY was specified in the flAttr parameter when the muxwait semaphore was created, this will be the user field of the semaphore that was posted or released. If the muxwait semaphore consists of mutex semaphores, any mutex semaphore that is released is owned by the caller.
If DCMW_WAIT_ALL was specified in the flAttr parameter when the muxwait semaphore was created, this will be the user field of the last semaphore that was posted or released. (If the thread did not block, the last semaphore that was posted or released will also be the last semaphore in the muxwait-semaphore list.) If the muxwait semaphore consists of mutex semaphores, all of the mutex semaphores that are released are owned by the caller.

Returns

ulrc (APIRET) - returns
Return Code.
0: NO_ERROR
6: ERROR_INVALID_HANDLE
8: ERROR_NOT_ENOUGH_MEMORY
87: ERROR_INVALID_PARAMETER
95: ERROR_INTERRUPT
103: ERROR_TOO_MANY_SEM_REQUESTS
105: ERROR_SEM_OWNER_DIED
286: ERROR_EMPTY_MUXWAIT
287: ERROR_MUTEX_OWNED
292: ERROR_WRONG_TYPE
640: ERROR_TIMEOUT

Errors

Possible returns from WinGetLastError

0 (NO_ERROR)
Successful completion.
6 (ERROR_INVALID_HANDLE)
An invalid handle was specified.
8 (ERROR_NOT_ENOUGH_MEMORY)
Not enough memory was available.
87 (ERROR_INVALID_PARAMETER)
An invalid parameter was specified.
95 (ERROR_INTERRUPT)
The call was interrupted.
103 (ERROR_TOO_MANY_SEM_REQUESTS)
Too many semaphore requests were made.
105 (ERROR_SEM_OWNER_DIED)
The semaphore owner died.
286 (ERROR_EMPTY_MUXWAIT)
The muxwait semaphore list was empty.
287 (ERROR_MUTEX_OWNED)
The mutex semaphore is already owned.
292 (ERROR_WRONG_TYPE)
The semaphore type was incorrect.
640 (ERROR_TIMEOUT)
The call timed out.

Remarks

WinWaitMuxWaitSem is similar to DosWaitMuxWaitSem and enables a thread to wait for a muxwait semaphore to clear or for a window message sent by the WinSendMsg function from another thread to be received.

This function can be issued by any thread in the process that created the semaphore. Threads in other processes can also issue this function, but they must first gain access to the semaphore by issuing DosOpenMuxWaitSem.

Since the processing of a window message may take longer than the value specified by the ulTimeout parameter, this function may not return within the time specified by that value.

If the main thread (which normally processes messages) is waiting on a semaphore for a prolonged period of time, messages will not be processed, and the "Bad App" dialog can occur. This is expected behavior. To avoid this, a message-box can be displayed telling the user that the window is being initialized. While the window loads, that message box can process the incoming system messages.

Example Code

Declaration:

#define INCL_WINMESSAGEMGR
#include <os2.h>

HMUX     hmux;       /* The handle of the muxwait semaphore to wait for. */
ULONG    ulTimeout;  /* Time-out in milliseconds. */
PULONG   pulUser;    /* Pointer to receive the user field. */
APIRET   ulrc;       /* Return Code. */

ulrc = WinWaitMuxWaitSem(hmux, ulTimeout, pulUser);

This example waits for a muxwait semaphore to clear. The handle of the semaphore is assumed to be in hmux. The ulTimeout is the number of milliseconds that the calling thread will wait for the muxwait semaphore to clear. If the specified muxwait semaphore is not cleared during this time interval, the request times out.

#define INCL_DOSSEMAPHORES   /* Semaphore values */
#define INCL_WINMESSAGEMGR
#include <os2.h>
#include <stdio.h>

#ifndef ERROR_TIMEOUT
  #define ERROR_TIMEOUT    640
  #define ERROR_INTERRUPT  95
#endif

HMUX  hmux;                  /* Muxwait semaphore handle */
ULONG ulTimeout;             /* Number of milliseconds to wait */
ULONG ulUser;                /* User field for the semaphore that was posted or released (returned) */
ULONG rc;                    /* Return code */

ulTimeout = 60000;  /* Wait for a maximum of 1 minute */

rc = WinWaitMuxWaitSem(hmux, ulTimeout, &ulUser);

/* On successful return, the ulUser variable contains the user identifier of the semaphore that caused the wait to terminate. */
/* If the caller had to wait for all the semaphores within the muxwait semaphore to clear, then the value corresponds to the last semaphore within the muxwait semaphore to clear. */
/* If the caller had to wait for any semaphore with the muxwait semaphore to clear, then the value corresponds to that semaphore. */

if (rc == ERROR_TIMEOUT)
{
    printf("WinWaitMuxWaitSem call timed out\n");
    return;
}

if (rc == ERROR_INTERRUPT)
{
    printf("WinWaitMuxWaitSem call was interrupted\n");
    return;
}

if (rc != 0)
{
    printf("WinWaitMuxWaitSem error: return code = %ld\n", rc);
    return;
}

Related Functions