summaryrefslogtreecommitdiff
path: root/test_general/ecos/src/mutex.c
diff options
context:
space:
mode:
Diffstat (limited to 'test_general/ecos/src/mutex.c')
-rw-r--r--test_general/ecos/src/mutex.c105
1 files changed, 105 insertions, 0 deletions
diff --git a/test_general/ecos/src/mutex.c b/test_general/ecos/src/mutex.c
new file mode 100644
index 0000000000..390bbc4a39
--- /dev/null
+++ b/test_general/ecos/src/mutex.c
@@ -0,0 +1,105 @@
+/* Cesar project {{{
+ *
+ * Copyright (C) 2008 Spidcom
+ *
+ * <<<Licence>>>
+ *
+ * }}} */
+/**
+ * \file mutex.c
+ * \brief « brief description »
+ * \ingroup « module »
+ *
+ * « long description »
+ */
+#include <cyg/kernel/kapi.h>
+#include <cyg/infra/diag.h>
+#include <cyg/io/io.h>
+#include <stdio.h>
+
+#define THREAD_STACK_SIZE (40960 / sizeof(int))
+
+int thread_a_stack[THREAD_STACK_SIZE];
+cyg_handle_t thread_a_handle;
+cyg_thread thread_a_obj;
+int thread_b_stack[THREAD_STACK_SIZE];
+cyg_handle_t thread_b_handle;
+cyg_thread thread_b_obj;
+
+cyg_mutex_t mut_shared;
+
+unsigned char transfert_message[] = "Nobody";
+
+//
+// Thread A.
+//
+void thread_a(cyg_addrword_t index)
+{
+ unsigned char write_buffer[] = "Thread A";
+
+ // Run this thread forever.
+ while (1)
+ {
+ // Delay for 5 seconds (10ms * 500ticks).
+ cyg_thread_delay(500);
+
+ // Get the mutex.
+ cyg_mutex_lock(&mut_shared);
+
+ diag_printf("Thrd A : %s\n",transfert_message);
+ // Write data to the global buffer.
+ memcpy(transfert_message, write_buffer, sizeof(write_buffer));
+
+ // Release the mutex.
+ cyg_mutex_unlock(&mut_shared);
+ }
+}
+
+//
+// Thread B.
+//
+void thread_b(cyg_addrword_t index)
+{
+ unsigned char write_buffer[] = "Thread B";
+
+ // Run this thread forever.
+ while (1)
+ {
+ // Delay for 2 seconds (10ms * 200ticks).
+ cyg_thread_delay(200);
+
+ // Get the mutex.
+ cyg_mutex_lock(&mut_shared);
+
+ diag_printf("Thrd B : %s\n",transfert_message);
+ // Write data to the global buffer.
+ memcpy(transfert_message, write_buffer, sizeof(write_buffer));
+
+ // Release the mutex.
+ cyg_mutex_unlock(&mut_shared);
+ }
+}
+
+//
+// Main.
+//
+void cyg_user_start(void)
+{
+ cyg_thread_create(12, thread_a, (cyg_addrword_t) 0,
+ "Thread A", &thread_a_stack, THREAD_STACK_SIZE,
+ &thread_a_handle, &thread_a_obj);
+ cyg_thread_create(12, thread_b, (cyg_addrword_t) 0,
+ "Thread B", &thread_b_stack, THREAD_STACK_SIZE,
+ &thread_b_handle, &thread_b_obj);
+
+ // Mutex creation
+ cyg_mutex_init(&mut_shared);
+
+ cyg_thread_resume(thread_a_handle);
+ cyg_thread_resume(thread_b_handle);
+
+ diag_write_string("Starting Scheduler...\n");
+
+ cyg_scheduler_start();
+}
+