Back to index

lightning-sunbird  0.9+nobinonly
ipcLockService.cpp
Go to the documentation of this file.
00001 /* vim:set ts=4 sw=4 sts=4 et cindent: */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla IPC.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 2002
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Darin Fisher <darin@netscape.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include <stdlib.h>
00040 #include "nsDependentString.h"
00041 #include "nsHashKeys.h"
00042 #include "nsAutoPtr.h"
00043 #include "ipcLockService.h"
00044 #include "ipcLockProtocol.h"
00045 #include "ipcLog.h"
00046 #include "prthread.h"
00047 
00048 static const nsID kLockTargetID = IPC_LOCK_TARGETID;
00049 
00050 //-----------------------------------------------------------------------------
00051 
00052 struct ipcPendingLock
00053 {
00054     const char *name;
00055     nsresult    status;
00056     PRBool      complete;
00057 };
00058 
00059 //-----------------------------------------------------------------------------
00060 
00061 nsresult
00062 ipcLockService::Init()
00063 {
00064     if (PR_NewThreadPrivateIndex(&mTPIndex, nsnull) != PR_SUCCESS)
00065         return NS_ERROR_OUT_OF_MEMORY;
00066 
00067     // Configure OnMessageAvailable to be called on the IPC thread.  This is
00068     // done to allow us to proxy OnAcquireLockComplete events to the right
00069     // thread immediately even if the main thread is blocked waiting to acquire
00070     // some other lock synchronously.
00071 
00072     return IPC_DefineTarget(kLockTargetID, this, PR_FALSE);
00073 }
00074 
00075 NS_IMPL_THREADSAFE_ISUPPORTS2(ipcLockService, ipcILockService, ipcIMessageObserver)
00076 
00077 NS_IMETHODIMP
00078 ipcLockService::AcquireLock(const char *lockName, PRBool waitIfBusy)
00079 {
00080     LOG(("ipcLockService::AcquireLock [lock=%s wait=%u]\n", lockName, waitIfBusy));
00081 
00082     ipcLockMsg msg;
00083     msg.opcode = IPC_LOCK_OP_ACQUIRE;
00084     msg.flags = (waitIfBusy ? 0 : IPC_LOCK_FL_NONBLOCKING);
00085     msg.key = lockName;
00086 
00087     PRUint32 bufLen;
00088     nsAutoPtr<PRUint8> buf( IPC_FlattenLockMsg(&msg, &bufLen) );
00089     if (!buf)
00090         return NS_ERROR_OUT_OF_MEMORY;
00091 
00092     ipcPendingLock pendingLock;
00093     pendingLock.name = lockName;
00094     pendingLock.status = 0xDEADBEEF; // something bogus
00095     pendingLock.complete = PR_FALSE;
00096     if (PR_SetThreadPrivate(mTPIndex, &pendingLock) != PR_SUCCESS)
00097         return NS_ERROR_UNEXPECTED;
00098 
00099     // prevent our OnMessageAvailable from being called until we explicitly ask
00100     // for it to be called via IPC_WaitMessage.
00101     IPC_DISABLE_MESSAGE_OBSERVER_FOR_SCOPE(kLockTargetID);
00102 
00103     nsresult rv = IPC_SendMessage(0, kLockTargetID, buf, bufLen);
00104     if (NS_SUCCEEDED(rv)) {
00105         do {
00106             // block the calling thread until we get a response from the daemon
00107             rv = IPC_WaitMessage(0, kLockTargetID, this, PR_INTERVAL_NO_TIMEOUT);
00108         }
00109         while (NS_SUCCEEDED(rv) && !pendingLock.complete);
00110 
00111         if (NS_SUCCEEDED(rv))
00112             rv = pendingLock.status;
00113     }
00114 
00115     // we could clear the TPD, but that isn't really necessary.
00116 
00117     return rv;
00118 }
00119 
00120 NS_IMETHODIMP
00121 ipcLockService::ReleaseLock(const char *lockName)
00122 {
00123     LOG(("ipcLockService::ReleaseLock [lock=%s]\n", lockName));
00124 
00125     ipcLockMsg msg;
00126     msg.opcode = IPC_LOCK_OP_RELEASE;
00127     msg.flags = 0;
00128     msg.key = lockName;
00129 
00130     PRUint32 bufLen;
00131     PRUint8 *buf = IPC_FlattenLockMsg(&msg, &bufLen);
00132     if (!buf)
00133         return NS_ERROR_OUT_OF_MEMORY;
00134 
00135     nsresult rv = IPC_SendMessage(0, kLockTargetID, buf, bufLen);
00136     delete buf;
00137 
00138     if (NS_FAILED(rv))
00139         return rv;
00140 
00141     return NS_OK;
00142 }
00143 
00144 // called on the same thread that called IPC_WaitMessage
00145 NS_IMETHODIMP
00146 ipcLockService::OnMessageAvailable(PRUint32 unused, const nsID &target,
00147                                    const PRUint8 *data, PRUint32 dataLen)
00148 {
00149     ipcLockMsg msg;
00150     IPC_UnflattenLockMsg(data, dataLen, &msg);
00151 
00152     LOG(("ipcLockService::OnMessageAvailable [lock=%s opcode=%u]\n", msg.key, msg.opcode));
00153 
00154     ipcPendingLock *pendingLock = (ipcPendingLock *) PR_GetThreadPrivate(mTPIndex);
00155     if (strcmp(pendingLock->name, msg.key) == 0) {
00156         pendingLock->complete = PR_TRUE;
00157         if (msg.opcode == IPC_LOCK_OP_STATUS_ACQUIRED)
00158             pendingLock->status = NS_OK;
00159         else
00160             pendingLock->status = NS_ERROR_FAILURE;
00161         return NS_OK;
00162     }
00163 
00164     LOG(("message does not match; waiting for another...\n"));
00165 
00166     // else, we got a message that another thread is waiting to receive.
00167     return IPC_WAIT_NEXT_MESSAGE;
00168 }