Squashed 'third_party/rawrtc/re/' content from commit f3163ce8b
Change-Id: I6a235e6ac0f03269d951026f9d195da05c40fdab
git-subtree-dir: third_party/rawrtc/re
git-subtree-split: f3163ce8b526a13b35ef71ce4dd6f43585064d8a
diff --git a/src/lock/win32/lock.c b/src/lock/win32/lock.c
new file mode 100644
index 0000000..8fb4362
--- /dev/null
+++ b/src/lock/win32/lock.c
@@ -0,0 +1,72 @@
+/**
+ * @file win32/lock.c Locking for Windows
+ *
+ * Copyright (C) 2010 Creytiv.com
+ */
+#undef _WIN32_WINNT
+#define _WIN32_WINNT 0x0400
+#include <windows.h>
+#include <re_types.h>
+#include <re_mem.h>
+#include <re_lock.h>
+
+
+struct lock {
+ CRITICAL_SECTION c;
+};
+
+
+static void lock_destructor(void *data)
+{
+ struct lock *l = data;
+
+ DeleteCriticalSection(&l->c);
+}
+
+
+int lock_alloc(struct lock **lp)
+{
+ struct lock *l;
+
+ if (!lp)
+ return EINVAL;
+
+ l = mem_alloc(sizeof(*l), lock_destructor);
+ if (!l)
+ return ENOMEM;
+
+ InitializeCriticalSection(&l->c);
+
+ *lp = l;
+ return 0;
+}
+
+
+void lock_read_get(struct lock *l)
+{
+ EnterCriticalSection(&l->c);
+}
+
+
+void lock_write_get(struct lock *l)
+{
+ EnterCriticalSection(&l->c);
+}
+
+
+int lock_read_try(struct lock *l)
+{
+ return TryEnterCriticalSection(&l->c) ? 0 : ENODEV;
+}
+
+
+int lock_write_try(struct lock *l)
+{
+ return TryEnterCriticalSection(&l->c) ? 0 : ENODEV;
+}
+
+
+void lock_rel(struct lock *l)
+{
+ LeaveCriticalSection(&l->c);
+}