summaryrefslogtreecommitdiffstats
path: root/tinySAK/src/tsk_safeobj.h
diff options
context:
space:
mode:
Diffstat (limited to 'tinySAK/src/tsk_safeobj.h')
-rw-r--r--tinySAK/src/tsk_safeobj.h81
1 files changed, 81 insertions, 0 deletions
diff --git a/tinySAK/src/tsk_safeobj.h b/tinySAK/src/tsk_safeobj.h
new file mode 100644
index 0000000..f3190f5
--- /dev/null
+++ b/tinySAK/src/tsk_safeobj.h
@@ -0,0 +1,81 @@
+/*
+* Copyright (C) 2009-2010 Mamadou Diop.
+*
+* Contact: Mamadou Diop <diopmamadou(at)doubango.org>
+*
+* This file is part of Open Source Doubango Framework.
+*
+* DOUBANGO is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* DOUBANGO is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with DOUBANGO.
+*
+*/
+
+/**@file tsk_safeobj.h
+ * @brief Thread-Safe object.
+ *
+ * @author Mamadou Diop <diopmamadou(at)doubango.org>
+ *
+ * @date Created: Sat Nov 8 16:54:58 2009 mdiop
+ */
+#ifndef _TINYSAK_SAFEOBJ_H_
+#define _TINYSAK_SAFEOBJ_H_
+
+#include "tinysak_config.h"
+#include "tsk_mutex.h"
+
+TSK_BEGIN_DECLS
+
+//#define TSK_DECLARE_SAFEOBJ\
+// union{ \
+// tsk_mutex_handle_t *mutex; \
+// }
+
+/**@ingroup tsk_safeobj_group
+* Macro helper to declare an object as thread-safe.
+*/
+#define TSK_DECLARE_SAFEOBJ tsk_mutex_handle_t *mutex
+
+#define TSK_SAFEOBJ_MUTEX(safeobj) ((safeobj)->mutex)
+
+/**@ingroup tsk_safeobj_group
+* @def tsk_safeobj_init
+* You MUST call this method before using @ref tsk_safeobj_lock or @ref tsk_safeobj_unlock to initilize the internal mutex.
+* Before freeing your safeobject you MUST call @ref tsk_safeobj_deinit to deinitialize the internal mutex.
+* @param safeobj The thread-safe object to initialize.
+* @sa @ref tsk_safeobj_deinit.
+*/
+/**@ingroup tsk_safeobj_group
+*@def tsk_safeobj_lock
+* Locks a previously initialized safeobject.
+* @param safeobj The thread-safe object to lock.
+*/
+/**@ingroup tsk_safeobj_group
+*@def tsk_safeobj_unlock
+* Unlocks a previously locked safeobject.
+* @param safeobj The thread-safe object to unlock.
+*/
+/**@ingroup tsk_safeobj_group
+*@def tsk_safeobj_deinit
+* Deinitialize a previously initialize safeobject.
+* @param safeobj The thread-safe object to deinitialize.
+* @sa @ref tsk_safeobj_init.
+*/
+#define tsk_safeobj_init(safeobj) TSK_SAFEOBJ_MUTEX(safeobj) = (TSK_SAFEOBJ_MUTEX(safeobj) ? TSK_SAFEOBJ_MUTEX(safeobj) : tsk_mutex_create())
+#define tsk_safeobj_lock(safeobj) tsk_mutex_lock(TSK_SAFEOBJ_MUTEX(safeobj))
+#define tsk_safeobj_unlock(safeobj) tsk_mutex_unlock(TSK_SAFEOBJ_MUTEX(safeobj))
+#define tsk_safeobj_deinit(safeobj) tsk_mutex_destroy(&TSK_SAFEOBJ_MUTEX(safeobj))
+
+TSK_END_DECLS
+
+#endif /* _TINYSAK_SAFEOBJ_H_ */
+
OpenPOWER on IntegriCloud