Commit d282724c authored by Ulrich Sibiller's avatar Ulrich Sibiller Committed by Mike Gabriel

os/Waitfor.c: simplify macro handling

1. indent ifdefs 2. add some logic at start that unset NX_TRANS_WAKEUP and NX_TRANS_DEBUG if NX_TRANS_SOCKET is unset. This way we only have to check for one macro and not all three. 3. remove redundant macro checks 4. decouple debug printfs by always running the original code and adding the identical check in #ifdef NX_TRANS_DEBUG
parent fbf18cfd
...@@ -96,10 +96,21 @@ SOFTWARE. ...@@ -96,10 +96,21 @@ SOFTWARE.
#include "dpmsproc.h" #include "dpmsproc.h"
#endif #endif
#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_WAKEUP) /*
* unset defines without NX_TRANS_SOCKET. This allows for shorter
* ifdefs below
*/
#ifndef NX_TRANS_SOCKET
#ifdef NX_TRANS_DEBUG
#undef NX_TRANS_DEBUG
#endif
#ifdef NX_TRANS_WAKEUP
#undef NX_TRANS_WAKEUP
#endif
#endif
#ifdef NX_TRANS_WAKEUP
static unsigned long startTimeInMillis; static unsigned long startTimeInMillis;
#endif #endif
/* This is just a fallback to errno to hide the differences between unix and /* This is just a fallback to errno to hide the differences between unix and
...@@ -171,17 +182,15 @@ WaitForSomething(int *pClientsReady) ...@@ -171,17 +182,15 @@ WaitForSomething(int *pClientsReady)
Bool someReady = FALSE; Bool someReady = FALSE;
Bool someNotifyWriteReady = FALSE; Bool someNotifyWriteReady = FALSE;
#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_DEBUG) #ifdef NX_TRANS_DEBUG
fprintf(stderr, "WaitForSomething: Got called.\n"); fprintf(stderr, "WaitForSomething: Got called.\n");
#endif #endif
FD_ZERO(&clientsReadable); FD_ZERO(&clientsReadable);
#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_WAKEUP) #ifdef NX_TRANS_WAKEUP
startTimeInMillis = GetTimeInMillis(); startTimeInMillis = GetTimeInMillis();
#endif
#endif
/* We need a while loop here to handle /* We need a while loop here to handle
crashed connections and the screen saver timeout */ crashed connections and the screen saver timeout */
...@@ -224,44 +233,44 @@ WaitForSomething(int *pClientsReady) ...@@ -224,44 +233,44 @@ WaitForSomething(int *pClientsReady)
if (NewOutputPending) if (NewOutputPending)
FlushAllOutput(); FlushAllOutput();
#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_WAKEUP) #ifdef NX_TRANS_WAKEUP
/* /*
* If caller has marked the first element of pClientsReady[], * If caller has marked the first element of pClientsReady[],
* bail out of select after a short timeout. We need this to * bail out of select after a short (NX_TRANS_WAKEUP)
* let the NX agent remove the splash screen when the timeout * timeout. We need this to let the NX agent remove the splash
* is expired. A better option would be to use the existing * screen when the timeout is expired. A better option would
* screen-saver timeout but it can be modified by clients, so * be to use the existing screen-saver timeout but it can be
* we would need a special handling. This hack is trivial and * modified by clients, so we would need a special
* keeps WaitForSomething() backward compatible with the exis- * handling. This hack is trivial and keeps WaitForSomething()
* ting servers. * backward compatible with the existing servers.
*/ */
if (pClientsReady[0] == -1) if (pClientsReady[0] == -1)
{ {
unsigned long timeoutInMillis; unsigned long timeoutInMillis;
#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_WAKEUP) && defined(NX_TRANS_DEBUG) #ifdef NX_TRANS_DEBUG
fprintf(stderr, "WaitForSomething: pClientsReady[0] is [%d], pClientsReady[1] is [%d].\n", fprintf(stderr, "WaitForSomething: pClientsReady[0] is [%d], pClientsReady[1] is [%d].\n",
pClientsReady[0], pClientsReady[1]); pClientsReady[0], pClientsReady[1]);
#endif #endif
timeoutInMillis = GetTimeInMillis(); timeoutInMillis = GetTimeInMillis();
if (timeoutInMillis - startTimeInMillis >= NX_TRANS_WAKEUP) if (timeoutInMillis - startTimeInMillis >= NX_TRANS_WAKEUP)
{ {
#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_WAKEUP) && defined(NX_TRANS_DEBUG) #ifdef NX_TRANS_DEBUG
fprintf(stderr, "WaitForSomething: Returning 0 because of wakeup timeout.\n"); fprintf(stderr, "WaitForSomething: Returning 0 because of wakeup timeout.\n");
#endif #endif
return 0; return 0;
} }
timeoutInMillis = NX_TRANS_WAKEUP - (timeoutInMillis - startTimeInMillis); timeoutInMillis = NX_TRANS_WAKEUP - (timeoutInMillis - startTimeInMillis);
#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_WAKEUP) && defined(NX_TRANS_DEBUG) #ifdef NX_TRANS_DEBUG
fprintf(stderr, "WaitForSomething: Milliseconds to next wakeup are %ld.\n", fprintf(stderr, "WaitForSomething: Milliseconds to next wakeup are %ld.\n",
timeoutInMillis); timeoutInMillis);
#endif #endif
if (wt == NULL || (wt -> tv_sec * MILLI_PER_SECOND + if (wt == NULL || (wt -> tv_sec * MILLI_PER_SECOND +
wt -> tv_usec / MILLI_PER_SECOND) > timeoutInMillis) wt -> tv_usec / MILLI_PER_SECOND) > timeoutInMillis)
{ {
...@@ -274,38 +283,33 @@ WaitForSomething(int *pClientsReady) ...@@ -274,38 +283,33 @@ WaitForSomething(int *pClientsReady)
wt = &waittime; wt = &waittime;
} }
#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_WAKEUP) && defined(NX_TRANS_DEBUG) #ifdef NX_TRANS_DEBUG
fprintf(stderr, "WaitForSomething: Next wakeup timeout set to %ld milliseconds.\n", fprintf(stderr, "WaitForSomething: Next wakeup timeout set to %ld milliseconds.\n",
(waittime.tv_sec * MILLI_PER_SECOND) + (waittime.tv_sec * MILLI_PER_SECOND) +
(waittime.tv_usec / MILLI_PER_SECOND)); (waittime.tv_usec / MILLI_PER_SECOND));
#endif #endif
} }
#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_WAKEUP) && defined(NX_TRANS_DEBUG) #ifdef NX_TRANS_DEBUG
else else
{ {
fprintf(stderr, "WaitForSomething: Using existing timeout of %ld milliseconds.\n", fprintf(stderr, "WaitForSomething: Using existing timeout of %ld milliseconds.\n",
(waittime.tv_sec * MILLI_PER_SECOND) + (waittime.tv_sec * MILLI_PER_SECOND) +
(waittime.tv_usec / MILLI_PER_SECOND)); (waittime.tv_usec / MILLI_PER_SECOND));
} }
#endif #endif
} }
#endif #endif /* defined(NX_TRANS_WAKEUP) */
/* keep this check close to select() call to minimize race */ /* keep this check close to select() call to minimize race */
#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_DEBUG) #ifdef NX_TRANS_DEBUG
if (dispatchException) if (dispatchException)
{
i = -1;
fprintf(stderr, "WaitForSomething: Value of dispatchException is true. Set i = -1.\n"); fprintf(stderr, "WaitForSomething: Value of dispatchException is true. Set i = -1.\n");
} #endif
#else
if (dispatchException) if (dispatchException)
i = -1; i = -1;
#endif
else if (AnyWritesPending) else if (AnyWritesPending)
{ {
#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_DEBUG) #ifdef NX_TRANS_DEBUG
if (wt == NULL) if (wt == NULL)
{ {
fprintf(stderr, "WaitForSomething: Executing select with LastSelectMask and " fprintf(stderr, "WaitForSomething: Executing select with LastSelectMask and "
...@@ -317,14 +321,14 @@ WaitForSomething(int *pClientsReady) ...@@ -317,14 +321,14 @@ WaitForSomething(int *pClientsReady)
"clientsWritable, %ld secs and %ld usecs.\n", "clientsWritable, %ld secs and %ld usecs.\n",
wt -> tv_sec, wt -> tv_usec); wt -> tv_sec, wt -> tv_usec);
} }
#endif #endif
XFD_COPYSET(&ClientsWriteBlocked, &LastSelectWriteMask); XFD_COPYSET(&ClientsWriteBlocked, &LastSelectWriteMask);
XFD_ORSET(&LastSelectWriteMask, &NotifyWriteFds, &LastSelectWriteMask); XFD_ORSET(&LastSelectWriteMask, &NotifyWriteFds, &LastSelectWriteMask);
i = Select(MaxClients, &LastSelectMask, &LastSelectWriteMask, NULL, wt); i = Select(MaxClients, &LastSelectMask, &LastSelectWriteMask, NULL, wt);
} }
else else
{ {
#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_DEBUG) #ifdef NX_TRANS_DEBUG
if (wt == NULL) if (wt == NULL)
{ {
fprintf(stderr, "WaitForSomething: Executing select with LastSelectMask and null timeout.\n"); fprintf(stderr, "WaitForSomething: Executing select with LastSelectMask and null timeout.\n");
...@@ -334,17 +338,16 @@ WaitForSomething(int *pClientsReady) ...@@ -334,17 +338,16 @@ WaitForSomething(int *pClientsReady)
fprintf(stderr, "WaitForSomething: Executing select with LastSelectMask, %ld secs and %ld usecs.\n", fprintf(stderr, "WaitForSomething: Executing select with LastSelectMask, %ld secs and %ld usecs.\n",
wt -> tv_sec, wt -> tv_usec); wt -> tv_sec, wt -> tv_usec);
} }
#endif #endif
i = Select (MaxClients, &LastSelectMask, NULL, NULL, wt); i = Select (MaxClients, &LastSelectMask, NULL, NULL, wt);
} }
#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_DEBUG) #ifdef NX_TRANS_DEBUG
fprintf(stderr, "WaitForSomething: Bailed out with i = [%d] and errno = [%d].\n", i, errno); fprintf(stderr, "WaitForSomething: Bailed out with i = [%d] and errno = [%d].\n", i, errno);
if (i < 0) if (i < 0)
{ {
fprintf(stderr, "WaitForSomething: Error is [%s].\n", strerror(errno)); fprintf(stderr, "WaitForSomething: Error is [%s].\n", strerror(errno));
} }
#endif #endif
selecterr = GetErrno(); selecterr = GetErrno();
WakeupHandler(i, (void *)&LastSelectMask); WakeupHandler(i, (void *)&LastSelectMask);
...@@ -352,31 +355,29 @@ WaitForSomething(int *pClientsReady) ...@@ -352,31 +355,29 @@ WaitForSomething(int *pClientsReady)
if (i <= 0) /* An error or timeout occurred */ if (i <= 0) /* An error or timeout occurred */
{ {
#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_DEBUG) #ifdef NX_TRANS_DEBUG
if (dispatchException) if (dispatchException)
{ {
fprintf(stderr, "WaitForSomething: Returning 0 because of (dispatchException).\n"); fprintf(stderr, "WaitForSomething: Returning 0 because of (dispatchException).\n");
return 0;
} }
#else #endif
if (dispatchException) if (dispatchException)
return 0; return 0;
#endif
if (i < 0) if (i < 0)
{ {
if (selecterr == EBADF) /* Some client disconnected */ if (selecterr == EBADF) /* Some client disconnected */
{ {
CheckConnections (); CheckConnections ();
#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_DEBUG)
#ifdef NX_TRANS_DEBUG
if (! XFD_ANYSET (&AllClients)) if (! XFD_ANYSET (&AllClients))
{ {
fprintf(stderr, "WaitForSomething: Returning 0 because of (! XFD_ANYSET (&AllClients)).\n"); fprintf(stderr, "WaitForSomething: Returning 0 because of (! XFD_ANYSET (&AllClients)).\n");
return 0;
} }
#else #endif
if (! XFD_ANYSET (&AllClients)) if (! XFD_ANYSET (&AllClients))
return 0; return 0;
#endif
} }
else if (selecterr == EINVAL) else if (selecterr == EINVAL)
{ {
...@@ -398,18 +399,14 @@ WaitForSomething(int *pClientsReady) ...@@ -398,18 +399,14 @@ WaitForSomething(int *pClientsReady)
XFD_COPYSET(&ClientsWithInput, &clientsReadable); XFD_COPYSET(&ClientsWithInput, &clientsReadable);
break; break;
} }
#if defined(NX_TRANS_SOCKET) #ifdef NX_TRANS_DEBUG
if (*checkForInput[0] != *checkForInput[1]) if (*checkForInput[0] != *checkForInput[1])
{ {
#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_DEBUG)
fprintf(stderr, "WaitForSomething: Returning 0 because of (*checkForInput[0] != *checkForInput[1]).\n"); fprintf(stderr, "WaitForSomething: Returning 0 because of (*checkForInput[0] != *checkForInput[1]).\n");
#endif
return 0;
} }
#else #endif
if (*checkForInput[0] != *checkForInput[1]) if (*checkForInput[0] != *checkForInput[1])
return 0; return 0;
#endif
if (timers) if (timers)
{ {
...@@ -490,9 +487,9 @@ WaitForSomething(int *pClientsReady) ...@@ -490,9 +487,9 @@ WaitForSomething(int *pClientsReady)
} }
} }
} }
#if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_DEBUG) #ifdef NX_TRANS_DEBUG
fprintf(stderr, "WaitForSomething: Returning nready.\n"); fprintf(stderr, "WaitForSomething: Returning nready.\n");
#endif #endif
return nready; return nready;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment