aboutsummaryrefslogtreecommitdiff
path: root/kernel/include/proc/kmutex.h
blob: 37d8ecea1604629995290e54137568dc0e98ce25 (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
#pragma once

#include "proc/sched.h"
#include "proc/spinlock.h"

/*===========
 * Structures
 *==========*/

typedef struct kmutex
{
    ktqueue_t km_waitq;        /* wait queue */
    struct kthread *km_holder; /* current holder */
    list_link_t km_link;
} kmutex_t;

#define KMUTEX_INITIALIZER(mtx)                                             \
    {                                                                       \
        .km_waitq = KTQUEUE_INITIALIZER((mtx).km_waitq), .km_holder = NULL, \
        .km_link = LIST_LINK_INITIALIZER((mtx).km_link),                    \
    }

/*==========
 * Functions
 *=========*/

/**
 * Initializes a mutex.
 *
 * @param mtx the mutex
 */
void kmutex_init(kmutex_t *mtx);

/**
 * Locks the specified mutex.
 *
 * Note: This function may block.
 *
 * Note: These locks are not re-entrant
 *
 * @param mtx the mutex to lock
 */
void kmutex_lock(kmutex_t *mtx);

/**
 * Unlocks the specified mutex.
 *
 * @param mtx the mutex to unlock
 */
void kmutex_unlock(kmutex_t *mtx);

/**
 * Indicates if a mutex has waiters.
 */
long kmutex_has_waiters(kmutex_t *mtx);

/**
 * Indicates if curthr owns a mutex.
 */
long kmutex_owns_mutex(kmutex_t *mtx);