1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2026-01-01 01:41:18 +01:00

Merge pull request #1440 from Kijewski/issue-1436

core: simplify mutex signatures
This commit is contained in:
Ludwig Ortmann 2014-07-29 14:19:33 +02:00
commit 6296658912
13 changed files with 40 additions and 60 deletions

View File

@ -122,13 +122,12 @@ void hwtimer_wait(unsigned long ticks)
{
DEBUG("hwtimer_wait ticks=%lu\n", ticks);
mutex_t mutex;
if (ticks <= 6 || inISR()) {
hwtimer_spin(ticks);
return;
}
mutex_init(&mutex);
mutex_t mutex = MUTEX_INIT;
mutex_lock(&mutex);
/* -2 is to adjust the real value */
int res = hwtimer_set(ticks - 2, hwtimer_releasemutex, &mutex);

View File

@ -44,14 +44,22 @@ typedef struct mutex_t {
} mutex_t;
/**
* @brief Initializes a mutex object.
*
* @param[out] mutex pre-allocated mutex structure, must not be NULL.
*
* @return Always returns 1, always succeeds.
* @brief Static initializer for mutex_t.
* @details This initializer is preferrable to mutex_init().
*/
int mutex_init(struct mutex_t *mutex);
#define MUTEX_INIT { 0, QUEUE_NODE_INIT }
/**
* @brief Initializes a mutex object.
* @details For intialization of variables use MUTEX_INIT instead.
* Only use the function call for dynamically allocated mutexes.
* @param[out] mutex pre-allocated mutex structure, must not be NULL.
*/
static inline void mutex_init(mutex_t *mutex)
{
mutex_t empty_mutex = MUTEX_INIT;
*mutex = empty_mutex;
}
/**
* @brief Tries to get a mutex, non-blocking.
@ -62,32 +70,28 @@ int mutex_init(struct mutex_t *mutex);
* @return 1 if mutex was unlocked, now it is locked.
* @return 0 if the mutex was locked.
*/
int mutex_trylock(struct mutex_t *mutex);
int mutex_trylock(mutex_t *mutex);
/**
* @brief Tries to get a mutex, blocking.
* @brief Locks a mutex, blocking.
*
* @param[in] mutex Mutex object to lock. Has to be initialized first. Must not
* be NULL.
*
* @return 1 getting the mutex was successful
* @return <1 there was an error.
* @param[in] mutex Mutex object to lock. Has to be initialized first. Must not be NULL.
*/
int mutex_lock(struct mutex_t *mutex);
void mutex_lock(mutex_t *mutex);
/**
* @brief Unlocks the mutex.
*
* @param[in] mutex Mutex object to unlock, must not be NULL.
*/
void mutex_unlock(struct mutex_t *mutex);
void mutex_unlock(mutex_t *mutex);
/**
* @brief Unlocks the mutex and sends the current thread to sleep
*
* @param[in] mutex Mutex object to unlock, must not be NULL.
*/
void mutex_unlock_and_sleep(struct mutex_t *mutex);
void mutex_unlock_and_sleep(mutex_t *mutex);
#endif /* __MUTEX_H_ */
/** @} */

View File

@ -20,6 +20,7 @@
#ifndef __QUEUE_H
#define __QUEUE_H
#include <stddef.h>
#include <stdint.h>
#include <inttypes.h>
@ -41,6 +42,11 @@ typedef struct queue_node_t {
uint32_t priority; /**< queue node priority */
} queue_node_t;
/**
* @brief Static initializer for queue_node_t.
*/
#define QUEUE_NODE_INIT { NULL, 0, 0 }
/**
* @brief attach `new_obj` to the tail of the queue (identified
* `root`)

View File

@ -35,24 +35,13 @@
static void mutex_wait(struct mutex_t *mutex);
int mutex_init(struct mutex_t *mutex)
{
mutex->val = 0;
mutex->queue.priority = 0;
mutex->queue.data = 0;
mutex->queue.next = NULL;
return 1;
}
int mutex_trylock(struct mutex_t *mutex)
{
DEBUG("%s: trylocking to get mutex. val: %u\n", sched_active_thread->name, mutex->val);
return (atomic_set_return(&mutex->val, 1) == 0);
}
int mutex_lock(struct mutex_t *mutex)
void mutex_lock(struct mutex_t *mutex)
{
DEBUG("%s: trying to get mutex. val: %u\n", sched_active_thread->name, mutex->val);
@ -60,8 +49,6 @@ int mutex_lock(struct mutex_t *mutex)
/* mutex was locked. */
mutex_wait(mutex);
}
return 1;
}
static void mutex_wait(struct mutex_t *mutex)

View File

@ -82,7 +82,7 @@ static packet_monitor_t packet_monitor;
static handler_entry_t handlers[MAX_PACKET_HANDLERS];
static const pm_table_t handler_table;
static const char *cc1100_event_handler_name = "cc1100_event_handler";
static mutex_t cc1100_mutex;
static mutex_t cc1100_mutex = MUTEX_INIT;
volatile int cc1100_mutex_pid;
static vtimer_t cc1100_watch_dog;
static timex_t cc1100_watch_dog_period;
@ -175,7 +175,6 @@ void cc1100_phy_init(void)
/* Initialize mutex */
cc1100_mutex_pid = -1;
mutex_init(&cc1100_mutex);
/* Allocate event numbers and start cc1100 event process */
cc1100_event_handler_pid = thread_create(event_handler_stack, sizeof(event_handler_stack), PRIORITY_CC1100, CREATE_STACKTEST,

View File

@ -85,7 +85,7 @@ static void transmission_start(void);
static inline void clk_signal(void);
/* mutex for exclusive measurement operation */
mutex_t sht11_mutex;
mutex_t sht11_mutex = MUTEX_INIT;
/*---------------------------------------------------------------------------*/
static inline void clk_signal(void)
@ -266,7 +266,6 @@ static uint8_t measure(uint8_t *p_value, uint8_t *p_checksum, uint8_t mode)
void sht11_init(void)
{
sht11_temperature_offset = 0;
mutex_init(&sht11_mutex);
SHT11_INIT;
hwtimer_wait(11 * HWTIMER_TICKS(1000));
}

View File

@ -121,14 +121,14 @@ uint8_t frag_size;
uint8_t reas_buf[512];
uint8_t comp_buf[512];
uint8_t first_frag = 0;
mutex_t fifo_mutex;
mutex_t fifo_mutex = MUTEX_INIT;
int ip_process_pid = 0;
int nd_nbr_cache_rem_pid = 0;
int contexts_rem_pid = 0;
int transfer_pid = 0;
mutex_t lowpan_context_mutex;
mutex_t lowpan_context_mutex = MUTEX_INIT;
/* registered upper layer threads */
int sixlowpan_reg[SIXLOWPAN_MAX_REGISTERED];
@ -1788,12 +1788,6 @@ int sixlowpan_lowpan_init(void)
/* init mac-layer and radio transceiver */
sixlowpan_mac_init();
/* init lowpan context mutex */
mutex_init(&lowpan_context_mutex);
/* init packet_fifo mutex */
mutex_init(&fifo_mutex);
if (!ip_process_pid) {
ip_process_pid = thread_create(ip_process_buf, IP_PROCESS_STACKSIZE,
PRIORITY_MAIN - 1, CREATE_STACKTEST,

View File

@ -98,7 +98,7 @@ static etx_neighbor_t candidates[ETX_MAX_CANDIDATE_NEIGHBORS];
* In this time, no packet may be handled, otherwise it could assume values
* from the last round to count for this round.
*/
mutex_t etx_mutex;
mutex_t etx_mutex = MUTEX_INIT;
//Transceiver command for sending ETX probes
transceiver_command_t tcmd;
@ -141,7 +141,6 @@ void etx_show_candidates(void)
void etx_init_beaconing(ipv6_addr_t *address)
{
mutex_init(&etx_mutex);
own_address = address;
//set code
puts("ETX BEACON INIT");

View File

@ -50,8 +50,8 @@ char addr_str[IPV6_MAX_ADDR_STR_LEN];
rpl_of_t *rpl_objective_functions[NUMBER_IMPLEMENTED_OFS];
rpl_routing_entry_t rpl_routing_table[RPL_MAX_ROUTING_ENTRIES];
unsigned int rpl_process_pid;
mutex_t rpl_recv_mutex;
mutex_t rpl_send_mutex;
mutex_t rpl_recv_mutex = MUTEX_INIT;
mutex_t rpl_send_mutex = MUTEX_INIT;
msg_t rpl_msg_queue[RPL_PKT_RECV_BUF_SIZE];
char rpl_process_buf[RPL_PROCESS_STACKSIZE];
uint8_t rpl_buffer[BUFFER_SIZE - LL_HDR_LEN];
@ -73,8 +73,6 @@ rpl_of_t *rpl_get_of_for_ocp(uint16_t ocp)
uint8_t rpl_init(int if_id)
{
mutex_init(&rpl_send_mutex);
mutex_init(&rpl_recv_mutex);
rpl_instances_init();
/* initialize routing table */

View File

@ -343,8 +343,7 @@ int vtimer_sleep(timex_t time)
int ret;
vtimer_t t;
mutex_t mutex;
mutex_init(&mutex);
mutex_t mutex = MUTEX_INIT;
mutex_lock(&mutex);
t.action = vtimer_callback_unlock;

View File

@ -22,7 +22,7 @@
#include "thread.h"
#include "mutex.h"
static mutex_t mutex;
static mutex_t mutex = MUTEX_INIT;
static volatile int indicator, count;
static char stack[KERNEL_CONF_STACKSIZE_MAIN];
@ -42,7 +42,6 @@ int main(void)
{
indicator = 0;
count = 0;
mutex_init(&mutex);
thread_create(stack,
sizeof(stack),

View File

@ -22,7 +22,7 @@
#include "pthread.h"
#include "thread.h"
static mutex_t mutex;
static mutex_t mutex = MUTEX_INIT;
static struct pthread_cond_t cv;
static volatile int is_finished;
static volatile long count;
@ -55,7 +55,6 @@ int main(void)
count = 0;
is_finished = 0;
expected_value = 1000*1000;
mutex_init(&mutex);
pthread_cond_init(&cv, NULL);
int pid = thread_create(stack,

View File

@ -26,7 +26,7 @@
#define PROBLEM 12
mutex_t mtx;
mutex_t mtx = MUTEX_INIT;
volatile int storage = 1;
int main_id;
@ -63,8 +63,6 @@ int main(void)
{
main_id = thread_getpid();
mutex_init(&mtx);
printf("Problem: %d\n", PROBLEM);
msg_t args[PROBLEM];