summaryrefslogtreecommitdiffstats
path: root/events.c
diff options
context:
space:
mode:
authorSuren A. Chilingaryan <csa@suren.me>2015-08-06 03:08:42 +0200
committerSuren A. Chilingaryan <csa@suren.me>2015-08-06 03:08:42 +0200
commit8b83e894d3b8859703304936b1b21f3d3ff6d211 (patch)
treec4c7f7e8249d8ec1475f47a115adf1f22ad921b1 /events.c
parent3e4b1ebf881a61900c4bab96dc7a7f3963b6d891 (diff)
downloadipecamera-8b83e894d3b8859703304936b1b21f3d3ff6d211.tar.gz
ipecamera-8b83e894d3b8859703304936b1b21f3d3ff6d211.tar.bz2
ipecamera-8b83e894d3b8859703304936b1b21f3d3ff6d211.tar.xz
ipecamera-8b83e894d3b8859703304936b1b21f3d3ff6d211.zip
Use new locking subsystem to ensure integrity
Diffstat (limited to 'events.c')
-rw-r--r--events.c25
1 files changed, 25 insertions, 0 deletions
diff --git a/events.c b/events.c
index d286c73..8f740b8 100644
--- a/events.c
+++ b/events.c
@@ -19,6 +19,20 @@
#include "private.h"
#include "events.h"
+#define LOCK(lock_name) \
+ err = pcilib_lock(ctx->lock_name##_lock); \
+ if (err) { \
+ pcilib_error("IPECamera is busy"); \
+ return PCILIB_ERROR_BUSY; \
+ } \
+ ctx->lock_name##_locked = 1;
+
+#define UNLOCK(lock_name) \
+ if (ctx->lock_name##_locked) { \
+ pcilib_unlock(ctx->lock_name##_lock); \
+ ctx->lock_name##_locked = 0; \
+ }
+
int ipecamera_stream(pcilib_context_t *vctx, pcilib_event_callback_t callback, void *user) {
int run_flag = 1;
int res, err = 0;
@@ -34,6 +48,8 @@ int ipecamera_stream(pcilib_context_t *vctx, pcilib_event_callback_t callback, v
ipecamera_debug(API, "ipecamera: start streaming");
+ LOCK(stream);
+
ctx->streaming = 1;
ctx->run_streamer = 1;
@@ -79,6 +95,8 @@ int ipecamera_stream(pcilib_context_t *vctx, pcilib_event_callback_t callback, v
ctx->streaming = 0;
+ UNLOCK(stream);
+
ipecamera_debug(API, "ipecamera: streaming finished");
if (do_stop) {
@@ -89,6 +107,7 @@ int ipecamera_stream(pcilib_context_t *vctx, pcilib_event_callback_t callback, v
}
int ipecamera_next_event(pcilib_context_t *vctx, pcilib_timeout_t timeout, pcilib_event_id_t *evid, size_t info_size, pcilib_event_info_t *info) {
+ int err;
struct timeval tv;
ipecamera_t *ctx = (ipecamera_t*)vctx;
@@ -109,6 +128,8 @@ int ipecamera_next_event(pcilib_context_t *vctx, pcilib_timeout_t timeout, pcili
ipecamera_debug(API, "ipecamera: next_event");
+ LOCK(stream);
+
#ifdef IPECAMERA_ANNOUNCE_READY
if (((!ctx->preproc)&&(ctx->reported_id == ctx->event_id))||((ctx->preproc)&&(ctx->reported_id == ctx->preproc_id))) {
#else /* IPECAMERA_ANNOUNCE_READY */
@@ -138,6 +159,7 @@ int ipecamera_next_event(pcilib_context_t *vctx, pcilib_timeout_t timeout, pcili
}
if (ctx->reported_id == ctx->event_id) {
+ UNLOCK(stream);
ipecamera_debug(API, "ipecamera: next_event timed out");
return PCILIB_ERROR_TIMEOUT;
}
@@ -156,6 +178,7 @@ retry:
else if (info_size >= sizeof(pcilib_event_info_t))
memcpy(info, ctx->frame + ((ctx->reported_id-1)%ctx->buffer_size), sizeof(pcilib_event_info_t));
else {
+ UNLOCK(stream);
ipecamera_debug(API, "ipecamera: next_event returned a error");
return PCILIB_ERROR_INVALID_ARGUMENT;
}
@@ -163,6 +186,8 @@ retry:
if ((ctx->event_id - ctx->reported_id) >= ctx->buffer_size) goto retry;
+ UNLOCK(stream);
+
ipecamera_debug(API, "ipecamera: next_event returned");
return 0;