2014-02-21 14:43:55 +01: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-02-21 14:43:55 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ingroup tests
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @brief pthread test application
|
|
|
|
*
|
|
|
|
* @author Raphael Hiesgen <raphael.hiesgen@haw-hamburg.de>
|
|
|
|
*
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
|
|
|
|
#define NUM_THREADS 12
|
|
|
|
|
2017-10-29 11:50:13 +01:00
|
|
|
#define FACTORIAL_EXPECTED (479001600UL)
|
|
|
|
|
2014-02-21 14:43:55 +01:00
|
|
|
pthread_t ths[NUM_THREADS];
|
|
|
|
|
|
|
|
pthread_mutex_t mtx;
|
make: fix various compile errors with Wextra
pkg, nordic_softdevice_ble: disable CFLAGS to omit compiler error
sys, pm_layered: fix casting nonscalar to the same type
cpu, stm32_common: fix type-limits, remove always true assert
cpu, stm32f4: fix pointer arithmetic in periph/i2c
drivers, at86rf2xx: fix type-limits where condition always true
saul, gpio: fix if no gpio configured for saul
cpu, saml21: add frequency check to periph/timer
driver, cc110x: fix unused param and type-limts errors
boards, wsn430-common: fix old-style-declaration
make: fix old style definition
drivers, sdcard_spi: fix old style typedef
driver, at30tse: remove unnecessary check
driver, nrf24: fix type-limit
driver, pn532: change buffer from char to uint8_t
tests/driver_sdcard: fix type limits
boards, feather-m0: add missing field inits
driver, tcs37727: fix type limits
pkg, emb6: disable some compiler warnings
tests/emb6: disable some compiler warings
pkg, openthread: fix sign compare and unused params
tests/trickle: fix struct init
tests/pthread_cooperation: fix type limits
board, mips-malta: remove feature periph_uart
shell: fix var size for netif command
gnrc, netif: fix sign-compare
gnrc, nib: fix sign-compare
shell: fix output in netif command
posix: fix type-limits in pthread_cond
2017-10-31 11:52:18 +01:00
|
|
|
volatile uint32_t storage = 1;
|
2014-02-21 14:43:55 +01:00
|
|
|
|
|
|
|
void *run(void *parameter)
|
|
|
|
{
|
|
|
|
int arg = (int) parameter;
|
|
|
|
printf("My arg: %d\n", arg);
|
|
|
|
|
|
|
|
int err = pthread_mutex_lock(&mtx);
|
|
|
|
|
2014-03-03 22:48:31 +01:00
|
|
|
if (err != 0) {
|
2014-02-21 14:43:55 +01:00
|
|
|
printf("[!!!] pthread_mutex_lock failed with %d\n", err);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
storage *= arg;
|
make: fix various compile errors with Wextra
pkg, nordic_softdevice_ble: disable CFLAGS to omit compiler error
sys, pm_layered: fix casting nonscalar to the same type
cpu, stm32_common: fix type-limits, remove always true assert
cpu, stm32f4: fix pointer arithmetic in periph/i2c
drivers, at86rf2xx: fix type-limits where condition always true
saul, gpio: fix if no gpio configured for saul
cpu, saml21: add frequency check to periph/timer
driver, cc110x: fix unused param and type-limts errors
boards, wsn430-common: fix old-style-declaration
make: fix old style definition
drivers, sdcard_spi: fix old style typedef
driver, at30tse: remove unnecessary check
driver, nrf24: fix type-limit
driver, pn532: change buffer from char to uint8_t
tests/driver_sdcard: fix type limits
boards, feather-m0: add missing field inits
driver, tcs37727: fix type limits
pkg, emb6: disable some compiler warnings
tests/emb6: disable some compiler warings
pkg, openthread: fix sign compare and unused params
tests/trickle: fix struct init
tests/pthread_cooperation: fix type limits
board, mips-malta: remove feature periph_uart
shell: fix var size for netif command
gnrc, netif: fix sign-compare
gnrc, nib: fix sign-compare
shell: fix output in netif command
posix: fix type-limits in pthread_cond
2017-10-31 11:52:18 +01:00
|
|
|
printf("val = %"PRIu32"\n", storage);
|
2014-02-21 14:43:55 +01:00
|
|
|
pthread_mutex_unlock(&mtx);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
2017-10-29 11:50:13 +01:00
|
|
|
puts("START");
|
2014-02-21 14:43:55 +01:00
|
|
|
pthread_attr_t th_attr;
|
|
|
|
pthread_attr_init(&th_attr);
|
|
|
|
pthread_mutex_init(&mtx, NULL);
|
|
|
|
|
|
|
|
for (int i = 0; i < NUM_THREADS; ++i) {
|
|
|
|
printf("Creating thread with arg %d\n", (i + 1));
|
|
|
|
pthread_create(&ths[i], &th_attr, run, (void *)(i + 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < NUM_THREADS; ++i) {
|
2017-10-29 11:50:13 +01:00
|
|
|
printf("join thread %d\n", (i + 1));
|
2014-02-21 14:43:55 +01:00
|
|
|
pthread_join(ths[i], NULL);
|
|
|
|
}
|
|
|
|
|
make: fix various compile errors with Wextra
pkg, nordic_softdevice_ble: disable CFLAGS to omit compiler error
sys, pm_layered: fix casting nonscalar to the same type
cpu, stm32_common: fix type-limits, remove always true assert
cpu, stm32f4: fix pointer arithmetic in periph/i2c
drivers, at86rf2xx: fix type-limits where condition always true
saul, gpio: fix if no gpio configured for saul
cpu, saml21: add frequency check to periph/timer
driver, cc110x: fix unused param and type-limts errors
boards, wsn430-common: fix old-style-declaration
make: fix old style definition
drivers, sdcard_spi: fix old style typedef
driver, at30tse: remove unnecessary check
driver, nrf24: fix type-limit
driver, pn532: change buffer from char to uint8_t
tests/driver_sdcard: fix type limits
boards, feather-m0: add missing field inits
driver, tcs37727: fix type limits
pkg, emb6: disable some compiler warnings
tests/emb6: disable some compiler warings
pkg, openthread: fix sign compare and unused params
tests/trickle: fix struct init
tests/pthread_cooperation: fix type limits
board, mips-malta: remove feature periph_uart
shell: fix var size for netif command
gnrc, netif: fix sign-compare
gnrc, nib: fix sign-compare
shell: fix output in netif command
posix: fix type-limits in pthread_cond
2017-10-31 11:52:18 +01:00
|
|
|
printf("Factorial: %"PRIu32"\n", storage);
|
2014-02-21 14:43:55 +01:00
|
|
|
|
|
|
|
pthread_mutex_destroy(&mtx);
|
|
|
|
pthread_attr_destroy(&th_attr);
|
|
|
|
|
2017-10-29 11:50:13 +01:00
|
|
|
if (storage == FACTORIAL_EXPECTED) {
|
|
|
|
puts("SUCCESS");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
puts("FAILURE: Error, expected: 12!= 479001600.");
|
|
|
|
}
|
|
|
|
|
2014-02-21 14:43:55 +01:00
|
|
|
return 0;
|
|
|
|
}
|