1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 /*
5  * pkix_pl_monitorlock.c
6  *
7  * Read/Write Lock Functions
8  *
9  */
10 
11 #include "pkix_pl_monitorlock.h"
12 
13 /* --Private-Functions-------------------------------------------- */
14 
15 static PKIX_Error *
pkix_pl_MonitorLock_Destroy(PKIX_PL_Object * object,void * plContext)16 pkix_pl_MonitorLock_Destroy(
17         PKIX_PL_Object *object,
18         void *plContext)
19 {
20         PKIX_PL_MonitorLock* monitorLock = NULL;
21 
22         PKIX_ENTER(MONITORLOCK, "pkix_pl_MonitorLock_Destroy");
23         PKIX_NULLCHECK_ONE(object);
24 
25         PKIX_CHECK(pkix_CheckType(object, PKIX_MONITORLOCK_TYPE, plContext),
26                     PKIX_OBJECTNOTMONITORLOCK);
27 
28         monitorLock = (PKIX_PL_MonitorLock*) object;
29 
30         PKIX_MONITORLOCK_DEBUG("Calling PR_DestroyMonitor)\n");
31         PR_DestroyMonitor(monitorLock->lock);
32         monitorLock->lock = NULL;
33 
34 cleanup:
35 
36         PKIX_RETURN(MONITORLOCK);
37 }
38 
39 /*
40  * FUNCTION: pkix_pl_MonitorLock_RegisterSelf
41  * DESCRIPTION:
42  * Registers PKIX_MONITORLOCK_TYPE and its related functions with
43  * systemClasses[].
44  * THREAD SAFETY:
45  *  Not Thread Safe - for performance and complexity reasons
46  *
47  *  Since this function is only called by PKIX_PL_Initialize, which should
48  *  only be called once, it is acceptable that this function is not
49  *  thread-safe.
50  */
51 PKIX_Error *
pkix_pl_MonitorLock_RegisterSelf(void * plContext)52 pkix_pl_MonitorLock_RegisterSelf(
53         void *plContext)
54 {
55 
56         extern pkix_ClassTable_Entry systemClasses[PKIX_NUMTYPES];
57         pkix_ClassTable_Entry entry;
58 
59         PKIX_ENTER(MONITORLOCK, "pkix_pl_MonitorLock_RegisterSelf");
60 
61         entry.description = "MonitorLock";
62         entry.objCounter = 0;
63         entry.typeObjectSize = sizeof(PKIX_PL_MonitorLock);
64         entry.destructor = pkix_pl_MonitorLock_Destroy;
65         entry.equalsFunction = NULL;
66         entry.hashcodeFunction = NULL;
67         entry.toStringFunction = NULL;
68         entry.comparator = NULL;
69         entry.duplicateFunction = NULL;
70 
71         systemClasses[PKIX_MONITORLOCK_TYPE] = entry;
72 
73         PKIX_RETURN(MONITORLOCK);
74 }
75 
76 /* --Public-Functions--------------------------------------------- */
77 
78 PKIX_Error *
PKIX_PL_MonitorLock_Create(PKIX_PL_MonitorLock ** pNewLock,void * plContext)79 PKIX_PL_MonitorLock_Create(
80         PKIX_PL_MonitorLock **pNewLock,
81         void *plContext)
82 {
83         PKIX_PL_MonitorLock *monitorLock = NULL;
84 
85         PKIX_ENTER(MONITORLOCK, "PKIX_PL_MonitorLock_Create");
86         PKIX_NULLCHECK_ONE(pNewLock);
87 
88         PKIX_CHECK(PKIX_PL_Object_Alloc
89                     (PKIX_MONITORLOCK_TYPE,
90                     sizeof (PKIX_PL_MonitorLock),
91                     (PKIX_PL_Object **)&monitorLock,
92                     plContext),
93                     PKIX_ERRORALLOCATINGMONITORLOCK);
94 
95         PKIX_MONITORLOCK_DEBUG("\tCalling PR_NewMonitor)\n");
96         monitorLock->lock = PR_NewMonitor();
97 
98         if (monitorLock->lock == NULL) {
99                 PKIX_DECREF(monitorLock);
100                 PKIX_ERROR(PKIX_OUTOFMEMORY);
101         }
102 
103         *pNewLock = monitorLock;
104 
105 cleanup:
106 
107         PKIX_RETURN(MONITORLOCK);
108 }
109 
110 PKIX_Error *
PKIX_PL_MonitorLock_Enter(PKIX_PL_MonitorLock * monitorLock,void * plContext)111 PKIX_PL_MonitorLock_Enter(
112         PKIX_PL_MonitorLock *monitorLock,
113         void *plContext)
114 {
115         PKIX_ENTER_NO_LOGGER(MONITORLOCK, "PKIX_PL_MonitorLock_Enter");
116         PKIX_NULLCHECK_ONE(monitorLock);
117 
118         PKIX_MONITORLOCK_DEBUG("\tCalling PR_EnterMonitor)\n");
119         (void) PR_EnterMonitor(monitorLock->lock);
120 
121         PKIX_RETURN_NO_LOGGER(MONITORLOCK);
122 }
123 
124 PKIX_Error *
PKIX_PL_MonitorLock_Exit(PKIX_PL_MonitorLock * monitorLock,void * plContext)125 PKIX_PL_MonitorLock_Exit(
126         PKIX_PL_MonitorLock *monitorLock,
127         void *plContext)
128 {
129         PKIX_ENTER_NO_LOGGER(MONITORLOCK, "PKIX_PL_MonitorLock_Exit");
130         PKIX_NULLCHECK_ONE(monitorLock);
131 
132         PKIX_MONITORLOCK_DEBUG("\tCalling PR_ExitMonitor)\n");
133         PR_ExitMonitor(monitorLock->lock);
134 
135         PKIX_RETURN_NO_LOGGER(MONITORLOCK);
136 }
137