File: XnLinuxSysVNamedEvents.cpp

package info (click to toggle)
openni2 2.2.0.33%2Bdfsg-11
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 22,216 kB
  • sloc: cpp: 111,197; ansic: 35,511; sh: 10,542; python: 1,313; java: 952; makefile: 575; xml: 12
file content (265 lines) | stat: -rw-r--r-- 8,501 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*****************************************************************************
*                                                                            *
*  PrimeSense PSCommon Library                                               *
*  Copyright (C) 2012 PrimeSense Ltd.                                        *
*                                                                            *
*  This file is part of PSCommon.                                            *
*                                                                            *
*  Licensed under the Apache License, Version 2.0 (the "License");           *
*  you may not use this file except in compliance with the License.          *
*  You may obtain a copy of the License at                                   *
*                                                                            *
*      http://www.apache.org/licenses/LICENSE-2.0                            *
*                                                                            *
*  Unless required by applicable law or agreed to in writing, software       *
*  distributed under the License is distributed on an "AS IS" BASIS,         *
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  *
*  See the License for the specific language governing permissions and       *
*  limitations under the License.                                            *
*                                                                            *
*****************************************************************************/
//---------------------------------------------------------------------------
// Includes
//---------------------------------------------------------------------------
#include "XnLinuxSysVNamedEvents.h"

#ifndef XN_PLATFORM_LINUX_NO_SYSV

//---------------------------------------------------------------------------
// Defines
//---------------------------------------------------------------------------
enum _XN_EVENT_SEM_TYPE
{
	XN_EVENT_SEM_REF_COUNT = 0,
	XN_EVENT_SEM_SIGNALED,
	XN_EVENT_SEM_MANUAL_RESET,
	XN_EVENT_NUM_OF_SEMS
};

//---------------------------------------------------------------------------
// Types
//---------------------------------------------------------------------------
#ifndef XN_PLATFORM_HAS_BUILTIN_SEMUN
union semun {
               int              val;    /* Value for SETVAL */
               struct semid_ds *buf;    /* Buffer for IPC_STAT, IPC_SET */
               unsigned short  *array;  /* Array for GETALL, SETALL */
               struct seminfo  *__buf;  /* Buffer for IPC_INFO
                                           (Linux-specific) */
           };
#endif

//---------------------------------------------------------------------------
// Code
//---------------------------------------------------------------------------
XnLinuxSysVNamedEvent::XnLinuxSysVNamedEvent(XnBool bManualReset, const XnChar* strName, XnBool bCreate) : XnLinuxNamedEvent(bManualReset, strName, bCreate)
{
}

XnStatus XnLinuxSysVNamedEvent::CreateNamed(const XnChar* strName)
{
	// translate event name to key file name
	XnUInt32 nBytesWritten;
	xnOSStrFormat(m_csSemFileName, XN_FILE_MAX_PATH, &nBytesWritten, "/tmp/XnCore.Event.%s.key", strName);

	// create the file (must exist for ftok)
	m_hSemFile = open(m_csSemFileName, O_CREAT | O_RDONLY, S_IRWXU | S_IRWXG | S_IRWXO);
	if (-1 == m_hSemFile)
	{
		XN_LOG_WARNING_RETURN(XN_STATUS_OS_EVENT_CREATION_FAILED, XN_MASK_OS, "Create named event: failed to create key file (%d)", errno);
	}

	// create the key
	key_t key = ftok(m_csSemFileName, 1);

	// Try to create the semaphore
	// Creating two semaphores. First semaphore will be used as reference count, and second for the signaled state
	m_hSem = semget(key, XN_EVENT_NUM_OF_SEMS, IPC_CREAT | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
	if (-1 == m_hSem)
	{
		XN_LOG_WARNING_RETURN(XN_STATUS_OS_EVENT_CREATION_FAILED, XN_MASK_OS, "Create named event: failed to create semaphore (%d)", errno);
	}

	// if no one is using it so far (this is a newly created event), reset it
	if (0 == semctl(m_hSem, XN_EVENT_SEM_REF_COUNT, GETVAL))
	{
		// set it to the non-signaled state
		semun val;
		val.val = 0;
		if (0 != semctl(m_hSem, XN_EVENT_SEM_SIGNALED, SETVAL, val))
		{
			xnLogWarning(XN_MASK_OS, "Create named event: semctl for signaled failed (%d)", errno);
			semctl(m_hSem, 0, IPC_RMID);
			return XN_STATUS_OS_EVENT_CREATION_FAILED;
		}

		// set its manual reset value
		val.val = m_bManualReset;
		if (0 != semctl(m_hSem, XN_EVENT_SEM_MANUAL_RESET, SETVAL, val))
		{
			xnLogWarning(XN_MASK_OS, "Create named event: semctl for manual reset failed (%d)", errno);
			semctl(m_hSem, 0, IPC_RMID);
			return XN_STATUS_OS_EVENT_CREATION_FAILED;
		}
	}

	// adding 1 to reference count
	struct sembuf op;
	op.sem_op = 1;
	op.sem_num = XN_EVENT_SEM_REF_COUNT;
	op.sem_flg = SEM_UNDO;
	semop(m_hSem, &op, 1);

	m_bManualReset = semctl(m_hSem, XN_EVENT_SEM_MANUAL_RESET, GETVAL);

	return (XN_STATUS_OK);
}

XnStatus XnLinuxSysVNamedEvent::OpenNamed(const XnChar* strName)
{
	// translate event name to key file name
	XnUInt32 nBytesWritten;
	xnOSStrFormat(m_csSemFileName, XN_FILE_MAX_PATH, &nBytesWritten, "/tmp/XnCore.Event.%s.key", strName);

	// create the key
	key_t key = ftok(m_csSemFileName, 1);
	if (-1 == key)
	{
		// event does not exist
		XN_LOG_WARNING_RETURN(XN_STATUS_OS_EVENT_OPEN_FAILED, XN_MASK_OS, "Open named event: failed to open key file (%d) - event might not exist...", errno);
	}

	// Try to open the semaphore
	m_hSem = semget(key, XN_EVENT_NUM_OF_SEMS, 0);
	if (-1 == m_hSem)
	{
		XN_LOG_WARNING_RETURN(XN_STATUS_OS_EVENT_OPEN_FAILED, XN_MASK_OS, "Open named event: failed to get semaphore (%d)", errno);
	}

	// if ref count is 0, the event does not really exist
	if (0 == semctl(m_hSem, XN_EVENT_SEM_REF_COUNT, GETVAL))
	{
		XN_LOG_WARNING_RETURN(XN_STATUS_OS_EVENT_OPEN_FAILED, XN_MASK_OS, "Open named event: ref count is zero - event does not exist!", errno);
	}

	// adding 1 to reference count
	struct sembuf op;
	op.sem_op = 1;
	op.sem_num = XN_EVENT_SEM_REF_COUNT;
	op.sem_flg = SEM_UNDO;
	semop(m_hSem, &op, 1);

	m_bManualReset = semctl(m_hSem, XN_EVENT_SEM_MANUAL_RESET, GETVAL);
	
	return XN_STATUS_OK;
}

XnStatus XnLinuxSysVNamedEvent::Destroy()
{
	// dec ref count
	struct sembuf op;
	op.sem_num = XN_EVENT_SEM_REF_COUNT;
	op.sem_op = -1;
	op.sem_flg = IPC_NOWAIT | SEM_UNDO;
	semop(m_hSem, &op, 1); 

	// check current ref count. If 0, destroy it
	int val = semctl(m_hSem, 0, GETVAL);
	if (0 == val)
	{
		// destroy the semaphore
		semctl(m_hSem, 0, IPC_RMID);
		// and remove file
		xnOSDeleteFile(m_csSemFileName);
	}

	return (XN_STATUS_OK);
}

XnStatus XnLinuxSysVNamedEvent::Set()
{
	struct sembuf op[2];

	// check if it's not signaled (by waiting for 0, with IPC_NOWAIT)
	op[0].sem_num = XN_EVENT_SEM_SIGNALED;
	op[0].sem_op = 0;
	op[0].sem_flg = IPC_NOWAIT;

	// signal (if zero)
	op[1].sem_num = XN_EVENT_SEM_SIGNALED;
	op[1].sem_op = 1;
	op[1].sem_flg = SEM_UNDO;

	semop(m_hSem, op, 2);
	// no need to check error code. either it succeeded, or the event is already signaled

	return (XN_STATUS_OK);
}

XnStatus XnLinuxSysVNamedEvent::Reset()
{
	// unsignal it, with NOWAIT (that way, if it's not signaled, nothing will happen)
	struct sembuf op;
	op.sem_num = XN_EVENT_SEM_SIGNALED;
	op.sem_op = -1;
	op.sem_flg = IPC_NOWAIT;
	semop(m_hSem, &op, 1);

	return XN_STATUS_OK;
}

XnStatus XnLinuxSysVNamedEvent::Wait(XnUInt32 nMilliseconds)
{
	XnStatus nRetVal = XN_STATUS_OK;

	struct timespec time = {0};
	if (nMilliseconds != XN_WAIT_INFINITE)
	{
		nRetVal = xnOSGetTimeout(&time, nMilliseconds);
		if (nRetVal != XN_STATUS_OK)
		{
			return XN_STATUS_OS_EVENT_WAIT_FAILED;
		}
	}

	struct sembuf op[2];

	// wait for it to be 1
	op[0].sem_num = XN_EVENT_SEM_SIGNALED;
	op[0].sem_op = -1;
	op[0].sem_flg = 0;

	// re set it (if manual reset)
	op[1].sem_num = XN_EVENT_SEM_SIGNALED;
	op[1].sem_op = 1;
	op[1].sem_flg = 0;

	XnInt32 nOpsCount = m_bManualReset ? 2 : 1;

	if (XN_WAIT_INFINITE != nMilliseconds)
	{
#if _GNU_SOURCE
		if (0 != semtimedop(m_hSem, op, nOpsCount, &time))
#else
		if (0 != semop(m_hSem, op, nOpsCount))	
#endif
		{
			if(EAGAIN == errno)
			{
				return XN_STATUS_OS_EVENT_TIMEOUT;
			}
			return XN_STATUS_OS_EVENT_WAIT_FAILED;
		}
	}
	else
	{
		if (0 != semop(m_hSem, op, nOpsCount))
		{
			return XN_STATUS_OS_EVENT_WAIT_FAILED;
		}
	}

	return (XN_STATUS_OK);
}

#endif // if we have SysV