15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/* 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * The contents of this file are subject to the Mozilla Public 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * License Version 1.1 (the "License"); you may not use this file 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * except in compliance with the License. You may obtain a copy of 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * the License at http://www.mozilla.org/MPL/ 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Software distributed under the License is distributed on an "AS 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * implied. See the License for the specific language governing 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * rights and limitations under the License. 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * The Original Code is the Netscape Portable Runtime (NSPR). 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * The Initial Developer of the Original Code is Netscape 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Communications Corporation. Portions created by Netscape are 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Copyright (C) 1998-2000 Netscape Communications Corporation. All 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Rights Reserved. 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Contributor(s): 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Alternatively, the contents of this file may be used under the 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * terms of the GNU General Public License Version 2 or later (the 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * "GPL"), in which case the provisions of the GPL are applicable 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * instead of those above. If you wish to allow use of your 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * version of this file only under the terms of the GPL and not to 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * allow others to use your version of this file under the MPL, 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * indicate your decision by deleting the provisions above and 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * replace them with the notice and other provisions required by 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * the GPL. If you do not delete the provisions above, a recipient 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * may use your version of this file under either the MPL or the 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * GPL. 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */ 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef prsem_h___ 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define prsem_h___ 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/* 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)** API for counting semaphores. Semaphores are counting synchronizing 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)** variables based on a lock and a condition variable. They are lightweight 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)** contention control for a given count of resources. 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)*/ 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "prtypes.h" 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)PR_BEGIN_EXTERN_C 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef struct PRSemaphore PRSemaphore; 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/* 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)** Create a new semaphore object. 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)*/ 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)NSPR_API(PRSemaphore*) PR_NewSem(PRUintn value); 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/* 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)** Destroy the given semaphore object. 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)** 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)*/ 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)NSPR_API(void) PR_DestroySem(PRSemaphore *sem); 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/* 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)** Wait on a Semaphore. 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)** 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)** This routine allows a calling thread to wait or proceed depending upon the 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)** state of the semahore sem. The thread can proceed only if the counter value 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)** of the semaphore sem is currently greater than 0. If the value of semaphore 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)** sem is positive, it is decremented by one and the routine returns immediately 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)** allowing the calling thread to continue. If the value of semaphore sem is 0, 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)** the calling thread blocks awaiting the semaphore to be released by another 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)** thread. 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)** 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)** This routine can return PR_PENDING_INTERRUPT if the waiting thread 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)** has been interrupted. 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)*/ 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)NSPR_API(PRStatus) PR_WaitSem(PRSemaphore *sem); 755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/* 775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)** This routine increments the counter value of the semaphore. If other threads 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)** are blocked for the semaphore, then the scheduler will determine which ONE 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)** thread will be unblocked. 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)*/ 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)NSPR_API(void) PR_PostSem(PRSemaphore *sem); 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/* 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)** Returns the value of the semaphore referenced by sem without affecting 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)** the state of the semaphore. The value represents the semaphore vaule 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)F** at the time of the call, but may not be the actual value when the 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)** caller inspects it. 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)*/ 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)NSPR_API(PRUintn) PR_GetValueSem(PRSemaphore *sem); 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)PR_END_EXTERN_C 925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif /* prsem_h___ */ 94