2014-02-21 14:51:17 +01:00
|
|
|
/*
|
2014-06-18 15:06:23 +02:00
|
|
|
* Copyright (C) 2014 Hamburg University of Applied Sciences
|
|
|
|
*
|
2014-07-31 19:45:27 +02:00
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser
|
|
|
|
* General Public License v2.1. See the file LICENSE in the top level
|
|
|
|
* directory for more details.
|
2014-06-18 15:06:23 +02:00
|
|
|
*/
|
2014-02-21 14:51:17 +01:00
|
|
|
|
|
|
|
/**
|
2014-06-18 15:06:23 +02:00
|
|
|
* @ingroup tests
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @brief riot thread test application
|
|
|
|
*
|
|
|
|
* @author Raphael Hiesgen <raphael.hiesgen@haw-hamburg.de>
|
|
|
|
*
|
|
|
|
* @}
|
|
|
|
*/
|
2014-02-21 14:51:17 +01:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <inttypes.h>
|
2014-07-15 18:47:39 +02:00
|
|
|
|
|
|
|
#include "thread.h"
|
|
|
|
#include "mutex.h"
|
2014-02-21 14:51:17 +01:00
|
|
|
|
|
|
|
#define PROBLEM 12
|
|
|
|
|
2014-07-17 12:27:29 +02:00
|
|
|
mutex_t mtx = MUTEX_INIT;
|
2014-02-21 14:51:17 +01:00
|
|
|
|
|
|
|
volatile int storage = 1;
|
|
|
|
int main_id;
|
|
|
|
int ths[PROBLEM];
|
2014-07-09 07:16:02 +02:00
|
|
|
char stacks[PROBLEM][KERNEL_CONF_STACKSIZE_MAIN];
|
2014-02-21 14:51:17 +01:00
|
|
|
|
2014-03-04 20:20:01 +01:00
|
|
|
void *run(void *arg)
|
2014-02-21 14:51:17 +01:00
|
|
|
{
|
2014-03-04 20:20:01 +01:00
|
|
|
(void) arg;
|
|
|
|
|
2014-02-21 14:51:17 +01:00
|
|
|
int me = thread_getpid();
|
|
|
|
printf("I am alive (%d)\n", me);
|
2014-03-04 20:20:01 +01:00
|
|
|
msg_t m;
|
2014-07-25 08:17:06 +02:00
|
|
|
msg_receive(&m);
|
2014-03-04 20:20:01 +01:00
|
|
|
printf("Thread %d has arg %" PRIu32 "\n", me, m.content.value);
|
2014-02-21 14:51:17 +01:00
|
|
|
|
2014-07-25 08:17:06 +02:00
|
|
|
mutex_lock(&mtx);
|
2014-02-21 14:51:17 +01:00
|
|
|
|
2014-03-04 20:20:01 +01:00
|
|
|
storage *= m.content.value;
|
2014-02-21 14:51:17 +01:00
|
|
|
mutex_unlock(&mtx);
|
|
|
|
|
|
|
|
msg_t final;
|
|
|
|
final.content.value = me;
|
2014-07-25 08:17:06 +02:00
|
|
|
int err = msg_send(&final, main_id, 1);
|
2014-02-21 14:51:17 +01:00
|
|
|
|
|
|
|
if (err < 0) {
|
|
|
|
printf("[!!!] Failed to send message from %d to main\n", me);
|
|
|
|
}
|
2014-03-04 20:20:01 +01:00
|
|
|
|
|
|
|
return NULL;
|
2014-02-21 14:51:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
main_id = thread_getpid();
|
|
|
|
|
2014-05-02 13:43:33 +02:00
|
|
|
printf("Problem: %d\n", PROBLEM);
|
2014-02-21 14:51:17 +01:00
|
|
|
|
|
|
|
msg_t args[PROBLEM];
|
|
|
|
|
|
|
|
for (int i = 0; i < PROBLEM; ++i) {
|
|
|
|
printf("Creating thread with arg %d\n", (i + 1));
|
2014-03-04 20:20:01 +01:00
|
|
|
ths[i] = thread_create(stacks[i], sizeof(stacks[i]),
|
|
|
|
PRIORITY_MAIN - 1, CREATE_WOUT_YIELD | CREATE_STACKTEST,
|
|
|
|
run, NULL, "thread");
|
2014-02-21 14:51:17 +01:00
|
|
|
|
|
|
|
if (ths[i] < 0) {
|
2014-07-25 08:17:06 +02:00
|
|
|
printf("[!!!] Creating thread failed.\n");
|
2014-02-21 14:51:17 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
args[i].content.value = i + 1;
|
|
|
|
|
2014-07-25 08:17:06 +02:00
|
|
|
int err = msg_send(&args[i], ths[i], 1);
|
2014-02-21 14:51:17 +01:00
|
|
|
if (err < 0) {
|
|
|
|
printf("[!!!] Sending message to thread %d failed\n", ths[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < PROBLEM; ++i) {
|
|
|
|
msg_t msg;
|
|
|
|
msg_receive(&msg);
|
2014-05-02 13:43:33 +02:00
|
|
|
printf("Reveiced message %d from thread %" PRIu32 "\n", i, msg.content.value);
|
2014-02-21 14:51:17 +01:00
|
|
|
}
|
|
|
|
|
2014-05-02 13:43:33 +02:00
|
|
|
printf("Factorial: %d\n", storage);
|
2014-02-21 14:51:17 +01:00
|
|
|
|
|
|
|
if (storage != 479001600) {
|
|
|
|
puts("[!!!] Error, expected: 12!= 479001600.");
|
|
|
|
}
|
|
|
|
|
|
|
|
puts("finished");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|