summaryrefslogtreecommitdiff
path: root/cesar/lib/mbox.h
blob: e5174d0a8720f73e238fb218a3c58ee3be6162f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#ifndef mbox_h
#define mbox_h
/* Cesar project {{{
 *
 * Copyright (C) 2009 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    mbox.c
 * \brief   Mailbox.
 * \ingroup lib
 *
 * Mailbox implementation.
 */
#include "hal/arch/sem.h"

struct mbox_node_t
{
    struct mbox_node_t *next;
};
typedef struct mbox_node_t mbox_node_t;

struct mbox_t
{
    /** Semaphore counting the numbers of messages pending. */
    hal_arch_sem_t sem;
    /** The list head. */
    mbox_node_t *head;
    /** The list tail. */
    mbox_node_t *tail;
};
typedef struct mbox_t mbox_t;

/**
 * Initialise the mailbox.
 * \param  ctx  the mailbox context.
 */
void
mbox_init (mbox_t *ctx);

/**
 * Uninitialise the mailbox.
 * \param  ctx  the mailbox context.
 */
void
mbox_uninit (mbox_t *ctx);

/**
 * Get the first message of the mailbox.
 * \param  ctx  the mailbox context.
 *
 * Block if the mailbox is empty, do not use in DSR.
 */
mbox_node_t*
mbox_get (mbox_t *ctx);

/**
 * Try to get a message of the mailbox.
 * \param  ctx  the mailbox context.
 * \return  NULL if no message present.
 */
mbox_node_t*
mbox_try_get (mbox_t *ctx);

/**
 * Try to get the first message of the mailbox.
 * \param  ctx  the mailbox context.
 * \return  the first message on success.
 *
 * Wait until the delay expire for a message, if no message was inserted it
 * returns NULL.
 */
mbox_node_t*
mbox_timed_get (mbox_t *ctx, uint delay);

/**
 * Insert a message in the mailbox.
 * \param  ctx  the mailbox context.
 * \param  msg  the message to insert.
 *
 * Can be called in the DSR.
 */
void
mbox_put (mbox_t *ctx, mbox_node_t *msg);

/**
 * Get the number of messages in the mailbox.
 * \param  ctx  the mailbox context.
 * \return  the number of pending messages,
 */
uint
mbox_peek (mbox_t *ctx);

#endif /* mbox_h */