This is Unofficial EPICS BASE Doxygen Site
epicsSingletonMutex.cpp
Go to the documentation of this file.
1 /*************************************************************************\
2 * Copyright (c) 2002 The University of Chicago, as Operator of Argonne
3 * National Laboratory.
4 * Copyright (c) 2002 The Regents of the University of California, as
5 * Operator of Los Alamos National Laboratory.
6 * EPICS BASE Versions 3.13.7
7 * and higher are distributed subject to a Software License Agreement found
8 * in file LICENSE that is included with this distribution.
9 \*************************************************************************/
10 
11 /*
12  * Author: Jeff O. Hill
13  */
14 
15 #include <climits>
16 
17 #include "epicsMutex.h"
18 #include "epicsGuard.h"
19 #include "epicsThread.h"
20 #include "epicsSingleton.h"
21 
22 #ifndef SIZE_MAX
23 # define SIZE_MAX UINT_MAX
24 #endif
25 
26 static epicsThreadOnceId epicsSigletonOnceFlag ( EPICS_THREAD_ONCE_INIT );
27 static epicsMutex * pEPICSSigletonMutex = 0;
28 
29 extern "C" void SingletonMutexOnce ( void * /* pParm */ )
30 {
31  // This class exists for the purpose of avoiding file scope
32  // object chicken and egg problems. Therefore, pEPICSSigletonMutex
33  // is never destroyed.
34  pEPICSSigletonMutex = newEpicsMutex;
35 }
36 
37 void SingletonUntyped :: incrRefCount ( PBuild pBuild )
38 {
39  epicsThreadOnce ( & epicsSigletonOnceFlag, SingletonMutexOnce, 0 );
41  guard ( *pEPICSSigletonMutex );
42  assert ( _refCount < SIZE_MAX );
43  if ( _refCount == 0 ) {
44  _pInstance = ( * pBuild ) ();
45  }
46  _refCount++;
47 }
48 
50 {
52  guard ( *pEPICSSigletonMutex );
53  assert ( _refCount > 0 );
54  _refCount--;
55  if ( _refCount == 0 ) {
56  ( *pDestroy ) ( _pInstance );
57  _pInstance = 0;
58  }
59 }
#define assert(exp)
Declare that a condition should be true.
Definition: epicsAssert.h:70
void(* PDestroy)(void *)
void decrRefCount(PDestroy)
#define SIZE_MAX
void SingletonMutexOnce(void *)
#define EPICS_THREAD_ONCE_INIT
Definition: epicsThread.h:109
APIs for the epicsMutex mutual exclusion semaphore.
LIBCOM_API void epicsStdCall epicsThreadOnce(epicsThreadOnceId *id, EPICSTHREADFUNC, void *arg)
C++ and C descriptions for a thread.