VirtualBox

Changeset 101885 in vbox


Ignore:
Timestamp:
Nov 6, 2023 5:52:28 PM (15 months ago)
Author:
vboxsync
Message:

libs/xpcom: Start converting some code to use IPRT exclusively, bugref:10545

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/libs/xpcom18a4/ipc/ipcd/client/src/ipcConnectionUnix.cpp

    r99775 r101885  
    3838#include "private/pprio.h"
    3939#include "prerror.h"
    40 #include "prthread.h"
    41 #include "prlock.h"
    42 #include "prlog.h" // for PR_ASSERT (we don't actually use NSPR logging)
    4340#include "prio.h"
    4441
     
    4946
    5047#ifdef VBOX
    51 # include "prenv.h"
     48# include <iprt/assert.h>
     49# include <iprt/critsect.h>
     50# include <iprt/err.h>
     51# include <iprt/env.h>
     52# include <iprt/log.h>
     53# include <iprt/thread.h>
     54
    5255# include <stdio.h>
    53 # include <VBox/log.h>
    5456#endif
    5557
     
    6466
    6567
    66 // single user systems, like OS/2, don't need these security checks.
    67 #ifndef XP_OS2
    68 #define IPC_SKIP_SECURITY_CHECKS
    69 #endif
    70 
    71 #ifndef IPC_SKIP_SECURITY_CHECKS
    7268#include <unistd.h>
    7369#include <sys/stat.h>
    74 #endif
    7570
    7671static PRStatus
    7772DoSecurityCheck(PRFileDesc *fd, const char *path)
    7873{
    79 #ifndef IPC_SKIP_SECURITY_CHECKS
    8074    //
    8175    // now that we have a connected socket; do some security checks on the
     
    116110        }
    117111    }
    118 #endif
     112
    119113    return PR_SUCCESS;
    120114}
     
    134128struct ipcConnectionState
    135129{
    136   PRLock      *lock;
     130  RTCRITSECT   CritSect;
    137131  PRPollDesc   fds[2];
    138132  ipcCallbackQ callback_queue;
     
    146140#define POLL 1
    147141
    148 static void
    149 ConnDestroy(ipcConnectionState *s)
    150 {
    151   if (s->lock)
    152     PR_DestroyLock(s->lock); 
     142static void ConnDestroy(ipcConnectionState *s)
     143{
     144  if (RTCritSectIsInitialized(&s->CritSect))
     145    RTCritSectDelete(&s->CritSect);
    153146
    154147  if (s->fds[SOCK].fd)
     
    165158}
    166159
    167 static ipcConnectionState *
    168 ConnCreate(PRFileDesc *fd)
     160static ipcConnectionState *ConnCreate(PRFileDesc *fd)
    169161{
    170162  ipcConnectionState *s = new ipcConnectionState;
     
    172164    return NULL;
    173165
    174   s->lock = PR_NewLock();
     166  int vrc = RTCritSectInit(&s->CritSect);
     167  if (RT_FAILURE(vrc))
     168  {
     169    ConnDestroy(s);
     170    return NULL;
     171  }
     172
    175173  s->fds[SOCK].fd = NULL;
    176174  s->fds[POLL].fd = PR_NewPollableEvent();
     
    179177  s->shutdown = PR_FALSE;
    180178
    181   if (!s->lock || !s->fds[1].fd)
     179  if (!s->fds[1].fd)
    182180  {
    183181    ConnDestroy(s);
     
    251249        }
    252250
    253         PR_ASSERT(PRUint32(n) >= bytesRead);
     251        Assert(PRUint32(n) >= bytesRead);
    254252        n -= bytesRead;
    255253        pdata += bytesRead;
     
    276274  nsresult rv = NS_OK;
    277275
    278   PR_Lock(s->lock);
     276  RTCritSectEnter(&s->CritSect);
    279277
    280278  // write one message and then return.
     
    312310  }
    313311
    314   PR_Unlock(s->lock);
     312  RTCritSectLeave(&s->CritSect);
    315313  return rv;
    316314}
    317315
    318 PR_STATIC_CALLBACK(void)
    319 ConnThread(void *arg)
    320 {
     316static DECLCALLBACK(int) ipcConnThread(RTTHREAD hSelf, void *pvArg)
     317{
     318  RT_NOREF(hSelf);
     319
    321320  PRInt32 num;
    322321  nsresult rv = NS_OK;
    323322
    324   ipcConnectionState *s = (ipcConnectionState *) arg;
     323  ipcConnectionState *s = (ipcConnectionState *)pvArg;
    325324
    326325  // we monitor two file descriptors in this thread.  the first (at index 0) is
     
    352351      {
    353352        PR_WaitForPollableEvent(s->fds[POLL].fd);
    354         PR_Lock(s->lock);
     353        RTCritSectEnter(&s->CritSect);
    355354
    356355        if (!s->send_queue.IsEmpty())
     
    360359          s->callback_queue.MoveTo(cbs_to_run);
    361360
    362         PR_Unlock(s->lock);
     361        RTCritSectLeave(&s->CritSect);
    363362      }
    364363
     
    382381      // request until after all queued up messages have been sent and until
    383382      // after all queued up callbacks have been run.
    384       PR_Lock(s->lock);
     383      RTCritSectEnter(&s->CritSect);
    385384      if (s->shutdown && s->send_queue.IsEmpty() && s->callback_queue.IsEmpty())
    386385        rv = NS_ERROR_ABORT;
    387       PR_Unlock(s->lock);
     386      RTCritSectLeave(&s->CritSect);
    388387    }
    389388    else
     
    401400
    402401  LOG(("IPC thread exiting\n"));
     402  return VINF_SUCCESS;
    403403}
    404404
     
    408408
    409409static ipcConnectionState *gConnState = NULL;
    410 static PRThread *gConnThread = NULL;
     410static RTTHREAD gConnThread = NULL;
    411411
    412412#ifdef DEBUG
    413 static PRThread *gMainThread = NULL;
     413static RTTHREAD gMainThread = NULL;
    414414#endif
    415415
     
    436436
    437437#ifdef VBOX
    438   if (PR_GetEnv("TESTBOX_UUID"))
     438  if (RTEnvExist("TESTBOX_UUID"))
    439439    fprintf(stderr, "IPC socket path: %s\n", addr.local.path);
    440440  LogRel(("IPC socket path: %s\n", addr.local.path));
     
    467467  PRFileDesc *fd = NULL;
    468468  nsresult rv = NS_ERROR_FAILURE;
     469  int vrc = VINF_SUCCESS;
    469470
    470471  if (gConnState)
     
    505506  fd = NULL; // connection state now owns the socket
    506507
    507   gConnThread = PR_CreateThread(PR_USER_THREAD,
    508                                 ConnThread,
    509                                 gConnState,
    510                                 PR_PRIORITY_NORMAL,
    511                                 PR_GLOBAL_THREAD,
    512                                 PR_JOINABLE_THREAD,
    513                                 0);
    514   if (!gConnThread)
     508  vrc = RTThreadCreate(&gConnThread, ipcConnThread, gConnState, 0 /*cbStack*/,
     509                       RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "Ipc-Conn");
     510  if (RT_FAILURE(vrc))
    515511  {
    516512    rv = NS_ERROR_OUT_OF_MEMORY;
     
    519515
    520516#ifdef DEBUG
    521   gMainThread = PR_GetCurrentThread();
     517  gMainThread = RTThreadSelf();
    522518#endif
    523519  return NS_OK;
     
    538534{
    539535  // Must disconnect on same thread used to connect!
    540   PR_ASSERT(gMainThread == PR_GetCurrentThread());
     536  Assert(gMainThread == RTThreadSelf());
    541537
    542538  if (!gConnState || !gConnThread)
    543539    return NS_ERROR_NOT_INITIALIZED;
    544540
    545   PR_Lock(gConnState->lock);
     541  RTCritSectEnter(&gConnState->CritSect);
    546542  gConnState->shutdown = PR_TRUE;
    547543  PR_SetPollableEvent(gConnState->fds[POLL].fd);
    548   PR_Unlock(gConnState->lock);
    549 
    550   PR_JoinThread(gConnThread);
     544  RTCritSectLeave(&gConnState->CritSect);
     545
     546  int rcThread;
     547  RTThreadWait(gConnThread, RT_INDEFINITE_WAIT, &rcThread);
     548  AssertRC(rcThread);
    551549
    552550  ConnDestroy(gConnState);
     
    563561    return NS_ERROR_NOT_INITIALIZED;
    564562
    565   PR_Lock(gConnState->lock);
     563  RTCritSectEnter(&gConnState->CritSect);
    566564  gConnState->send_queue.Append(msg);
    567565  PR_SetPollableEvent(gConnState->fds[POLL].fd);
    568   PR_Unlock(gConnState->lock);
     566  RTCritSectLeave(&gConnState->CritSect);
    569567
    570568  return NS_OK;
     
    583581  callback->arg = arg;
    584582
    585   PR_Lock(gConnState->lock);
     583  RTCritSectEnter(&gConnState->CritSect);
    586584  gConnState->callback_queue.Append(callback);
    587585  PR_SetPollableEvent(gConnState->fds[POLL].fd);
    588   PR_Unlock(gConnState->lock);
     586  RTCritSectLeave(&gConnState->CritSect);
    589587  return NS_OK;
    590588}
    591589
    592 //-----------------------------------------------------------------------------
    593 
    594 #ifdef TEST_STANDALONE
    595 
    596 void IPC_OnConnectionFault(nsresult rv)
    597 {
    598   LOG(("IPC_OnConnectionFault [rv=%x]\n", rv));
    599 }
    600 
    601 void IPC_OnMessageAvailable(ipcMessage *msg)
    602 {
    603   LOG(("IPC_OnMessageAvailable\n"));
    604   delete msg;
    605 }
    606 
    607 int main()
    608 {
    609   IPC_InitLog(">>>");
    610   IPC_Connect("/builds/moz-trunk/seamonkey-debug-build/dist/bin/mozilla-ipcd");
    611   IPC_Disconnect();
    612   return 0;
    613 }
    614 
    615 #endif
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette